-
Notifications
You must be signed in to change notification settings - Fork 7
/
cm.c
4063 lines (3817 loc) · 127 KB
/
cm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#if !NO_DYNAMIC_LINKING
#include "dlloader.h"
#endif
#include <stdio.h>
#include <string.h>
#undef NDEBUG
#include <assert.h>
#include <ctype.h>
#include <math.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#include <limits.h>
#ifdef HAVE_WINDOWS_H
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#include <winsock2.h>
#define __ANSI_CPP__
#define lrand48() rand()
#define srand48(x) srand((unsigned int)(x))
#else
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <inttypes.h>
#include <ffs.h>
#include <atl.h>
#include "evpath.h"
#include "chr_time.h"
#include "cm_internal.h"
#include "cm_transport.h"
#if NO_DYNAMIC_LINKING
struct select_data;
extern void libcmselect_LTX_add_select(CMtrans_services svc, struct select_data *sdp, int fd,
select_list_func func, void *arg1, void *arg2);
extern void libcmselect_LTX_write_select(CMtrans_services svc, struct select_data *sdp, int fd,
select_list_func func, void *arg1, void *arg2);
extern periodic_task_handle libcmselect_LTX_add_periodic(CMtrans_services svc, struct select_data *sdp, int interval_sec, int interval_usec, select_list_func func, void *arg1, void *arg2);
extern periodic_task_handle libcmselect_LTX_add_delayed_task(CMtrans_services svc,
struct select_data *sdp, int delay_sec, int delay_usec, select_list_func func,void *arg1,
void *arg2);
extern void libcmselect_LTX_remove_periodic(CMtrans_services svc, struct select_data *sdp, periodic_task_handle handle);
extern void libcmselect_LTX_remove_select(CMtrans_services svc, struct select_data *sdp, int fd);
extern void libcmselect_LTX_wake_function(CMtrans_services svc, struct select_data *sdp);
extern void libcmselect_LTX_blocking_function(CMtrans_services svc, void *client_data);
extern void libcmselect_LTX_polling_function(CMtrans_services svc,void *client_data);
extern void libcmselect_LTX_select_initialize(CMtrans_services svc,CManager cm,void *client_data);
extern void libcmselect_LTX_select_shutdown(CMtrans_services svc,CManager cm,void *client_data);
extern void libcmselect_LTX_select_free(CMtrans_services svc,CManager cm,void *client_data);
extern void libcmselect_LTX_select_stop(CMtrans_services svc,void *client_data);
#endif
static void CMinitialize (CManager cm);
static atom_t CM_TRANSPORT = -1;
static atom_t CM_CMANAGER_ID = -1;
static atom_t CM_NETWORK_POSTFIX = -1;
static atom_t CM_CONN_BLOCKING = -1;
atom_t CM_REBWM_RLEN = -1;
atom_t CM_REBWM_REPT = -1;
atom_t CM_BW_MEASURE_INTERVAL = -1;
atom_t CM_BW_MEASURE_TASK = -1;
atom_t CM_BW_MEASURED_VALUE = -1;
atom_t CM_BW_MEASURED_COF = -1;
atom_t CM_BW_MEASURE_SIZE = -1;
atom_t CM_BW_MEASURE_SIZEINC = -1;
static atom_t CM_EVENT_SIZE = -1;
static atom_t CM_INCOMING_CONNECTION = -1;
static atom_t CM_TRANSPORT_RELIABLE = -1;
static atom_t CM_IP_INTERFACE = -1;
void wait_for_pending_write(CMConnection conn);
static void cm_wake_any_pending_write(CMConnection conn);
static void transport_wake_any_pending_write(CMConnection conn);
static void cm_set_pending_write(CMConnection conn);
static int drop_CM_lock(CManager cm, const char *file, int line);
static int acquire_CM_lock(CManager cm, const char *file, int line);
static int return_CM_lock_status(CManager cm, const char *file, int line);
static void add_buffer_to_pending_queue(CManager cm, CMConnection conn, CMbuffer buf, long length);
static void cond_wait_CM_lock(CManager cm, void *cond, char *file, int line);
struct CMtrans_services_s CMstatic_trans_svcs = {INT_CMmalloc, INT_CMrealloc, INT_CMfree,
INT_CM_fd_add_select,
CM_fd_write_select,
CM_fd_remove_select,
CMtransport_trace,
CMtransport_verbose,
CMConnection_create,
INT_CMadd_shutdown_task,
INT_CMadd_periodic_task,
INT_CMremove_periodic,
INT_CMadd_poll,
cm_get_data_buf,
cm_return_data_buf,
internal_connection_close,
cm_create_transport_buffer,
cm_create_transport_and_link_buffer,
INT_CMget_transport_data,
cm_set_pending_write,
transport_wake_any_pending_write,
drop_CM_lock,
acquire_CM_lock,
return_CM_lock_status,
cond_wait_CM_lock,
add_buffer_to_pending_queue,
INT_CMConnection_dereference,
INT_CMConnection_add_reference,
INT_CMConnection_failed,
CMwake_server_thread,
INT_CMCondition_signal
};
static void INT_CMControlList_close(CMControlList cl, CManager cm);
static int CMcontrol_list_poll(CMControlList cl);
int CMdo_non_CM_handler(CMConnection conn, int header,
char *buffer, size_t length);
void CMdo_performance_response(CMConnection conn, size_t length,
int func, int byte_swap,
char *buffer);
void CMhttp_handler(CMConnection conn, char* buffer, size_t length);
static void CM_init_select(CMControlList cl, CManager cm);
static void cond_wait_CM_lock(CManager cm, void *vcond, char *file, int line)
{
thr_condition_t *cond = vcond;
CMtrace_out(cm, CMLowLevelVerbose, "CManager Condition wait at \"%s\" line %d\n",
file, line);
cm->locked--;
thr_condition_wait(*cond, cm->exchange_lock);
CMtrace_out(cm, CMLowLevelVerbose, "CManager Condition wake at \"%s\" line %d\n",
file, line);
cm->locked++;
}
static int drop_CM_lock(CManager cm, const char *file, int line)
{
int ret = cm->locked;
IntCManager_unlock(cm, file, line);
return ret;
}
static int acquire_CM_lock(CManager cm, const char *file, int line)
{
IntCManager_lock(cm, file, line);
return cm->locked;
}
static int return_CM_lock_status(CManager cm, const char *file, int line)
{
(void) file;
(void) line;
return cm->locked;
}
static void
CMpoll_forever(CManager cm)
{
CMControlList cl = cm->control_list;
int should_exit = 0;
CManager_lock(cm);
if (!cm->control_list->select_initialized) {
CM_init_select(cm->control_list, cm);
}
if (cl->has_thread > 0 && cl->server_thread == thr_thread_self()) {
/*
* if we're actually the server thread here, do a thread exit when
* we're done
*/
should_exit++;
}
while(!cl->closed) {
CMtrace_out(cm, CMLowLevelVerbose, "CM Poll Forever - thread %zx doing wait\n", (size_t)thr_thread_self());
if (CMcontrol_list_wait(cl) == -1) {
CMtrace_out(cm, CMLowLevelVerbose, "CM Poll Forever - doing close and exit\n");
/*
* error. others will free the CM too, add to the ref count
* here so we can close.
*/
cm->reference_count++;
CManager_unlock(cm);
CManager_close(cm);
exit(1);
}
}
CMtrace_out(cm, CMLowLevelVerbose, "CM Poll Forever - doing close\n");
CManager_unlock(cm);
CManager_close(cm);
if (should_exit != 0) thr_thread_exit(NULL);
}
static void CManager_free(CManager cm);
static void
server_thread_func(CManager cm)
{
CMpoll_forever(cm);
CManager_free(cm);
}
extern void
INT_CMrun_network(CManager cm)
{
if (!cm->control_list->select_initialized) {
CM_init_select(cm->control_list, cm);
}
if ((cm->control_list->server_thread != 0) &&
(cm->control_list->server_thread != thr_thread_self())) {
/* What? We're polling, but we're not the server thread? */
fprintf(stderr, "Warning: CMrun_network() called when another thread may already be handling the network\n");
fprintf(stderr, " This situation may result in unexpected I/O blocking.\n");
fprintf(stderr, " Server thread set to %zx.\n", (size_t) thr_thread_self());
}
cm->control_list->server_thread = thr_thread_self();
cm->control_list->has_thread = 1;
CManager_unlock(cm);
CMpoll_forever(cm);
}
static int
CM_test_thread_func()
{
return 1;
}
static thr_thread_t
thr_fork(void*(*func)(void*), void *arg)
{
thr_thread_t new_thread = 0;
int err = thr_thread_create(&new_thread, NULL, (void*)func, arg);
if (err != 0) {
return (thr_thread_t) (intptr_t)NULL;
} else {
return (thr_thread_t) new_thread;
}
}
int
INT_CMfork_comm_thread(CManager cm)
{
/* if we're on a kernel-level-threads package, for the thread and
return 1, else return 0; */
if (!cm->control_list->select_initialized) {
CM_init_select(cm->control_list, cm);
}
if (cm->control_list->has_thread == 0) {
if (cm->control_list->network_blocking_function.func) {
thr_thread_t server_thread =
thr_fork((void*(*)(void*))server_thread_func,
(void*)cm);
CMtrace_out(cm, CMLowLevelVerbose,
"CM - Forked comm thread %p\n", (void*)(intptr_t)server_thread);
if (server_thread == (thr_thread_t)(intptr_t) NULL) {
return 0;
}
cm->control_list->server_thread = thr_get_thread_id(server_thread);
cm->control_list->has_thread = 1;
cm->reference_count++;
CMtrace_out(cm, CMFreeVerbose, "Forked - CManager %p ref count now %d\n",
cm, cm->reference_count);
cm->control_list->cl_reference_count++;
cm->control_list->free_reference_count++;
} else {
/*
* Can't start a server thread yet, but lets see
* if we can fork anything successfully.
*/
thr_thread_t test_thread =
thr_fork((void*(*)(void*))CM_test_thread_func,
(void*)cm);
if (test_thread == (thr_thread_t)(intptr_t) NULL) {
/* No. Say we can't. */
CMtrace_out(cm, CMLowLevelVerbose,
"CM - Test fork failed, no comm thread\n");
return 0;
}
/* OK, we'll fork it later. */
CMtrace_out(cm, CMLowLevelVerbose,
"CM - Will fork comm thread later\n");
cm->control_list->has_thread = -1; /* should fork one */
}
}
return 1;
}
extern
void
CMControlList_set_blocking_func(CMControlList cl, CManager cm,
CMPollFunc bfunc, CMPollFunc pfunc,
void *client_data)
{
}
extern void
INT_CMpoll_network(CManager cm)
{
CMControlList cl = cm->control_list;
CMtrace_out(cm, CMLowLevelVerbose, "CM Poll Network\n");
cl->network_polling_function.func((void*)&CMstatic_trans_svcs,
cl->network_polling_function.client_data);
CMcontrol_list_poll(cl);
}
static void
add_contact_list(CManager cm, attr_list attrs)
{
int list_size = 0;
if (cm->contact_lists == NULL) {
cm->contact_lists = INT_CMmalloc(sizeof(attr_list) *2);
list_size = 0;
} else {
while(cm->contact_lists[list_size] != NULL) list_size++;
cm->contact_lists = INT_CMrealloc(cm->contact_lists,
sizeof(attr_list) * (list_size + 2));
}
cm->contact_lists[list_size] = attrs;
cm->contact_lists[list_size+1] = NULL;
}
void
INT_CM_insert_contact_info(CManager cm, attr_list attrs)
{
attr_merge_lists(cm->contact_lists[0], attrs);
}
attr_list
INT_CMget_contact_list(CManager cm)
{
if (cm->contact_lists == NULL) return NULL;
CMadd_ref_attr_list(cm, cm->contact_lists[0]);
return (cm->contact_lists[0]);
}
extern attr_list
INT_CMderef_and_copy_list(CManager cm, attr_list attrs)
{
// done inside the CM lock, so a safe way to convert a shared list to an owned list
attr_list ret = attr_copy_list(attrs);
free_attr_list(attrs);
return ret;
}
extern attr_list
INT_CMget_specific_contact_list(CManager cm, attr_list attrs)
{
char *chosen_transport = NULL, *chosen_net = NULL, *chosen_interface = NULL;
char *freeable_transport = NULL;
int i = 0;
if (attrs != NULL) {
get_string_attr(attrs, CM_TRANSPORT, &chosen_transport);
}
if (chosen_transport && (strchr(chosen_transport, ':') != NULL)) {
freeable_transport = strdup(chosen_transport);
*(strchr(freeable_transport, ':')) = 0;
chosen_transport = freeable_transport;
}
if (attrs != NULL) {
get_string_attr(attrs, CM_NETWORK_POSTFIX, &chosen_net);
}
if (attrs != NULL) {
get_string_attr(attrs, CM_IP_INTERFACE, &chosen_interface);
}
if ((chosen_transport == NULL) && (chosen_net == NULL) && (chosen_interface == NULL)) {
CMadd_ref_attr_list(cm, cm->contact_lists[0]);
return cm->contact_lists[0];
}
/* specific transport or interface chosen */
i = 0;
while (cm->contact_lists && (cm->contact_lists[i] != NULL)) {
char *this_transport = NULL, *this_postfix = NULL, *this_interface = NULL;
get_string_attr(cm->contact_lists[i], CM_TRANSPORT, &this_transport);
get_string_attr(cm->contact_lists[i], CM_NETWORK_POSTFIX, &this_postfix);
get_string_attr(cm->contact_lists[i], CM_IP_INTERFACE, &this_interface);
if (this_transport == NULL) {
this_transport = "sockets";
}
if (chosen_transport == NULL) {
chosen_transport = "sockets";
}
if (strcmp(this_transport, chosen_transport) == 0) {
if ((chosen_net != NULL) || (this_postfix != NULL)) {
/* one is not null */
if (chosen_net && this_postfix) {
if (strcmp(chosen_net, this_postfix) != 0) {
i++;
continue;
}
} else {
i++;
continue;
}
}
if ((chosen_interface != NULL) || (this_interface != NULL)) {
/* one is not null */
if (chosen_interface && this_interface) {
if (strcmp(chosen_interface, this_interface) != 0) {
i++;
continue;
}
} else {
i++;
continue;
}
}
CMadd_ref_attr_list(cm, cm->contact_lists[i]);
if (freeable_transport) free(freeable_transport);
return cm->contact_lists[i];
}
i++;
}
/* chosen transport not listened? */
CMinternal_listen(cm, attrs, /* try others*/ 0);
/* try again */
i = 0;
while (cm->contact_lists && (cm->contact_lists[i] != NULL)) {
char *this_transport = NULL, *this_postfix = NULL, *this_interface = NULL;
get_string_attr(cm->contact_lists[i], CM_TRANSPORT, &this_transport);
get_string_attr(cm->contact_lists[i], CM_NETWORK_POSTFIX,
&this_postfix);
get_string_attr(cm->contact_lists[i], CM_IP_INTERFACE, &this_interface);
if (this_transport == NULL) {
this_transport = "sockets";
}
if (chosen_transport == NULL) {
chosen_transport = "sockets";
}
if (strcmp(this_transport, chosen_transport) == 0) {
if ((chosen_net != NULL) || (this_postfix != NULL)) {
/* one is not null */
if (chosen_net && this_postfix) {
if (strcmp(chosen_net, this_postfix) != 0) {
i++;
continue;
}
} else {
i++;
continue;
}
}
if ((chosen_interface != NULL) || (this_interface != NULL)) {
/* one is not null */
if (chosen_interface && this_interface) {
if (strcmp(chosen_interface, this_interface) != 0) {
i++;
continue;
}
} else {
i++;
continue;
}
}
CMadd_ref_attr_list(cm, cm->contact_lists[i]);
if (freeable_transport) free(freeable_transport);
return cm->contact_lists[i];
}
i++;
}
/* maybe it failed to load */
if (freeable_transport) free(freeable_transport);
return NULL;
}
int
INT_CMlisten(CManager cm)
{
return INT_CMlisten_specific (cm, NULL);
}
static attr_list
split_transport_attributes(attr_list list)
{
char *chosen_transport = NULL;
if (list) {
get_string_attr(list, CM_TRANSPORT, &chosen_transport);
}
if (chosen_transport && (strchr(chosen_transport, ':') != NULL)) {
attr_list new_list = attr_copy_list(list);
atom_t atom;
char *old_transport, *params;
char *next_param;
get_string_attr(new_list, CM_TRANSPORT, &old_transport);
params = strchr(old_transport, ':');
*(params++) = 0;
set_string_attr(new_list, CM_TRANSPORT, strdup(old_transport));
while (params != NULL) {
char *equal, *end;
next_param = strchr(params, ',');
if (next_param) *(next_param++) = 0; /* kill comma */
if ((equal = strchr(params, '=')) != NULL) {
/* there's an equal sign */
*(equal++) = 0;
/* we'll deal with this later */
}
while (isspace(*params)) params++; /* skip white */
end = params + strlen(params) - 1;
while(end > params && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
atom = attr_atom_from_string(params);
if (equal == NULL) {
set_int_attr(new_list, atom, 1);
} else {
char *tail;
long value;
while (isspace(*equal)) equal++; /* skip white */
end = equal + strlen(equal) - 1;
while(end > equal && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
value = strtol(equal, &tail, 10);
if (strlen(tail) == 0) {
/* valid integer! */
if ((value < INT_MAX) && (value > INT_MIN)) {
set_int_attr(new_list, atom, (int)value);
} else {
set_long_attr(new_list, atom, value);
}
} else {
/* string... */
set_string_attr(new_list, atom, strdup(equal));
}
}
params = next_param;
}
free(old_transport); /* not free'd by replace */
free_attr_list(list);
list = new_list;
}
return list;
}
extern int
CMinternal_listen(CManager cm, attr_list listen_info, int try_others)
{
int success = 0;
transport_entry *trans_list;
char *chosen_transport = NULL;
char *iface = NULL;
if (listen_info) {
listen_info = split_transport_attributes(attr_copy_list(listen_info));
get_string_attr(listen_info, CM_TRANSPORT, &chosen_transport);
get_string_attr(listen_info, CM_IP_INTERFACE, &iface);
}
if (chosen_transport != NULL) {
CMtrace_out(cm, CMConnectionVerbose,
"CM - Listening only on transport \"%s\"\n",
chosen_transport);
if (load_transport(cm, chosen_transport, 1) == 0) {
CMtrace_out(cm, CMConnectionVerbose,
"Failed to load transport \"%s\". Revert to default.\n",
chosen_transport);
CMtrace_out(cm, CMTransportVerbose,
"Failed to load transport \"%s\". Revert to default.\n",
chosen_transport);
if (!try_others) {
if (listen_info) free_attr_list(listen_info);
return success;
}
chosen_transport = NULL;
}
}
trans_list = cm->transports;
while ((trans_list != NULL) && (*trans_list != NULL)) {
attr_list attrs;
if ((chosen_transport == NULL) ||
(strcmp((*trans_list)->trans_name, chosen_transport) == 0)) {
attrs = (*trans_list)->listen(cm, &CMstatic_trans_svcs,
*trans_list,
listen_info);
add_attr(attrs, CM_CMANAGER_ID, Attr_Int4, (intptr_t)cm->CManager_ID);
if (iface) {
add_string_attr(attrs, CM_IP_INTERFACE, strdup(iface));
}
add_contact_list(cm, attrs);
if (CMtrace_on(cm, CMConnectionVerbose)) {
fprintf(cm->CMTrace_file, "Adding contact list -> ");
fdump_attr_list(cm->CMTrace_file, attrs);
}
if (attrs != NULL) {
success++;
}
}
trans_list++;
}
if (listen_info) free_attr_list(listen_info);
return success;
}
int
INT_CMlisten_specific(CManager cm, attr_list listen_info)
{
int success = 0;
if (!cm->initialized) CMinitialize(cm);
success = CMinternal_listen(cm, listen_info, /* try others*/ 1);
return (success != 0);
}
#ifdef CM_DEFAULT_TRANSPORT
static char *CMglobal_default_transport = CM_DEFAULT_TRANSPORT;
#else
static char *CMglobal_default_transport = NULL;
#endif
static char *CMglobal_alternate_transports[] = {NULL};
static void
CMinitialize(CManager cm)
{
char **transport_names = CMglobal_alternate_transports;
char *def = getenv("CMDefaultTransport");
if (def != NULL) CMglobal_default_transport = def;
if (CMglobal_default_transport) {
if (load_transport(cm, CMglobal_default_transport, 0) == 0) {
fprintf(stderr, "Failed to initialize default transport. Exiting.\n");
exit(1);
}
}
while ((transport_names != NULL) && (transport_names[0] != NULL)) {
load_transport(cm, transport_names[0], 1);
transport_names++;
}
cm->initialized++;
}
static int
CMcontrol_list_poll(CMControlList cl)
{
func_entry *poll_list = cl->polling_function_list;
while ((poll_list != NULL) && (poll_list->func != NULL)){
int consistency_number = cl->cl_consistency_number;
CManager_unlock(poll_list->cm);
poll_list->func(poll_list->cm, poll_list->client_data);
CManager_lock(poll_list->cm);
/* do function */
if (consistency_number != cl->cl_consistency_number) {
return 1;
}
poll_list++;
}
return 1;
}
static
void
CMControlList_add_poll(CMControlList cl, CManager cm, CMPollFunc func,
void *client_data)
{
func_entry *poll_list;
int count = 0;
poll_list = cl->polling_function_list;
while ((poll_list != NULL) && (poll_list[count].func != NULL)) {
count++;
}
/*
* We're going to navigate the poll list without locks. This is
* somewhat dangerous, but safe enough if we only have a couple of
* functions so we never realloc. If we ever hit the realloc() below,
* there's a chance of some bad data references. At this point, using
* that many poll functions seems unlikely, but later, maybe not...
*/
if (poll_list != NULL) {
if (cl->pflist_size < count - 2) {
cl->pflist_size *= 2;
poll_list = INT_CMrealloc(poll_list, sizeof(func_entry) * (cl->pflist_size));
}
} else {
poll_list = INT_CMmalloc(sizeof(func_entry)*10);
cl->pflist_size = 10;
}
poll_list[count].cm = cm;
poll_list[count].func = func;
poll_list[count].client_data = client_data;
poll_list[count+1].func = NULL;
cl->polling_function_list = poll_list;
}
extern
void
INT_CMadd_poll(CManager cm, CMPollFunc func, void *client_data)
{
CMControlList_add_poll(cm->control_list, cm, func, client_data);
}
extern
int
CMcontrol_list_wait(CMControlList cl)
{
/* associated CM should be locked */
if ((cl->server_thread != 0) &&
(cl->server_thread != thr_thread_self())) {
/* What? We're polling, but we're not the server thread? */
fprintf(stderr, "Warning: Multiple threads calling CMnetwork_wait\n");
fprintf(stderr, " This situation may result in unexpected I/O blocking.\n");
fprintf(stderr, " Server thread set to %zx.\n", (size_t) thr_thread_self());
}
cl->server_thread = thr_thread_self();
if (cl->network_blocking_function.func != NULL) {
cl->network_blocking_function.func((void*)&CMstatic_trans_svcs,
cl->network_blocking_function.client_data);
}
CMcontrol_list_poll(cl);
return 1;
}
static CMControlList CMControlList_create();
static thr_mutex_t atl_mutex;
static int atl_mutex_initialized = 0;
static void process_pending_queue(CManager cm, void *junk);
extern
CManager
INT_CManager_create()
{
return INT_CManager_create_control(NULL);
}
static void
atl_mutex_lock(void* lock)
{
thr_mutex_lock(*((thr_mutex_t*)lock));
}
static void
atl_mutex_unlock(void* lock)
{
thr_mutex_unlock(*((thr_mutex_t*)lock));
}
extern
CManager
INT_CManager_create_control(char *control_module)
{
CManager cm = (CManager) INT_CMmalloc(sizeof(CManager_s));
int atom_init = 0;
if (!atl_mutex_initialized) {
atl_mutex_initialized++;
thr_mutex_init(atl_mutex);
atl_install_mutex_funcs((atl_lock_func)atl_mutex_lock, (atl_lock_func)atl_mutex_unlock,
&atl_mutex);
}
if (cm == NULL)
return NULL;
memset(cm, 0, sizeof(CManager_s));
if (atom_init == 0) {
CM_TRANSPORT = attr_atom_from_string("CM_TRANSPORT");
CM_CMANAGER_ID = attr_atom_from_string("CM_CMANAGER_ID");
CM_NETWORK_POSTFIX = attr_atom_from_string("CM_NETWORK_POSTFIX");
CM_CONN_BLOCKING = attr_atom_from_string("CM_CONN_BLOCKING");
CM_REBWM_RLEN = attr_atom_from_string("CM_REG_BW_RUN_LEN");
CM_REBWM_REPT = attr_atom_from_string("CM_REG_BW_REPEAT_CNT");
CM_BW_MEASURE_INTERVAL = attr_atom_from_string("CM_BW_MEASURE_INTERVAL");
CM_BW_MEASURE_TASK = attr_atom_from_string("CM_BW_MEASURE_TASK");
CM_BW_MEASURED_VALUE = attr_atom_from_string("CM_BW_MEASURED_VALUE");
CM_BW_MEASURED_COF = attr_atom_from_string("CM_BW_MEASURED_COF");
CM_BW_MEASURE_SIZE = attr_atom_from_string("CM_BW_MEASURE_SIZE");
CM_BW_MEASURE_SIZEINC = attr_atom_from_string("CM_BW_MEASURE_SIZEINC");
CM_EVENT_SIZE = attr_atom_from_string("CM_EVENT_SIZE");
CM_INCOMING_CONNECTION = attr_atom_from_string("CM_INCOMING_CONNECTION");
CM_TRANSPORT_RELIABLE = attr_atom_from_string("CM_TRANSPORT_RELIABLE");
CM_IP_INTERFACE = attr_atom_from_string("IP_INTERFACE");
}
/* initialize data structs */
cm->transports = NULL;
cm->initialized = 0;
cm->reference_count = 1;
uint64_t seed = getpid() + time(NULL);
srand48(seed);
cm->CManager_ID = (int)lrand48();
char *tmp;
if ((tmp = getenv("CMControlModule"))) {
control_module = tmp;
}
if (control_module != NULL) {
char *tmp = strdup(control_module);
char *c;
for (c = tmp; *c; ++c) *c = tolower(*c);
#ifdef HAVE_SYS_EPOLL_H
if (strcmp(tmp, "epoll") == 0) {
cm->control_module_choice = "epoll";
} else
#endif
if (strcmp(tmp, "select") == 0) {
cm->control_module_choice = "select";
} else {
fprintf(stderr, "Warning: Specified CM/EVPath control module \"%s\" unknown or not built.\n", control_module);
/* force to default */
control_module = NULL;
}
free(tmp);
}
if (control_module == NULL) {
#ifdef HAVE_SYS_EPOLL_H
cm->control_module_choice = "epoll";
#else
cm->control_module_choice = "select";
#endif
}
cm->control_list = CMControlList_create();
thr_mutex_init(cm->exchange_lock);
cm->locked = 0;
cm->closed = 0;
cm->abort_read_ahead = 0;
cm->CMTrace_file = NULL;
CMtrace_init(cm, EVerbose);
CMinit_local_formats(cm);
thr_mutex_init(cm->context_lock);
cm->in_format_count = 0;
cm->in_formats = INT_CMmalloc(1);
cm->reg_format_count = 0;
cm->reg_formats = INT_CMmalloc(1);
cm->pending_request_max = 1;
cm->pbio_requests = INT_CMmalloc(sizeof(struct _pending_format_requests));
cm->pbio_requests[0].server_id = NULL;
cm->pbio_requests[0].id_length = 0;
cm->pbio_requests[0].condition = 0;
cm->pbio_requests[0].top_request = 0;
cm->connection_count = 0;
cm->connections = INT_CMmalloc(1);
cm->reg_user_format_count = 0;
cm->reg_user_formats = INT_CMmalloc(1);
cm->cm_buffer_list = NULL;
cm->pending_data_queue = NULL;
cm->contact_lists = NULL;
cm->shutdown_functions = NULL;
cm->perf_upcall = NULL;
INT_CMadd_poll(cm, process_pending_queue, NULL);
#ifdef EV_INTERNAL_H
CManager_lock(cm);
EVPinit(cm);
CManager_unlock(cm);
#endif
return cm;
}
static void CMControlList_free(CManager cm, CMControlList cl);
static void remove_conn_from_CM(CManager cm, CMConnection conn);
static void
CManager_free(CManager cm)
{
int i;
CMbuffer list = NULL;
INT_CMfree(cm->transports);
cm->transports = NULL;
/* free_FFSContext(cm->FFScontext);*/
cm->FFScontext = NULL;
INT_CMfree(cm->in_formats);
for (i=0 ; i < cm->reg_format_count; i++) {
INT_CMfree(cm->reg_formats[i]->format_name);
INT_CMfree(cm->reg_formats[i]);
}
INT_CMfree(cm->reg_formats);
/*
* Applications are expected to free the user contexts that
* they request. If they do this, there will be no user formats to
* free at this point. (Doing so might result in double freeing.)
*/
INT_CMfree(cm->reg_user_formats);
INT_CMfree(cm->pbio_requests);
INT_CMfree(cm->connections);
thr_mutex_free(cm->exchange_lock);
thr_mutex_free(cm->context_lock);
if (cm->contact_lists != NULL) {
i = 0;
while(cm->contact_lists[i] != NULL) {
INT_CMfree_attr_list(cm, cm->contact_lists[i]);
i++;
}
INT_CMfree(cm->contact_lists);
}
list = cm->cm_buffer_list;
i=0;
while (list != NULL) {
CMbuffer next = list->next;
CMtrace_out(cm, CMBufferVerbose, "Final buffer disposition buf %d, %p, size %zd, ref_count %d\n", i++, list, list->size, list->ref_count);
if (list->return_callback) {
(list->return_callback)(list->return_callback_data);
} else {
INT_CMfree(list->buffer);
}
INT_CMfree(list);
list = next;
}
cm->cm_buffer_list = NULL;
if (cm->shutdown_functions) INT_CMfree(cm->shutdown_functions);
INT_CMfree(cm->avail);
INT_CMfree(cm);
}
extern void
INT_CMinstall_perf_upcall(CManager cm, CMperf_upcall upcall)
{
cm->perf_upcall = upcall;
}
extern void
INT_CManager_close(CManager cm)
{
CMControlList cl = cm->control_list;
CMtrace_out(cm, CMFreeVerbose, "CManager %p closing, ref count %d\n", cm,
cm->reference_count);
CMtrace_out(cm, CMFreeVerbose, "CMControlList close CL=%p current reference count will be %d, sdp = %p\n",
cl, cl->cl_reference_count - 1, cl->select_data);
INT_CMControlList_close(cl, cm);
CMtrace_out(cm, CMFreeVerbose, "CMControlList CL=%p is closed\n", cl);
while (cm->connection_count != 0) {
/* connections are moved down as they are closed... */
CMtrace_out(cm, CMFreeVerbose, "CManager in close, closing connection %p , ref count %d\n", cm->connections[0],
cm->connections[0]->conn_ref_count);
internal_connection_close(cm->connections[0]);
INT_CMConnection_failed(cm->connections[0]);
}
if (cm->shutdown_functions != NULL) {
func_entry *shutdown_functions = cm->shutdown_functions;
int i = 0;
while (shutdown_functions[i].func != NULL) {
if (shutdown_functions[i].task_type == SHUTDOWN_TASK) {
CMtrace_out(cm, CMFreeVerbose, "CManager calling shutdown function SHUTDOWN %d, %p\n", i, shutdown_functions[i].func);
shutdown_functions[i].func(cm, shutdown_functions[i].client_data);
shutdown_functions[i].task_type = NO_TASK;
}
i++;
}
}
cm->reference_count--;
CMtrace_out(cm, CMFreeVerbose, "CManager %p ref count now %d\n",
cm, cm->reference_count);
if (cm->reference_count == 0) {
if (cm->shutdown_functions != NULL) {
int i = 0;
func_entry *shutdown_functions = cm->shutdown_functions;
cm->shutdown_functions = NULL;
while (shutdown_functions[i].func != NULL) {
i++;
}
i--;
for ( ; i >= 0; i--) {
if (shutdown_functions[i].task_type == FREE_TASK) {
CMtrace_out(cm, CMFreeVerbose, "CManager calling shutdown function FREE %d, %p\n", i, shutdown_functions[i].func);
shutdown_functions[i].func(cm, shutdown_functions[i].client_data);
shutdown_functions[i].func = NULL;
}
}
INT_CMfree(shutdown_functions);
}
CMtrace_out(cm, CMFreeVerbose, "Freeing CManager %p\n", cm);
cl->free_reference_count = 1;
CMControlList_free(cm, cl);
CManager_unlock(cm);
CManager_free(cm);
} else {
CManager_unlock(cm);
}
}
extern void
internal_add_shutdown_task(CManager cm, CMPollFunc func, void *client_data, int task_type)
{
int func_count = 0;
if (!cm->control_list->select_initialized) {
CM_init_select(cm->control_list, cm);
}