File: gasnet_bootstrap_ssh.c

package info (click to toggle)
gasnet 2025.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 17,424 kB
  • sloc: ansic: 114,758; cpp: 5,158; sh: 4,847; makefile: 2,715; perl: 1,774
file content (2793 lines) | stat: -rw-r--r-- 79,355 bytes parent folder | download | duplicates (3)
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
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
/*   $Source: bitbucket.org:berkeleylab/gasnet.git/other/ssh-spawner/gasnet_bootstrap_ssh.c $
 * Description: GASNet conduit-independent ssh-based spawner
 * Copyright 2005, The Regents of the University of California
 * Terms of use are as specified in license.txt
 */
#include <gasnet_internal.h>
#include <gasnet_core_internal.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/select.h>
#if HAVE_NETINET_TCP_H
  #include <netinet/tcp.h>
#endif
#if HAVE_SYS_UIO_H
  #include <sys/uio.h>
#endif
#if HAVE_SYS_SOCKIO_H
  #include <sys/sockio.h>
#endif
#ifdef HAVE_PR_SET_PDEATHSIG
  /* Under Linux we can ask to be sent a given signal when our parent exits.
   * We use if mainly because of a bug in some versions of OpenSSH that
   * fail to kill spawned children when the ssh exits.  However, this will
   * also make us more resistant to runaways if somebody starts sending
   * SIGKILL to our processes (which leaves us no other way to cleanup
   * gracefully).  Not safe on some Linux kernels.
   */
  #include <sys/utsname.h>
  #include <sys/prctl.h>
  static int use_pdeathsig = 0;
#endif
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <limits.h>

#ifndef GASNET_SOCKLEN_T
  #error "Don't know socklen_t or equivalent"
#endif

#ifndef PATH_MAX
  #define PATH_MAX 1024
#endif

/* NOTES

   This is a ssh-based (or rsh if you want) spawner for GASNet.  It is
   intended to be conduit-neutral.
  
   In the interest of scalability the ssh processes are started up in a
   balanced N-ary tree, where N can be controlled at run time (via env
   var GASNET_SSH_OUT_DEGREE).  Typically we want this value to be
   reasonably large, since deep trees would result in multiple steps of
   forwarding for standard I/O (which is performed entirely by the ssh
   processes at this point).  IF GASNETI_SSH_OUT_DEGREE is set to zero
   then the tree effectively has infinite out-degree and the tree of
   control processes (defined below) is only one level deep.

   The leaf processes are assigned gasnet ranks ("rank processes"), while
   internal nodes in the tree are known as "control processes".  The root
   of the tree is always a control process (even when running a single
   rank) and is also known as the "master process".  Each control process
   may have both rank processes and additional control processes as
   children.  In normal use, there is a single control process per host.

   NOTE: "normal use" is the default case in which the environment variable
   GASNET_SSH_KEEPDUP is unset or "false" AND there are no uses of "multi-homed"
   hosts.  With GASNET_SSH_KEEPDUP set to a "true" value OR multiple names for
   the same host which cannot be de-duplicated, there will be one control
   process for each time a given host is used from the list.

   In addition to the tree of ssh connections, there is a control socket
   created between each process and its parent (this is true for both
   the rank and control processes).  This socket is used for control
   information, during startup.  For instance, the environment and
   arguments are transferred over this socket.

   The control sockets are used to send each control process only a
   portion of the list of host names.  Rather than send the entire list,
   each process receives the hostnames of any children it may have.

   The spawner is able to (in most cases) avoid orphaned processes
   by using TCP out-of-band data to generate a SIGURG.  The handler for
   this signal will send the out-of-band data both up and down the tree
   before exiting.

   If a child has the same hostname as its parent, it will be started
   directly, rather than via ssh IFF GASNETI_BOOTSTRAP_LOCAL_SPAWN is 1.
   By default this is enabled.

   The tree structure is used to provide scalable implementations of
   the following "service" routines for use during the bootstrap, as
   required in the template-conduit:
      void Barrier(void);
      void Exchange(void *src, size_t len, void *dest);
      void Broadcast(void *src, size_t len, void *dest, int rootnode);
      void NbrhdBroadcast(void *src, size_t len, void *dest, int rootnode);
      void HostBroadcast(void *src, size_t len, void *dest, int rootnode);
   
   Additionally, the following is useful (at least in ibv-conduit)
   for exchanging endpoint identifiers in a scalable manner:
      void Alltoall(void *src, size_t len, void *dest);

   If demand exists, scalable Scatter and Gather are possible.

   The following are needed to handle startup and termination:
      extern gasneti_spawnerfn_t const * gasneti_bootstrapInit_ssh(int *argc_p, char ***argv_p,
                                            gex_Rank_t *nodes_p,
                                            gex_Rank_t *mynode_p);
      void Fini(void);
      void Abort(int exitcode);

   In the case of normal termination, all processes should call
   Fini() before they call exit().  In the event
   that gasnet is unable to arrange for an orderly shutdown, a call to
   Abort() will try to force all processes to exit
   with the given exit code.

   To control the spawner, there are a few environment variables, all of
   which are processed only by the master process (which send the
   relevant information on to the others via the control sockets).  See
   README for documentation on these variables.

   XXX: still to do
   + Give master its own rank children too?
   + Implement "custom" spawner in the spirit of udp-conduit.
   + Look at udp-conduit for things missing from this list. :-)
   + Can write/writev loops use O_NONBLOCK to write portions to each
     socket w/o blocking?

 */

GASNETI_IDENT(gasnetc_IdentString_HaveSSHSpawner, "$GASNetSSHSpawner: 1 $");

static gasneti_spawnerfn_t const spawnerfn;

#ifndef GASNETI_BOOTSTRAP_LOCAL_SPAWN
  #define GASNETI_BOOTSTRAP_LOCAL_SPAWN 1
#endif

#define WHITESPACE " \t\n\r"
#define SSH_SERVERS_DELIM_CHARS  ",/;:" WHITESPACE

extern char **environ;
#ifndef ENV_PREFIX
  #define ENV_PREFIX "GASNET_"
#endif

enum {
  BOOTSTRAP_CMD_FINI0,
  BOOTSTRAP_CMD_FINI1,
  BOOTSTRAP_CMD_BARR0,
  BOOTSTRAP_CMD_BARR1,
  BOOTSTRAP_CMD_BCAST0,
  BOOTSTRAP_CMD_BCAST1,
  BOOTSTRAP_CMD_EXCHG0,
  BOOTSTRAP_CMD_EXCHG1,
  BOOTSTRAP_CMD_TRANS0,
  BOOTSTRAP_CMD_TRANS1,
  BOOTSTRAP_CMD_SBCAST0,
  BOOTSTRAP_CMD_SBCAST1,
};

static const int c_one  = 1;
static const int c_zero = 0;

static int is_root = 0;
static int is_control = 0;
static int is_verbose = 0;
static char args_delim = ':';
static gex_Rank_t nranks = 0;
static char cwd[PATH_MAX];
static int listener = -1;
static int listen_port = -1;
static const char *argv0 = "[unknown]";
static int null_init = 0;
static char **nodelist;
static char **ssh_argv = NULL;
static int ssh_argc = 0;
static const char *wrapper = NULL;
static const char *envcmd = "env";
static char *master_env = NULL;
static size_t master_env_len = 0;
static struct child {
  int                 sock;
  pid_t               pid;
  gex_Rank_t       rank;
  gex_Rank_t       tree_ranks; /* ranks in this sub-tree, including self (1 for rank procs) */
  gex_Rank_t       tree_nodes; /* nodes in this sub-tree, including self (1 for rank procs) */
  char **             nodelist;
} *child = NULL;
static volatile int initialized = 0;
static int finalized = 0;
static gasneti_atomic_t live = gasneti_atomic_init(0);
static volatile int in_abort = 0;
static gex_Rank_t out_degree = GASNETI_DEFAULT_SSH_OUT_DEGREE;
static struct fds {
  fd_set set;
  int    max;
} child_fds, all_fds;
static int parent = -1; /* socket */
static gex_Rank_t myrank = 0;
static int myname = -1;
static char my_host[1024] = "[unknown hostname]";
static int children = 0;
static int ctrl_children = 0;
static gex_Rank_t tree_ranks = GEX_RANK_INVALID;
static gex_Rank_t tree_nodes = GEX_RANK_INVALID;
static int mypid;
static volatile int exit_status = 0;
static gex_Rank_t nnodes = 0;	/* nodes, as distinct from ranks */
static int nnodes_set = 0;		/* non-zero if nnodes set explicitly */
static int keepdup = -1; // Value of [PREFIX]_SSH_KEEPDUP

GASNETI_FORMAT_PRINTF(do_message,1,2,
static void do_message(const char *fmt, ...)) {
  va_list args;
  va_start(args, fmt);
  gasneti_console_messageVA(0,0,0,-1, "SSH-SPAWNER", fmt, args);
  va_end(args);
}
#define BOOTSTRAP_VERBOSE(ARGS) do { \
  if_pf (is_verbose) do_message ARGS; \
} while (0)

/* Add single quotes around a string, taking care of any existing quotes */
static char *quote_arg(const char *arg) {
  char *p, *q, *tmp;
  char *result;

  result = gasneti_strdup("'");
  p = tmp = gasneti_strdup(arg);
  while ((q = strchr(p, '\'')) != NULL) {
    *q = '\0';
    result = gasneti_sappendf(result, "%s'\\''", p);
    p = q + 1;
  }
  result = gasneti_sappendf(result, "%s'", p);
  gasneti_free(tmp);
  return result;
}

/* Like gasneti_fatalerror, but w/o dumping core
 * This is used for probable user errors
 */
GASNETI_FORMAT_PRINTF(die,2,3, 
GASNETI_NORETURN
static void die(int exitcode, const char *msg, ...)) {
  va_list argptr;
  va_start(argptr, msg);
    gasneti_console_messageVA(0,0,0,-1, "ERROR",msg,argptr);
  va_end(argptr);
  gasneti_killmyprocess(exitcode);
}


/* Fetch a variable from the environment on the master node.
 * (more or less copied from amudp_spmd.cpp)
 */
static char *do_getenv(const char *var) {
  if (master_env && var && (*var != '\0')) {
    char *p = master_env;
    size_t len = strlen(var);

    while (*p) {
      if (!strncmp(var, p, len) && (p[len] == '=')) {
        return p + len + 1;
      } else {
        p += strlen(p) + 1;
      }
    }
  }
  return NULL;
}

static void do_propagate_env(const char * keyname, int flags) {
  if (master_env) {
    gasneti_propagate_env_helper(master_env, keyname, flags);
  }
}

static const char* do_check_env_prefix_hook(const char *prefix) {
  return master_env ? gasneti_check_env_prefix_helper(master_env, prefix) : NULL;
}

#if HAVE_SETPGID || HAVE_SETPGRP
  /* signals sent to entire process groups */
  #define pid_to_kill(pid) (-(pid))
  static int my_setpgid(pid_t pid)
  {
  #if HAVE_SETPGID
    return setpgid(pid, pid);
  #elif SETPGRP_VOID
    /* Note this case is a no-op in the parent */
    return pid ? 0 : setpgrp();
  #else
    return setpgrp(pid, pid);
  #endif
  }
#else
  /* signals sent to individual processes only */
  #define pid_to_kill(pid) (pid)
  #define my_setpgid(pid) (0)
#endif

// POSIX.1-2008 designated siginterrupt() as obsolete.
// This favors the recommended replacement via the SA_RESTART flag with
// sigaction(), while providing a fallback if sigaction or SA_RESTART
// are not available.
static int do_siginterrupt(int signum, int flag)
{
#if GASNETI_HAVE_SA_RESTART
  struct sigaction act;

  int rc = sigaction(signum, NULL, &act);
  if (rc) return rc;

  if (flag) {
    act.sa_flags &= ~SA_RESTART;
  } else {
    act.sa_flags |= SA_RESTART;
  }

  return sigaction(signum, &act, NULL);
#else
  return siginterrupt(signum, flag);
#endif
}

/* returns count of signals sent */
static int signal_rank_procs(int signo)
{
  gex_Rank_t j;
  int count = 0;

  for (j = ctrl_children; j < children; ++j) { /* loop over rank processes only */
    pid_t pid = pid_to_kill(child[j].pid);
    if (! pid) continue;
    if (kill(pid, signo) < 0) {
      if ((kill(pid, 0) < 0) && (errno == ESRCH || errno == EPERM)) {
        /* Dead or recycled pid - not a failure */
        child[j].pid = 0;
      }
    } else {
      if (signo) BOOTSTRAP_VERBOSE(("[%d] kill(rank=%d, %d)\n", myname, child[j].rank, signo));
      ++count;
    }
  }
  return count;
}

static void kill_all_ranks(void)
{
  int done, loops = 30;

  gasneti_assert_always(is_control);

  gasneti_reghandler(SIGQUIT, SIG_DFL);
  gasneti_reghandler(SIGINT,  SIG_DFL);
  gasneti_reghandler(SIGTERM, SIG_DFL);
  gasneti_reghandler(SIGHUP,  SIG_DFL);
  gasneti_reghandler(SIGPIPE, SIG_DFL);

  /* First try to ensure that processes are not stopped */
  (void) signal_rank_procs(SIGCONT);

  /* First try the polite signal */
  done = !signal_rank_procs(SIGTERM);

  /* Loop until all rank processes are gone (kill fails) or time runs out */
  while (!done && (loops--)) {
    (void)sleep(1);
    done = !signal_rank_procs(0);
  }

  /* Be forcefull if still around */
  if (!done) signal_rank_procs(SIGKILL);
}

/* Send SIGURG up and down the tree
 * If bit 8 is set send ONLY toward leaves (signal forwarding)
 * If bit 8 is clear send BOTH directions (abnormal exit handling)
 */
static void do_oob(unsigned char byte) {
  const int oob_flags = MSG_OOB 
#ifdef MSG_DONTWAIT
	  		| MSG_DONTWAIT
#endif
#ifdef MSG_NOSIGNAL
			| MSG_NOSIGNAL
#endif
			;
  int j;

  gasneti_reghandler(SIGPIPE, SIG_IGN);
  if (0x80 & byte) gasneti_reghandler(SIGURG, SIG_IGN);

  for (j = 0; j < children; ++j) {
    if (j >= ctrl_children) continue;
    (void)send(child[j].sock, &byte, 1, oob_flags);
  }
  if (!(0x80 & byte) && !is_root) {
    (void)send(parent, &byte, 1, oob_flags);
  }
}

/* master forwards fatal signals if possible */
static void sigforward(int sig)
{
  gasneti_assert_always(is_control);

  {
    gasneti_sighandlerfn_t fp;
    switch (sig) {
      /* Termination signals are forwarded only once */
      case SIGQUIT: case SIGINT:
      case SIGTERM: case SIGHUP:
        gasneti_reghandler(sig, SIG_DFL);
        break;

      /* Other signals are forwarded every time they occur */
      default:
        gasneti_reghandler(sig, &sigforward);
        break;
    }
  }

  BOOTSTRAP_VERBOSE(("[%d] Forwarding signal %d\n", myname, sig));
  do_oob(sig | 0x80);
  signal_rank_procs(sig);
}

static void signal_forward(int enable) {
  gasneti_sighandlerfn_t fp = enable ? &sigforward : SIG_DFL;

  /* Termination signals */
  gasneti_reghandler(SIGQUIT, fp);
  gasneti_reghandler(SIGINT,  fp);
  gasneti_reghandler(SIGTERM, fp);
  gasneti_reghandler(SIGHUP,  fp);

  /* Some non-fatal signals (e.g. for freeze/unfreeze or backtrace) */
  /* TODO: parse environment for GASNET_FREEZE_SIGNAL and GASNET_BACKTRACE_SIGNAL? */
  #ifdef SIGCONT
    gasneti_reghandler(SIGCONT, fp);
  #endif
  #ifdef SIGUSR1
    gasneti_reghandler(SIGUSR1, fp);
  #endif
  #ifdef SIGUSR2
    gasneti_reghandler(SIGUSR2, fp);
  #endif
}


/* Cause everything to exit.
 * In a control process this returns to allow a full cleanup.
 * In worker processes there is no returning.
 */
static void do_abort(unsigned char exitcode) {
  /* avoid reentrance */
  if (in_abort) {
    return;
  } else {
    in_abort = 1;
  }

#if HAVE_PR_SET_PDEATHSIG
  /* disarm signal, if any, for termination of our parent */
  if (use_pdeathsig) {
    (void)prctl(PR_SET_PDEATHSIG, 0);
  }
#endif

  if (is_control) {
    signal_forward(0);
    do_oob(exitcode & 0x7f);
    kill_all_ranks();
  } else {
    gasneti_killmyprocess(exitcode);

    /* paranoia... */
    gasneti_reghandler(SIGABRT, SIG_DFL);
    gasneti_fatalerror("do_abort aborting...");

    /* NOT REACHED */
  }
}

static void reap_one(pid_t pid, int status)
{
  gasneti_assert_always(pid);

  gasneti_atomic_decrement(&live, 0);

  if (child) {
    int j;

    for (j = 0; j < children; ++j) {
      if (pid == child[j].pid) {
        const char *kind = (j < ctrl_children) ? "Ctrl" : "Rank";
        const char *fini = finalized ? "" : " before finalize";
        const int sock = child[j].sock;
        if (sock) (void)close(sock);
        child[j].pid = 0;
	if (WIFEXITED(status)) {
          int tmp = WEXITSTATUS(status);
	  if (exit_status == 0) exit_status = tmp;
	  BOOTSTRAP_VERBOSE(("[%d] %s proc %d exited with status %d%s\n",
				  myname, kind, child[j].rank, tmp, fini));
          if (!sock && (j < ctrl_children)) { // Ctrl proc which did not yet connect
            const char *host = child[j].nodelist ? child[j].nodelist[0] : nodelist[0];
            do_message("Failed to start processes on %s, possibly due to an "
                            "inability to establish an ssh connection from %s without "
                            "interactive authentication.",
                            host, my_host);
          }
	} else if (WIFSIGNALED(status)) {
          int tmp = WTERMSIG(status);
	  if (exit_status == 0) exit_status = tmp;
	  BOOTSTRAP_VERBOSE(("[%d] %s proc %d died with signal %d%s\n",
				  myname, kind, child[j].rank, tmp, fini));
	} else {
	  BOOTSTRAP_VERBOSE(("[%d] %s proc %d exited with unknown stats%s\n",
				  myname, kind, child[j].rank, fini));
	}
        if (!finalized) {
	  do_abort(exit_status ? exit_status : -1);
	}
	break;
      }
    }
    if (j == children) {
      BOOTSTRAP_VERBOSE(("[%d] Reaped unknown pid %d\n", myname, (int)pid));
    }
  }

  if (initialized != children) {
    gasneti_fatalerror("One or more processes died before setup was completed");
  }
}

static void reaper(int sig)
{
  gasneti_assert_always(!sig || sig == SIGCHLD);
  gasneti_reghandler(SIGCHLD, sig ? &reaper : SIG_DFL);

  {
    pid_t pid;
    int status;
    while((pid = waitpid(-1,&status,WNOHANG)) > 0) {
      reap_one(pid, status);
    }
  }
}

static void wait_for_all(void)
{
  sigset_t child_set;
  sigset_t old_set;

  sigemptyset(&child_set);
  sigaddset(&child_set, SIGCHLD);
  sigprocmask(SIG_BLOCK, &child_set, &old_set);

  /* Call reaper() to collect any children that may have exited before
   * we got here.
   * Also calls gasneti_reghandler(SIGCHLD, &reaper) for us.
   */
  reaper(SIGCHLD);

  while (gasneti_atomic_read(&live, 0)) {
    BOOTSTRAP_VERBOSE(("[%d] Sigsuspend with %d children left\n",
			    myname, gasneti_atomic_read(&live, 0)));
    sigsuspend(&old_set);
  }
}

static void sigurg_handler(int sig)
{
  unsigned char byte = 127;

  BOOTSTRAP_VERBOSE(("[%d] Received SIGURG\n", myname));

  if (is_control) {
    /* We need to read our single byte of OOB data here.
     * Use of select() can tell us who sent it to us.
     * MSG_OOB is supposed to always be non-blocking.i
     * If multiple sockets have OOB data we read them all.
     * If the SIGURG came fron kill() rather than OOB,
     * then we'll keep the value 127, leading to an abort.
     */

    struct timeval timeout = {0,0};
    fd_set fds = all_fds.set;
    int count = select(all_fds.max+1, NULL, NULL, &fds, &timeout);
    if (!count) {
      BOOTSTRAP_VERBOSE(("[%d] sigurg with NO exception fds\n", myname));
      do_abort(-1);
    }
    while (count) {
      int j, fd = -1;
      for (j = 0; j < ctrl_children; ++j) { /* Only check the control sockets */
        if (FD_ISSET(child[j].sock, &fds)) {
          fd = child[j].sock;
          break;
        }
      }
      if ((fd < 0) && (parent >= 0) && FD_ISSET(parent, &fds)) {
        fd = parent;
      }
      if (fd < 0) break; /* Unknown source! */
      (void)recv(fd, &byte, 1, MSG_OOB);
      FD_CLR(fd, &fds);
      --count;
    }
  }

  if (0x80 & byte) {
    /* signal forwarding */
    gasneti_reghandler(SIGURG, &sigurg_handler); /* rearm */
    signal_rank_procs(0x7f & byte);
  } else {
    /* abortive exit (or failure to locate any OOB data) */
    gasneti_reghandler(SIGURG, SIG_IGN); /* ignore */
    do_abort(byte);
  }
}

static void do_write(int fd, const void *buf, size_t len)
{
  const char *p = (const char *)buf;
  while (len) {
    ssize_t rc = write(fd, p, len);
    if_pf (!rc || ((rc < 0) && (errno != EINTR))) {
      do_message("write() returned %d, errno = %d(%s)",
              (int)rc, errno, strerror(errno));
      do_abort(-1);
    }
    if_pf (in_abort) break;
    if_pf (rc < 0) continue; // EINTR
    p += rc;
    len -= rc;
  }
}

/* NOTE that unlike writev() we clobber the iovec */
static void do_writev(int fd, struct iovec *iov, int iovcnt)
{
#if defined(IOV_MAX)
  static int iov_max = IOV_MAX;
#elif defined(MAXIOV)
  static int iov_max = MAXIOV;
#else
  static int iov_max = 1024;
#endif

  while (iovcnt) {
    ssize_t rc;

    while (0 == iov->iov_len) {
      ++iov;
      if (0 == --iovcnt) return;
    }

    rc = writev(fd, iov, MIN(iovcnt, iov_max));
    if_pf ((rc < 0) && (errno == EINVAL) && (iov_max > 32)) {
      iov_max /= 2;
      continue;
    }
    if_pf (!rc || ((rc < 0) && (errno != EINTR))) {
      do_message("writev() returned %d, errno = %d(%s)",
              (int)rc, errno, strerror(errno));
      do_abort(-1);
    }
    if_pf (in_abort) break;
    if_pf (rc < 0) continue; // EINTR

    do {
      size_t len = iov->iov_len;
      if (rc >= len) {
      #if GASNET_DEBUG
        iov->iov_base = NULL; /* detect reuse, which is forbidden */
      #endif
        ++iov;
        --iovcnt;
        rc -= len;
      } else {
        iov->iov_base = (void*)((uintptr_t)iov->iov_base + rc);
        iov->iov_len -= rc;
        break;
      }
    } while (rc);
  }
}

static void do_write_string(int fd, const char *string) {
  size_t len = string ? strlen(string) : 0;
  struct iovec iov[2];
  iov[0].iov_base = (void *)&len;
  iov[0].iov_len  = sizeof(len);
  iov[1].iov_base = (void *)string;
  iov[1].iov_len  = len;
  do_writev(fd, iov, 2);
}

static void do_read(int fd, void *buf, size_t len)
{
  char *p = (char *)buf;
  while (len) {
    ssize_t rc = read(fd, p, len);
    if_pf (!rc) {
      do_message("read() returned 0 (EOF)");
      do_abort(-1);
    } else if_pf ((rc < 0) && (errno != EINTR)) {
      do_message("read() returned %d, errno = %d(%s)",
              (int)rc, errno, strerror(errno));
      do_abort(-1);
    }
    if_pf (in_abort) break;
    if_pf (rc < 0) continue; // EINTR
    p += rc;
    len -= rc;
  }
}

/* NOTE that unlike readv() we clobber the iovec */
static void do_readv(int fd, struct iovec *iov, int iovcnt)
{
#if defined(IOV_MAX)
  static int iov_max = IOV_MAX;
#elif defined(MAXIOV)
  static int iov_max = MAXIOV;
#else
  static int iov_max = 1024;
#endif
{ size_t len = 0;
  int c;
  for(c=0; c<iovcnt;++c) len += iov[c].iov_len;
}

  while (iovcnt) {
    ssize_t rc;

    while (0 == iov->iov_len) {
      ++iov;
      if (0 == --iovcnt) return;
    }

    rc = readv(fd, iov, MIN(iovcnt, iov_max));
    if_pf ((rc < 0) && (errno == EINVAL) && (iov_max > 32)) {
      iov_max /= 2;
      continue;
    }
    if_pf (!rc) {
      do_message("readv() returned 0 (EOF)");
      do_abort(-1);
    } else if_pf ((rc < 0) && (errno != EINTR)) {
      do_message("readv() returned %d, errno = %d(%s)",
              (int)rc, errno, strerror(errno));
      do_abort(-1);
    }
    if_pf (in_abort) break;
    if_pf (rc < 0) continue; // EINTR

    do {
      size_t len = iov->iov_len;
      if (rc >= len) {
      #if GASNET_DEBUG
        iov->iov_base = NULL; /* detect reuse, which is forbidden */
      #endif
        ++iov;
        --iovcnt;
        rc -= len;
      } else {
        iov->iov_base = (void*)((uintptr_t)iov->iov_base + rc);
        iov->iov_len -= rc;
        break;
      }
    } while (rc);
  }
}

static char *do_read_string(int fd) {
  char *result = NULL;
  size_t len;

  do_read(fd, &len, sizeof(size_t));
  if (len) {
    result = gasneti_malloc(len + 1);
    do_read(fd, result, len);
    result[len] = '\0';
  }

  return result;
}

static int my_socketpair(int sv[2]) {
  #if defined(PF_LOCAL)
    const int domain = PF_LOCAL;
  #elif defined(PF_UNIX)
    const int domain = PF_UNIX;
  #endif
    int rc = socketpair(domain, SOCK_STREAM, 0, sv);
    return rc;
}

static int options_helper(char **list, const char *string, const char *where)
{
  int count = 0;
  int in_quotes = 0;
  const char *special[] = {WHITESPACE "\\\"'",	/* special chars outside dbl quotes */
			   "\\\""};		/* special chars inside dbl quotes */
  
  if (!string) {
    return 0;
  }

  /* Outer loop adds a word to the list on each pass
     with the possible exception of the last
   */
  while (*string) {
    char tmp[1024];
    char *p = tmp;
    while (*string && strchr(WHITESPACE,*string)) ++string; /* eat leading whitespace */
    if (!*string) { break; /* reached end of string */ }
    /* This loop brings together pieces of a "word", possible w/ quotes */
    in_quotes = 0;
    do {
      int i = strcspn(string, special[in_quotes]);
      GASNETI_MEMCPY(p , string, i); p += i;
      gasneti_assert_always((uintptr_t)(p-tmp) < (sizeof(tmp)-1));
      string += i;
      switch (*string) {
	case '\0':
	  break;
	case '\\':
	  if (!string[1]) {
	    die(1, "string ends with \\ %s", where);
	  } else if (strchr(special[in_quotes],string[1])) {
	    /* Drop the backslash if it quotes a special character */
            *(p++) = string[1];
            gasneti_assert_always((uintptr_t)(p-tmp) < (sizeof(tmp)-1));
	  } else {
	    /* Keep the backslash */
	    GASNETI_MEMCPY(p , string, 2); p += 2;
            gasneti_assert_always((uintptr_t)(p-tmp) < (sizeof(tmp)-1));
	  }
	  string += 2;
	  break;
	case '\'':
	  ++string;
	  i = strcspn(string, "\'");
	  if (string[i] != '\'') {
	    die(1, "unbalanced ' %s", where);
	  }
          GASNETI_MEMCPY(p , string, i); p += i;
          gasneti_assert_always((uintptr_t)(p-tmp) < (sizeof(tmp)-1));
	  string += i + 1;
	  break;
	case '"':
	  ++string;
	  in_quotes = !in_quotes;
	  break;
	default: /* WHITESPACE */
	  break;
      }
    } while (*string && (in_quotes || !strchr(WHITESPACE,*string)));
    if (in_quotes) {
      die(1, "unbalanced \" %s", where);
    }
    if (list) {
      gasneti_assert_always((uintptr_t)(p-tmp) < sizeof(tmp));
      *p = '\0';
      list[count] = gasneti_strdup(tmp);
    }
    ++count;
  }
  if (list) {
    list[count] = NULL;
  }
  return count;
}

/* Parse a string into an array of "words", following shell rules for '," and \ */
static char **parse_options(const char *string, int *count_p, const char *where)
{
  int count;
  char **list;

  /* First parse pass will just count the words */
  count = options_helper(NULL, string, where);
  list = gasneti_malloc(sizeof(char *) * (count+1));

  /* Second pass fills the list of words */
  (void)options_helper(list, string, where);

  if (count_p) *count_p = count;
  return list;
}

/* wrappers that map unset/empty to NULL */
static const char *my_getenv_withdefault(const char *key, const char *defval) {
  char *env_string = gasneti_getenv_withdefault(key, defval);
  return ((env_string != NULL) && strlen(env_string)) ? env_string : NULL;
}
static const char *my_getenv(const char *key) {
  return my_getenv_withdefault(key, NULL);
}

static void configure_ssh(void) {
  const char *env_string;
  const char *ssh_argv0;
  char **ssh_options = NULL;
  int is_openssh = 0;
  int optcount = 0;
  int i, argi;

  envcmd = my_getenv_withdefault(ENV_PREFIX "ENVCMD", "env");

  /* Determine the ssh command */
  ssh_argv0 = my_getenv_withdefault(ENV_PREFIX "SSH_CMD", GASNETI_DEFAULT_SSH_CMD);
  if (ssh_argv0 == NULL) {
      BOOTSTRAP_VERBOSE(("Ignoring empty value in environment variable " ENV_PREFIX "SSH_CMD\n"));
      ssh_argv0 = GASNETI_DEFAULT_SSH_CMD;
  }

  /* Check for OpenSSH */
  {
    char *cmd = gasneti_sappendf(NULL, "%s -V 2>&1 | grep OpenSSH >/dev/null 2>/dev/null", ssh_argv0);
    is_openssh = (0 == system(cmd));
    gasneti_free(cmd);
    BOOTSTRAP_VERBOSE(("Configuring for OpenSSH\n"));
  }

  /* Check for user-supplied options */
  if ((env_string = my_getenv_withdefault(ENV_PREFIX "SSH_OPTIONS", GASNETI_DEFAULT_SSH_OPTIONS)) != NULL) {
    ssh_options = parse_options(env_string, &optcount, "while parsing " ENV_PREFIX "SSH_OPTIONS");
  }

  /* Now build the command line */
  ssh_argc = optcount + (is_openssh ? 10 : 1);
  ssh_argv = gasneti_calloc((ssh_argc + 3 /* host + cmd + NULL = 3 */), sizeof(char *));
  ssh_argv[0] = gasneti_strdup(ssh_argv0);
  argi = 1;
  if (is_openssh) {
    ssh_argv[argi++] = (char *)"-o"; ssh_argv[argi++] = (char *)"StrictHostKeyChecking no";
    ssh_argv[argi++] = (char *)"-o"; ssh_argv[argi++] = (char *)"FallBackToRsh no";
    ssh_argv[argi++] = (char *)"-o"; ssh_argv[argi++] = (char *)"BatchMode yes";
    ssh_argv[argi++] = (char *)"-o"; ssh_argv[argi++] = (char *)"ForwardX11 no";
    ssh_argv[argi++] = (char *)"-q";
  }
  if (optcount) {
    for (i=0; i<optcount; ++i, ++argi) {
      ssh_argv[argi] = ssh_options[i];
    }
    gasneti_free(ssh_options);
  }

  BOOTSTRAP_VERBOSE(("Constructed ssh command line:\n"));
  for (i=0; i<ssh_argc; ++i) {
    BOOTSTRAP_VERBOSE(("\t%s\n", ssh_argv[i]));
  }
  BOOTSTRAP_VERBOSE(("\tHOST"));
  BOOTSTRAP_VERBOSE(("\tCMD"));
}

/* Reduce nnodes when presented with a short nodelist */
static char ** short_nodelist(char **nodelist, gex_Rank_t count) {
  if (nnodes_set) {
    do_message("WARNING: "
                    "Request for %d nodes ignored because only %d nodes are available%s.",
                    nnodes, count, keepdup?"":" after de-duplication");
  }

  nnodes = count;
  nodelist = gasneti_realloc(nodelist, nnodes * sizeof(char *));
  BOOTSTRAP_VERBOSE(("Node count set to available: %d\n", (int)nnodes));

  return nodelist;
}

// Return non-zero if the given `elem` does not yet appear in `list`
// (of `len` elements) OR if GASNET_SSH_KEEPDUP is `true`.
static int keep_nodelist_elem(char **list, size_t len, char *elem) {
  // 0) empty list is trivially free of dups:
  if (!len) return 1;

  // 1) check GASNET_SSH_KEEPDUP env var:
  gasneti_assert(keepdup == !!keepdup); // checks for uninitialised use
  if (keepdup) return 1;

  // 2) check for consecutive dups
  // This optimizes for a data source which replicate entries per-core
  const size_t last = len - 1;
  if (! strcmp(elem, list[last])) return 0;

  // 3) scan the remainder for dups
  // Working from start (as opposed to reverse) works well for cyclic repetition
  for (size_t i = 0; i < last; ++i) {
    if (! strcmp(elem, list[i])) return 0;
  }

  return 1;
}

/* Build an array of hostnames from a stdio stream */
static char ** parse_nodestream(FILE *fp) {
  char *buf = NULL;
  size_t buflen = 0;

  char **result = gasneti_malloc(nnodes * sizeof(char *));

  for (gex_Rank_t i = 0; i < nnodes;) {
    if (gasneti_getline(&buf, &buflen, fp) == -1) {
      /* ran out of lines */
      result = short_nodelist(result, i);
      break;
    }
 
    char *p = buf;
    while (*p && strchr(WHITESPACE, *p)) ++p; /* eat leading whitespace */
    if (*p != '#') {
      p[strcspn(p, WHITESPACE)] = '\0';
      if (keep_nodelist_elem(result, i, p)) {
        result[i++] = gasneti_strdup(p);
        BOOTSTRAP_VERBOSE(("\t%s\n", p));
      }
    }
  }

  gasneti_free(buf);
  return result;
}

/* Build an array of hostnames from a file */
static char ** parse_nodefile(const char *filename) {
  char **result = NULL;
  FILE *fp;

  BOOTSTRAP_VERBOSE(("Parsing nodefile '%s'\n", filename));
  fp = fopen(filename, "r");
  if (!fp) {
    die(1, "failed to open nodefile '%s'", filename);
  }

  result = parse_nodestream(fp);

  (void)fclose(fp);

  return result;
}

/* Build an array of hostnames from a pipe */
static char ** parse_nodepipe(const char *cmd) {
  char **result = NULL;
  FILE *fp;

  BOOTSTRAP_VERBOSE(("Parsing nodes from command '%s'\n", cmd));
  fp = popen(cmd, "r");
  if (!fp) {
    die(1, "failed to spawn command '%s'", cmd);
  }

  result = parse_nodestream(fp);

  (void)pclose(fp);

  return result;
}

/* Build an array of hostnames from a delimited string */
static char ** parse_servers(const char *list) {
  static const char *delims = SSH_SERVERS_DELIM_CHARS;

  char *string;
  char *alloc = string = gasneti_strdup(list);
  char **result = gasneti_malloc(nnodes * sizeof(char *));

  BOOTSTRAP_VERBOSE(("Parsing servers list '%s'\n", string));
  for (gex_Rank_t i = 0; i < nnodes; ) {
    while (*string && strchr(delims,*string)) ++string; /* eat leading delimiters */
    if (!*string) {
      /* ran out of words */
      result = short_nodelist(result, i);
      break;
    }
    char *p = string;
    string += strcspn(string, delims);
    if (*string) *(string++) = '\0';
    if (keep_nodelist_elem(result, i, p)) {
      result[i++] = gasneti_strdup(p);
      BOOTSTRAP_VERBOSE(("\t%s\n", p));
    }
  }
  gasneti_free(alloc);

  return result;
}

static void build_nodelist(void)
{
  keepdup = gasneti_getenv_yesno_withdefault(ENV_PREFIX "SSH_KEEPDUP", 0);

  const char *env_string;
  if ((env_string = my_getenv_withdefault(ENV_PREFIX "SSH_NODEFILE",
                                          GASNETI_DEFAULT_SSH_NODEFILE)) != NULL) {
    nodelist = parse_nodefile(env_string);
  } else if ((env_string = my_getenv(ENV_PREFIX "SSH_SERVERS")) != NULL) {
    nodelist = parse_servers(env_string);
  } else if ((env_string = my_getenv(ENV_PREFIX "NODEFILE")) != NULL) {
    nodelist = parse_nodefile(env_string);
  } else if ((env_string = my_getenv("PBS_NODEFILE")) != NULL) {
    nodelist = parse_nodefile(env_string);
  } else if ((env_string = my_getenv("PE_HOSTFILE")) != NULL) {
    nodelist = parse_nodefile(env_string);
  } else if ((env_string = my_getenv("SSS_HOSTLIST")) != NULL) {
    nodelist = parse_servers(env_string);
  } else if ((env_string = my_getenv("LSB_HOSTS")) != NULL) {
    nodelist = parse_servers(env_string);
  } else if ((env_string = my_getenv("OAR_NODEFILE")) != NULL) {
    nodelist = parse_nodefile(env_string);
  } else if (my_getenv("SLURM_JOB_ID") != NULL) {
    nodelist = parse_nodepipe("scontrol show hostname");
  } else {
    die(1, "No " ENV_PREFIX "SSH_NODEFILE, " ENV_PREFIX "SSH_SERVERS, or " ENV_PREFIX "NODEFILE in environment");
  }
}

static void send_nodelist(int s, int count, char ** list) {
  gex_Rank_t i;

  /* length of list is already known to the recipient */
  count = MAX(count, 1);
  for (i = 0; i < count; ++i) {
    do_write_string(s, list[i]);
  }
}

static void recv_nodelist(int s, int count) {
  gex_Rank_t i;
  count = MAX(count, 1);

  nodelist = gasneti_malloc(count * sizeof(char *));
  gasneti_leak(nodelist);
  for (i = 0; i < count; ++i) {
    nodelist[i] = do_read_string(s);
    gasneti_leak(nodelist[i]);
  }
}

static void send_identity(int s, struct child *ch) {
  struct iovec iov[4];
  iov[0].iov_base = (void*) &nranks;
  iov[0].iov_len  = sizeof(gex_Rank_t);
  iov[1].iov_base = (void*) &ch->rank;
  iov[1].iov_len  = sizeof(gex_Rank_t);
  iov[2].iov_base = (void*) &ch->tree_ranks;
  iov[2].iov_len  = sizeof(gex_Rank_t);
  iov[3].iov_base = (void*) &ch->tree_nodes;
  iov[3].iov_len  = sizeof(gex_Rank_t);
  do_writev(s, iov, 4);
}

static void recv_identity(int s) {
  struct iovec iov[4];
  iov[0].iov_base = (void*) &nranks;
  iov[0].iov_len  = sizeof(gex_Rank_t);
  iov[1].iov_base = (void*) &myrank;
  iov[1].iov_len  = sizeof(gex_Rank_t);
  iov[2].iov_base = (void*) &tree_ranks;
  iov[2].iov_len  = sizeof(gex_Rank_t);
  iov[3].iov_base = (void*) &tree_nodes;
  iov[3].iov_len  = sizeof(gex_Rank_t);
  do_readv(s, iov, 4);
  gasneti_assert_always(nranks > 0);
  gasneti_assert_always(myrank < nranks);
}

/*
 * Send environment as a big char[] with \0 between each 'VAR=VAL'
 * and a double \0 to terminate. (inspired by amudp)
 */
static void send_env(int s) {
  if (!master_env) {
    int i;
    const char *p;
    char *q;
    size_t rlen = strlen(ENV_PREFIX "SSH_");

    gasneti_assert_always(is_control);

    /* First pass over environment to get its size */
    master_env_len = 1; /* for the doubled \0 at the end */
    for (i = 0, p = environ[0]; p != NULL; p = environ[++i]) {
      if (!strncmp(ENV_PREFIX "SSH_", p, rlen)) {
        /* We parse these ourselves, don't forward */
      } else {
        master_env_len += strlen(p) + 1;
      }
    }

    /* Append all the strings together */
    q = master_env = gasneti_malloc(master_env_len);
    for (i = 0, p = environ[0]; p != NULL; p = environ[++i]) {
      if (!strncmp(ENV_PREFIX "SSH_", p, rlen)) {
        /* We parse these ourselves, don't forward */
      } else {
        size_t tmp = strlen(p) + 1;
        GASNETI_MEMCPY(q, p, tmp);
        q += tmp;
      }
    }
    *q = '\0';
  }

  /* send it */
  do_write(s, &master_env_len, sizeof(master_env_len));
  do_write(s, master_env, master_env_len);
}

static void recv_env(int s) {
  do_read(s, &master_env_len, sizeof(master_env_len));
  master_env = gasneti_malloc(master_env_len);
  gasneti_leak(master_env);
  do_read(s, master_env, master_env_len);
}

static void send_ssh_argv(int s) {
  int i;

  do_write(s, &ssh_argc, sizeof(int));
  for (i = 0; i < ssh_argc; ++i) {
    do_write_string(s, ssh_argv[i]);
  }
}

static void recv_ssh_argv(int s) {
  int i;

  do_read(s, &ssh_argc, sizeof(int));
  ssh_argv = gasneti_calloc(ssh_argc+3, sizeof(char *));
  gasneti_leak(ssh_argv);
  for (i = 0; i < ssh_argc; ++i) {
    ssh_argv[i] = do_read_string(s);
    gasneti_leak(ssh_argv[i]);
  }
}

static void send_argv(int s, int argc, char * const *argv) {
  int i;

  do_write(s, &argc, sizeof(int));
  for (i = 0; i < argc; ++i) {
    do_write_string(s, argv[i]);
  }
}

static void recv_argv(int s, int *argc_p, char ***argv_p) {
  int argc, i;
  char **argv;

  do_read(s, &argc, sizeof(int));
  argv = gasneti_malloc((argc+1) * sizeof(char*));
  gasneti_leak(argv);
  for (i = 0; i < argc; ++i) {
    argv[i] = do_read_string(s);
    gasneti_leak(argv[i]);
  }
  argv[argc] = NULL;

  *argc_p = argc;
  *argv_p = argv;
  argv0 = argv[0];
}

static int fcntl_setfl(int fd, int flag) {
  int rc;
  rc = fcntl(fd, F_GETFL, 0);
  if (rc >= 0) rc = fcntl(fd, F_SETFL, rc | flag);
  return rc;
}

#if 0 /* Unused */
static int fcntl_clrfl(int fd, int flag) {
  int rc;
  rc = fcntl(fd, F_GETFL, 0);
  if (rc >= 0) rc = fcntl(fd, F_SETFL, rc & ~flag);
  return rc;
}
#endif

static int fcntl_setfd(int fd, int flag) {
  int rc;
  rc = fcntl(fd, F_GETFD, 0);
  if (rc >= 0) rc = fcntl(fd, F_SETFD, rc | flag);
  return rc;
}

static int fcntl_clrfd(int fd, int flag) {
  int rc;
  rc = fcntl(fd, F_GETFD, 0);
  if (rc >= 0) rc = fcntl(fd, F_SETFD, rc & ~flag);
  return rc;
}

static void fd_sets_init(void) {
  FD_ZERO(&child_fds.set);
  child_fds.max = -1;
  FD_ZERO(&all_fds.set);
  all_fds.max = -1;
}

static void fd_sets_add(int fd) {
  FD_SET(fd, &all_fds.set);
  all_fds.max = MAX(all_fds.max, fd);
  if (fd != parent) {
    FD_SET(fd, &child_fds.set);
    child_fds.max = MAX(child_fds.max, fd);
  }
}


static void pre_spawn(int count) {
  struct sockaddr_in sock_addr;
  GASNET_SOCKLEN_T addr_len;
  const char *env_string;

  /* Get the cwd */
  if ((env_string = my_getenv(ENV_PREFIX "SSH_REMOTE_PATH")) != NULL) {
    strncpy(cwd, env_string, sizeof(cwd)-1);
    cwd[sizeof(cwd) - 1] = '\0';
  } else if (!getcwd(cwd, sizeof(cwd))) {
    gasneti_fatalerror("getcwd() failed");
  }

  /* Create listening socket */
  if ((listener = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    gasneti_fatalerror("listener = socket() failed");
  }
  (void)fcntl_setfd(listener, FD_CLOEXEC);
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_port = 0;
  sock_addr.sin_addr.s_addr = INADDR_ANY;
  addr_len = sizeof(sock_addr);
  if (bind(listener, (struct sockaddr *)&sock_addr, addr_len) < 0) {
    gasneti_fatalerror("bind() failed");
  }
  if (listen(listener, count) < 0) {
    gasneti_fatalerror("listen() failed w/ errno=%d", errno);
  }
  if (getsockname(listener, (struct sockaddr *)&sock_addr, &addr_len) < 0) {
    gasneti_fatalerror("getsockname() failed");
  }
  listen_port = ntohs(sock_addr.sin_port);
}

static void post_spawn(int count, int argc, char * const *argv) {
  /* Accept count connections */
  while (count--) {
    struct sockaddr_in sock_addr;
    GASNET_SOCKLEN_T addr_len = sizeof(sock_addr);
    gex_Rank_t child_id;
    struct child *ch = NULL;
    int s;

    reaper(SIGCHLD); /* Take notice of children dying while blocked in accept() */
    if ((s = accept(listener, (struct sockaddr *)&sock_addr, &addr_len)) < 0) {
      gasneti_fatalerror("accept() failed w/ errno=%d", errno);
    }
    reaper(0); /* Disarm signal handler */

    fd_sets_add(s);
    gasneti_assert_always(mypid == getpid());
    (void)ioctl(s, SIOCSPGRP, &mypid); /* Enable SIGURG delivery on OOB data */
    (void)fcntl_setfd(s, FD_CLOEXEC);

#ifdef TCP_CORK
    (void)setsockopt(s, IPPROTO_TCP, TCP_CORK, (char *) &c_one, sizeof(c_one));
#endif
    do_read(s, &child_id, sizeof(gex_Rank_t));
    gasneti_assert_always(child_id < children);
    ch = &(child[child_id]);
    gasneti_assert_always(ch->rank < nranks);
    ch->sock = s;

    send_identity(s, ch);
    send_argv(s, argc, argv);
    send_env(s);
    if (ch->tree_nodes > 1) {
      do_write(s, &out_degree, sizeof(gex_Rank_t));
      send_nodelist(s, ch->tree_nodes - 1, ch->nodelist + 1);
      send_ssh_argv(s);
      do_write_string(s, wrapper);
    }

#ifdef TCP_CORK
    (void)setsockopt(s, IPPROTO_TCP, TCP_CORK, (char *) &c_zero, sizeof(c_zero));
#endif
#ifdef TCP_NODELAY
    (void)setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &c_one, sizeof(c_one));
#endif

    initialized++;
  }

  /* Close listener */
  close(listener);
}

static void do_connect(const char *spawn_args, int *argc_p, char ***argv_p)
{
  gex_Rank_t child_id;
  const char *parent_name;
  int parent_port;

  /* Extract child_id, parent_port and parent_name from spawn args
   * Format of spawn_args: "id:port:host"
   *  :    indicates the delimiter character (args_delim)
   *  id   is the non-negative integer child_id (rank among my parent's children)
   *  port is the positive integer TCP port number
   *  host is the parent's hostname, address or 'localhost' (everything after the last delimiter)
   */
  {
    const char *p = spawn_args;
    char *endptr;
    long ltmp;

    gasneti_assert_always(p && *p);

    /* Required non-negative child id */
    child_id = ltmp = strtol(p,&endptr,0);
    if ((endptr == p) || (ltmp < 0) || ((long)child_id != ltmp)) {
      die(1, "Unable to parse child id in " ENV_PREFIX "SPAWN_ARGS");
    }
    if (*endptr != args_delim) {
      die(1, "Unable to parse " ENV_PREFIX "SPAWN_ARGS");
    }
    p = endptr + 1;

    /* Required non-negative port */
    parent_port = ltmp = strtol(p,&endptr,0);
    if ((endptr == p) || (ltmp < 0) || ((long)parent_port != ltmp)) {
      die(1, "Unable to parse port number in " ENV_PREFIX "SPAWN_ARGS");
    }
    if (*endptr != args_delim) {
      die(1, "Unable to parse " ENV_PREFIX "SPAWN_ARGS");
    }
    p = endptr + 1;

    /* Parent_name is everything that remains */
    parent_name = p;
  }

  /* Connect */
  {
    struct sockaddr_in sock_addr;
    GASNET_SOCKLEN_T addr_len;
    struct hostent *h = gethostbyname(parent_name);
    int rc, retry = 4;

    if (h == NULL) {
      gasneti_fatalerror("gethostbyname(%s) failed", parent_name);
    }
    if ((parent = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
      gasneti_fatalerror("parent = socket() failed");
    }
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_port = htons(parent_port);
    sock_addr.sin_addr = *(struct in_addr *)(h->h_addr_list[0]);
    addr_len = sizeof(sock_addr);
    while ((rc = connect(parent, (struct sockaddr *)&sock_addr, addr_len)) < 0) {
      if ((errno != ECONNREFUSED) || !retry) {
        gasneti_fatalerror("connect(host=%s, port=%d) failed w/ errno=%d", parent_name, parent_port, errno);
      } else {
        sleep(1);
        --retry;
      }
    }
  }

  fd_sets_add(parent);
  gasneti_assert_always(mypid == getpid());
  (void)ioctl(parent, SIOCSPGRP, &mypid); /* Enable SIGURG delivery on OOB data */
  (void)fcntl_setfd(parent, FD_CLOEXEC);

#ifdef TCP_NODELAY
  (void)setsockopt(parent, IPPROTO_TCP, TCP_NODELAY, (char *) &c_one, sizeof(c_one));
#endif

  do_write(parent, &child_id, sizeof(gex_Rank_t));

  recv_identity(parent);
  recv_argv(parent, argc_p, argv_p);
  recv_env(parent);

  if (tree_nodes > 1) {
    do_read(parent, &out_degree, sizeof(out_degree));
    recv_nodelist(parent, tree_nodes - 1);
    recv_ssh_argv(parent);
    wrapper = do_read_string(parent);
    gasneti_leak((/*non-const*/ void *)wrapper);
  }

  gasneti_getenv_hook = &do_getenv;
  gasneti_propagate_env_hook = &do_propagate_env;
  gasneti_check_env_prefix_hook = &do_check_env_prefix_hook;
  envcmd = my_getenv_withdefault(ENV_PREFIX "ENVCMD", "env");

  myname = myrank;
  BOOTSTRAP_VERBOSE(("[%d] Connected via fd=%d\n", myname, parent));
}

static void spawn_one_control(gex_Rank_t child_id, const char *cmdline, const char *my_host) {
  const char *host = child[child_id].nodelist ? child[child_id].nodelist[0] : nodelist[0];
  pid_t pid;
  int is_local = (GASNETI_BOOTSTRAP_LOCAL_SPAWN && (!host || !strcmp(host, my_host)));

  child[child_id].pid = pid = fork();

  if (pid < 0) {
    gasneti_fatalerror("fork() failed");
  } else if (pid == 0) {
    char *cmd =
        gasneti_sappendf(NULL,
                         "cd %s; exec %s %s " ENV_PREFIX GASNET_CORE_NAME_STR "_SPAWNER=ssh "
                                              ENV_PREFIX "SPAWN_ARGS='%c%s%c%d%c%d%c%s' "
                                              "%s",
                                      quote_arg(cwd),
                                      (wrapper ? wrapper : ""),
                                      envcmd,
                                      'C',
                                      (is_verbose ? "v" : ""), args_delim,
                                      (int)child_id, args_delim,
                                      listen_port, args_delim,
                                      (is_local ? "localhost" : my_host),
                                      cmdline);
    if (is_local) {
      /* XXX: if we are clever enough, we might be able to "unwind" w/o the exec() */
      BOOTSTRAP_VERBOSE(("[%d] spawning process %d on %s via fork()\n",
			 myname,
			 (int)child[child_id].rank, my_host));
      execlp("sh", "sh", "-c", cmd, NULL);
      gasneti_fatalerror("execlp(sh) failed");
    } else {
      #if HAVE_PR_SET_PDEATHSIG
      if (use_pdeathsig) {
	/* If its parent exits early (an abnormal condition) then we want ssh to exit too */
	(void)prctl(PR_SET_PDEATHSIG, SIGHUP);
      }
      #endif
      BOOTSTRAP_VERBOSE(("[%d] spawning process %d on %s via %s\n\tCMD: %s",
			 myname,
			 (int)child[child_id].rank, host, ssh_argv[0], cmd));
      ssh_argv[ssh_argc] = (/* noconst */ char *)host;
      ssh_argv[ssh_argc+1] = cmd;
      execvp(ssh_argv[0], ssh_argv);
      gasneti_fatalerror("execvp(ssh) failed");
    }
  }
  gasneti_atomic_increment(&live, 0);
}

/* do_worker could probably be called "undo_control" */
static int do_worker(void) {
  gasneti_reghandler(SIGURG, SIG_DFL);
  gasneti_reghandler(SIGCHLD, SIG_DFL);

  gasneti_free(child-1); /* NOTE: was offset at allocation time */
  child = NULL;
  children = 0;
  ctrl_children = 0;

  if (tree_nodes > 1) {
    int i;

    for (i = 0; i < tree_nodes - 1; ++i) {
      gasneti_free(nodelist[i]);
    }
    gasneti_free(nodelist);
    nodelist = NULL;

    for (i = 0; i < ssh_argc; ++i) {
      gasneti_free(ssh_argv[i]);
    }
    gasneti_free(ssh_argv);
    ssh_argv = NULL;

    gasneti_free((/*non-const*/ void *)wrapper);
    wrapper = NULL;
  }
  tree_nodes = GEX_RANK_INVALID;
  tree_ranks = GEX_RANK_INVALID;

  (void)fcntl_setfd(parent, FD_CLOEXEC);

#if HAVE_PR_SET_PDEATHSIG
  if (use_pdeathsig) {
    /* If our parent exits early (an abnormal condition) then we should too */
    (void)prctl(PR_SET_PDEATHSIG, SIGHUP);
  }
#endif

  BOOTSTRAP_VERBOSE(("[r%d] Connected via fd=%d\n", myrank, parent));

  return GASNET_OK;
}


/* Fork the rank process(es) */
static void spawn_rank(int argc, char **argv) {
  int j;

  for (j = ctrl_children; j < children; ++j) {
    int rc, sv[2];

    rc = my_socketpair(sv);
    if (rc < 0) {
      /* TODO: is any recovery possible? */
      gasneti_fatalerror("socketpair() failed!");
    }

    child[j].sock = sv[1];
    fd_sets_add(sv[1]);
    (void)fcntl_setfd(sv[1], FD_CLOEXEC);

    {
      pid_t pid = fork();
      if (!pid) {
        int k;
        for (k = 0; k <= j; ++k) {
          (void)close(child[k].sock);
        }

        (void)close(parent);
        parent = sv[0];

        signal_forward(0);
        is_control = 0;
        myrank = child[j].rank;
        myname = -911;
        mypid  = getpid();
        (void)my_setpgid(0);

        return;
      } else if (pid < 0) {
        gasneti_fatalerror("fork() failed!");
      }
      (void)my_setpgid(pid);
      child[j].pid = pid;
      ++initialized;
    }

    (void)close(sv[0]);

    gasneti_atomic_increment(&live, 0);
  }
}

/* Spawn control procs via ssh (or fork() when possible) */
static void spawn_ctrl(int argc, char **argv) {
  char *cmdline = quote_arg(argv[0]);
  int j;

  const char *masterip;
  if (is_root && (NULL != (masterip = my_getenv(ENV_PREFIX "MASTERIP")))) {
    strncpy(my_host, masterip, sizeof(my_host) - 1);
    my_host[sizeof(my_host) - 1] = '\0';
  } else if (gethostname(my_host, sizeof(my_host)) < 0) {
    die(1, "gethostname() failed");
  }

  /* Before we began to support gasnet_init(NULL,NULL) the workers were spawned
   * with no arguments, and the gasnet_init() call actual *populated* the passed
   * argc_p and argv_p with the command line sent over the control socket.
   * NOW if we see a NULL-init, then we need to use the full command line in order
   * to be certain that main() will see the arguments.
   */
  if (null_init) {
    for (j = 1; j < argc; ++j) {
      char *tmp = quote_arg(argv[j]);
      cmdline = gasneti_sappendf(cmdline, " %s", tmp);
      gasneti_free(tmp);
    }
  }

  pre_spawn(ctrl_children);
  for (j = 0; j < ctrl_children; ++j) {
    spawn_one_control(j, cmdline, my_host);
  }
  post_spawn(ctrl_children, argc, argv);

  gasneti_free(cmdline);
}

/*----------------------------------------------------------------------------------------------*/

static int do_select(int num_fds, fd_set *rd_fds, fd_set *wr_fds)
{
  int rc;

  do {
    rc = select(num_fds, rd_fds, wr_fds, NULL, NULL);
    if_pf (!rc || ((rc < 0) && (errno != EINTR))) do_abort(-1);
    if_pf (in_abort) return -1;
  } while (rc <= 0);

  return rc;
}

/* Return index of a writable child, removing it from the fd_set */
static int next_write(fd_set *fds)
{
  int j, rc;
  fd_set write_fds = *fds;

  rc = do_select(child_fds.max+1, NULL, &write_fds);
  if (in_abort) return -1;

  for (j = 0; j < children; ++j) {
    if (FD_ISSET(child[j].sock, &write_fds)) {
      FD_CLR(child[j].sock, fds);
      return j;
    }
  }

  /* NOT REACHED */
  return -1; 
}

#define WRITE_EACH_CHILD(_remain, _child, _fdset)         \
  if (0 != (_remain = children))                          \
    for (_fdset = child_fds.set;                          \
         _remain && ((_child = next_write(&(_fdset))),1); \
         --_remain)

static void build_all2all_iov(struct iovec *iov, uint8_t *buf, size_t len, int rank, int size)
{
  size_t row_len = len * nranks;
  size_t run_len = row_len - len * size;
  uint8_t *p = buf + row_len * (rank - myrank);
  int i;

  /* First partial */
  iov[0].iov_base = p;
  iov[0].iov_len  = len * rank;

  /* Skip that partial and the "local" piece */
  p += len * (rank + size);

  /* Contiguous "wrap around" runs */
  for (i = 1; i < size; ++i) {
    iov[i].iov_base = p;
    iov[i].iov_len  = run_len;
    p += row_len;
  }

  /* Final partial */
  iov[size].iov_base = p;
  iov[size].iov_len  = run_len - len * rank;
}

/* In-place square matrix transpose
 * TODO: there are better algorithms than this */
static void transpose(uint8_t *buf, size_t len, size_t n)
{
  const size_t row_len = len * nranks;
  uint8_t *tmp = gasneti_malloc(len);
  gex_Rank_t j, k;
  uint8_t *p0, *q0;
  uint8_t *p1, *q1;

  for (  j = 0, p0 = q0 = buf;     j < n;  ++j, q0 += len, p0 += row_len) {
    for (k = 0, p1 = p0, q1 = q0;  k < j;  ++k, p1 += len, q1 += row_len) {
      GASNETI_MEMCPY(tmp, p1, len);
      GASNETI_MEMCPY(p1,  q1, len);
      GASNETI_MEMCPY(q1, tmp, len);
    }
  }
}

static void cmd_FINI(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_FINI0;
  const char cmd1 = BOOTSTRAP_CMD_FINI1;

  /* State: */
  static int count = 0;

  if (cmd == cmd0) {
    if (++count < children) return;
    count = 0;
    if (! is_root) {
      do_write(parent, &cmd0, sizeof(cmd0));
      return;
    }
  }

  // Children can exit as quickly as they read the writes issued below.
  // So, temporarily disarm SIGCHLD to ensure the loop runs to completion.
  gasneti_blocksig(SIGCHLD);
  {
    fd_set fds;
    int j, k;
    WRITE_EACH_CHILD(j,k,fds) {
      if (k < 0) return;
      do_write(child[k].sock, &cmd1, sizeof(cmd1));
    }
    finalized = 1;
  }
  gasneti_unblocksig(SIGCHLD);
}

static void cmd_BARR(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_BARR0;
  const char cmd1 = BOOTSTRAP_CMD_BARR1;

  /* State: */
  static int count = 0;

  if (cmd == cmd0) {
    if (++count < children) return;
    count = 0;
    if (! is_root) {
      do_write(parent, &cmd0, sizeof(cmd0));
      return;
    }
  }

  {
    fd_set fds;
    int j, k;
    WRITE_EACH_CHILD(j,k,fds) {
      if (k < 0) return;
      do_write(child[k].sock, &cmd1, sizeof(cmd1));
    }
  }
}

static void cmd_BCAST(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_BCAST0;
  const char cmd1 = BOOTSTRAP_CMD_BCAST1;

  /* State: none */

  /* Local: */
  int s = child[i].sock;
  static size_t len;
  struct iovec iov[3];
  uint8_t *data;

  if (cmd == cmd0) {
    do_read(s, &len, sizeof(len));
    data = gasneti_malloc(len);
    do_read(s, data, len);

    if (! is_root) {
      iov[0].iov_base = (void*)&cmd0;
      iov[0].iov_len  = sizeof(cmd0);
      iov[1].iov_base = (void*)&len;
      iov[1].iov_len  = sizeof(len);
      iov[2].iov_base = data;
      iov[2].iov_len  = len;
      do_writev(parent, iov, 3);
    }
  } else {
    do_read(s, &len, sizeof(len));
    data = gasneti_malloc(len);
    do_read(s, data, len);
  }

  {
    fd_set fds;
    int j, k;
    WRITE_EACH_CHILD(j,k,fds) {
      if (k < 0) return;
      if (k == i) continue;
      iov[0].iov_base = (void*)&cmd1;
      iov[0].iov_len  = sizeof(cmd1);
      iov[1].iov_base = &len;
      iov[1].iov_len  = sizeof(len);
      iov[2].iov_base = data;
      iov[2].iov_len  = len;
      do_writev(child[k].sock, iov, 3);
    }
    gasneti_free(data);
  }
}

static void cmd_EXCHG(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_EXCHG0;
  const char cmd1 = BOOTSTRAP_CMD_EXCHG1;

  /* State: */
  static uint8_t *data = NULL;
  static int count = 0;
  static size_t len;

  /* Local: */
  struct iovec iov[3];
  int s = child[i].sock;

  if (cmd == cmd0) {
    static size_t exchg_len;

    do_read(s, &len, sizeof(len));
    if (!count) {
      gasneti_assert_always(!data);
      data = gasneti_malloc(len * nranks);
      exchg_len = len;
    } else {
      gasneti_assert_always_int(len ,==, exchg_len);
    }
    do_read(s, data + len * child[i].rank, len * child[i].tree_ranks);

    if (++count < children) return;
    count = 0;

    if (! is_root) {
      iov[0].iov_base = (void*)&cmd0;
      iov[0].iov_len  = sizeof(cmd0);
      iov[1].iov_base = (void*)&len;
      iov[1].iov_len  = sizeof(len);
      iov[2].iov_base = data + len * myrank;
      iov[2].iov_len  = len * tree_ranks;
      do_writev(parent, iov, 3);
      return;
    }
  } else {
    gex_Rank_t next = myrank + tree_ranks;
    iov[0].iov_base = data;
    iov[0].iov_len  = len * myrank;
    iov[1].iov_base = data + len*next;
    iov[1].iov_len  = len * (nranks - next);
    do_readv(s, iov, 2);
  }

  {
    gex_Rank_t rank, next;
    fd_set fds;
    int j, k;

    WRITE_EACH_CHILD(j,k,fds) {
      if (k < 0) return;
      rank = child[k].rank;
      next = rank + child[k].tree_ranks;
      iov[0].iov_base = (void*)&cmd1;
      iov[0].iov_len  = sizeof(cmd1);
      iov[1].iov_base = data;
      iov[1].iov_len  = len * rank;
      iov[2].iov_base = data + len*next;
      iov[2].iov_len  = len * (nranks - next);
      do_writev(child[k].sock, iov, 3);
    }
    gasneti_free(data);
    data = NULL;
  }
}

static void cmd_TRANS(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_TRANS0;
  const char cmd1 = BOOTSTRAP_CMD_TRANS1;

  /* State: */
  static uint8_t *data = NULL;
  static struct iovec *iov = NULL;
  static int count = 0;
  static size_t len;

  /* Local: */
  int s = child[i].sock;

  if (cmd == cmd0) {
    static size_t trans_len;
    size_t row_len;

    do_read(s, &len, sizeof(len));
    row_len = len * nranks;
    if (!count) {
      gasneti_assert_always(!data);
      data = gasneti_malloc(row_len * tree_ranks);
      iov = gasneti_calloc(3 + tree_ranks, sizeof(struct iovec));
      trans_len = len;
    } else {
      gasneti_assert_always_int(len ,==, trans_len);
    }
    build_all2all_iov(iov, data, len, child[i].rank, child[i].tree_ranks);
    do_readv(s, iov, child[i].tree_ranks + 1);

    if (++count < children) return;
    count = 0;

    transpose(data + len*myrank, len, tree_ranks);
    if (! is_root) {
      iov[0].iov_base = (void*)&cmd0;
      iov[0].iov_len  = sizeof(cmd0);
      iov[1].iov_base = (void*)&len;
      iov[1].iov_len  = sizeof(len);
      build_all2all_iov(iov+2, data, len, myrank, tree_ranks);
      do_writev(parent, iov, 3 + tree_ranks);
      return;
    }
  } else {
    build_all2all_iov(iov, data, len, myrank, tree_ranks);
    do_readv(s, iov, 1 + tree_ranks);
  }

  {
    fd_set fds;
    int j, k;
    WRITE_EACH_CHILD(j,k,fds) {
      if (k < 0) return;
      iov[0].iov_base = (void*)&cmd1;
      iov[0].iov_len  = sizeof(cmd1);
      build_all2all_iov(iov+1, data, len, child[k].rank, child[k].tree_ranks);
      do_writev(child[k].sock, iov, 2 + child[k].tree_ranks);
    }
    gasneti_free(data);
    gasneti_free(iov);
    data = NULL;
    iov = NULL;
  }
}

// TODO: this gets *much* easier if/when we truly have a single control proc per host
static void cmd_SBCAST(char cmd, int i) {
  /* Comands: */
  const char cmd0 = BOOTSTRAP_CMD_SBCAST0;
  const char cmd1 = BOOTSTRAP_CMD_SBCAST1;

  /* State: */
  static uint8_t *data = NULL;
  static gex_Rank_t *roots = NULL;
  static struct iovec *iov = NULL;
  static int count = 0;
  static size_t len;

  /* Local: */
  int s = child[i].sock;

  if (cmd == cmd0) {
    static size_t snbcast_len;
    gex_Rank_t *r;

    do_read(s, &len, sizeof(len));
    if (!count) {
      gasneti_assert_always(!data);
      data = gasneti_malloc(tree_ranks * len);
      roots = gasneti_malloc(tree_ranks * sizeof(gex_Rank_t));
      if (! is_root) {
        iov = gasneti_calloc(3 + children, sizeof(struct iovec));
      }
      snbcast_len = len;
    } else {
      gasneti_assert_always_int(len ,==, snbcast_len);
    }

    {
      gex_Rank_t offset = (child[i].rank - myrank);
      gex_Rank_t *r = roots + offset;
      int j, root_count;
      do_read(s, r, child[i].tree_ranks * sizeof(gex_Rank_t));
      for (j = root_count = 0; j < child[i].tree_ranks; ++j) {
        root_count += (r[j] == child[i].rank + j);
      }
      do_read(s, data + len * offset, root_count * len);
      if (! is_root) {
        int order = (i < ctrl_children) ? (i + (children - ctrl_children)) : (i - ctrl_children);
        iov[3 + order].iov_base = data + len * offset;
        iov[3 + order].iov_len  = root_count * len;
      }
    }

    if (++count < children) return;
    count = 0;

    if (is_root) {
      uint8_t **index = gasneti_calloc(nranks, sizeof(uint8_t*));
      uint8_t *p, *tmp = gasneti_malloc(nranks * len);
      gex_Rank_t r = 0;
      int j, k;
      for (j = 0; j < children; ++j) {
        r = child[j].rank;
        p = data + len * r;
        for (k = 0; k < child[j].tree_ranks; ++k, ++r) {
          if (r == roots[r]) {
            index[r] = p;
            p += len;
          }
        }
      }
      for (r = 0; r < nranks; ++r) {
        gasneti_assert_always(index[roots[r]]);
        GASNETI_MEMCPY(tmp + len * r, index[roots[r]], len);
      }
      gasneti_free(data);
      gasneti_free(roots);
      gasneti_free(index);
      data = tmp;
    } else {
      iov[0].iov_base = (void*)&cmd0;
      iov[0].iov_len  = sizeof(cmd0);
      iov[1].iov_base = (void*)&len;
      iov[1].iov_len  = sizeof(len);
      iov[2].iov_base = roots;
      iov[2].iov_len  = tree_ranks * sizeof(gex_Rank_t);
      do_writev(parent, iov, 3 + children);
      gasneti_free(roots);
      gasneti_free(iov);
      return;
    }
  } else {
    do_read(s, data, len * tree_ranks);
  }

  {
    fd_set fds;
    int j, k;
    WRITE_EACH_CHILD(j,k,fds) {
      struct iovec iov[2];
      iov[0].iov_base = (void*)&cmd1;
      iov[0].iov_len  = sizeof(cmd1);
      iov[1].iov_base = data + len * (child[k].rank - myrank);
      iov[1].iov_len  = len * child[k].tree_ranks;
      do_writev(child[k].sock, iov, 2);
    }
    gasneti_free(data);
    data = NULL;
  }
}

static void dispatch(char cmd, int k) {
  switch (cmd) {
    case BOOTSTRAP_CMD_FINI0:
    case BOOTSTRAP_CMD_FINI1:
      cmd_FINI(cmd, k);
      break;

    case BOOTSTRAP_CMD_BARR0:
    case BOOTSTRAP_CMD_BARR1:
      cmd_BARR(cmd, k);
      break;

    case BOOTSTRAP_CMD_BCAST0:
    case BOOTSTRAP_CMD_BCAST1:
      cmd_BCAST(cmd, k);
      break;

    case BOOTSTRAP_CMD_EXCHG0:
    case BOOTSTRAP_CMD_EXCHG1:
      cmd_EXCHG(cmd, k);
      break;

    case BOOTSTRAP_CMD_TRANS0:
    case BOOTSTRAP_CMD_TRANS1:
      cmd_TRANS(cmd, k);
      break;

    case BOOTSTRAP_CMD_SBCAST0:
    case BOOTSTRAP_CMD_SBCAST1:
      cmd_SBCAST(cmd, k);
      break;

    default:
      do_message("Spawner protocol error");
      do_abort(-1);
  }
}

static void event_loop(void) GASNETI_NORETURN;
static void event_loop(void)
{
    int done = 0;

    do_siginterrupt(SIGCHLD, 1);
    reaper(SIGCHLD);

    while (!finalized && !in_abort) {
      char cmd;
      int k, rc;

      /* Use select to find a fd w/ available work or EOF */
      {
        static int next = 0; /* fairness: start search after prev result */
        int n = all_fds.max + 1;
        fd_set read_fds = all_fds.set;
        int j;

        rc = do_select(n, &read_fds, NULL);
        if (in_abort) break;

        for (j = 0, k = next; j <= children; ++j, ++k) {
          if (k == children) k = is_root ? 0 : -1;
          if (FD_ISSET(child[k].sock, &read_fds)) {
            break;
          }
        }
        next = k + 1;
      }
      gasneti_assert_always(k >= -1);
      gasneti_assert_always(k < children);

      /* Read 1 command byte */
      do_read(child[k].sock, &cmd, sizeof(cmd));
      if (in_abort) break;

      dispatch(cmd, k);
    }

    /* Wait for all children to terminate */
    wait_for_all();

    BOOTSTRAP_VERBOSE(("[%d] Exit with status %d\n", myname, (int)(unsigned char)exit_status));
    _exit (exit_status); // See UPC++ Issue #419
}

/* rank process wait for a specific command byte from parent */
static void wait_cmd(char the_cmd) {
  do {
    char cmd;
    do_read(parent, &cmd, sizeof(cmd));
    if ((cmd == the_cmd) || in_abort) return;
    /* TODO: dispatch any valid rank-process commands (none yet) */
    do_abort(127);
  } while (!in_abort);
}

/*----------------------------------------------------------------------------------------------*/

extern int (*gasneti_verboseenv_fn)(void);

static void usage(const char *argv0) {
  die(1,
      "usage: %s [-GASNET-SPAWN-master] [OPTIONS] NPROC[:NODES] [--] [ARGS...]\n"
      "    NPROC: required count of processes to launch\n"
      "    NODES: optional count of nodes to use (defaults to NPROC)\n"
      "       --: optional separator\n"
      "  ARGS...: optional arguments passed to launched processes\n"
      "  OPTIONS:\n"
      "      -v        enables verbose output from spawner\n"
      "      -W[arg]   prefix launch of remote ssh processes with [arg]\n"
      "                example: \"-W'env LD_LIBRARY_PATH=/usr/local/lib64'\"\n"
      , argv0);
}

#if GASNET_MAXNODES <= INT_MAX
  typedef div_t nodediv_t;
  #define nodediv div
#else
  typedef ldiv_t nodediv_t;
  #define nodediv ldiv
#endif

/* Allocate 'n' from the passed nodediv_t */
static gex_Rank_t nodediv_alloc(nodediv_t *x, gex_Rank_t n) {
  gex_Rank_t rem = MIN(x->rem, n);
  x->rem -= rem;
  return n * x->quot + rem;
}

/* Work common to do_master and do_control */
static void do_common(int argc, char **argv)
{
  gex_Rank_t rank_children;

  /* Arrange to forward various signals */
  signal_forward(1);

  /* Layout children: */
  {
    char **sublist;
    gex_Rank_t rank;
    nodediv_t ppn, npc = {0,0};
    int j;

    ppn = nodediv(tree_ranks, tree_nodes);
    if (is_root) {
      rank_children = 0;
      ctrl_children = MIN(tree_nodes, out_degree);
      npc = nodediv(tree_nodes, ctrl_children);
    } else {
      rank_children = nodediv_alloc(&ppn, 1);
      ctrl_children = MIN(tree_nodes - 1, out_degree);
      if (ctrl_children) {
        npc = nodediv(tree_nodes - 1, ctrl_children);
      }
    }
    children = ctrl_children + rank_children;

    child = gasneti_calloc(1 + children, sizeof(struct child));
    gasneti_leak(child);

    /* parent socket stored as child[-1] */
    child[0].sock = parent;
    ++child;

    /* rank processes take the lower ranks, but the higher slots in child[] */
    rank = myrank;
    for (j = ctrl_children; j < children; ++j) {
      child[j].rank       = rank;
      child[j].tree_ranks = 1;
      child[j].tree_nodes = 1;
      rank += 1;
    }
    gasneti_assert_always(rank == myrank + rank_children);

    /* ctrl processes take the upper ranks, but the lower slots in child[] */
    sublist = nodelist;
    for (j = 0; j < ctrl_children; ++j) {
      const gex_Rank_t nodes = nodediv_alloc(&npc, 1);
      const gex_Rank_t procs = nodediv_alloc(&ppn, nodes);
      child[j].rank       = rank;
      child[j].tree_ranks = procs;
      child[j].tree_nodes = nodes;
      child[j].nodelist   = sublist;
      rank += procs;
      sublist += nodes;
    }
    gasneti_assert_always(rank == myrank + tree_ranks);
  }

  if (ctrl_children) spawn_ctrl(argc, argv);

  if (rank_children) spawn_rank(argc, argv);
  if (!is_control) return; /* A rank process return from spawn_rank */

  event_loop();

  /* NOT REACHED */
}

/* The "main()" for the root master process */
static void do_master(const char *spawn_args, int *argc_p, char ***argv_p) GASNETI_NORETURN;
static void do_master(const char *spawn_args, int *argc_p, char ***argv_p) {
  int argc    = *argc_p;
  char **argv = *argv_p;
  long lnproc = 0;
  long lnnodes = 0;

  is_root = 1;
  is_control = 1;
  is_verbose = gasneti_getenv_yesno_withdefault(ENV_PREFIX "SPAWN_VERBOSE",0); // can be provided via env or cmdline

  fd_sets_init();
  gasneti_reghandler(SIGURG, &sigurg_handler);
  do_siginterrupt(SIGURG, 1);

  if (NULL == spawn_args) { /* Explicit-master support */
    int argi;

    gasneti_assert_always(argc && argv);
    gasneti_assert_always(0 == strcmp(argv[1], "-GASNET-SPAWN-master"));

    /* Optional args in any order */
    for (argi = 2; argi < argc; ++argi) {
      if (0 == strcmp(argv[argi], "-v")) {
        is_verbose = 1;
      } else if (0 == strncmp(argv[argi], "-W", 2)) {
        wrapper = &argv[argi][2];
      } else {
        break;
      }
    }

    /* Required "nproc" optionally followed by ":nnodes" */
    if (argi >= argc) {
      usage(argv0); /* ran out of args */
    } else {
      const char *p = argv[argi];
      char *endptr;
      lnproc = strtol(p,&endptr,0);
      if ((endptr != p) && (':' == *endptr)) {
        const char *q = endptr + 1;
        lnnodes = strtol(q,&endptr,0);
        nnodes_set = (endptr != q);
      }
      if ((endptr == p) || *endptr) {
        die(1, "Unable to parse process and node count in '%s'", p);
      }
    }
    argi++;

    /* Optional "--" */
    if ((argi < argc) && (strcmp(argv[argi], "--") == 0)) {
      argi++;
    }

    /* Splice out all the args we consumed above */
    argv[argi-1] = argv[0];
    argc -= argi-1;
    argv += argi-1;
  } else {
    /* Format of spawn_args: "fd:N:[M]:[W]"
     *  :   indicates the delimiter character
     *  fd  is an integer file descriptor to provide the command line
     *  N   is the positive integer process count
     *  M   is the positive node count, or empty
     *  W   is the "wrapper" and is everything (possibly empty) after the last delimiter
     */
    const char *p = spawn_args;
    char *endptr;
    int argv_fd;

    gasneti_assert_always(p && *p);

    /* Required fd number (non-negative unless restarting) */
    argv_fd = strtol(p,&endptr,0);
    if ((endptr == p) || (*endptr != args_delim) || (argv_fd < 0)) {
      die(1, "Failed to parse " ENV_PREFIX "SPAWN_ARGS");
    }
    p = endptr + 1;

    /* Required process count */
    lnproc = strtol(p,&endptr,0);
    if ((endptr == p) || (*endptr != args_delim)) {
      die(1, "Failed to parse " ENV_PREFIX "SPAWN_ARGS");
    }
    p = endptr + 1;

    /* Optional node count */
    lnnodes = strtol(p,&endptr,0);
    if ((endptr == p) && (*p == args_delim)) {
      /* Not present */
    } else if ((endptr == p) || (*endptr != args_delim)) {
      die(1, "Failed to parse " ENV_PREFIX "SPAWN_ARGS");
    } else {
      nnodes_set = 1;
    }
    p = endptr + 1;

    /* Wrapper is whatever remains */
    wrapper = p;

    /* Load the command line (if necessary) from argv_fd */
    if (!argv) {
      size_t len;
      char *q, *cmdline;
      int i;
      { struct stat s;
        if (fstat(argv_fd, &s) < 0) {
          die(1, "Unable to read the command line from temporary file %d(%s)",
                 errno, strerror(errno));
        }
        len = s.st_size;
      }
      gasneti_leak(cmdline = gasneti_malloc(len));
      do_read(argv_fd, cmdline, len);
      for (q = cmdline, argc = 0; q < (cmdline+len); ++argc) {
        q += strlen(q) + 1;
      }
      argv = gasneti_malloc((argc+1) * sizeof(char*));
      gasneti_leak((void*)argv);
      for (q = cmdline, i = 0; i < argc; ++i) {
        argv[i] = q;
        q += strlen(q) + 1;
      }
      argv[argc] = NULL;
      argv0 = argv[0];
    }
    (void)close(argv_fd);
  }

  nranks = lnproc;
  if ((lnproc < 1) || ((long)nranks != lnproc)) { /* Non-positive or Overflow */
    die(1, "Process count %ld is out-of-range of gex_Rank_t", lnproc);
  }

  if (nnodes_set) {
    nnodes = lnnodes;
    if ((lnnodes < 1) || ((long)nnodes != lnnodes)) { /* Non-positive or Overflow */
      die(1, "Node count %ld is out-of-range of gex_Rank_t", lnnodes);
    }
    if (nnodes > nranks) {
      do_message("WARNING: requested node count reduced from %d to process count of %d",
                      (int)nnodes, (int)nranks);
      nnodes = nranks;
    }
    BOOTSTRAP_VERBOSE(("Spawning '%s': %d processes on %d nodes\n", argv0, (int)nranks, (int)nnodes));
  } else {
    nnodes = nranks;
    BOOTSTRAP_VERBOSE(("Spawning '%s': %d processes\n", argv0, (int)nranks));
  }

  {
    const char *str = my_getenv(ENV_PREFIX "SSH_OUT_DEGREE");
    if (str) out_degree = atoi(str);
    if (out_degree <= 0) out_degree = nnodes; /* unbounded */
  }

  /* Enable VERBOSEENV */
  gasneti_verboseenv_fn = NULL;

  configure_ssh();

  build_nodelist(); /* May reduce nnodes */

  tree_ranks = nranks;
  tree_nodes = nnodes;

  /* Work common to all ctrl processes: */
  do_common(argc, argv);

  /* NOT REACHED */
  gasneti_fatalerror("Unexpected return from do_common()");
}

/* This is "main()" for control procs other than the root */
static void do_control(const char *spawn_args, int *argc_p, char ***argv_p)
{
  is_root = 0;
  is_control = 1;

  fd_sets_init();
  gasneti_reghandler(SIGURG, &sigurg_handler);
  do_siginterrupt(SIGURG, 1);

  #if HAVE_PR_SET_PDEATHSIG
  if (use_pdeathsig) {
    /* If parent exits before us (an abnormal condition) then trigger an abort */
    (void)prctl(PR_SET_PDEATHSIG, SIGURG);
  }
  #endif

  /* Connect w/ parent */
  do_connect(spawn_args, argc_p, argv_p);

  /* Work common to all ctrl processes: */
  do_common(*argc_p, *argv_p);

  /* Only rank process should ever return from do_common() */
  gasneti_assert_always(! is_control);
}

/*----------------------------------------------------------------------------------------------*/

/* gasneti_bootstrapInit
 *
 * Upon return:
 *   + argc and argv are those the user specified
 *   + *nodes_p and *mynode_p are set
 *   + the global environment is available via gasneti_getenv()
 *
 * There is no barrier at the end, so it is possible that in a multi-level
 * tree, there are still some processes not yet spawned.  This is OK, since
 * we assume that at least one gasneti_bootstrap*() collectives will follow.
 * Not waiting here allows any subsequent that first collective to overlap
 * with the spawning.
 */
extern gasneti_spawnerfn_t const * gasneti_bootstrapInit_ssh(int *argc_p, char ***argv_p, gex_Rank_t *nodes_p, gex_Rank_t *mynode_p) {
  const char *spawner, *spawn_args;                                                                             int explicit_master = 0;

  null_init = !(argc_p && argv_p);

  if (!null_init && (*argc_p > 1) && !strcmp((*argv_p)[1], "-GASNET-SPAWN-master")) {
    /* Force legacy explict-master support: */
    explicit_master = 1;
    spawn_args = "XX"; // unused, but avoids "may be used uninitialized" warnings
  } else {
    spawn_args = my_getenv(ENV_PREFIX "SPAWN_ARGS");
    if (!spawn_args || (strlen(spawn_args) < 2)) {
      return NULL;
    }
    gasnett_unsetenv(ENV_PREFIX "SPAWN_ARGS");
  }

  /* Use a "shadow" cmdline if necessary to have a place to receive them */
  if (null_init) {
    static int dummy_argc = 0;
    static char **dummy_argv = NULL;
    argc_p = &dummy_argc;
    argv_p = &dummy_argv;
  } else {
    argv0 = (*argv_p)[0];
  }

  /* set O_APPEND on stdout and stderr (see bug 2136) */
  (void)fcntl_setfl(STDOUT_FILENO, O_APPEND);
  (void)fcntl_setfl(STDERR_FILENO, O_APPEND);

  #ifdef HAVE_PR_SET_PDEATHSIG
  { /* check safety of prctl(PR_SET_PDEATHSIG, ...) */
    struct utsname name;
    if (0 == uname(&name)) {
      const char *dot = strchr(name.release,'.');
      if (NULL != dot) {
        int major = atoi(name.release);
        int minor = atoi(dot + 1);
	use_pdeathsig = ((100 * major + minor) >= 206); /* 2.6.0 kernel or newer */
      }
    }
  }
  #endif

  mypid = getpid();

  if (explicit_master) {
    do_master(NULL, argc_p, argv_p); /* Does not return */
  } else {
    /* Common processing:
     * Format of leading portion of spawn_args is 2 or 3 charachters: "C[v]d"
     *   C = single command character used to dispatch switch() below
     *   v = optional 'v' to enable verbose operation
     *   d = delimiter - the single character in this position will separate the per-command args
     * The commands are passes the portion starting at the delimiter character
     * See do_[foo]() for the per-command arguments
     */
    char spawn_cmd = spawn_args[0];
    is_verbose = (spawn_args[1] == 'v');
    args_delim = spawn_args[1 + is_verbose];
    spawn_args += (2 + is_verbose);

    switch (spawn_cmd) {
      case 'M':  /* The master (root control process) */
        do_master(spawn_args, argc_p, argv_p); /* Does not return */
        break;

      case 'C':  /* Non-root control process */
        do_control(spawn_args, argc_p, argv_p);
        break;

      default:
        return NULL;
    }
  }

  /* Reach here only in the rank processes */
  gasneti_assert_always(! is_control);

  gasneti_getenv_hook = &do_getenv;
  gasneti_propagate_env_hook = &do_propagate_env;
  gasneti_check_env_prefix_hook = &do_check_env_prefix_hook;
  *nodes_p  = nranks;
  *mynode_p = myrank;

  if (do_worker() == GASNET_OK) return &spawnerfn;
  else return NULL;
}

/* bootstrapFini
 */
static void bootstrapFini(void) {
  char cmd0 = BOOTSTRAP_CMD_FINI0;
  char cmd1 = BOOTSTRAP_CMD_FINI1;

  do_write(parent, &cmd0, sizeof(cmd0));

  wait_cmd(cmd1);
}

/* bootstrapAbort
 *
 * Force immediate (abnormal) termination.
 */
static void bootstrapAbort(int exitcode) {
  BOOTSTRAP_VERBOSE(("[r%d] Abort(%d)\n", myrank, exitcode));
  do_abort((unsigned char)exitcode);
  gasneti_fatalerror("do_abort failed.");
  /* NOT REACHED */
}

static void bootstrapBarrier(void) {
  const char cmd0 = BOOTSTRAP_CMD_BARR0;
  const char cmd1 = BOOTSTRAP_CMD_BARR1;

  do_write(parent, &cmd0, sizeof(cmd0));

  wait_cmd(cmd1);
}

static void bootstrapExchange(void *src, size_t len, void *dest) {
  char cmd0 = BOOTSTRAP_CMD_EXCHG0;
  char cmd1 = BOOTSTRAP_CMD_EXCHG1;
  const gex_Rank_t next = myrank + 1;
  struct iovec iov[3];

  iov[0].iov_base = &cmd0;
  iov[0].iov_len  = sizeof(cmd0);
  iov[1].iov_base = (void *)&len;
  iov[1].iov_len  = sizeof(len);
  iov[2].iov_base = src;
  iov[2].iov_len  = len;
  do_writev(parent, iov, 3);

  GASNETI_MEMCPY_SAFE_IDENTICAL((uint8_t*)dest + len*myrank, src, len);

  iov[0].iov_base = dest;
  iov[0].iov_len  = len * myrank;
  iov[1].iov_base = (uint8_t*)dest + len * next;
  iov[1].iov_len  = len * (nranks - next);
  wait_cmd(cmd1);
  do_readv(parent, iov, 2);
}

static void bootstrapAlltoall(void *src, size_t len, void *dest) {
  char cmd0 = BOOTSTRAP_CMD_TRANS0;
  char cmd1 = BOOTSTRAP_CMD_TRANS1;
  struct iovec iov[4];

  iov[0].iov_base = &cmd0;
  iov[0].iov_len  = sizeof(cmd0);
  iov[1].iov_base = (void *)&len;
  iov[1].iov_len  = sizeof(len);
  build_all2all_iov(iov+2, src, len, myrank, 1);
  do_writev(parent, iov, 4);

  GASNETI_MEMCPY_SAFE_IDENTICAL((uint8_t*)dest + len*myrank, (uint8_t*)src + len*myrank, len);

  build_all2all_iov(iov, dest, len, myrank, 1);
  wait_cmd(cmd1);
  do_readv(parent, iov, 2);
}

static void bootstrapBroadcast(void *src, size_t len, void *dest, int rootnode) {
  const char cmd0 = BOOTSTRAP_CMD_BCAST0;
  const char cmd1 = BOOTSTRAP_CMD_BCAST1;
  struct iovec iov[3];

  if (myrank == rootnode) {
    iov[0].iov_base = (void *)&cmd0;
    iov[0].iov_len  = sizeof(cmd0);
    iov[1].iov_base = (void *)&len;
    iov[1].iov_len  = sizeof(len);
    iov[2].iov_base = src;
    iov[2].iov_len  = len;
    do_writev(parent, iov, 3);

    GASNETI_MEMCPY_SAFE_IDENTICAL(dest, src, len);
  } else {
    size_t bcast_len;
    wait_cmd(cmd1);
    iov[0].iov_base = (void *)&bcast_len;
    iov[0].iov_len  = sizeof(bcast_len);
    iov[1].iov_base = dest;
    iov[1].iov_len  = len;
    do_readv(parent, iov, 2);
    gasneti_assert_always_int(len ,==, bcast_len);
  }
}

// Since every caller receives the desired rootnode's contribution from
// the control procs, the NbrhdBroadcast and HostBroadcast are identical.
static void bootstrapSubsetBroadcast(void *src, size_t len, void *dest, int rootnode_arg) {
  char cmd0 = BOOTSTRAP_CMD_SBCAST0;
  char cmd1 = BOOTSTRAP_CMD_SBCAST1;
  const gex_Rank_t rootnode = rootnode_arg;
  struct iovec iov[4];

  iov[0].iov_base = &cmd0;
  iov[0].iov_len  = sizeof(cmd0);
  iov[1].iov_base = (void *)&len;
  iov[1].iov_len  = sizeof(len);
  iov[2].iov_base = (void *)&rootnode;
  iov[2].iov_len  = sizeof(rootnode);
  iov[3].iov_base = src;
  iov[3].iov_len  = len;
  do_writev(parent, iov, (myrank == rootnode) ? 4 : 3);

  wait_cmd(cmd1);
  do_read(parent, dest, len);
}

static void bootstrapCleanup(void) {
  /* TODO: anything we can free at end of bootstrap collectives? */
}

/*----------------------------------------------------------------------------------------------*/

static gasneti_spawnerfn_t const spawnerfn = {
  bootstrapBarrier,
  bootstrapExchange,
  bootstrapBroadcast,
  bootstrapSubsetBroadcast, // Nbrhd
  bootstrapSubsetBroadcast, // Host
  bootstrapAlltoall,
  bootstrapAbort,
  bootstrapCleanup,
  bootstrapFini,
};