openpmix-src 0.1.0-rc.1

Vendored build of OpenPMIx, developed for use by the Lamellar runtime.
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2014-2020 Intel, Inc.  All rights reserved.
 * Copyright (c) 2017-2019 Research Organization for Information Science
 *                         and Technology (RIST).  All rights reserved.
 * Copyright (c) 2017      IBM Corporation. All rights reserved.
 *
 * Copyright (c) 2021-2023 Nanook Consulting.  All rights reserved.
 * Copyright (c) 2023      Triad National Security, LLC. All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */
#include "src/include/pmix_config.h"

#include "include/pmix.h"
#include "pmix_common.h"
#include "include/pmix_server.h"

#include "src/threads/pmix_threads.h"
#include "src/util/pmix_error.h"
#include "src/util/pmix_name_fns.h"
#include "src/util/pmix_output.h"

#include "src/client/pmix_client_ops.h"
#include "src/include/pmix_globals.h"
#include "src/mca/bfrops/bfrops.h"
#include "src/server/pmix_server_ops.h"

static void progress_local_event_hdlr(pmix_status_t status, pmix_info_t *results, size_t nresults,
                                      pmix_op_cbfunc_t cbfunc, void *thiscbdata,
                                      void *notification_cbdata);

/* if we are a client, we call this function to notify the server of
 * an event. If we are a server, our host RM will call this function
 * to notify us of an event */
PMIX_EXPORT pmix_status_t PMIx_Notify_event(pmix_status_t status, const pmix_proc_t *source,
                                            pmix_data_range_t range, const pmix_info_t info[],
                                            size_t ninfo, pmix_op_cbfunc_t cbfunc, void *cbdata)
{
    int rc;

    PMIX_ACQUIRE_THREAD(&pmix_global_lock);

    if (pmix_globals.init_cntr <= 0) {
        PMIX_RELEASE_THREAD(&pmix_global_lock);
        return PMIX_ERR_INIT;
    }

    if (PMIX_PEER_IS_SERVER(pmix_globals.mypeer) ||
        PMIX_PEER_IS_TOOL(pmix_globals.mypeer)) {
        PMIX_RELEASE_THREAD(&pmix_global_lock);

        pmix_output_verbose(2, pmix_server_globals.event_output,
                            "pmix_server_notify_event source = %s:%d event_status = %s",
                            (NULL == source) ? "UNKNOWN" : source->nspace,
                            (NULL == source) ? PMIX_RANK_WILDCARD : source->rank,
                            PMIx_Error_string(status));

        rc = pmix_server_notify_client_of_event(status, source, range, info, ninfo, cbfunc, cbdata);

        if (PMIX_SUCCESS != rc && PMIX_OPERATION_SUCCEEDED != rc) {
            PMIX_ERROR_LOG(rc);
        }
        if (PMIX_PEER_IS_SERVER(pmix_globals.mypeer) && !PMIX_PEER_IS_TOOL(pmix_globals.mypeer)) {
            return rc;
        }
        PMIX_ACQUIRE_THREAD(&pmix_global_lock);
    }

    /* if we aren't connected, don't attempt to send */
    if (!pmix_globals.connected && PMIX_RANGE_PROC_LOCAL != range) {
        PMIX_RELEASE_THREAD(&pmix_global_lock);
        return PMIX_ERR_UNREACH;
    }
    PMIX_RELEASE_THREAD(&pmix_global_lock);
    pmix_output_verbose(2, pmix_client_globals.event_output,
                        "pmix_client_notify_event source = %s:%d event_status =%d",
                        (NULL == source) ? pmix_globals.myid.nspace : source->nspace,
                        (NULL == source) ? pmix_globals.myid.rank : source->rank, status);

    rc = pmix_notify_server_of_event(status, source, range, info, ninfo, cbfunc, cbdata, true);
    if (PMIX_SUCCESS != rc) {
        PMIX_ERROR_LOG(rc);
    }
    return rc;
}

static void notify_event_cbfunc(struct pmix_peer_t *pr,
                                pmix_ptl_hdr_t *hdr,
                                pmix_buffer_t *buf,
                                void *cbdata)
{
    (void) hdr;
    pmix_status_t rc, ret = PMIX_ERR_LOST_CONNECTION;
    int32_t cnt = 1;
    pmix_cb_t *cb = (pmix_cb_t *) cbdata;

    if (0 < buf->bytes_used) {
        /* unpack the status */
        PMIX_BFROPS_UNPACK(rc, pr, buf, &ret, &cnt, PMIX_STATUS);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            ret = rc;
        }
    }
    /* do the cback */
    if (NULL != cb->cbfunc.opfn) {
        cb->cbfunc.opfn(ret, cb->cbdata);
    }
    PMIX_RELEASE(cb);
}

pmix_status_t pmix_notify_event_cache(pmix_notify_caddy_t *cd)
{
    pmix_status_t rc;
    int j;
    pmix_notify_caddy_t *pk;
    int idx;
    time_t etime;

    /* add to our cache */
    rc = pmix_hotel_checkin(&pmix_globals.notifications, cd, &cd->room);
    /* if there wasn't room, then search for the longest tenured
     * occupant and evict them */
    if (PMIX_SUCCESS != rc) {
        etime = 0;
        idx = -1;
        for (j = 0; j < pmix_globals.max_events; j++) {
            pmix_hotel_knock(&pmix_globals.notifications, j, (void **) &pk);
            if (NULL == pk) {
                /* hey, there is room! */
                pmix_hotel_checkin_with_res(&pmix_globals.notifications, cd, &cd->room);
                return PMIX_SUCCESS;
            }
            /* check the age */
            if (0 == j) {
                etime = pk->ts;
                idx = j;
            } else {
                if (difftime(pk->ts, etime) < 0) {
                    etime = pk->ts;
                    idx = j;
                }
            }
        }
        if (0 <= idx) {
            /* we found the oldest occupant - evict it */
            pmix_hotel_checkout_and_return_occupant(&pmix_globals.notifications, idx,
                                                    (void **) &pk);
            PMIX_RELEASE(pk);
            rc = pmix_hotel_checkin(&pmix_globals.notifications, cd, &cd->room);
        }
    }
    return rc;
}

/* as a client, we pass the notification to our server */
pmix_status_t pmix_notify_server_of_event(pmix_status_t status, const pmix_proc_t *source,
                                          pmix_data_range_t range, const pmix_info_t info[],
                                          size_t ninfo, pmix_op_cbfunc_t cbfunc, void *cbdata,
                                          bool dolocal)
{
    pmix_status_t rc;
    pmix_buffer_t *msg = NULL;
    pmix_cmd_t cmd = PMIX_NOTIFY_CMD;
    pmix_cb_t *cb;
    pmix_event_chain_t *chain = NULL;
    size_t n;
    bool holdcd;
    pmix_notify_caddy_t *cd;

    pmix_output_verbose(2, pmix_client_globals.event_output,
                        "[%s:%d] client: notifying server %s:%d of status %s for range %s",
                        pmix_globals.myid.nspace, pmix_globals.myid.rank,
                        pmix_client_globals.myserver->info->pname.nspace,
                        pmix_client_globals.myserver->info->pname.rank, PMIx_Error_string(status),
                        PMIx_Data_range_string(range));

    holdcd = true;
    if (0 < ninfo) {
        /* check for caching instructions */
        for (n = 0; n < ninfo; n++) {
            if (PMIX_CHECK_KEY(&info[n], PMIX_EVENT_DO_NOT_CACHE)) {
                if (PMIX_INFO_TRUE(&info[n])) {
                    holdcd = false;
                }
                break;
            }
        }
    }

    if (PMIX_RANGE_PROC_LOCAL != range) {
        /* create the msg object */
        msg = PMIX_NEW(pmix_buffer_t);
        if (NULL == msg) {
            return PMIX_ERR_NOMEM;
        }
        /* pack the command */
        PMIX_BFROPS_PACK(rc, pmix_client_globals.myserver, msg, &cmd, 1, PMIX_COMMAND);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            goto cleanup;
        }
        /* pack the status */
        PMIX_BFROPS_PACK(rc, pmix_client_globals.myserver, msg, &status, 1, PMIX_STATUS);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            goto cleanup;
        }
        /* no need to pack the source as it is us */

        /* pack the range */
        PMIX_BFROPS_PACK(rc, pmix_client_globals.myserver, msg, &range, 1, PMIX_DATA_RANGE);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            goto cleanup;
        }
        /* pack the info */
        PMIX_BFROPS_PACK(rc, pmix_client_globals.myserver, msg, &ninfo, 1, PMIX_SIZE);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            goto cleanup;
        }
        if (0 < ninfo) {
            PMIX_BFROPS_PACK(rc, pmix_client_globals.myserver, msg, info, ninfo, PMIX_INFO);
            if (PMIX_SUCCESS != rc) {
                PMIX_ERROR_LOG(rc);
                goto cleanup;
            }
        }
    }

    if (dolocal) {
        /* setup for our own local callbacks */
        chain = PMIX_NEW(pmix_event_chain_t);
        chain->status = status;
        chain->range = range;
        if (NULL == source) {
            PMIX_LOAD_PROCID(&chain->source, pmix_globals.myid.nspace, pmix_globals.myid.rank);
        } else {
            PMIX_LOAD_PROCID(&chain->source, source->nspace, source->rank);
        }
        /* we always leave space for event hdlr name and a callback object */
        chain->nallocated = ninfo + 2;
        PMIX_INFO_CREATE(chain->info, chain->nallocated);
        /* prep the chain for processing */
        pmix_prep_event_chain(chain, info, ninfo, true);

        if (PMIX_RANGE_PROC_LOCAL == range && holdcd) {
            /* we need to cache this event so we can pass it into
             * ourselves should someone later register for it */
            cd = PMIX_NEW(pmix_notify_caddy_t);
            cd->status = status;
            PMIX_LOAD_PROCID(&cd->source, chain->source.nspace, chain->source.rank);
            cd->range = chain->range;
            if (0 < chain->ninfo) {
                cd->ninfo = chain->ninfo;
                PMIX_INFO_CREATE(cd->info, cd->ninfo);
                cd->nondefault = chain->nondefault;
                /* need to copy the info */
                for (n = 0; n < cd->ninfo; n++) {
                    PMIX_INFO_XFER(&cd->info[n], &chain->info[n]);
                }
            }
            if (NULL != chain->targets) {
                cd->ntargets = chain->ntargets;
                PMIX_PROC_CREATE(cd->targets, cd->ntargets);
                memcpy(cd->targets, chain->targets, cd->ntargets * sizeof(pmix_proc_t));
            }
            if (NULL != chain->affected) {
                cd->naffected = chain->naffected;
                PMIX_PROC_CREATE(cd->affected, cd->naffected);
                if (NULL == cd->affected) {
                    cd->naffected = 0;
                    rc = PMIX_ERR_NOMEM;
                    goto cleanup;
                }
                memcpy(cd->affected, chain->affected, cd->naffected * sizeof(pmix_proc_t));
            }
            rc = pmix_notify_event_cache(cd);
            if (PMIX_SUCCESS != rc) {
                PMIX_ERROR_LOG(rc);
                PMIX_RELEASE(cd);
                goto cleanup;
            }
            chain->cached = true;
        }
    }

    if (PMIX_RANGE_PROC_LOCAL != range && NULL != msg) {
        /* if this is a "lost-connection" event, then there is no
         * server to pass it to! Likewise, we don't pass it to
         * ourselves */
        if (PMIX_ERR_LOST_CONNECTION == status ||
            pmix_globals.mypeer == pmix_client_globals.myserver) {
            PMIX_RELEASE(msg);
            goto local;
        }
        /* create a callback object as we need to pass it to the
         * recv routine so we know which callback to use when
         * the server acks/nacks the register events request. The
         * server will _not_ send this notification back to us,
         * so we handle it locally */
        cb = PMIX_NEW(pmix_cb_t);
        cb->cbfunc.opfn = cbfunc;
        cb->cbdata = cbdata;
        /* send to the server */
        pmix_output_verbose(2, pmix_client_globals.event_output,
                            "[%s:%d] client: notifying server %s:%d - sending",
                            pmix_globals.myid.nspace, pmix_globals.myid.rank,
                            pmix_client_globals.myserver->info->pname.nspace,
                            pmix_client_globals.myserver->info->pname.rank);
        PMIX_PTL_SEND_RECV(rc, pmix_client_globals.myserver, msg, notify_event_cbfunc, cb);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
            PMIX_RELEASE(cb);
            goto cleanup;
        }
    } else if (NULL != cbfunc) {
        cbfunc(PMIX_SUCCESS, cbdata);
    }

local:
    if (dolocal) {
        /* now notify any matching registered callbacks we have */
        pmix_invoke_local_event_hdlr(chain);
    }

    return PMIX_SUCCESS;

cleanup:
    pmix_output_verbose(2, pmix_client_globals.event_output,
                        "client: notifying server - unable to send");
    if (NULL != msg) {
        PMIX_RELEASE(msg);
    }
    /* we were unable to send anything, so we just return the error */
    return rc;
}

static void cycle_events(int sd, short args, void *cbdata)
{
    pmix_event_chain_t *chain = (pmix_event_chain_t *) cbdata;
    size_t n, nsave, cnt;
    pmix_list_item_t *item;
    pmix_event_hdlr_t *nxt;
    pmix_info_t *newinfo;

    pmix_output_verbose(2, pmix_client_globals.event_output,
                        "%s progressing local event with status %s",
                        PMIX_NAME_PRINT(&pmix_globals.myid),
                        PMIx_Error_string(chain->interim_status));

    PMIX_HIDE_UNUSED_PARAMS(sd, args);

    /* aggregate the results per RFC0018 - first search the
     * prior chained results to see if any keys have been NULL'd
     * as this indicates that info struct should be removed */
    nsave = 0;
    for (n = 0; n < chain->nresults; n++) {
        if (0 < strlen(chain->results[n].key)) {
            ++nsave;
        }
    }
    /* we have to at least record the status returned by each
     * stage of the event handler chain, so we have to reallocate
     * the array to make space */

    /* add in any new results plus space for the returned status */
    nsave += chain->ninterim + 1;
    /* create the new space */
    PMIX_INFO_CREATE(newinfo, nsave);
    /* transfer over the prior data */
    cnt = 0;
    for (n = 0; n < chain->nresults; n++) {
        if (0 < strlen(chain->results[n].key)) {
            PMIX_INFO_XFER(&newinfo[cnt], &chain->results[n]);
            ++cnt;
        }
    }

    /* save this handler's returned status */
    if (NULL != chain->evhdlr && NULL != chain->evhdlr->name) {
        pmix_strncpy(newinfo[cnt].key, chain->evhdlr->name, PMIX_MAX_KEYLEN);
    } else {
        pmix_strncpy(newinfo[cnt].key, "UNKNOWN", PMIX_MAX_KEYLEN);
    }
    newinfo[cnt].value.type = PMIX_STATUS;
    newinfo[cnt].value.data.status = chain->status;
    ++cnt;
    /* transfer across the new results */
    for (n = 0; n < chain->ninterim; n++) {
        PMIX_INFO_XFER(&newinfo[cnt], &chain->interim[n]);
        ++cnt;
    }
    /* release the prior results */
    if (0 < chain->nresults) {
        PMIX_INFO_FREE(chain->results, chain->nresults);
    }
    /* pass along the new ones */
    chain->results = newinfo;
    chain->nresults = cnt;
    /* clear any loaded name and object */
    if (chain->nallocated > chain->ninfo) {
        chain->ninfo = chain->nallocated - 2;
        PMIX_INFO_DESTRUCT(&chain->info[chain->nallocated - 2]);
        PMIX_INFO_DESTRUCT(&chain->info[chain->nallocated - 1]);
    }
    // call their interim cbfunc
    if (NULL != chain->opcbfunc) {
        chain->opcbfunc(chain->interim_status, chain->cbdata);
    }

    /* if the caller indicates that the chain is completed,
     * or we completed the "last" event */
    if (PMIX_EVENT_ACTION_COMPLETE == chain->interim_status ||
        PMIX_EVENT_ORDER_LAST_OVERALL == chain->evhdlr->precedence || chain->endchain) {
        if (PMIX_EVENT_ACTION_COMPLETE == chain->interim_status) {
            chain->interim_status = PMIX_SUCCESS;
        }
        if (chain->evhdlr->oneshot) {
            /* remove this handler */
            pmix_deregister_event_hdlr(chain->evhdlr->index, NULL);
        }
        goto complete;
    }
    item = NULL;

    /* see if we need to continue, starting with the single code events */
    if (1 == chain->evhdlr->ncodes) {
        /* the last handler was for a single code - see if there are
         * any others that match this event */
        if (PMIX_EVENT_ORDER_FIRST_OVERALL == chain->evhdlr->precedence) {
            item = pmix_list_get_begin(&pmix_globals.events.single_events);
        } else {
            item = &chain->evhdlr->super;
        }
        while (pmix_list_get_end(&pmix_globals.events.single_events) != (item = pmix_list_get_next(item))) {
            nxt = (pmix_event_hdlr_t *) item;
            if (nxt->codes[0] == chain->status && pmix_notify_check_range(&nxt->rng, &chain->source)
                && pmix_notify_check_affected(nxt->affected, nxt->naffected, chain->affected,
                                              chain->naffected)) {
                chain->evhdlr = nxt;
                /* reset our count to the info provided by the caller */
                chain->ninfo = chain->nallocated - 2;
                /* if the handler has a name, then provide it */
                if (NULL != chain->evhdlr->name) {
                    PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                                   chain->evhdlr->name, PMIX_STRING);
                    chain->ninfo++;
                }

                /* if there is an evhdlr cbobject, provide it */
                if (NULL != chain->evhdlr->cbobject) {
                    PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                                   chain->evhdlr->cbobject, PMIX_POINTER);
                    chain->ninfo++;
                }
                nxt->evhdlr(nxt->index, chain->status, &chain->source, chain->info, chain->ninfo,
                            chain->results, chain->nresults, progress_local_event_hdlr,
                            (void *) chain);
                return;
            }
        }
        /* if we get here, then there are no more single code
         * events that match */
        item = pmix_list_get_begin(&pmix_globals.events.multi_events);
    }

    /* see if we need to continue with the multi code events */
    if (NULL != chain->evhdlr->codes || NULL != item) {
        /* the last handler was for a multi-code event, or we exhausted
         * all the single code events */
        if (PMIX_EVENT_ORDER_FIRST_OVERALL == chain->evhdlr->precedence) {
            item = pmix_list_get_begin(&pmix_globals.events.multi_events);
        } else if (NULL == item) {
            /* if the last handler was multi-code, then start from that point */
            item = &chain->evhdlr->super;
        }
        while (pmix_list_get_end(&pmix_globals.events.multi_events)
               != (item = pmix_list_get_next(item))) {
            nxt = (pmix_event_hdlr_t *) item;
            if (!pmix_notify_check_range(&nxt->rng, &chain->source)
                || !pmix_notify_check_affected(nxt->affected, nxt->naffected, chain->affected,
                                               chain->naffected)) {
                continue;
            }
            for (n = 0; n < nxt->ncodes; n++) {
                /* if this event handler provided a range, check to see if
                 * the source fits within it */
                if (nxt->codes[n] == chain->status) {
                    chain->evhdlr = nxt;
                    /* reset our count to the info provided by the caller */
                    chain->ninfo = chain->nallocated - 2;
                    /* if the handler has a name, then provide it */
                    if (NULL != chain->evhdlr->name) {
                        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                                       chain->evhdlr->name, PMIX_STRING);
                        chain->ninfo++;
                    }

                    /* if there is an evhdlr cbobject, provide it */
                    if (NULL != chain->evhdlr->cbobject) {
                        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                                       chain->evhdlr->cbobject, PMIX_POINTER);
                        chain->ninfo++;
                    }
                    nxt->evhdlr(nxt->index, chain->status, &chain->source, chain->info,
                                chain->ninfo, chain->results, chain->nresults,
                                progress_local_event_hdlr, (void *) chain);
                    return;
                }
            }
        }
        /* if we get here, then there are no more multi-mode
         * events that match */
        item = pmix_list_get_begin(&pmix_globals.events.default_events);
    }

    /* if they didn't want it to go to a default handler, then ignore them */
    if (!chain->nondefault) {
        if (PMIX_EVENT_ORDER_FIRST_OVERALL == chain->evhdlr->precedence) {
            item = pmix_list_get_begin(&pmix_globals.events.default_events);
        } else if (NULL == item) {
            item = &chain->evhdlr->super;
        }
        if (pmix_list_get_end(&pmix_globals.events.default_events) != (item = pmix_list_get_next(item))) {
            nxt = (pmix_event_hdlr_t *) item;
            /* if this event handler provided a range, check to see if
             * the source fits within it */
            if (pmix_notify_check_range(&nxt->rng, &chain->source)
                && pmix_notify_check_affected(nxt->affected, nxt->naffected, chain->affected,
                                              chain->naffected)) {
                chain->evhdlr = nxt;
                /* reset our count to the info provided by the caller */
                chain->ninfo = chain->nallocated - 2;
                /* if the handler has a name, then provide it */
                if (NULL != chain->evhdlr->name) {
                    PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                                   chain->evhdlr->name, PMIX_STRING);
                    chain->ninfo++;
                }

                /* if there is an evhdlr cbobject, provide it */
                if (NULL != chain->evhdlr->cbobject) {
                    PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                                   chain->evhdlr->cbobject, PMIX_POINTER);
                    chain->ninfo++;
                }
                nxt->evhdlr(nxt->index, chain->status, &chain->source, chain->info, chain->ninfo,
                            chain->results, chain->nresults, progress_local_event_hdlr,
                            (void *) chain);
                return;
            }
        }
    }

    /* if we registered a "last" handler, and it fits the given range
     * and code, then invoke it now */
    if (NULL != pmix_globals.events.last
        && pmix_notify_check_range(&pmix_globals.events.last->rng, &chain->source)
        && pmix_notify_check_affected(pmix_globals.events.last->affected,
                                      pmix_globals.events.last->naffected, chain->affected,
                                      chain->naffected)) {
        chain->endchain = true; // ensure we don't do this again
        if (1 == pmix_globals.events.last->ncodes
            && pmix_globals.events.last->codes[0] == chain->status) {
            chain->evhdlr = pmix_globals.events.last;
            /* reset our count to the info provided by the caller */
            chain->ninfo = chain->nallocated - 2;
            /* if the handler has a name, then provide it */
            if (NULL != chain->evhdlr->name) {
                PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                               chain->evhdlr->name, PMIX_STRING);
                chain->ninfo++;
            }

            /* if there is an evhdlr cbobject, provide it */
            if (NULL != chain->evhdlr->cbobject) {
                PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                               chain->evhdlr->cbobject, PMIX_POINTER);
                chain->ninfo++;
            }
            chain->evhdlr->evhdlr(chain->evhdlr->index, chain->status, &chain->source, chain->info,
                                  chain->ninfo, chain->results, chain->nresults,
                                  progress_local_event_hdlr, (void *) chain);
            return;
        } else if (NULL != pmix_globals.events.last->codes) {
            /* need to check if this code is included in the array */
            for (n = 0; n < pmix_globals.events.last->ncodes; n++) {
                if (pmix_globals.events.last->codes[n] == chain->status) {
                    chain->evhdlr = pmix_globals.events.last;
                    /* reset our count to the info provided by the caller */
                    chain->ninfo = chain->nallocated - 2;
                    /* if the handler has a name, then provide it */
                    if (NULL != chain->evhdlr->name) {
                        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                                       chain->evhdlr->name, PMIX_STRING);
                        chain->ninfo++;
                    }

                    /* if there is an evhdlr cbobject, provide it */
                    if (NULL != chain->evhdlr->cbobject) {
                        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                                       chain->evhdlr->cbobject, PMIX_POINTER);
                        chain->ninfo++;
                    }
                    chain->evhdlr->evhdlr(chain->evhdlr->index, chain->status, &chain->source,
                                          chain->info, chain->ninfo, chain->results,
                                          chain->nresults, progress_local_event_hdlr,
                                          (void *) chain);
                    return;
                }
            }
        } else {
            /* gets run for all codes */
            chain->evhdlr = pmix_globals.events.last;
            /* reset our count to the info provided by the caller */
            chain->ninfo = chain->nallocated - 2;
            /* if the handler has a name, then provide it */
            if (NULL != chain->evhdlr->name) {
                PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME,
                               chain->evhdlr->name, PMIX_STRING);
                chain->ninfo++;
            }

            /* if there is an evhdlr cbobject, provide it */
            if (NULL != chain->evhdlr->cbobject) {
                PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                               chain->evhdlr->cbobject, PMIX_POINTER);
                chain->ninfo++;
            }
            chain->evhdlr->evhdlr(chain->evhdlr->index, chain->status, &chain->source, chain->info,
                                  chain->ninfo, chain->results, chain->nresults,
                                  progress_local_event_hdlr, (void *) chain);
            return;
        }
    }

complete:
    /* if we get here, there was nothing more to do, but
     * we still have to call their final callback */
    if (NULL != chain->final_cbfunc) {
        chain->final_cbfunc(chain->interim_status, chain->final_cbdata);
        return;
    }
    /* maintain acctng */
    PMIX_RELEASE(chain);
}

static void progress_local_event_hdlr(pmix_status_t status, pmix_info_t *results, size_t nresults,
                                      pmix_op_cbfunc_t cbfunc, void *thiscbdata,
                                      void *notification_cbdata)
{
    /* this may be in the host's thread, so we need to threadshift it
     * before accessing our internal data */

    pmix_event_chain_t *chain = (pmix_event_chain_t *) notification_cbdata;

    chain->interim_status = status;
    chain->interim = results;
    chain->ninterim = nresults;
    chain->opcbfunc = cbfunc;
    chain->cbdata = thiscbdata;
    PMIX_THREADSHIFT(chain, cycle_events);
}

/* given notification of an event, cycle thru our list of
 * registered callbacks and invoke the matching ones. Note
 * that we will invoke the callbacks in order from single
 * to multi-event to default, keeping a log of any returned
 * info and passing it down to the next invoked event handler.
 * Thus, each handler is given the opportunity to see what
 * prior handlers did, and decide if anything further needs
 * to be done.
 */
void pmix_invoke_local_event_hdlr(pmix_event_chain_t *chain)
{
    /* We need to parse thru each registered handler and determine
     * which one(s) to call for the specific error */
    size_t i;
    pmix_event_hdlr_t *evhdlr;
    pmix_status_t rc = PMIX_SUCCESS;
    bool found;

    pmix_output_verbose(2, pmix_client_globals.event_output,
                        "%s invoke_local_event_hdlr for status %s",
                        PMIX_NAME_PRINT(&pmix_globals.myid),
                        PMIx_Error_string(chain->status));

    /* sanity check */
    if (NULL == chain->info) {
        /* should never happen as space must always be
         * reserved for handler name and callback object*/
        rc = PMIX_ERR_BAD_PARAM;
        pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                            PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
        goto complete;
    }

    /* if we are not a target, then we can simply ignore this event */
    if (NULL != chain->targets) {
        found = false;
        for (i = 0; i < chain->ntargets; i++) {
            pmix_output_verbose(8, pmix_client_globals.event_output, "%s CHECKING TARGET %s",
                                PMIX_NAME_PRINT(&pmix_globals.myid),
                                PMIX_NAME_PRINT(&chain->targets[i]));
            if (PMIX_CHECK_PROCID(&chain->targets[i], &pmix_globals.myid)) {
                found = true;
                break;
            }
        }
        if (!found) {
            pmix_output_verbose(8, pmix_client_globals.event_output,
                                "%s Ignoring event %s:%d",
                                PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
            goto complete;
        }
    }
    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);

    /* if we registered a "first" handler, and it fits the given range,
     * then invoke it first */
    if (NULL != pmix_globals.events.first) {
        if (1 == pmix_globals.events.first->ncodes
            && pmix_globals.events.first->codes[0] == chain->status
            && pmix_notify_check_range(&pmix_globals.events.first->rng, &chain->source)
            && pmix_notify_check_affected(pmix_globals.events.first->affected,
                                          pmix_globals.events.first->naffected, chain->affected,
                                          chain->naffected)) {
            /* invoke the handler */
            chain->evhdlr = pmix_globals.events.first;
            pmix_output_verbose(8, pmix_client_globals.event_output, "%s INVOKING FIRST %s:%d",
                                PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
            goto invk;
        } else if (NULL != pmix_globals.events.first->codes) {
            /* need to check if this code is included in the array */
            found = false;
            for (i = 0; i < pmix_globals.events.first->ncodes; i++) {
                if (pmix_globals.events.first->codes[i] == chain->status) {
                    found = true;
                    break;
                }
            }
            /* if this event handler provided a range, check to see if
             * the source fits within it */
            if (found && pmix_notify_check_range(&pmix_globals.events.first->rng, &chain->source)) {
                /* invoke the handler */
                chain->evhdlr = pmix_globals.events.first;
                pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                    PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                goto invk;
            }
        } else {
            /* take all codes for a default handler */
            if (pmix_notify_check_range(&pmix_globals.events.first->rng, &chain->source)) {
                /* invoke the handler */
                chain->evhdlr = pmix_globals.events.first;
                pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                    PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                goto invk;
            }
        }
        /* get here if there is no match, so fall thru */
    }
    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);

    /* cycle thru the single-event registrations first */
    PMIX_LIST_FOREACH (evhdlr, &pmix_globals.events.single_events, pmix_event_hdlr_t) {
        if (evhdlr->codes[0] == chain->status) {
            if (pmix_notify_check_range(&evhdlr->rng, &chain->source)
                && pmix_notify_check_affected(evhdlr->affected, evhdlr->naffected, chain->affected,
                                              chain->naffected)) {
                /* invoke the handler */
                chain->evhdlr = evhdlr;
                pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                    PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                goto invk;
            }
        }
    }

    /* if we didn't find any match in the single-event registrations,
     * then cycle thru the multi-event registrations next */
    PMIX_LIST_FOREACH (evhdlr, &pmix_globals.events.multi_events, pmix_event_hdlr_t) {
        for (i = 0; i < evhdlr->ncodes; i++) {
            if (evhdlr->codes[i] == chain->status) {
                if (pmix_notify_check_range(&evhdlr->rng, &chain->source)
                    && pmix_notify_check_affected(evhdlr->affected, evhdlr->naffected,
                                                  chain->affected, chain->naffected)) {
                    /* invoke the handler */
                    chain->evhdlr = evhdlr;
                    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                    goto invk;
                }
            }
        }
    }
    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);

    /* if they didn't want it to go to a default handler, then ignore them */
    if (!chain->nondefault) {
        /* pass it to any default handlers */
        PMIX_LIST_FOREACH (evhdlr, &pmix_globals.events.default_events, pmix_event_hdlr_t) {
            if (pmix_notify_check_range(&evhdlr->rng, &chain->source)
                && pmix_notify_check_affected(evhdlr->affected, evhdlr->naffected, chain->affected,
                                              chain->naffected)) {
                /* invoke the handler */
                chain->evhdlr = evhdlr;
                pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                    PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                goto invk;
            }
        }
    }

    /* if we registered a "last" handler, and it fits the given range
     * and code, then invoke it now */
    if (NULL != pmix_globals.events.last
        && pmix_notify_check_range(&pmix_globals.events.last->rng, &chain->source)
        && pmix_notify_check_affected(pmix_globals.events.last->affected,
                                      pmix_globals.events.last->naffected, chain->affected,
                                      chain->naffected)) {
        chain->endchain = true; // ensure we don't do this again
        if (1 == pmix_globals.events.last->ncodes
            && pmix_globals.events.last->codes[0] == chain->status) {
            chain->evhdlr = pmix_globals.events.last;
            pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
            goto invk;
        } else if (NULL != pmix_globals.events.last->codes) {
            /* need to check if this code is included in the array */
            for (i = 0; i < pmix_globals.events.last->ncodes; i++) {
                if (pmix_globals.events.last->codes[i] == chain->status) {
                    chain->evhdlr = pmix_globals.events.last;
                    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
                    goto invk;
                }
            }
        } else {
            /* gets run for all codes */
            chain->evhdlr = pmix_globals.events.last;
            pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                                PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
            goto invk;
        }
    }
    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                        PMIX_NAME_PRINT(&pmix_globals.myid),
                        __FILE__, __LINE__);

    /* if we got here, then nothing was found */
    rc = PMIX_ERR_NOT_FOUND;

complete:
    /* we still have to call their final callback */
    if (NULL != chain->final_cbfunc) {
        chain->final_cbfunc(rc, chain->final_cbdata);
    } else {
        PMIX_RELEASE(chain);
    }
    return;

invk:
    pmix_output_verbose(8, pmix_client_globals.event_output, "%s %s:%d",
                        PMIX_NAME_PRINT(&pmix_globals.myid), __FILE__, __LINE__);
    /* start with the chain holding only the given info */
    chain->ninfo = chain->nallocated - 2;

    /* if the handler has a name, then provide it */
    if (NULL != chain->evhdlr->name) {
        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_HDLR_NAME, chain->evhdlr->name,
                       PMIX_STRING);
        chain->ninfo++;
    }

    /* if there is an evhdlr cbobject, provide it */
    if (NULL != chain->evhdlr->cbobject) {
        PMIX_INFO_LOAD(&chain->info[chain->ninfo], PMIX_EVENT_RETURN_OBJECT,
                       chain->evhdlr->cbobject, PMIX_POINTER);
        chain->ninfo++;
    }

    /* invoke the handler */
    pmix_output_verbose(2, pmix_client_globals.event_output, "[%s:%d] INVOKING EVHDLR %s", __FILE__,
                        __LINE__, (NULL == chain->evhdlr->name) ? "NULL" : chain->evhdlr->name);
    chain->evhdlr->evhdlr(chain->evhdlr->index, chain->status, &chain->source, chain->info,
                          chain->ninfo, NULL, 0, progress_local_event_hdlr, (void *) chain);
    return;
}

static void local_cbfunc(pmix_status_t status, void *cbdata)
{
    pmix_notify_caddy_t *cd = (pmix_notify_caddy_t *) cbdata;

    if (NULL != cd->cbfunc) {
        cd->cbfunc(status, cd->cbdata);
    }
    PMIX_RELEASE(cd);
}

static void _notify_client_event(int sd, short args, void *cbdata)
{
    (void) sd;
    (void) args;
    pmix_notify_caddy_t *cd = (pmix_notify_caddy_t *) cbdata;
    pmix_regevents_info_t *reginfoptr;
    pmix_peer_events_info_t *pr;
    pmix_event_chain_t *chain;
    size_t n, nleft;
    bool matched, holdcd;
    pmix_buffer_t *bfr;
    pmix_cmd_t cmd = PMIX_NOTIFY_CMD;
    pmix_status_t rc;
    pmix_list_t trk;
    pmix_namelist_t *nm;
    pmix_namespace_t *nptr, *tmp;
    pmix_range_trkr_t rngtrk;
    pmix_proc_t proc;

    /* need to acquire the object from its originating thread */
    PMIX_ACQUIRE_OBJECT(cd);

    pmix_output_verbose(2, pmix_server_globals.event_output,
                        "pmix_server: _notify_client_event notifying clients of event %s range %s",
                        PMIx_Error_string(cd->status), PMIx_Data_range_string(cd->range));

    /* check for caching instructions */
    holdcd = true;
    if (0 < cd->ninfo) {
        /* check for caching instructions */
        for (n = 0; n < cd->ninfo; n++) {
            if (PMIX_CHECK_KEY(&cd->info[n], PMIX_EVENT_DO_NOT_CACHE)) {
                if (PMIX_INFO_TRUE(&cd->info[n])) {
                    holdcd = false;
                }
                break;
            }
        }
    }
    if (holdcd) {
        /* we cannot know if everyone who wants this notice has had a chance
         * to register for it - the notice may be coming too early. So cache
         * the message until all local procs have received it, or it ages to
         * the point where it gets pushed out by more recent events */
        PMIX_RETAIN(cd);
        rc = pmix_notify_event_cache(cd);
        if (PMIX_SUCCESS != rc) {
            PMIX_ERROR_LOG(rc);
        }
    }

    /* we may also have registered for events, so setup to check this
     * against our registrations */
    chain = PMIX_NEW(pmix_event_chain_t);
    chain->status = cd->status;
    if (holdcd) {
        chain->cached = true;
    }
    PMIX_LOAD_PROCID(&chain->source, cd->source.nspace, cd->source.rank);
    /* we always leave space for a callback object and
     * the evhandler name. */
    chain->nallocated = cd->ninfo + 2;
    PMIX_INFO_CREATE(chain->info, chain->nallocated);
    /* prep the chain for processing */
    pmix_prep_event_chain(chain, cd->info, cd->ninfo, true);
    cd->nondefault = chain->nondefault;
    /* if the range is PMIX_RANGE_RM, then we only process this
     * event ourselves - the PMIx server may aggregate the
     * events from a namespace prior to passing it to the host */
    if (PMIX_RANGE_RM == cd->range) {
        goto local;
    }
    /* if we are a tool, we send it to our clients regardless
     * of the range - we assume that if we are in range, then
     * so are any clients attached to us
     *
     * if we are a server but not a tool, then check the range
     * before sending it to our client */
    if (PMIX_PEER_IS_TOOL(pmix_globals.mypeer)) {
        /* remove the target designations */
        if (NULL != chain->targets) {
            free(chain->targets);
            chain->targets = NULL;
            chain->ntargets = 0;
        }
    }

    /* copy setup to the cd object */
    cd->nondefault = chain->nondefault;
    if (NULL != chain->targets) {
        cd->ntargets = chain->ntargets;
        PMIX_PROC_CREATE(cd->targets, cd->ntargets);
        memcpy(cd->targets, chain->targets, cd->ntargets * sizeof(pmix_proc_t));
        /* compute the number of targets that need to be notified */
        nleft = 0;
        for (n = 0; n < cd->ntargets; n++) {
            /* if this is a single proc, then increment by one */
            if (PMIX_RANK_VALID >= cd->targets[n].rank) {
                ++nleft;
            } else {
                /* look up the nspace for this proc */
                nptr = NULL;
                PMIX_LIST_FOREACH (tmp, &pmix_globals.nspaces, pmix_namespace_t) {
                    if (PMIX_CHECK_NSPACE(tmp->nspace, cd->targets[n].nspace)) {
                        nptr = tmp;
                        break;
                    }
                }
                /* if we don't yet know it, then nothing to do */
                if (NULL == nptr) {
                    nleft = SIZE_MAX;
                    break;
                }
                /* might notify all local members */
                nleft += nptr->nlocalprocs;
            }
        }
        cd->nleft = nleft;
    }
    if (NULL != chain->affected) {
        cd->naffected = chain->naffected;
        PMIX_PROC_CREATE(cd->affected, cd->naffected);
        if (NULL == cd->affected) {
            cd->naffected = 0;
            /* notify the caller */
            if (NULL != cd->cbfunc) {
                cd->cbfunc(PMIX_ERR_NOMEM, cd->cbdata);
            }
            PMIX_RELEASE(cd);
            PMIX_RELEASE(chain);
            return;
        }
        memcpy(cd->affected, chain->affected, cd->naffected * sizeof(pmix_proc_t));
    }

    /* if they provided a PMIX_EVENT_CUSTOM_RANGE info object but
     * specified a range other than PMIX_RANGE_CUSTOM, then this
     * is an error */
    if (PMIX_RANGE_CUSTOM != cd->range && NULL != cd->targets) {
        PMIX_ERROR_LOG(PMIX_ERR_BAD_PARAM);
        /* notify the caller */
        if (NULL != cd->cbfunc) {
            cd->cbfunc(PMIX_ERR_BAD_PARAM, cd->cbdata);
        }
        PMIX_RELEASE(cd);
        PMIX_RELEASE(chain);
        return;
    }

    holdcd = false;
    if (PMIX_RANGE_PROC_LOCAL != cd->range) {
        PMIX_CONSTRUCT(&trk, pmix_list_t);
        rngtrk.procs = NULL;
        rngtrk.nprocs = 0;
        /* cycle across our registered events and send the message to
         * any client who registered for it */
        PMIX_LIST_FOREACH (reginfoptr, &pmix_server_globals.events, pmix_regevents_info_t) {
            if ((PMIX_MAX_ERR_CONSTANT == reginfoptr->code && !cd->nondefault) ||
                cd->status == reginfoptr->code) {
                PMIX_LIST_FOREACH (pr, &reginfoptr->peers, pmix_peer_events_info_t) {
                    /* if this client was the source of the event, then
                     * don't send it back as they will have processed it
                     * when they generated it */
                    if (PMIX_CHECK_NAMES(&cd->source, &pr->peer->info->pname)) {
                        continue;
                    }
                    /* don't notify ourselves - we handle this internally */
                    if (PMIX_CHECK_NAMES(&pmix_globals.myid, &pr->peer->info->pname)) {
                        continue;
                    }
                    /* if we have already notified this client, then don't do it again */
                    matched = false;
                    PMIX_LIST_FOREACH (nm, &trk, pmix_namelist_t) {
                        if (nm->pname == &pr->peer->info->pname) {
                            matched = true;
                            break;
                        }
                    }
                    if (matched) {
                        continue;
                    }
                    /* check if the affected procs (if given) match those they
                     * wanted to know about */
                    if (!pmix_notify_check_affected(cd->affected, cd->naffected, pr->affected,
                                                    pr->naffected)) {
                        continue;
                    }
                    if (!PMIX_PEER_IS_TOOL(pmix_globals.mypeer) && NULL != cd->targets) {
                        rngtrk.procs = cd->targets;
                        rngtrk.nprocs = cd->ntargets;
                        rngtrk.range = cd->range;
                        PMIX_LOAD_PROCID(&proc, pr->peer->info->pname.nspace,
                                         pr->peer->info->pname.rank);
                        if (!pmix_notify_check_range(&rngtrk, &proc)) {
                            continue;
                        }
                    }
                    pmix_output_verbose(2, pmix_server_globals.event_output,
                                        "pmix_server: notifying client %s:%u on status %s",
                                        pr->peer->info->pname.nspace, pr->peer->info->pname.rank,
                                        PMIx_Error_string(cd->status));

                    /* record that we notified this client */
                    nm = PMIX_NEW(pmix_namelist_t);
                    nm->pname = &pr->peer->info->pname;
                    pmix_list_append(&trk, &nm->super);

                    bfr = PMIX_NEW(pmix_buffer_t);
                    if (NULL == bfr) {
                        continue;
                    }
                    /* pack the command */
                    PMIX_BFROPS_PACK(rc, pr->peer, bfr, &cmd, 1, PMIX_COMMAND);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_ERROR_LOG(rc);
                        PMIX_RELEASE(bfr);
                        continue;
                    }

                    /* pack the status */
                    PMIX_BFROPS_PACK(rc, pr->peer, bfr, &cd->status, 1, PMIX_STATUS);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_ERROR_LOG(rc);
                        PMIX_RELEASE(bfr);
                        continue;
                    }

                    /* pack the source */
                    PMIX_BFROPS_PACK(rc, pr->peer, bfr, &cd->source, 1, PMIX_PROC);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_ERROR_LOG(rc);
                        PMIX_RELEASE(bfr);
                        continue;
                    }
                    /* pack any info */
                    PMIX_BFROPS_PACK(rc, pr->peer, bfr, &cd->ninfo, 1, PMIX_SIZE);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_ERROR_LOG(rc);
                        PMIX_RELEASE(bfr);
                        continue;
                    }

                    if (0 < cd->ninfo) {
                        PMIX_BFROPS_PACK(rc, pr->peer, bfr, cd->info, cd->ninfo, PMIX_INFO);
                        if (PMIX_SUCCESS != rc) {
                            PMIX_ERROR_LOG(rc);
                            PMIX_RELEASE(bfr);
                            continue;
                        }
                    }
                    /* pack the range in case they need to relay */
                    PMIX_BFROPS_PACK(rc, pr->peer, bfr, &cd->range, 1, PMIX_DATA_RANGE);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_ERROR_LOG(rc);
                        PMIX_RELEASE(bfr);
                        continue;
                    }

                    PMIX_SERVER_QUEUE_REPLY(rc, pr->peer, 0, bfr);
                    if (PMIX_SUCCESS != rc) {
                        PMIX_RELEASE(bfr);
                    }
                    if (NULL != cd->targets && 0 < cd->nleft) {
                        /* track the number of targets we have left to notify */
                        --cd->nleft;
                        /* if the event was cached and this is the last one,
                         * then evict this event from the cache */
                        if (0 == cd->nleft) {
                            pmix_hotel_checkout(&pmix_globals.notifications, cd->room);
                            holdcd = false;
                            break;
                        }
                    }
                }
            }
        }
        PMIX_LIST_DESTRUCT(&trk);
        if (PMIX_RANGE_LOCAL != cd->range &&
            PMIX_CHECK_PROCID(&cd->source, &pmix_globals.myid)) {
            /* if we are the source, then we need to post this upwards as
             * well so the host RM can broadcast it as necessary */
            if (NULL != pmix_host_server.notify_event) {
                /* mark that we sent it upstairs so we don't release
                 * the caddy until we return from the host RM */
                rc = pmix_host_server.notify_event(cd->status, &cd->source, cd->range, cd->info,
                                                   cd->ninfo, local_cbfunc, cd);
                if (PMIX_SUCCESS == rc) {
                    holdcd = true;
                }
            }
        }
    }

local:
    /* process it ourselves */
    pmix_invoke_local_event_hdlr(chain);

    if (!holdcd) {
        /* notify the caller */
        if (NULL != cd->cbfunc) {
            cd->cbfunc(PMIX_SUCCESS, cd->cbdata);
        }
        PMIX_RELEASE(cd);
    }
}

/* as a server, we must do two things:
 *
 * (a) notify all clients that have registered for this event
 *
 * (b) callback any of our own functions that have registered
 *     for this event
 */
pmix_status_t pmix_server_notify_client_of_event(pmix_status_t status, const pmix_proc_t *source,
                                                 pmix_data_range_t range, const pmix_info_t info[],
                                                 size_t ninfo, pmix_op_cbfunc_t cbfunc,
                                                 void *cbdata)
{
    pmix_notify_caddy_t *cd;
    size_t n;

    pmix_output_verbose(2, pmix_server_globals.event_output,
                        "pmix_server: notify client of event %s range %s",
                        PMIx_Error_string(status), PMIx_Data_range_string(range));

    cd = PMIX_NEW(pmix_notify_caddy_t);
    cd->status = status;
    if (NULL == source) {
        PMIX_LOAD_PROCID(&cd->source, "UNDEF", PMIX_RANK_UNDEF);
    } else {
        PMIX_LOAD_PROCID(&cd->source, source->nspace, source->rank);
    }
    cd->range = range;
    /* have to copy the info to preserve it for future when cached */
    if (0 < ninfo && NULL != info) {
        cd->ninfo = ninfo;
        PMIX_INFO_CREATE(cd->info, cd->ninfo);
        /* need to copy the info */
        for (n = 0; n < cd->ninfo; n++) {
            PMIX_INFO_XFER(&cd->info[n], &info[n]);
        }
    }

    /* track the eventual callback info */
    cd->cbfunc = cbfunc;
    cd->cbdata = cbdata;

    pmix_output_verbose(2, pmix_server_globals.event_output,
                        "pmix_server_notify_event status =%d, source = %s:%d, ninfo =%lu", status,
                        cd->source.nspace, cd->source.rank, ninfo);

    /* we have to push this into our event library to avoid
     * potential threading issues */
    PMIX_THREADSHIFT(cd, _notify_client_event);
    return PMIX_SUCCESS;
}

bool pmix_notify_check_range(pmix_range_trkr_t *rng, const pmix_proc_t *proc)
{
    size_t n;

    if (PMIX_RANGE_UNDEF == rng->range || PMIX_RANGE_GLOBAL == rng->range
        || PMIX_RANGE_SESSION == rng->range
        || PMIX_RANGE_LOCAL == rng->range) { // assume RM took care of session & local for now
        return true;
    }
    if (PMIX_RANGE_NAMESPACE == rng->range) {
        for (n = 0; n < rng->nprocs; n++) {
            if (PMIX_CHECK_NSPACE(rng->procs[n].nspace, proc->nspace)) {
                return true;
            }
        }
        return false;
    }
    if (PMIX_RANGE_PROC_LOCAL == rng->range) {
        for (n = 0; n < rng->nprocs; n++) {
            if (PMIX_CHECK_PROCID(&rng->procs[n], proc)) {
                return true;
            }
        }
        return false;
    }
    if (PMIX_RANGE_CUSTOM == rng->range) {
        /* see if this proc was included */
        for (n = 0; n < rng->nprocs; n++) {
            if (0 != strncmp(rng->procs[n].nspace, proc->nspace, PMIX_MAX_NSLEN)) {
                continue;
            }
            if (PMIX_RANK_WILDCARD == rng->procs[n].rank || rng->procs[n].rank == proc->rank) {
                return true;
            }
        }
        /* if we get here, then this proc isn't in range */
        return false;
    }

    /* if it is anything else, then reject it */
    return false;
}

bool pmix_notify_check_affected(pmix_proc_t *interested, size_t ninterested, pmix_proc_t *affected,
                                size_t naffected)
{
    size_t m, n;

    /* if they didn't restrict their interests, then accept it */
    if (NULL == interested) {
        return true;
    }
    /* if we weren't given the affected procs, then accept it */
    if (NULL == affected) {
        return true;
    }
    /* check if the two overlap */
    for (n = 0; n < naffected; n++) {
        for (m = 0; m < ninterested; m++) {
            if (PMIX_CHECK_PROCID(&affected[n], &interested[m])) {
                return true;
            }
        }
    }
    /* if we get here, then this proc isn't in range */
    return false;
}

void pmix_event_timeout_cb(int fd, short flags, void *arg)
{
    (void) fd;
    (void) flags;
    pmix_event_chain_t *ch = (pmix_event_chain_t *) arg;

    /* need to acquire the object from its originating thread */
    PMIX_ACQUIRE_OBJECT(ch);

    ch->timer_active = false;

    /* remove it from the list */
    pmix_list_remove_item(&pmix_globals.cached_events, &ch->super);

    /* process this event thru the regular channels */
    if (PMIX_PEER_IS_SERVER(pmix_globals.mypeer) &&
        !PMIX_PEER_IS_LAUNCHER(pmix_globals.mypeer)) {
        pmix_server_notify_client_of_event(ch->status, &ch->source, ch->range,
                                           ch->info, ch->ninfo,
                                           ch->final_cbfunc, ch->final_cbdata);
    } else {
        pmix_invoke_local_event_hdlr(ch);
    }
}

pmix_status_t pmix_prep_event_chain(pmix_event_chain_t *chain, const pmix_info_t *info,
                                    size_t ninfo, bool xfer)
{
    size_t n;

    if (NULL != info && 0 < ninfo) {
        chain->ninfo = ninfo;
        if (NULL == chain->info) {
            PMIX_INFO_CREATE(chain->info, chain->ninfo);
        }
        /* need to copy the info */
        for (n = 0; n < ninfo; n++) {
            if (xfer) {
                /* chain doesn't already have a copy of the info */
                PMIX_INFO_XFER(&chain->info[n], &info[n]);
            }
            /* look for specific directives */
            if (0 == strncmp(info[n].key, PMIX_EVENT_NON_DEFAULT, PMIX_MAX_KEYLEN)) {
                chain->nondefault = PMIX_INFO_TRUE(&info[n]);
            } else if (PMIX_CHECK_KEY(&info[n], PMIX_EVENT_CUSTOM_RANGE)) {
                /* provides an array of pmix_proc_t identifying the procs
                 * that are to receive this notification, or a single pmix_proc_t  */
                if (PMIX_DATA_ARRAY == info[n].value.type && NULL != info[n].value.data.darray
                    && NULL != info[n].value.data.darray->array) {
                    chain->ntargets = info[n].value.data.darray->size;
                    PMIX_PROC_CREATE(chain->targets, chain->ntargets);
                    memcpy(chain->targets, info[n].value.data.darray->array,
                           chain->ntargets * sizeof(pmix_proc_t));
                } else if (PMIX_PROC == info[n].value.type) {
                    chain->ntargets = 1;
                    PMIX_PROC_CREATE(chain->targets, chain->ntargets);
                    memcpy(chain->targets, info[n].value.data.proc, sizeof(pmix_proc_t));
                } else {
                    /* this is an error */
                    PMIX_ERROR_LOG(PMIX_ERR_BAD_PARAM);
                    return PMIX_ERR_BAD_PARAM;
                }
            } else if (PMIX_CHECK_KEY(&info[n], PMIX_EVENT_AFFECTED_PROC)) {
                PMIX_PROC_CREATE(chain->affected, 1);
                if (NULL == chain->affected) {
                    return PMIX_ERR_NOMEM;
                }
                chain->naffected = 1;
                memcpy(chain->affected, info[n].value.data.proc, sizeof(pmix_proc_t));
            } else if (PMIX_CHECK_KEY(&info[n], PMIX_EVENT_AFFECTED_PROCS)) {
                chain->naffected = info[n].value.data.darray->size;
                PMIX_PROC_CREATE(chain->affected, chain->naffected);
                if (NULL == chain->affected) {
                    chain->naffected = 0;
                    return PMIX_ERR_NOMEM;
                }
                memcpy(chain->affected, info[n].value.data.darray->array,
                       chain->naffected * sizeof(pmix_proc_t));
            }
        }
    }
    return PMIX_SUCCESS;
}

/****    CLASS INSTANTIATIONS    ****/

static void sevcon(pmix_event_hdlr_t *p)
{
    p->name = NULL;
    p->index = UINT_MAX;
    p->precedence = PMIX_EVENT_ORDER_NONE;
    p->oneshot = false;
    p->locator = NULL;
    p->rng.range = PMIX_RANGE_UNDEF;
    p->rng.procs = NULL;
    p->rng.nprocs = 0;
    p->affected = NULL;
    p->naffected = 0;
    p->evhdlr = NULL;
    p->cbobject = NULL;
    p->codes = NULL;
    p->ncodes = 0;
}
static void sevdes(pmix_event_hdlr_t *p)
{
    if (NULL != p->name) {
        free(p->name);
    }
    if (NULL != p->locator) {
        free(p->locator);
    }
    if (NULL != p->rng.procs) {
        free(p->rng.procs);
    }
    if (NULL != p->affected) {
        PMIX_PROC_FREE(p->affected, p->naffected);
    }
    if (NULL != p->codes) {
        free(p->codes);
    }
}
PMIX_CLASS_INSTANCE(pmix_event_hdlr_t, pmix_list_item_t, sevcon, sevdes);

static void accon(pmix_active_code_t *p)
{
    p->nregs = 0;
}
PMIX_CLASS_INSTANCE(pmix_active_code_t, pmix_list_item_t, accon, NULL);

static void evcon(pmix_events_t *p)
{
    p->nhdlrs = 0;
    p->first = NULL;
    p->last = NULL;
    PMIX_CONSTRUCT(&p->actives, pmix_list_t);
    PMIX_CONSTRUCT(&p->single_events, pmix_list_t);
    PMIX_CONSTRUCT(&p->multi_events, pmix_list_t);
    PMIX_CONSTRUCT(&p->default_events, pmix_list_t);
}
static void evdes(pmix_events_t *p)
{
    if (NULL != p->first) {
        PMIX_RELEASE(p->first);
    }
    if (NULL != p->last) {
        PMIX_RELEASE(p->last);
    }
    PMIX_LIST_DESTRUCT(&p->actives);
    PMIX_LIST_DESTRUCT(&p->single_events);
    PMIX_LIST_DESTRUCT(&p->multi_events);
    PMIX_LIST_DESTRUCT(&p->default_events);
}
PMIX_CLASS_INSTANCE(pmix_events_t,
                    pmix_object_t,
                    evcon, evdes);

static void chcon(pmix_event_chain_t *p)
{
    p->timer_active = false;
    memset(p->source.nspace, 0, PMIX_MAX_NSLEN + 1);
    p->source.rank = PMIX_RANK_UNDEF;
    p->nondefault = false;
    p->endchain = false;
    p->cached = false;
    p->targets = NULL;
    p->ntargets = 0;
    p->range = PMIX_RANGE_UNDEF;
    p->affected = NULL;
    p->naffected = 0;
    p->info = NULL;
    p->ninfo = 0;
    p->nallocated = 0;
    p->interim_status = PMIX_ERROR;
    p->results = NULL;
    p->nresults = 0;
    p->interim = NULL;
    p->ninterim = 0;
    p->evhdlr = NULL;
    p->opcbfunc = NULL;
    p->cbdata = NULL;
    p->final_cbfunc = NULL;
    p->final_cbdata = NULL;
}
static void chdes(pmix_event_chain_t *p)
{
    if (p->timer_active) {
        pmix_event_del(&p->ev);
    }
    if (NULL != p->targets) {
        PMIX_PROC_FREE(p->targets, p->ntargets);
    }
    if (NULL != p->affected) {
        PMIX_PROC_FREE(p->affected, p->naffected);
    }
    if (NULL != p->info) {
        PMIX_INFO_FREE(p->info, p->nallocated);
    }
    if (NULL != p->results) {
        PMIX_INFO_FREE(p->results, p->nresults);
    }
}
PMIX_CLASS_INSTANCE(pmix_event_chain_t,
                    pmix_list_item_t,
                    chcon, chdes);