[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
[0456]
[0457]
[0458]
[0459]
[0460]
[0461]
[0462]
[0463]
[0464]
[0465]
[0466]
[0467]
[0468]
[0469]
[0470]
[0471]
[0472]
[0473]
[0474]
[0475]
[0476]
[0477]
[0478]
[0479]
[0480]
[0481]
[0482]
[0483]
[0484]
[0485]
[0486]
[0487]
[0488]
[0489]
[0490]
[0491]
[0492]
[0493]
[0494]
[0495]
[0496]
[0497]
[0498]
[0499]
[0500]
[0501]
[0502]
[0503]
[0504]
[0505]
[0506]
[0507]
[0508]
[0509]
[0510]
[0511]
[0512]
[0513]
[0514]
[0515]
[0516]
[0517]
[0518]
[0519]
[0520]
[0521]
[0522]
[0523]
[0524]
[0525]
[0526]
[0527]
[0528]
[0529]
[0530]
[0531]
[0532]
[0533]
[0534]
[0535]
[0536]
[0537]
[0538]
[0539]
[0540]
[0541]
[0542]
[0543]
[0544]
[0545]
[0546]
[0547]
[0548]
[0549]
[0550]
[0551]
[0552]
[0553]
[0554]
[0555]
[0556]
[0557]
[0558]
[0559]
[0560]
[0561]
[0562]
[0563]
[0564]
[0565]
[0566]
[0567]
[0568]
[0569]
[0570]
[0571]
[0572]
[0573]
[0574]
[0575]
[0576]
[0577]
[0578]
[0579]
[0580]
[0581]
[0582]
[0583]
[0584]
[0585]
[0586]
[0587]
[0588]
[0589]
[0590]
[0591]
[0592]
[0593]
[0594]
[0595]
[0596]
[0597]
[0598]
[0599]
[0600]
[0601]
[0602]
[0603]
[0604]
[0605]
[0606]
[0607]
[0608]
[0609]
[0610]
[0611]
[0612]
[0613]
[0614]
[0615]
[0616]
[0617]
[0618]
[0619]
[0620]
[0621]
[0622]
[0623]
[0624]
[0625]
[0626]
[0627]
[0628]
[0629]
[0630]
[0631]
[0632]
[0633]
[0634]
[0635]
[0636]
[0637]
[0638]
[0639]
[0640]
[0641]
[0642]
[0643]
[0644]
[0645]
[0646]
[0647]
[0648]
[0649]
[0650]
[0651]
[0652]
[0653]
[0654]
[0655]
[0656]
[0657]
[0658]
[0659]
[0660]
[0661]
[0662]
[0663]
[0664]
[0665]
[0666]
[0667]
[0668]
[0669]
[0670]
[0671]
[0672]
[0673]
[0674]
[0675]
[0676]
[0677]
[0678]
[0679]
[0680]
[0681]
[0682]
[0683]
[0684]
[0685]
[0686]
[0687]
[0688]
[0689]
[0690]
[0691]
[0692]
[0693]
[0694]
[0695]
[0696]
[0697]
[0698]
[0699]
[0700]
[0701]
[0702]
[0703]
[0704]
[0705]
[0706]
[0707]
[0708]
[0709]
[0710]
[0711]
[0712]
[0713]
[0714]
[0715]
[0716]
[0717]
[0718]
[0719]
[0720]
[0721]
[0722]
[0723]
[0724]
[0725]
[0726]
[0727]
[0728]
[0729]
[0730]
[0731]
[0732]
[0733]
[0734]
[0735]
[0736]
[0737]
[0738]
[0739]
[0740]
[0741]
[0742]
[0743]
[0744]
[0745]
[0746]
[0747]
[0748]
[0749]
[0750]
[0751]
[0752]
[0753]
[0754]
[0755]
[0756]
[0757]
[0758]
[0759]
[0760]
[0761]
[0762]
[0763]
[0764]
[0765]
[0766]
[0767]
[0768]
[0769]
[0770]
[0771]
[0772]
[0773]
[0774]
[0775]
[0776]
[0777]
[0778]
[0779]
[0780]
[0781]
[0782]
[0783]
[0784]
[0785]
[0786]
[0787]
[0788]
[0789]
[0790]
[0791]
[0792]
[0793]
[0794]
[0795]
[0796]
[0797]
[0798]
[0799]
[0800]
[0801]
[0802]
[0803]
[0804]
[0805]
[0806]
[0807]
[0808]
[0809]
[0810]
[0811]
[0812]
[0813]
[0814]
[0815]
[0816]
[0817]
[0818]
[0819]
[0820]
[0821]
[0822]
[0823]
[0824]
[0825]
[0826]
[0827]
[0828]
[0829]
[0830]
[0831]
[0832]
[0833]
[0834]
[0835]
[0836]
[0837]
[0838]
[0839]
[0840]
[0841]
[0842]
[0843]
[0844]
[0845]
[0846]
[0847]
[0848]
[0849]
[0850]
[0851]
[0852]
[0853]
[0854]
[0855]
[0856]
[0857]
[0858]
[0859]
[0860]
[0861]
[0862]
[0863]
[0864]
[0865]
[0866]
[0867]
[0868]
[0869]
[0870]
[0871]
[0872]
[0873]
[0874]
[0875]
[0876]
[0877]
[0878]
[0879]
[0880]
[0881]
[0882]
[0883]
[0884]
[0885]
[0886]
[0887]
[0888]
[0889]
[0890]
[0891]
[0892]
[0893]
[0894]
[0895]
[0896]
[0897]
[0898]
[0899]
[0900]
[0901]
[0902]
[0903]
[0904]
[0905]
[0906]
[0907]
[0908]
[0909]
[0910]
[0911]
[0912]
[0913]
[0914]
[0915]
[0916]
[0917]
[0918]
[0919]
[0920]
[0921]
[0922]
[0923]
[0924]
[0925]
[0926]
[0927]
[0928]
[0929]
[0930]
[0931]
[0932]
[0933]
[0934]
[0935]
[0936]
[0937]
[0938]
[0939]
[0940]
[0941]
[0942]
[0943]
[0944]
[0945]
[0946]
[0947]
[0948]
[0949]
[0950]
[0951]
[0952]
[0953]
[0954]
[0955]
[0956]
[0957]
[0958]
[0959]
[0960]
[0961]
[0962]
[0963]
[0964]
[0965]
[0966]
[0967]
[0968]
[0969]
[0970]
[0971]
[0972]
[0973]
[0974]
[0975]
[0976]
[0977]
[0978]
[0979]
[0980]
[0981]
[0982]
[0983]
[0984]
[0985]
[0986]
[0987]
[0988]
[0989]
[0990]
[0991]
[0992]
[0993]
[0994]
[0995]
[0996]
[0997]
[0998]
[0999]
[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]
[2794]
[2795]
[2796]
[2797]
[2798]
[2799]
[2800]
[2801]
[2802]
[2803]
[2804]
[2805]
[2806]
[2807]
[2808]
[2809]
[2810]
[2811]
[2812]
[2813]
[2814]
[2815]
[2816]
[2817]
[2818]
[2819]
[2820]
[2821]
[2822]
[2823]
[2824]
[2825]
[2826]
[2827]
[2828]
[2829]
[2830]
[2831]
[2832]
[2833]
[2834]
[2835]
[2836]
[2837]
[2838]
[2839]
[2840]
[2841]
[2842]
[2843]
[2844]
[2845]
[2846]
[2847]
[2848]
[2849]
[2850]
[2851]
[2852]
[2853]
[2854]
[2855]
[2856]
[2857]
[2858]
[2859]
[2860]
[2861]
[2862]
[2863]
[2864]
[2865]
[2866]
[2867]
[2868]
[2869]
[2870]
[2871]
[2872]
[2873]
[2874]
[2875]
[2876]
[2877]
[2878]
[2879]
[2880]
[2881]
[2882]
[2883]
[2884]
[2885]
[2886]
[2887]
[2888]
[2889]
[2890]
[2891]
[2892]
[2893]
[2894]
[2895]
[2896]
[2897]
[2898]
[2899]
[2900]
[2901]
[2902]
[2903]
[2904]
[2905]
[2906]
[2907]
[2908]
[2909]
[2910]
[2911]
[2912]
[2913]
[2914]
[2915]
[2916]
[2917]
[2918]
[2919]
[2920]
[2921]
[2922]
[2923]
[2924]
[2925]
[2926]
[2927]
[2928]
[2929]
[2930]
[2931]
[2932]
[2933]
[2934]
[2935]
[2936]
[2937]
[2938]
[2939]
[2940]
[2941]
[2942]
[2943]
[2944]
[2945]
[2946]
[2947]
[2948]
[2949]
[2950]
[2951]
[2952]
[2953]
[2954]
[2955]
[2956]
[2957]
[2958]
[2959]
[2960]
[2961]
[2962]
[2963]
[2964]
[2965]
[2966]
[2967]
[2968]
[2969]
[2970]
[2971]
[2972]
[2973]
[2974]
[2975]
[2976]
[2977]
[2978]
[2979]
[2980]
[2981]
[2982]
[2983]
[2984]
[2985]
[2986]
[2987]
[2988]
[2989]
[2990]
[2991]
[2992]
[2993]
[2994]
[2995]
[2996]
[2997]
[2998]
[2999]
[3000]
[3001]
[3002]
[3003]
[3004]
[3005]
[3006]
[3007]
[3008]
[3009]
[3010]
[3011]
[3012]
[3013]
[3014]
[3015]
[3016]
[3017]
[3018]
[3019]
[3020]
[3021]
[3022]
[3023]
[3024]
[3025]
[3026]
[3027]
[3028]
[3029]
[3030]
[3031]
[3032]
[3033]
[3034]
[3035]
[3036]
[3037]
[3038]
[3039]
[3040]
[3041]
[3042]
[3043]
[3044]
[3045]
[3046]
[3047]
[3048]
[3049]
[3050]
[3051]
[3052]
[3053]
[3054]
[3055]
[3056]
[3057]
[3058]
[3059]
[3060]
[3061]
[3062]
[3063]
[3064]
[3065]
[3066]
[3067]
[3068]
[3069]
[3070]
[3071]
[3072]
[3073]
[3074]
[3075]
[3076]
[3077]
[3078]
[3079]
[3080]
[3081]
[3082]
[3083]
[3084]
[3085]
[3086]
[3087]
[3088]
[3089]
[3090]
[3091]
[3092]
[3093]
[3094]
[3095]
[3096]
[3097]
[3098]
[3099]
[3100]
[3101]
[3102]
[3103]
[3104]
[3105]
[3106]
[3107]
[3108]
[3109]
[3110]
[3111]
[3112]
[3113]
[3114]
[3115]
[3116]
[3117]
[3118]
[3119]
[3120]
[3121]
[3122]
[3123]
[3124]
[3125]
[3126]
[3127]
[3128]
[3129]
[3130]
[3131]
[3132]
[3133]
[3134]
[3135]
[3136]
[3137]
[3138]
[3139]
[3140]
[3141]
[3142]
[3143]
[3144]
[3145]
[3146]
[3147]
[3148]
[3149]
[3150]
[3151]
[3152]
[3153]
[3154]
[3155]
[3156]
[3157]
[3158]
[3159]
[3160]
[3161]
[3162]
[3163]
[3164]
[3165]
[3166]
[3167]
[3168]
[3169]
[3170]
[3171]
[3172]
[3173]
[3174]
[3175]
[3176]
[3177]
[3178]
[3179]
[3180]
[3181]
[3182]
[3183]
[3184]
[3185]
[3186]
[3187]
[3188]
[3189]
[3190]
[3191]
[3192]
[3193]
[3194]
[3195]
[3196]
[3197]
[3198]
[3199]
[3200]
[3201]
[3202]
[3203]
[3204]
[3205]
[3206]
[3207]
[3208]
[3209]
[3210]
[3211]
[3212]
[3213]
[3214]
[3215]
[3216]
[3217]
[3218]
[3219]
[3220]
[3221]
[3222]
[3223]
[3224]
[3225]
[3226]
[3227]
[3228]
[3229]
[3230]
[3231]
[3232]
[3233]
[3234]
[3235]
[3236]
[3237]
[3238]
[3239]
[3240]
[3241]
[3242]
[3243]
[3244]
[3245]
[3246]
[3247]
[3248]
[3249]
[3250]
[3251]
[3252]
[3253]
[3254]
[3255]
[3256]
[3257]
[3258]
[3259]
[3260]
[3261]
[3262]
[3263]
[3264]
[3265]
[3266]
[3267]
[3268]
[3269]
[3270]
[3271]
[3272]
[3273]
[3274]
[3275]
[3276]
[3277]
[3278]
[3279]
[3280]
[3281]
[3282]
[3283]
[3284]
[3285]
[3286]
[3287]
[3288]
[3289]
[3290]
[3291]
[3292]
[3293]
[3294]
[3295]
[3296]
[3297]
[3298]
[3299]
[3300]
[3301]
[3302]
[3303]
[3304]
[3305]
[3306]
[3307]
[3308]
[3309]
[3310]
[3311]
[3312]
[3313]
[3314]
[3315]
[3316]
[3317]
[3318]
[3319]
[3320]
[3321]
[3322]
[3323]
[3324]
[3325]
[3326]
[3327]
[3328]
[3329]
[3330]
[3331]
[3332]
[3333]
[3334]
[3335]
[3336]
[3337]
[3338]
[3339]
[3340]
[3341]
[3342]
[3343]
[3344]
[3345]
[3346]
[3347]
[3348]
[3349]
[3350]
[3351]
[3352]
[3353]
[3354]
[3355]
[3356]
[3357]
[3358]
[3359]
[3360]
[3361]
[3362]
[3363]
[3364]
[3365]
[3366]
[3367]
[3368]
[3369]
[3370]
[3371]
[3372]
[3373]
[3374]
[3375]
[3376]
[3377]
[3378]
[3379]
[3380]
[3381]
[3382]
[3383]
[3384]
[3385]
[3386]
[3387]
[3388]
[3389]
[3390]
[3391]
[3392]
[3393]
[3394]
[3395]
[3396]
[3397]
[3398]
[3399]
[3400]
[3401]
[3402]
[3403]
[3404]
[3405]
[3406]
[3407]
[3408]
[3409]
[3410]
[3411]
[3412]
[3413]
[3414]
[3415]
[3416]
[3417]
[3418]
[3419]
[3420]
[3421]
[3422]
[3423]
[3424]
[3425]
[3426]
[3427]
[3428]
[3429]
[3430]
[3431]
[3432]
[3433]
[3434]
[3435]
[3436]
[3437]
[3438]
[3439]
[3440]
[3441]
[3442]
[3443]
[3444]
[3445]
[3446]
[3447]
[3448]
[3449]
[3450]
[3451]
[3452]
[3453]
[3454]
[3455]
[3456]
[3457]
[3458]
[3459]
[3460]
[3461]
[3462]
[3463]
[3464]
[3465]
[3466]
[3467]
[3468]
[3469]
[3470]
[3471]
[3472]
[3473]
[3474]
[3475]
[3476]
[3477]
[3478]
[3479]
[3480]
[3481]
[3482]
[3483]
[3484]
[3485]
[3486]
[3487]
[3488]
[3489]
[3490]
[3491]
[3492]
[3493]
[3494]
[3495]
[3496]
[3497]
[3498]
[3499]
[3500]
[3501]
[3502]
[3503]
[3504]
[3505]
[3506]
[3507]
[3508]
[3509]
[3510]
[3511]
[3512]
[3513]
[3514]
[3515]
[3516]
[3517]
[3518]
[3519]
[3520]
[3521]
[3522]
[3523]
[3524]
[3525]
[3526]
[3527]
[3528]
[3529]
[3530]
[3531]
[3532]
[3533]
[3534]
[3535]
[3536]
[3537]
[3538]
[3539]
[3540]
[3541]
[3542]
[3543]
[3544]
[3545]
[3546]
[3547]
[3548]
[3549]
[3550]
[3551]
[3552]
[3553]
[3554]
[3555]
[3556]
[3557]
[3558]
[3559]
[3560]
[3561]
[3562]
[3563]
[3564]
[3565]
[3566]
[3567]
[3568]
[3569]
[3570]
[3571]
[3572]
[3573]
[3574]
[3575]
[3576]
[3577]
[3578]
[3579]
[3580]
[3581]
[3582]
[3583]
[3584]
[3585]
[3586]
[3587]
[3588]
[3589]
[3590]
[3591]
[3592]
[3593]
[3594]
[3595]
[3596]
[3597]
[3598]
[3599]
[3600]
[3601]
[3602]
[3603]
[3604]
[3605]
[3606]
[3607]
[3608]
[3609]
[3610]
[3611]
[3612]
[3613]
[3614]
[3615]
[3616]
[3617]
[3618]
[3619]
[3620]
[3621]
[3622]
[3623]
[3624]
[3625]
[3626]
[3627]
[3628]
[3629]
[3630]
[3631]
[3632]
[3633]
[3634]
[3635]
[3636]
[3637]
[3638]
[3639]
[3640]
[3641]
[3642]
[3643]
[3644]
[3645]
[3646]
[3647]
[3648]
[3649]
[3650]
[3651]
[3652]
[3653]
[3654]
[3655]
[3656]
[3657]
[3658]
[3659]
[3660]
[3661]
[3662]
[3663]
[3664]
[3665]
[3666]
[3667]
[3668]
[3669]
[3670]
[3671]
[3672]
[3673]
[3674]
[3675]
[3676]
[3677]
[3678]
[3679]
[3680]
[3681]
[3682]
[3683]
[3684]
[3685]
[3686]
[3687]
[3688]
[3689]
[3690]
[3691]
[3692]
[3693]
[3694]
[3695]
[3696]
[3697]
[3698]
[3699]
[3700]
[3701]
[3702]
[3703]
[3704]
[3705]
[3706]
[3707]
[3708]
[3709]
[3710]
[3711]
[3712]
[3713]
[3714]
[3715]
[3716]
[3717]
[3718]
[3719]
[3720]
[3721]
[3722]
[3723]
[3724]
[3725]
[3726]
[3727]
[3728]
[3729]
[3730]
[3731]
[3732]
[3733]
[3734]
[3735]
[3736]
[3737]
/*****************************************************************************/
/*
                                Instance.c

The INSTANCE module contains functions used to setup, maintain and coordinate
action between, multiple servers running on a single system, alone or in
combination with multiple servers running across a cluster.  An "instance" in
this context refers to the (almost) completely autonomous server process.

A large portion of the required functionality concerns itself with
synchronization and communication between these instances (servers) using the
Distributed Lock Manager (DLM) and mutexes.

Multiple processes (instances) can share incoming requests by each assigning a
channel to the same BG: pseudo-device created with the appropriate listening
socket characteristics.  Each will receive a share of the incoming requests in
a round-robin distribution.  See NET.C for more information on this particular
aspect and it's implementation.


VMS CLUSTERING COMPARISON
-------------------------
The approach WASD has used in providing multiple instance serving may be
compared to VMS clustering.

A cluster is often described as a loosely-coupled, distributed operating
environment where autonomous processors can join, process and leave (even fail)
independently, participating in a single management domain and communicating
with one another for the purposes of resource sharing and high availability.

Similarly WASD instances run in autononmous, detached processes (across one or
more systems in a cluster) using a common configuration and management
interface, aware of the presence and activity of other instances (via the DLM
and shared memory), sharing processing load and providing rolling restart and
automatic failover as required.


LOAD SHARING
------------
On a multi-CPU system there are performance advantages to having processing
available for scheduling on each.  WASD employs AST (I/O) based processing and
was not originally designed to support VMS kernel threading.  Benchmarking has
shown this be quite fast and efficient even when compared to a kernel-threaded
server (OSU) across 2 CPUs.  The advantage of multiple CPUs for a single
multi-threaded server also diminishes where a site frequently activates scripts
for processing.  These of course (potentially) require a CPU each for
processing.  Where a system has many CPUs (and to a lesser extent with only two
and few script activations) WASD's single-process, AST-driven design would
scale more poorly.  Running multiple WASD instances addresses this.

Of course load sharing is not the only advantage to multiple instances ...


RESTART
-------
When multiple WASD instances are executing on a node and a restart is directed
only one process shuts down at a time.  The rest remain available for requests
until the one restarting is fully ready to again process them itself.


FAIL-THROUGH
------------
When multiple instances are executing on a node and one of these exits for some
reason (bugcheck, resource exhaustion, etc.) the other(s) will continue to
process requests.  Of course requests in-progress by the particular instance at
the time of instance  failure are disconnected.  If the former process has
actually exited (in contrast to just the image) a new server process will
automatically be created after a few seconds.


ACTIVE/PASSIVE
--------------
Implemented in NetActive() and NetPassive(), and under the control of CLI and
Server Admin directives, instances can operate in either of two modes.  ACTIVE
mode; (classic/historical WASD instance processing, with all instances sharing
the request processing load.  PASSIVE mode; where only the supervisor instance
is processing requests, other instances are instantiated but quiescent.

One of the issues with multiple instances is use of the WATCH facility.  WATCH
necessarily can deal with only one instance at a time (tied as it is via a
network connection and the associated per-process socket).  It becomes a very
hit-and-miss activity to try and capture particular events on multi-instance
sites.  The only solution, without (before) passive instances was to reduce the
site to a single instance (requires a restart) and WATCH only that.  Making
instance processing passive is a (relatively) transparent action that confines
request  processing to the one (supervisor) instance only.  This allows WATCH
to be used much more effectively.  When the activity is complete just move the
instances back to active mode.

Although described here in the INSTANCE.C module all the functionality is
implemented in the NET.C module.  To move into passive mode the mechanism is
simply to dequeue ($CANCEL) all the connection acceptance QIOs on all instances
but the supervisor.  Very simple all the other instances no longer respond to
connection requests.  To move to active mode new accepts are queued restoring
the instance(s) to processing.  Elegant and functional!  Instance failover is
still maintained by having a previously passive, non-supervisor instance
receiving the supervisor lock AST check and enable active mode on it's sockets
as required.


LOCK RESOURCE NAMES
-------------------
With IPv6 support with WASD v8.5 lock resource names needed to be changed from
the previously all ASCII to a binary representation.  To continue using locks
to coordinate socket usage the previously hexadecimal representation for the 32
bit IPv4 address and 16 bit port number needed to be expanded  to accomodate
the 128 bit IPv6 address and 16 bit port.  For this to fit into a 31 character
resource name the address/port data needed to be represented in binary and
other information (e.g. naming version) needed to be compressed (also into a
binary representation).

per-cluster specific  WASD|v|g|f                 WASD..
per-node specific     WASD|v|g|node|f            WASD.KLAATU.
per-node socket       WASD|v|g|node|ap           WASD.KLAATU.................
admin socket          WASD|v|g|node::WASD:port   WASD.KLAATU::WASD:80

where   v is the 4 bit WASD instance lock version
        g is the 4 bit "environment" number (0..15)
        f is the 8 bit lock function (0..31)
       ap is the 32 bit or 128 bit address plus the 16 bit port

These locks can be located in the System Dump Analyzer using

  SDa> SHOW LOCK /name=WASD

The "per-cluster specific" are used to synchronize and communicate through
locking across all nodes in a cluster.

The "per-node specific" are used to synchronize and communicate through locking
access to various resources shared amongst servers executing on the one node.

The "per-node socket" is used to distribute information about the BG: device
names of sockets already created by instances on the node.  The device names
store in the lock value blocks then allow subsequent instances to just assign
channels to share the listen-for requests.

The "admin socket" distributes per-instance (process) administration socket
port across the node/cluster.  This administration socket is required to allow
a site administrator consistent access to a single instance (normally of course
the socket sharing between instances means that requests are distributed
between processes in a round-robin fashion).  As this contains only the port
number (in decimal) it assumes that there is a host name entry for each of the
instance node names.


MUTEX USAGE
-----------
Using of the DLM for short-duration locking of shared memory access is probably
an overly-expensive approach.  So for these activities a mutex in shared memory
is used.  Multiple such mutexes are supported to provide maximum granularity
when distributed across various activities.  See InstanceMutexLock() for
further detail.


INSTANCE STATUS
---------------
This facility distributes basic instance status data to all instances on the
node and/or cluster.  The data comprises:

  o  instance name              e.g. "KLAATU::WASD:443"
  o  instance WASD version      e.g. "11.2.0"
  o  number of requests processed during the previous minute
  o  number of requests processed during the previous hour
  o  number of times the instance has started up
  o  date/time the instance last started
  o  date/time the instance last exited
  o  the VMS status at the last exit
  o  date/time the instance status was updated

The status data for the maximum instances per cluster (MAX_INSTANCE of 8 by a
maximum of 8 nodes, or 64 instances) is maintained in a table in the global
section accounting data.  For a single node (cf. clustered node) the data (for
a single or multiple instances) is updated by the individual instance from
which that data is generated.  On a single, non-clustered node the DLM is not
used for data distribution.

For clustered nodes the data is distributed to other nodes using the 64 byte
lock value block, once per and from that maintained in the node's global
section accounting data.  Data incoming via the DLM is ignored unless from a
different node name.  Only the supervisor on each node is required to listen
for and maintain incoming data.  When an instance becomes the node supervisor
it begins listening to the INSTANCE_CLUSTER_STATUS lock.

These statuses are then provided directly from the table to command-line and
in-browser reports.  The instances are listed in the order in which they were
introduced to the pool of per-node and/or per-cluster instances.  Obviously
these reports are intended for larger WASD installations, primarily those
operating across multiple nodes in a cluster.  With the data being stored in a
common, another of those other nodes can provide a per-cluster history even if
one or more nodes become completely non-operational.


VERSION HISTORY
---------------
28-APR-2018  MGD  refactor Admin..() AST delivery
                  InstanceSupervisor() simpify ticket key refresh
12-JAN-2018  MGD  bugfix; longstanding InstanceSocketForAdmin() sys$deq()
24-OCT-2017  MGD  InstanceStatus..() see above
                  some supporting changes to locking
21-JUN-2017  MGD  InstanceUseConfig() ensure config file values used 
09-MAY-2017  MGD  bugfix; SesolaSessionTicketNewKey() sigh some more :-[
28-APR-2017  MGD  InstanceSessionTicketKey() rework multi-instance/cluster
                    (sigh! yes again; the lack of a test cluster these days)
25-JUL-2016  MGD  InstanceSessionTicketKey() rework multi-instance rotate
09-JUL-2016  MGD  CLI /INSTANCE=<integer> now sets global section |InstanceMax|
                    to allow the created process to continue to exist and when
                    used needs to be reset with the likes of /INSTANCE=1
12-JUN-2016  MGD  InstanceSupervisor() refresh session ticket key every day
                  InstanceLockList() supervisor list process NL locks
                    allows node lists to be listed in supervisory order
10-MAY-2015  MGD  bugfix; move supervisor PID from InstanceNodeSupervisor()
                    to InstanceNodeSupervisorAst()
03-DEC-2014  MGD  InstanceSupervisor() global section ->InstanceNodeCurrent
13-JAN-2014  MGD  InstanceGblSecSetLong()
                  add InstanceNumber for identifying from process name
08-OCT-2009  MGD  if HttpdServerStartup delay additional instance startup
16-AUG-2009  MGD  bugfix; InstanceSupervisor() InstanceProcessName() prcnam
11-JUL-2009  MGD  InstanceSupervisor() and InstanceProcessName()
                  move process naming from "HTTPd:" to "WASD:"
                  with backward-compatibility via WASD_PROCESS_NAME
05-NOV-2006  MGD  it would appear that at least IA64 returns a lock value
                  block length of 64 regardless of whether it is empty or not!
15-JUL-2006  MGD  instance active and passive modes
                  InstanceNodeSupervisorAst() calls NetActive(TRUE)
                  refinements to controlled restart
04-JUL-2006  MGD  use PercentOf32() for more accurate percentages
25-MAY-2005  MGD  allow for VMS V8.2 64 byte lksb$b_valblk
17-NOV-2004  MGD  InstanceLockReportData() rework blocked-by/blocking
                  into general indication of non-GR queue (underline)
10-APR-2004  MGD  significant modifications to support IPv6,
                  lock names now contain non-ASCII, binary components,
                  remove never-used InstanceLockReportCli() and /LOCK
27-JUL-2003  MGD  bugfix; use _BBCCI() to clear the mutex in InstanceExit()!!
19-JUN-2003  MGD  bugfix; use _BBCCI() to clear the mutex
31-MAR-2003  MGD  bugfix; add &puser= to lock 'show process' report
30-MAY-2002  MGD  restart when 'quiet'
20-MAY-2002  MGD  move more 'locking' functions over to using a 'mutex'
10-APR-2002  MGD  some refinement to single-instance locking
31-MAR-2002  MGD  use a more light-weight 'mutex' instead of DLM lock
                  around general global section access
28-DEC-2001  MGD  refine 'instance' creation/destruction
22-SEP-2001  MGD  initial
*/
/*****************************************************************************/

#ifdef WASD_VMS_V7
#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000
#endif

/* standard C header files */
#include <builtins.h>
#include <stdarg.h>
#include <stdio.h>

/* VMS related header files */
#include <dvidef.h>
#include <descrip.h>
#include <jpidef.h>
#include <lckdef.h>
#include <libdtdef.h>
#include <lkidef.h>
#include <lnmdef.h>
#include <prvdef.h>
#include <ssdef.h>
#include <stsdef.h>
#include <syidef.h>

/* application-related header files */
#include "wasd.h"

#define WASD_MODULE "INSTANCE"

/******************/
/* global storage */
/******************/

BOOL  InstanceNodeSupervisor,
      InstanceWasdName = true;

int  InstanceClusterCurrent,
     InstanceEnvNumber,
     InstanceLockNameMagic,
     InstanceNodeConfig,
     InstanceNodeCurrent,
     InstanceNodeJoiningCount,
     InstanceNumber = 0,  /* instances disabled */
     InstanceStatusRetry,
     InstanceStatusRequestCount,
     InstanceLockReportNameWidth,
     InstancePrevRequestCount,
     InstanceSocketCount,
     InstanceSupervisorPoll;

char  *InstanceGroupChars [] = { "","","2","3","4","5","6","7","8","9",
                                 "a","b","c","d","e","f" },
      *InstanceHttpChars [] =  { "d","d","e","f","g","h","i","j","k" },
      *InstanceWasdChars [] =  { "","1","2","3","4","5","6","7","8" };

#if sizeof(WasdChars)/sizeof(char*) < INSTANCE_MAX
#error "InstanceProcessName() WasdChars[] needs adjustment"
#endif

INSTANCE_LOCK  InstanceLockAdmin;
INSTANCE_LOCK  InstanceLockTable [INSTANCE_LOCK_COUNT+1];
INSTANCE_SOCKET_LOCK InstanceSocketTable [INSTANCE_LOCK_SOCKET_MAX];
INSTANCE_STATUS  *InstanceStatusTablePtr;

BOOL  InstanceMutexHeld [INSTANCE_MUTEX_COUNT+1];
ulong  InstanceMutexCount [INSTANCE_MUTEX_COUNT+1],
       InstanceMutexWaitCount [INSTANCE_MUTEX_COUNT+1];
char *InstanceMutexDescr [INSTANCE_MUTEX_COUNT+1] = INSTANCE_MUTEX_DESCR;

/********************/
/* external storage */
/********************/

extern BOOL  CliInstanceNoCrePrc,
             CliInstancePassive,
             ControlRestartQuiet,
             ControlRestartRequested,
             HttpdNetworkMode,
             HttpdServerStartup,
             HttpdTicking;
             ProtocolHttpsAvailable,
             ProtocolHttpsConfigured;

extern int  CliInstanceMax,
            EfnWait,
            EfnNoWait,
            ExitStatus,
            HttpdTickSecond,
            NetCurrentProcessing,
            RequestCount,
            ServerPort,
            SesolaTicketKeySuperDay;

extern int64  HttpdTime64;

extern int  ToLowerCase[],
            ToUpperCase[];

extern int64  HttpdStartTime64;

extern ulong  CrePrcMask[],
              SysLckMask[],
              WorldMask[];

extern ushort  HttpdTime7[];

extern char  ErrorSanityCheck[],
             ErrorXvalNotValid[],
             HttpdVersion[];

extern uchar  SesolaTicketKey[];

extern ACCOUNTING_STRUCT  *AccountingPtr;
extern CONFIG_STRUCT  Config;
extern HTTPD_GBLSEC  *HttpdGblSecPtr;
extern HTTPD_PROCESS  HttpdProcess;
extern MSG_STRUCT  Msgs;
extern SYS_INFO  SysInfo;
extern WATCH_STRUCT  Watch;

/*****************************************************************************/
/*
Initialize the per-node and per-cluster lock resource names, queuing a NL lock
against each resource.  This NL lock will then be converted to other modes as
required.
*/

InstanceLockInit ()

{
   static int LockCode [] = { INSTANCE_LOCK_CODES };

   int  cnt, status,
        NameLength;
   char  *cptr, *sptr, *zptr;
   INSTANCE_LOCK  *ilptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceLockInit()");

   if ((HTTPD_LOCK_VERSION & 0xf) > 15)
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   if (sizeof(INSTANCE_STATUS) != 64)
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   if (InstanceEnvNumber > INSTANCE_ENV_NUMBER_MAX)
   {
      FaoToStdout ("%HTTPD-E-INSTANCE, environment range 1 to !UL\n",
                   DEMO_INSTANCE_GROUP_NUMBER);
      exit (SS$_BADPARAM);
   }

   /* a byte comprising two 4 bit fields, version and environment number */
   InstanceLockNameMagic = ((HTTPD_LOCK_VERSION & 0xf) << 4) |
                           (InstanceEnvNumber & 0xf);

   InstanceSupervisorPoll = INSTANCE_SUPERVISOR_POLL;

   sys$setprv (1, &SysLckMask, 0, 0);

   for (cnt = 1; cnt <= INSTANCE_LOCK_COUNT; cnt++)
   {
      ilptr = &InstanceLockTable[cnt];

      /* build the (binary) resource name for each non-socket lock */
      zptr = (sptr = ilptr->Name) + sizeof(ilptr->Name)-1;
      for (cptr = HTTPD_NAME; *cptr && sptr < zptr; *sptr++ = *cptr++);
      if (sptr < zptr) *sptr++ = (char)InstanceLockNameMagic;
      if (cnt > INSTANCE_CLUSTER_LOCK_COUNT)
      {
         cptr = SysInfo.NodeName;
         while (*cptr && sptr < zptr) *sptr++ = *cptr++;
      }
      if (sptr < zptr) *sptr++ = (char)LockCode[cnt];
      *sptr = '\0';  /* not at all necessary */
      NameLength = sptr - ilptr->Name; 

      ilptr->NameLength = NameLength;
      ilptr->NameDsc.dsc$w_length = NameLength;
      ilptr->NameDsc.dsc$a_pointer = &ilptr->Name;
      if (WATCH_MODULE(WATCH_MOD_INSTANCE))
         WatchDataDump (ilptr->Name, ilptr->NameLength);

      /* this is the basic place-holding, resource instantiating lock */
      status = sys$enqw (EfnWait, LCK$K_NLMODE, &ilptr->Lksb,
                         LCK$M_EXPEDITE | LCK$M_SYSTEM,
                         &ilptr->NameDsc, 0, 0, 0, 0, 0, 2, 0);
      if (VMSok (status)) status = ilptr->Lksb.lksb$w_status;
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);
   }

   sys$setprv (0, &SysLckMask, 0, 0);
}

/*****************************************************************************/
/*
Queue a conversion to a blocking EX mode lock on the node supervisor resource. 
Whichever process holds this lock for the image lifetime and has the dubious
honour of performing tasks related to the per-node instances (e.g. creating
processes to provide the configured instances of the server).
*/

InstanceServerInit ()

{
   int  cnt, status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceServerInit()");

   sys$setprv (1, &SysLckMask, 0, 0);

   /* convert to a CR lock on the cluster membership resource */
   InstanceLockTable[INSTANCE_CLUSTER].InUse = true;
   status = sys$enqw (EfnWait, LCK$K_CRMODE,
                      &InstanceLockTable[INSTANCE_CLUSTER].Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status))
      status = InstanceLockTable[INSTANCE_CLUSTER].Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   /* convert to a CR lock on the node membership resource */
   InstanceLockTable[INSTANCE_NODE].InUse = true;
   status = sys$enqw (EfnWait, LCK$K_CRMODE,
                      &InstanceLockTable[INSTANCE_NODE].Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status))
      status = InstanceLockTable[INSTANCE_NODE].Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   /* be notified whenever a node-instance joins */
   InstanceNotifySet (INSTANCE_NODE_JOINING, &InstanceNodeJoiningAst);

   /* notify others that we're joining */
   status = InstanceNotifyWait (INSTANCE_NODE_JOINING, NULL,
                                INSTANCE_JOINING_WAIT_SECS);
   if (VMSnok (status)) ErrorExitVmsStatus (status, "InstanceReady()", FI_LI);

   /* queue up for our turn to be the instance node supervisor */
   InstanceNodeSupervisor = false;
   InstanceLockTable[INSTANCE_NODE_SUPERVISOR].InUse = true;
   /* note: this is NOT a sys$enqw(), it's asynchronous */
   status = sys$enq (EfnNoWait, LCK$K_EXMODE,
                     &InstanceLockTable[INSTANCE_NODE_SUPERVISOR].Lksb,
                     LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0,
                     &InstanceNodeSupervisorAst, 0, 0, 0, 2, 0);
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enq()", FI_LI);

   sys$setprv (0, &SysLckMask, 0, 0);
}

/*****************************************************************************/
/*
Check if there is already a configured single instance executing on this node. 
If this instance is configured to be a single instance then check how many
other instances (potentially) are executing.  If more than one exit with an
error message - you can't have one instance wandering around thinking it's the
only one.  If this instance is configured to be one of multiple and there is
already one instance thinking it's the only one around then exit for the
complementary reason.
*/

InstanceSingleInit ()

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceSingleInit()");

   sys$setprv (1, &SysLckMask, 0, 0);

   /* convert to an EX lock on the single instance resource */
   InstanceLockTable[INSTANCE_NODE_SINGLE].InUse = true;
   status = sys$enqw (EfnWait, LCK$K_EXMODE,
                      &InstanceLockTable[INSTANCE_NODE_SINGLE].Lksb,
                      LCK$M_NOQUEUE | LCK$M_CONVERT | LCK$M_SYSTEM,
                      0, 0, 0, 0, 0, 0, 2, 0);

   if (status == SS$_NOTQUEUED)
   {
      FaoToStdout (
"%HTTPD-E-INSTANCE, single instance already executing - exiting\n");
      /* cancel any startup messages provided for the monitor */
      HttpdGblSecPtr->StatusMessage[0] = '\0';
      if (HttpdProcess.Mode == JPI$K_INTERACTIVE)
         exit (SS$_ABORT | STS$M_INHIB_MSG);
      else
      {
         InstanceExit ();
         sys$delprc (0, 0);
      }
   }
   else
   if (VMSnok (status))
      ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   if (InstanceNodeConfig > 1)
   {
      /* multiple instances; successfully queued, convert back to NL mode */
      status = sys$enqw (EfnWait, LCK$K_NLMODE,
                         &InstanceLockTable[INSTANCE_NODE_SINGLE].Lksb,
                         LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);
      if (VMSok (status))
         status = InstanceLockTable[INSTANCE_NODE_SINGLE].Lksb.lksb$w_status;
      if (VMSnok (status))
         ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);
      InstanceLockTable[INSTANCE_NODE_SINGLE].InUse = false;
   }
   /* else single instance; else leave it at EX mode */

   sys$setprv (0, &SysLckMask, 0, 0);
}

/*****************************************************************************/
/*
Establish how many per-node instances are allowed on this system.  Must be
called after the server configuration is loaded.  If the number of instances
specified is negative this sets the number of instances to be the system CPU
count minus that number.  At least one instance will always be set.
*/

InstanceFinalInit ()

{
   int  status,
        NodeCount,
        StartupMax;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceFinalInit()");

   if (CliInstanceMax < 0)
      StartupMax = SysInfo.AvailCpuCnt + CliInstanceMax;
   else
   if (CliInstanceMax > 0)
      StartupMax = CliInstanceMax;
   else
   if (CliInstanceMax == INSTANCE_PER_CPU)
      StartupMax = SysInfo.AvailCpuCnt;
   else
      StartupMax = 0;

   if (StartupMax)
   {
      /* for example: /INSTANCE=-99 */
      if (StartupMax < 0) StartupMax = 0; 
      InstanceMutexLock (INSTANCE_MUTEX_HTTPD);
      HttpdGblSecPtr->InstanceStartupMax = StartupMax;
      InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
   }

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);
   StartupMax = HttpdGblSecPtr->InstanceStartupMax;
   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   if (StartupMax == INSTANCE_PER_CPU)
      FaoToStdout ("%HTTPD-W-INSTANCE, explicitly set to CPU \
(not using configuration)\n");
   else
   if (StartupMax)
      FaoToStdout ("%HTTPD-W-INSTANCE, explicitly set to !SL \
(not using configuration)\n", StartupMax);

   if (StartupMax == INSTANCE_PER_CPU)
      InstanceNodeConfig = SysInfo.AvailCpuCnt;
   else
   if (StartupMax < 0)
      InstanceNodeConfig = SysInfo.AvailCpuCnt + StartupMax;
   else
   if (StartupMax > 0)
      InstanceNodeConfig = StartupMax;
   else
   if (Config.cfServer.InstanceMax == INSTANCE_PER_CPU)
      InstanceNodeConfig = SysInfo.AvailCpuCnt;
   else
   if (Config.cfServer.InstanceMax < 0)
      InstanceNodeConfig = SysInfo.AvailCpuCnt + Config.cfServer.InstanceMax;
   else
   if (Config.cfServer.InstanceMax > 0)
      InstanceNodeConfig = Config.cfServer.InstanceMax;
   else
      InstanceNodeConfig = 1;

   /* minimum one, maximum eight (somewhat arbitrary but let's be sensible) */
   if (InstanceNodeConfig < 1)
      InstanceNodeConfig = 1;
   else
   if (InstanceNodeConfig > INSTANCE_MAX)
      InstanceNodeConfig = INSTANCE_MAX;

   /* lets check that's it OK to go ahead with this configuration */
   InstanceSingleInit ();

   NodeCount = InstanceLockList (INSTANCE_NODE, NULL, NULL);

   if (NodeCount > 1 && InstanceLockTable[INSTANCE_NODE_SINGLE].InUse)
   {
      FaoToStdout (
"%HTTPD-W-INSTANCE, multiple instances already executing - exiting\n");
      /* cancel any startup messages provided for the monitor */
      HttpdGblSecPtr->StatusMessage[0] = '\0';
      InstanceExit ();
      sys$delprc (0, 0);
   }

   if (!CliInstanceNoCrePrc &&
       NodeCount > InstanceNodeConfig)
   {
      FaoToStdout ("%HTTPD-W-INSTANCE, sufficient processes - exiting\n");
      /* cancel any startup messages provided for the monitor */
      HttpdGblSecPtr->StatusMessage[0] = '\0';
      InstanceExit ();
      sys$delprc (0, 0);
   }

   if (NodeCount == 1)
   {
      /* first-in sets the pace */
      InstanceMutexLock (INSTANCE_MUTEX_HTTPD);
      HttpdGblSecPtr->InstancePassive = CliInstancePassive ||
                                        Config.cfServer.InstancePassive;
      InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
   }

   if (InstanceNodeConfig == 1)
      FaoToStdout ("%HTTPD-I-INSTANCE, 1 process\n");
   else
      FaoToStdout ("%HTTPD-I-INSTANCE, !UL processes\n", InstanceNodeConfig);
}

/*****************************************************************************/
/*
Ready to process requests, just do a lock conversion to CR to indicate this.
Queue lock requests for multi-instance notifications.
*/

InstanceReady ()

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceReady()");

   sys$setprv (1, &SysLckMask, 0, 0);

   /* ready to accept */
   InstanceLockTable[INSTANCE_NODE_READY].InUse = true;
   status = sys$enqw (EfnWait, LCK$K_CRMODE,
                      &InstanceLockTable[INSTANCE_NODE_READY].Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status))
      status = InstanceLockTable[INSTANCE_NODE_READY].Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   /* receive control messages from other instances */
   InstanceNotifySet (INSTANCE_NODE_DO, &ControlHttpdAst);
   InstanceNotifySet (INSTANCE_CLUSTER_DO, &ControlHttpdAst);

   sys$setprv (0, &SysLckMask, 0, 0);
}

/*****************************************************************************/
/*
Ensure config file values used not any lingering /DO=INSTANCE=..
*/

int InstanceUseConfig ()

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceUseConfig()");

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);
   HttpdGblSecPtr->InstanceStartupMax = 0;
   HttpdGblSecPtr->InstancePassive = false;
   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
A node has just notified that it's in the process of joining the group of node
instance(s).  Determine the new number of instances on this node so that this
information may be used when locking, displaying administration reports, etc.
*/

InstanceNodeJoiningAst ()

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceNodeJoiningAst()");

   /* note that the instance composition may have changed */
   InstanceNodeJoiningCount++;

   /* kick off ticking to initiate any supervisory activities */
   if (!HttpdTicking) HttpdTick (0);
}

/*****************************************************************************/
/*
We've just become the node supervisor!!  Either this is the first instance on
the node or some other server process (or image) has exited and this process
was the next in the conversion queue.
*/

InstanceNodeSupervisorAst (int AstParam)

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceNodeSupervisorAst()");

   InstanceNodeSupervisor = true;

   FaoToStdout ("%HTTPD-I-INSTANCE, supervisor\n");

   /* ensure supervisor is accepting connections! */
   NetActive (true);

    /* be provided with cluster-wide instance status updates */
   AccountingPtr->InstanceNodeData[InstanceNumber].SupervisorUpdate = false;
   InstanceNotifySet (INSTANCE_CLUSTER_STATUS, &InstanceStatusUpdate);

   /* kick off ticking to initiate any supervisory activities */
   if (!HttpdTicking) HttpdTick (0);
}

/*****************************************************************************/
/*
When the server is processing requests this function is called by HttpdTick()
every second.  Only one process per node is allowed to perform the activities
in this function.  At least one node must perform these activities.  Returns
true to keep the server supervisor ticking, false to say no longer necessary.

If a control restart has been requested then only the supervisor node is
allowed to restart at any one time (of course all get a turn after one has
exited because of the queued supervisor lock being delivered).  This is what
enables the rolling restart.

Using InstanceLockList() get the current number of locks queued against the
node lock and from that knows how many instances (processes) are currently
executing.  If less than the required number of instances then create a new
server process.
*/

BOOL InstanceSupervisor ()

{
   static BOOL  NeedsInstance;
   static int  NodeJoiningCount,
               PollHttpdTickSecond,
               RefreshTicketKeySeconds,
               RestartHttpdTickSecond,
               RestartQuietCount,
               ShutdownCount = 30;
   static char  PrcNam [16];
   static $DESCRIPTOR (PrcNamDsc, PrcNam);
   static ulong  JpiPid;
   static VMS_ITEM_LIST3  JpiItems [] =
   {
      { sizeof(JpiPid), JPI$_PID, &JpiPid, 0 },
      { 0,0,0,0 }
   };

   int  idx, status,
        LockCount,
        InstanceNodeReady,
        StartupMax;
   ushort  Length;
   IO_SB  IOsb;
   INSTANCE_STATUS  *isptr;
   INSTANCE_NODE_DATA  *indptr;

   /*********/
   /* begin */
   /*********/

   if (!InstanceNodeSupervisor)
   {
      RefreshTicketKeySeconds = -1;
      return (false);
   }

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceSupervisor()");

   InstanceNodeCurrent = InstanceLockList (INSTANCE_NODE, NULL, NULL);
   InstanceClusterCurrent = InstanceLockList (INSTANCE_CLUSTER, NULL, NULL);

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   HttpdGblSecPtr->InstanceNodeCurrent = InstanceNodeCurrent;
   HttpdGblSecPtr->InstanceClusterCurrent = InstanceClusterCurrent;

   /* check if any other instance has barfed and needs its status updated */
   for (idx = 0; idx < INSTANCE_MAX; idx++)
   {
      if (!AccountingPtr->InstanceNodeData[idx].SupervisorUpdate) continue;
      indptr = &AccountingPtr->InstanceNodeData[idx];
      if (isptr = InstanceStatusFind (indptr->InstanceName))
         if (VMSok (InstanceNotifyWait (INSTANCE_CLUSTER_STATUS, isptr, 0)))
            indptr->SupervisorUpdate = false;
   }

   /* the node supervisor gets to provide it's PID to HTTPDMON, etc. */
   HttpdGblSecPtr->HttpdProcessId = HttpdProcess.Pid;

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   /*******************/
   /* restart request */
   /*******************/

   if (ControlRestartRequested)
   {
     if (InstanceNodeCurrent > 1)
      {
         /* there is more than one instance executing */
         InstanceNodeReady = InstanceLockList (INSTANCE_NODE_READY, NULL, NULL);
         if (InstanceNodeReady == InstanceNodeCurrent)
         {
            /* all of those are processing so restart immediately */
            if (ShutdownCount > 0) ShutdownCount = 0;
         }
         else
         {
            /* 
               There are less ready to process that are executing so wait
               a maximum of thirty seconds for more to start processing.
            */
            ShutdownCount = 30;
         }
      }
      else
      if (InstanceNodeConfig == 1)
      {
         /* if started with only one instance then restart immediately */
         if (ShutdownCount > 0) ShutdownCount = 0;
      }
      else
      if (ShutdownCount > 5)
      {
         /*
            A single instance (this one) is currently executing.  Wait five
            seconds to see if more become current and if none does restart.
         */
         ShutdownCount = 5;
      }

      if (ShutdownCount <= 0)
      {
         if (!NetCurrentProcessing)
         {
            /* no outstanding requests */
            FaoToStdout ("%HTTPD-I-CONTROL, server restart\n");
            exit (SS$_NORMAL);
         }
         if (ShutdownCount == 0)
         {
            /* stop receiving incoming connections */
            NetShutdownServerSocket ();
         }
         if (ShutdownCount < -300)
         {
            /* five minutes is a *long* wait for a request to finish! */
            FaoToStdout ("%HTTPD-W-CONTROL, server restart timeout\n");
            exit (SS$_NORMAL);
         }
      }
      ShutdownCount--;

      /* don't want to do any of the normal supervisor duties if restarting! */
      return (true);
   }

   if (ControlRestartQuiet)
   {
      if (NetCurrentProcessing)
         RestartQuietCount = 0;
      else
      if (RestartQuietCount++ > 1)
      {
         FaoToStdout ("%HTTPD-I-CONTROL, server restart when quiet\n");
         exit (SS$_NORMAL);
      }
      return (true);
   }

   /**********************/
   /* ticket key refresh */
   /**********************/

   if (InstanceNodeJoiningCount != NodeJoiningCount)
   {
      /* ensure all nodes are using the same ticket */
      NodeJoiningCount = InstanceNodeJoiningCount;
      /* give it 30 seconds for the cluster to settle */
      RefreshTicketKeySeconds = 30;
   }
   else
   if (RefreshTicketKeySeconds > 0)
      RefreshTicketKeySeconds--;
   else
   if (RefreshTicketKeySeconds == 0)
   {
      /* (re)try session ticket key refresh */
      if (VMSok (InstanceSessionTicketKey (SesolaTicketKey)))
         RefreshTicketKeySeconds = -1;
      else
         RefreshTicketKeySeconds = 30;
   }
   else
   if (SesolaTicketKeySuperDay != HttpdTime7[2])
   {
      /* refresh session ticket key once a day */
      SesolaSessionTicketNewKey();
      RefreshTicketKeySeconds = SesolaTicketKeySuperDay = 0;
   }

   /*****************/
   /* new instance? */
   /*****************/

   if (HttpdTickSecond < PollHttpdTickSecond)
   {
      /* only every so-many seconds do we do a supervisor poll */
      if (!NeedsInstance) return (false);
      /* return true to keep it ticking only when a new instance is needed */
      return (true);
   }
   PollHttpdTickSecond = HttpdTickSecond + InstanceSupervisorPoll;
   
   if (!CliInstanceNoCrePrc && InstanceNodeCurrent < InstanceNodeConfig)
   {
      if (!NeedsInstance || HttpdServerStartup)
      {
         /* keep it ticking until the next supervisor poll */
         NeedsInstance = true;
         return (true);
      }

      for (idx = InstanceNodeConfig > 1 ? 1 : 0; idx < INSTANCE_MAX; idx++)
      {
         status = FaoToBuffer (PrcNam, sizeof(PrcNam),
                               &Length, "!AZ!AZ!AZ:!UL",
                               InstanceGroupChars[InstanceEnvNumber],
                               InstanceWasdName ? "WASD" : "HTTP",
                               InstanceWasdName ? InstanceWasdChars[idx] :
                                                  InstanceHttpChars[idx],
                               ServerPort);
         if (VMSnok (status) || status == SS$_BUFFEROVF)
            ErrorExitVmsStatus (status, NULL, FI_LI);
         if (WATCH_MODULE(WATCH_MOD_INSTANCE))
            WatchDataFormatted ("!&Z\n", PrcNam);
         PrcNamDsc.dsc$w_length = Length;
         status = sys$getjpiw (EfnWait, 0, &PrcNamDsc, &JpiItems, &IOsb, 0, 0);
         if (VMSok (status)) status = IOsb.Status;
         if (status == SS$_NONEXPR)
         {
            /* found a process name that should exist and doesn't */
            FaoToStdout ("%HTTPD-I-INSTANCE, !20%D, creating \"!AZ\"\n",
                         0, PrcNam);

            if (HttpdNetworkMode)
               if (VMSnok (status = sys$setprv (1, &CrePrcMask, 0, 0)))
                  ErrorExitVmsStatus (status, "sys$setprv()", FI_LI);

            HttpdDetachServerProcess ();

            if (HttpdNetworkMode)
               if (VMSnok (status = sys$setprv (0, &CrePrcMask, 0, 0)))
                  ErrorExitVmsStatus (status, "sys$setprv()", FI_LI);

            return (true);
         }
         if (VMSnok (status))
            ErrorNoticed (NULL, status, NULL, FI_LI);
      }
      /* instances fully populated, at least according to process names */
   }

   NeedsInstance = false;

   return (false);
}

/*****************************************************************************/
/*
If multi-instance propagate this to other instances (cluster-wide as
applicable) internally using the DLM and (/DO=)TICKET=KEY command.  If the
lock value block (LVB) is 64 bytes this propagates the one key to all instances
and supports session ticket reuse across all instances.  If 16 byte LVB only
the TICKEY=KEY command is propagated which refreshes the ticket keys
independently on each instance but does not provide cross-instance session
reuse.  If not multi-instance then directly use SesolaSessionTicketUseKey() to
propagate it to the local TLS services.
*/

int InstanceSessionTicketKey (uchar *keyptr)

{
   int  status;
   char  Command64 [64];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceSessionTicketKey()");

   if (!(ProtocolHttpsAvailable && ProtocolHttpsConfigured))
      return (SS$_NORMAL);

   if (HttpdGblSecPtr->InstanceNodeCurrent > 1)
   {
      memset (Command64, 0, sizeof(Command64));
      strcpy (Command64, CONTROL_SESSION_TICKET_KEY);
      memcpy (Command64 + sizeof(CONTROL_SESSION_TICKET_KEY), keyptr, 48);
      status = InstanceNotifyWait (INSTANCE_CLUSTER_DO, Command64,
                                   INSTANCE_TICKET_WAIT_SECS);
      if (VMSnok (status))
      {
         ErrorNoticed (NULL, status, CONTROL_SESSION_TICKET_KEY, FI_LI);
         return (status);
      }
   }
   else
      SesolaSessionTicketUseKey (keyptr);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
When a pointer to INSTANCE_STATUS data is supplied (i.e. contained in a lock
value block) then insert this into the instance's instance data buffer located
in the accounting global common.  You will not see this with a single node or
when there are no clustered instances.

When a NULL pointer is supplied populate this instance's INSTANCE_STATUS data
in the global common.  On a single node or with no clustered instances this is
all that's required.  If clustered instances use the DLM to provide that data
to them.  If a DLM update initially fails due to contention then try again with
a call each second until it succeeds (or fails after multiple attempts).
*/

void InstanceStatusUpdate (struct lksb *lksbptr)

{
   static int  PrevMinute = -1,
               RetryCount = 0;
   static uchar  Status64 [LOCK_VALUE_BLOCK_64];

   BOOL  UpdateNow = false;
   int  idx, max, status, minute;
   char  *cptr, *sptr, *zptr;
   INSTANCE_NODE_DATA  *indptr;
   INSTANCE_STATUS  *isptr, *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceStatusUpdate() !8XL \'!AZ\' !UL !UL",
                 lksbptr, Status64, InstanceNodeCurrent, SysInfo.ClusterMember);

   if (HttpdGblSecPtr->InstanceClusterCurrent >
       HttpdGblSecPtr->InstanceNodeCurrent)
   {
      /* for clustered WASD instances need that 64 byte lock value block */
      if (SysInfo.LockValueBlockSize != LOCK_VALUE_BLOCK_64) return;
   }

   if (lksbptr && !(UpdateNow = (lksbptr == &InstanceStatusNow)))
   {
      /*************************/
      /* receive remote update */
      /*************************/

      isptr = lksbptr->lksb$b_valblk;

      if (WATCH_CAT && Watch.Category)
         WatchThis (WATCHALL, WATCH_INTERNAL,
                    "STATUS remote !AZ !AZ !UL !UL",
                    isptr->InstanceName, isptr->HttpdVersion,
                    isptr->MinuteCount, isptr->HourCount);

      /* when developing always test without this code */
#if !defined(WATCH_MOD) || !(WATCH_MOD)
      if (InstanceStatusTablePtr)
      {
         /* make sure it is from another (clustered) node */
         istptr = InstanceStatusTablePtr;
         for (cptr = istptr->InstanceName; *cptr && *cptr != ':'; cptr++);
         for (sptr = isptr->InstanceName; *sptr && sptr != ':'; sptr++);
         if (cptr - istptr->InstanceName == sptr - isptr->InstanceName)
            if (!strncmp (istptr->InstanceName,
                         isptr->InstanceName,
                         cptr - istptr->InstanceName)) return;
      }
#endif

      InstanceMutexLock (INSTANCE_MUTEX_HTTPD);
      /* find existing or create new entry in storage table */
      if (istptr = InstanceStatusFind (isptr->InstanceName))
      {
/* 19-NOV-2020  MGD  VSI C V7.4-002 on OpenVMS Alpha V8.4-2L1 */
/* memcpy() generates alignment faults! */
//         memcpy (istptr, isptr, sizeof(INSTANCE_STATUS));
         memmove (istptr, isptr, sizeof(INSTANCE_STATUS));
         istptr->UpdateTime64 = HttpdTime64;

         if (WATCH_MODULE(WATCH_MOD_INSTANCE))
            WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!AZ !AZ !UL !UL",
                       istptr->InstanceName, istptr->HttpdVersion,
                       istptr->MinuteCount, istptr->HourCount);
      }
      InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
      return;
   }

   /************************/
   /* provide local update */
   /************************/

   /* not InstanceStatusNow() so only every minute or as a retry */
   if (!UpdateNow && !RetryCount && PrevMinute == HttpdTime7[4]) return;

   if (RetryCount)
   {
      /* retry of a previously failed-to-update (via the DLM) */
      istptr = (INSTANCE_STATUS*)&Status64;
      if (WATCH_CAT && Watch.Category)
         WatchThis (WATCHALL, WATCH_INTERNAL,
                    "STATUS retry !AZ !AZ !UL !UL",
                    istptr->InstanceName, istptr->HttpdVersion,
                    istptr->MinuteCount, istptr->HourCount);
   }
   else
   {
      indptr = &AccountingPtr->InstanceNodeData[InstanceNumber];

      InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

      if (InstanceStatusTablePtr)
         istptr = InstanceStatusTablePtr;
      else
      {
         /**************/
         /* initialise */
         /**************/

         zptr = (sptr = indptr->InstanceName) + sizeof(indptr->InstanceName)-1;
         for (cptr = SysInfo.NodeName; *cptr && sptr < zptr; *sptr++ = *cptr++);
         for (cptr = "::"; *cptr && sptr < zptr; *sptr++ = *cptr++);
         for (cptr = HttpdProcess.PrcNam;
              *cptr && sptr < zptr;
              *sptr++ = *cptr++);
         *sptr = '\0';

         if (!(istptr = InstanceStatusFind (indptr->InstanceName)))
         {
            /* table exhausted (unlikely but possible) */
            InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
            return;
         }
         InstanceStatusTablePtr = istptr;

         istptr->StartupCount = indptr->StartupCount;
         istptr->StartTime64 = indptr->StartTime64;

         istptr->ExitStatus = indptr->ExitStatus & 0x0fffffff;
         istptr->ExitTime64 = indptr->ExitTime64;

         zptr = (sptr = istptr->HttpdVersion) + sizeof(istptr->HttpdVersion)-1;
         for (cptr = HttpdVersion; *cptr && sptr < zptr; *sptr++ = *cptr++);
         *sptr = '\0';
      }

      if (PrevMinute != HttpdTime7[4])
      {
         /**************/
         /* per-minute */
         /**************/

         if (minute = (PrevMinute = HttpdTime7[4]))
            minute--;
         else
            minute = 59;

         AccountingPtr->InstanceNodeData[InstanceNumber].
            RequestCount[minute] = InstanceStatusRequestCount;

         InstanceStatusRequestCount = 0;
      }
      else
      if (minute = HttpdTime7[4])
         minute--;
      else
         minute = 59;

      /*******************/
      /* populate latest */
      /*******************/

      /* the minute that has just passed */
      istptr->MinuteCount = indptr->RequestCount[minute];

      /* the hour just passed by accumulating the last sixty minutes */
      istptr->HourCount = 0;
      for (minute = 0; minute < 60; minute++)
         istptr->HourCount += indptr->RequestCount[minute];

      /* with a single node (no cluster) the table is self-updating */
      istptr->UpdateTime64 = HttpdTime64;

      InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

      if (WATCH_CAT && WATCH_CATEGORY(WATCH_INTERNAL))
         WatchThis (WATCHALL, WATCH_INTERNAL,
                    "STATUS local !AZ !AZ !UL !UL",
                    istptr->InstanceName, istptr->HttpdVersion,
                    istptr->MinuteCount, istptr->HourCount);
   }

   /* always check the DLM distribution when developing */
#if !defined(WATCH_MOD) || !(WATCH_MOD)
   if (HttpdGblSecPtr->InstanceClusterCurrent >
       HttpdGblSecPtr->InstanceNodeCurrent)
#endif
   {
      /***********/
      /* use DLM */
      /***********/

      if (VMSok (InstanceNotifyWait (INSTANCE_CLUSTER_STATUS, istptr, 0)))
         RetryCount = 0;
      else
      if (RetryCount)
         RetryCount--;
      else
      {
         RetryCount = INSTANCE_STATUS_UPDATE_RETRIES;
         memcpy (Status64, istptr, sizeof(Status64));
      }
   }
}

/*****************************************************************************/
/*
The instance has been /DO=STATUS=NOW.  Call the update function with its own
code entry point as a sentinal.  This then generates a local update.
*/

void InstanceStatusNow ()

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusNow()");

   InstanceStatusUpdate (&InstanceStatusNow);
}

/*****************************************************************************/
/*
Called by HttpdExit() this is a "last-gasp" update to advise of the instance's
exit status.  Do not employ a mutex on the accounting data.  Do not employ the
DLM to advise other instances.  Have the node supervisor do that on the exiting
instances's behalf, or in the case of a single instance node when it restarts. 
The report will (eventually) indicate instance data staleness if it doen't come
back up.  Whatever, avoid the use of additional complexities during error
exits.
*/

void InstanceStatusExit (int ExitStatus)

{
   int  status;
   INSTANCE_NODE_DATA  *indptr;
   INSTANCE_STATUS  *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusExit()");

   indptr = &AccountingPtr->InstanceNodeData[InstanceNumber];

   indptr->ExitStatus = ExitStatus & 0x0fffffff;
   sys$gettim (&indptr->ExitTime64);

   /* if (for whatever reason) there is no entry in the status data table */
   if (!(istptr = InstanceStatusTablePtr)) return;

   istptr->ExitStatus = indptr->ExitStatus;
   istptr->ExitTime64 = indptr->ExitTime64;

   /* don't complicate it further by using the DLM during an error exit */
   if (VMSnok (ExitStatus))
   {
      indptr->SupervisorUpdate = true;
      return;
   }

   /* use the DLM for last-gasp */
   status = InstanceNotifyWait (INSTANCE_CLUSTER_STATUS, istptr,
                                INSTANCE_STATUS_UPDATE_WAIT_SECS);
   if (VMSnok (status)) indptr->SupervisorUpdate = true;
}

/*****************************************************************************/
/*
Accounting mutex MUST be held before calling this function.
Find the entry for the node::process-name supplied.  When found return a
pointer to that entry.  If not found create a new entry and return a pointer to
that, or NULL to indicate the table is full.
*/

INSTANCE_STATUS* InstanceStatusFind (char *InstanceName)

{
   int  idx, idx0, max;
   INSTANCE_STATUS  *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceStatusFind() \"!AZ\"", InstanceName);

   max = AccountingPtr->InstanceStatusTableCount;
   for (idx = idx0 = 0; idx < max; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];
      if (!istptr->UpdateTime64)
      {
         /* this is a purged entry so note it */
         idx0 = idx;
         continue;
      }
      if (!MATCH8 (istptr->InstanceName, InstanceName)) continue;
      if (!strcmp (istptr->InstanceName+8, InstanceName+8)) break;
   }

   if (idx >= max)
   {
      /* instance not found */
      if (!idx0 && max >= INSTANCE_STATUS_TABLE_MAX)
      {
         /* hmmm, all consumed! */
         ErrorNoticed (NULL, SS$_BUFFEROVF, NULL, FI_LI);
         return (NULL);
      }
      /* redeploy a purged entry or add a new one */
      if (idx0)
         idx = idx0;
      else
         idx = AccountingPtr->InstanceStatusTableCount++;
      istptr = &AccountingPtr->InstanceStatusTable[idx];
      strcpy (istptr->InstanceName, InstanceName);
   }

   return (istptr);
}

/*****************************************************************************/
/*
Remove stale entries from instance status table.
*/

void InstanceStatusPurge ()

{
   static ulong  LibDeltaMins = LIB$K_DELTA_MINUTES;

   int  idx, max, status;
   ulong  MinsAgo;
   INSTANCE_STATUS  *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusPurge()");

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   max = AccountingPtr->InstanceStatusTableCount;
   for (idx = 0; idx < max; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];
      MinsAgo = (HttpdTime64 - istptr->UpdateTime64) / TIME64_ONE_MIN;
      if (MinsAgo > INSTANCE_STATUS_STALE_MINS)
         memset (istptr, 0, sizeof(INSTANCE_STATUS));
   }

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
}

/*****************************************************************************/
/*
Zero all instance status data in the global common forcing it to be repopulated
by the various instances' status processing.  May take a minute or two before
it starts to look "normal" again.
*/

void InstanceStatusReset ()

{
   INSTANCE_NODE_DATA  *indptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusReset()");

   indptr = &AccountingPtr->InstanceNodeData[InstanceNumber];

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   if (AccountingPtr->InstanceStatusTableCount)
   {
      AccountingPtr->InstanceStatusTableCount = 0;
      memset (&AccountingPtr->InstanceStatusTable, 0,
              sizeof(AccountingPtr->InstanceStatusTable));
      memset (&indptr->RequestCount, 0, sizeof(indptr->RequestCount));
   }

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   InstanceStatusTablePtr = NULL;
}

/*****************************************************************************/
/*
Provide report element for inclusion in the Server Admin page.
*/

void InstanceStatusAdminReport (REQUEST_STRUCT *rqptr)

{
   static ulong  LibDeltaMins = LIB$K_DELTA_MINUTES;

   int  idx, len, maxtab, maxlen, status, total;
   ulong  MinsAgo;
   char  *stptr;
   char  ExitAgoBuf [16],
         ExitStatusBuf [16],
         StartAgoBuf [16],
         TimeExit [32],
         TimeStartup [32],
         UpdateAgoBuf [32];
   INSTANCE_STATUS  *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusAdminReport()");

   /* need that 64 byte lock value block */
   if (SysInfo.LockValueBlockSize != LOCK_VALUE_BLOCK_64)
   {
      status = FaolToNet (rqptr,
"<!!-- Instance status requires 64 byte lock value block!! -->\n", NULL);
      return;
   }

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   maxtab = AccountingPtr->InstanceStatusTableCount;
   for (idx = total = 0; idx < maxtab; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];
      if (!istptr->UpdateTime64) continue;
      total++;
   }

   if (!strsame (rqptr->rqHeader.PathInfoPtr, HTTPD_ADMIN_STATUS, -1))
      if (total <= 1)
      {
         /* nothing to see here! */
         InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);
         return;
      }

   status = FaolToNet (rqptr,
"<style type=\"text/css\">\n\
tr.strikerow td { position:relative; }\n\
tr.strikerow td:before { content:\" \";position:absolute;top:50%;\
left:0;border-bottom:1px dashed black;width:100%; }\n\
</style>\n\
<tr><td class=\"ctttl\" colspan=\"2\">\n\
<table class=\"admin1\" style=\"font-size:75%\">\n\
<tr>\
<th style=\"width:9em;min-width:9em;\">Instance</th>\
<th>Ago</th>\
<th style=\"width:7em;min-width:7em;\">Started</th>\
<th>Ago</th><th>Count</th>\
<th style=\"width:7em;min-width:7em;\">Exited</th>\
<th>Ago</th>\
<th style=\"width:4em;min-width:4em;\">Status</th>\
<th>/Min</th><th>/Hour</th>\
</tr>\n", NULL);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);

   maxtab = AccountingPtr->InstanceStatusTableCount;
   for (idx = total = 0; idx < maxtab; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];

      /* if a purged entry */
      if (!istptr->UpdateTime64) continue;

      ThisLongAgo (&istptr->ExitTime64, ExitAgoBuf);
      ThisLongAgo (&istptr->StartTime64, StartAgoBuf);
      ThisLongAgo (&istptr->UpdateTime64, UpdateAgoBuf);

      TimeSansYear (&istptr->StartTime64, TimeStartup);
      TimeSansYear (&istptr->ExitTime64, TimeExit);

      MinsAgo = (HttpdTime64 - istptr->UpdateTime64) / TIME64_ONE_MIN;
      if (MinsAgo > INSTANCE_STATUS_STALE_MINS)
         stptr = " class=\"strikerow\"";
      else
         stptr = "";

      if (istptr->ExitStatus)
         FaoToBuffer (ExitStatusBuf, sizeof(ExitStatusBuf), NULL, "%X!8XL",
                      istptr->ExitStatus);
      else
         ExitStatusBuf[0] = '\0';

      FaoToNet (rqptr, "<tr!AZ>\
<td>!AZ</td><td class=\"targht\">!AZ</td>\
<td>!AZ</td><td class=\"targht\">!AZ</td><td class=\"targht\">!UL</td>\
<td>!AZ</td><td class=\"targht\">!AZ</td><td>!AZ</td>\
<td class=\"targht\">!UL</td><td class=\"targht\">!UL</td></tr>\n",
                   stptr,
                   istptr->InstanceName, UpdateAgoBuf,
                   TimeStartup, StartAgoBuf, istptr->StartupCount,
                   TimeExit, ExitAgoBuf, ExitStatusBuf,
                   istptr->MinuteCount,
                   istptr->HourCount);
   }

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   status = FaolToNet (rqptr,
"</table>\n\
</td></tr>\n", NULL);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);
}

/*****************************************************************************/
/*
Report the instance table to <stdout>.
Adjusts report depending on line width 80 or 132.
*/

void InstanceStatusCliReport (REQUEST_STRUCT *rqptr)

{
   static ulong  LibDeltaMins = LIB$K_DELTA_MINUTES;
   static $DESCRIPTOR (ttDsc, "TT:");
   static ulong  LineLength;
   static struct {
      short BufferLength;
      short ItemCode;
      void  *BufferPtr;
      void  *LengthPtr;
   }
   DevBufSizItemList [] = 
   {
      { sizeof(LineLength), DVI$_DEVBUFSIZ, &LineLength, 0 },
      { 0, 0, 0, 0 }
   };

   BOOL  stale;
   int  idx, len, maxtab, maxlen, status, total;
   int64  NowTime64;
   ulong  MinsAgo;
   char  *cptr;
   char  buf [512],
         ExitAgoBuf [16],
         ExitStatusBuf [16],
         StartAgoBuf [16],
         TimeExit [32],
         TimeStartup [32],
         TmpAgoBuf [16],
         UpdateAgoBuf [16];
   IO_SB  IOsb;
   INSTANCE_STATUS  *istptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceStatusCliReport()");

   if (rqptr)
   {
      if (SysInfo.LockValueBlockSize != LOCK_VALUE_BLOCK_64)
      {
         ErrorVmsStatus (rqptr, SS$_UNSUPPORTED, FI_LI);
         AdminEnd (rqptr);
      }
      ResponseHeader (rqptr, 200, "text/plain", -1, NULL, NULL);
   }
   else
   {
      if (SysInfo.LockValueBlockSize != LOCK_VALUE_BLOCK_64)
         exit (SS$_NOSUCHREPORT);
      status = HttpdGblSecMap ();
      if (VMSnok (status)) exit (status);
   }

   status = sys$getdviw (EfnWait, 0, &ttDsc,
                         &DevBufSizItemList, &IOsb, 0, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSnok (status)) exit (status);

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   maxtab = AccountingPtr->InstanceStatusTableCount;
   for (idx = maxlen = 0; idx < maxtab; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];
      if ((len = strlen(istptr->InstanceName)) > maxlen) maxlen = len;
   }
   if (maxlen < 8) maxlen = 8;

   if (LineLength > 80)
      FaoToBuffer (buf, sizeof(buf), NULL, "\
  !#AZ !4AZ !15AZ !4AZ !5AZ !15AZ !4AZ !10AZ !7AZ !4AZ !6AZ\n\
  !#*~ !4*~ !15*~ !4*~ !5*~ !15*~ !4*~ !10*~ !7*~ !4*~ !6*~\n",
                   maxlen, "Instance",
                   " Ago", "Up", " Ago", "Count", "Exit", " Ago",
                   "Status", "Version", "/Min", " /Hour",
                   maxlen);
   else
      FaoToBuffer (buf, sizeof(buf), NULL, "\
  !#AZ !4AZ !4AZ !5AZ !4AZ !10AZ !7AZ !4AZ !6AZ\n\
  !#*~ !4*~ !4*~ !5*~ !4*~ !10*~ !7*~ !4*~ !6*~\n",
                   maxlen, "Instance",
                   " Ago", "  Up", "Count", "Exit",
                   "Status", "Version", "/Min", " /Hour",
                   maxlen);
   if (rqptr)
      FaoToNet (rqptr, "!AZ", buf);
   else
      FaoToStdout ("!AZ", buf);

   sys$gettim (&NowTime64);

   for (idx = total = 0; idx < maxtab; idx++)
   {
      istptr = &AccountingPtr->InstanceStatusTable[idx];

      /* if a purged entry */
      if (!istptr->UpdateTime64) continue;

      /* right justify each of these (really should incorporate in FAO.C) */
      ThisLongAgo (&istptr->ExitTime64, TmpAgoBuf);
      sprintf (ExitAgoBuf, "%4s", TmpAgoBuf);
      ThisLongAgo (&istptr->StartTime64, TmpAgoBuf);
      sprintf (StartAgoBuf, "%4s", TmpAgoBuf);
      ThisLongAgo (&istptr->UpdateTime64, TmpAgoBuf);
      sprintf (UpdateAgoBuf, "%4s", TmpAgoBuf);

      TimeSansYear (&istptr->StartTime64, TimeStartup);
      if (TimeStartup[0] == ' ') TimeStartup[0] = '0';
      TimeSansYear (&istptr->ExitTime64, TimeExit);
      if (TimeExit[0] == ' ') TimeExit[0] = '0';

      if (istptr->ExitStatus)
         FaoToBuffer (ExitStatusBuf, sizeof(ExitStatusBuf), NULL, "%X!8XL",
                      istptr->ExitStatus);
      else
         ExitStatusBuf[0] = '\0';

      MinsAgo = (NowTime64 - istptr->UpdateTime64) / TIME64_ONE_MIN;
      if (MinsAgo > INSTANCE_STATUS_STALE_MINS)
         stale = true;
      else
      {
         stale = false;
         total++;
      }

      if (LineLength > 80)
         FaoToBuffer (buf, sizeof(buf), NULL,
"!AZ!#AZ !4AZ !15AZ !4AZ !5UL !15AZ !4AZ !10AZ !7AZ !4UL !6UL!AZ\n",
                      stale ? " -" : "  ",
                      maxlen, istptr->InstanceName, UpdateAgoBuf,
                      TimeStartup, StartAgoBuf, istptr->StartupCount,
                      TimeExit, ExitAgoBuf, ExitStatusBuf,
                      istptr->HttpdVersion,
                      istptr->MinuteCount,
                      istptr->HourCount,
                      stale ? "-" : "");
      else
         FaoToBuffer (buf, sizeof(buf), NULL,
"!AZ!#AZ !4AZ !4AZ !5UL !4AZ !10AZ !7AZ !4UL !6UL!AZ\n",
                      stale ? " -" : "  ",
                      maxlen, istptr->InstanceName, UpdateAgoBuf,
                      StartAgoBuf, istptr->StartupCount,
                      ExitAgoBuf, ExitStatusBuf,
                      istptr->HttpdVersion,
                      istptr->MinuteCount,
                      istptr->HourCount,
                      stale ? "-" : "");
      if (stale)
         for (cptr = buf+2; *cptr; cptr++)
            if (*cptr == ' ') *cptr = '-';

      if (rqptr)
         FaoToNet (rqptr, "!AZ", buf);
      else
         FaoToStdout ("!AZ", buf);
   }

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   FaoToBuffer (buf, sizeof(buf), NULL, "  as at !20%D\n", 0);

   if (rqptr)
   {
      FaoToNet (rqptr, "!AZ", buf);
      AdminEnd (rqptr);
   }
   else
      FaoToStdout ("!AZ", buf);
}

/*****************************************************************************/
/*
Proactively dequeue all locks.  I would have thought image exit would have done
this "quickly enough", but it appears as if there are still sufficient locks
when a move of supervisor role occurs to defeat the logic in the restart and
create process code!  Perhaps this only occurs with the '$DELPRC(0,0)' and it
takes a while for the DLM to catch up?
*/

void InstanceExit ()

{
   int  idx, status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceExit()");

   /* unlock any instance-locked mutexes */
   for (idx = 1; idx <= INSTANCE_MUTEX_COUNT; idx++)
   {
      if (!InstanceMutexHeld[idx]) continue;
      _BBCCI (0, &HttpdGblSecPtr->Mutex[idx]);
   }

/***   
   sys$setprv (1, &SysLckMask, 0, 0);
   for (idx = 1; idx <= INSTANCE_LOCK_COUNT; idx++)
      sys$deq (InstanceLockTable[idx].Lksb.lksb$l_lkid, 0, 0, 0);
   sys$setprv (0, &SysLckMask, 0, 0);
***/
}

/*****************************************************************************/
/*
Set the server process name.  If multiple instances have been configured for
step through the process names available breaking at the first successful. 
This becomes the "instance" name of this particular process on the node.
*/

InstanceProcessName ()

{
   static $DESCRIPTOR (LogNameDsc, WASD_PROCESS_NAME);
   static $DESCRIPTOR (LnmFileDevDsc, "LNM$FILE_DEV");
   static char  NameBuffer [16];
   static VMS_ITEM_LIST3  NameLnmItem [] =
   {
      { sizeof(NameBuffer), LNM$_STRING, NameBuffer, 0 },
      { 0,0,0,0 }
   };

   int  idx, status;
   ushort  Length;
   $DESCRIPTOR (PrcNamDsc, HttpdProcess.PrcNam);

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceProcessName() !UL", InstanceNodeConfig);

   status = sys$trnlnm (0, &LnmFileDevDsc, &LogNameDsc, 0, &NameLnmItem);
   if (VMSok(status))
      if (NameBuffer[0] == '0' || TOUP(NameBuffer[0]) == 'F')
         InstanceWasdName = false;

   for (idx = InstanceNodeConfig > 1 ? 1 : 0; idx < INSTANCE_MAX; idx++)
   {
      status = FaoToBuffer (HttpdProcess.PrcNam,
                            sizeof(HttpdProcess.PrcNam),
                            &Length, "!AZ!AZ!AZ:!UL",
                            InstanceGroupChars[InstanceEnvNumber],
                            InstanceWasdName ? "WASD" : "HTTP",
                            InstanceWasdName ? InstanceWasdChars[idx] :
                                               InstanceHttpChars[idx],
                            ServerPort);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
         ErrorExitVmsStatus (status, NULL, FI_LI);
      if (WATCH_MODULE(WATCH_MOD_INSTANCE))
         WatchDataFormatted ("!&Z\n", HttpdProcess.PrcNam);
      PrcNamDsc.dsc$w_length = HttpdProcess.PrcNamLength = Length;
      if (VMSok (status = sys$setprn (&PrcNamDsc))) break;
   }
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$setprn()", FI_LI);
   FaoToStdout ("%HTTPD-I-INSTANCE, process name !AZ\n", HttpdProcess.PrcNam);
   InstanceNumber = idx;
}

/*****************************************************************************/
/*
The "administration socket" is used to to connect exclusively to a single
instance (normally connects are distributed between instances).  This function
distributes the IP port (in decimal) across the cluster via
InstanceSocketForAdmin().  Creates a lock resource with a name based on the
process name and stores in it's lock value block the number (in ASCII as
always) of it's "internal", per-instance (process) admininstration port. 
*/

int InstanceSocketAdmin (short IpPort)

{
   int  enqfl, status,
        NameLength;
   char  *cptr, *sptr, *zptr;
   IO_SB  IOsb;
   INSTANCE_LOCK  *ilptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceSocketAdmin() !UL", IpPort);

   ilptr = &InstanceLockAdmin;

   /* build the (binary) resource name the admin lock */
   zptr = (sptr = ilptr->Name) + sizeof(ilptr->Name)-1;
   for (cptr = HTTPD_NAME; *cptr && sptr < zptr; *sptr++ = *cptr++);
   if (sptr < zptr) *sptr++ = (char)InstanceLockNameMagic;
   for (cptr = SysInfo.NodeName; *cptr && sptr < zptr; *sptr++ = *cptr++);
   if (sptr < zptr) *sptr++ = ':';
   if (sptr < zptr) *sptr++ = ':';
   for (cptr = HttpdProcess.PrcNam; *cptr && sptr < zptr; *sptr++ = *cptr++);
   NameLength = sptr - ilptr->Name; 

   ilptr->NameDsc.dsc$w_length = NameLength;
   ilptr->NameDsc.dsc$a_pointer = ilptr->Name;

   FaoToBuffer (&ilptr->Lksb.lksb$b_valblk, SysInfo.LockValueBlockSize, NULL,
                "!UL", (ushort)IpPort);

   /* queue at EX then convert to NL causing lock value block to be written */
   sys$setprv (1, &SysLckMask, 0, 0);
   if (ilptr->InUse) ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
   ilptr->InUse = true;
   status = sys$enqw (EfnWait, LCK$K_EXMODE, &ilptr->Lksb,
                      LCK$M_NOQUEUE | LCK$M_SYSTEM,
                      &ilptr->NameDsc, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = ilptr->Lksb.lksb$w_status;
   /* this just shouldn't happen */
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   enqfl = LCK$M_VALBLK | LCK$M_CONVERT | LCK$M_SYSTEM;
   if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
      enqfl |= LCK$M_XVALBLK;

   status = sys$enqw (EfnWait, LCK$K_NLMODE, &ilptr->Lksb, enqfl,
                      0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = ilptr->Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);
   sys$setprv (0, &SysLckMask, 0, 0);

   if (status == SS$_XVALNOTVALID)
   {
      /* hmmm, change in cluster composition? whatever! go back to 16 bytes */
      SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
      ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
   }

   return (status);
}

/*****************************************************************************/
/*
Given a process name in the format 'node::WASD:port' (e.g. "DELTA::WASD:80")
generate the same lock name as InstanceSocketAdmin() and queue a NL lock, then
get the lock value block using sys$getlki().  Retrieve the decimal port into
the supplied pointed-to storage.  Return a VMS status code.
*/

int InstanceSocketForAdmin
(
char *ProcessName,
short *IpPortPtr
)
{
   static ulong  Lki_XVALNOTVALID;
   static char  LockName [31+1];
   static struct lksb  LockSb;
   static VMS_ITEM_LIST3  LkiItems [] =
   {
      /* careful, values are dynamically assigned in code below! */
      { 0, 0, 0, 0 },  /* reserved for LKI$_[X]VALBLK item */
      { 0, 0, 0, 0 },  /* reserved for LKI$_XVALNOTVALID item */
      {0,0,0,0}
   };
   static $DESCRIPTOR (LockNameDsc, LockName);

   int  retval, status;
   char  *cptr, *sptr, *zptr;
   IO_SB  IOsb;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceSocketForAdmin() !&Z", ProcessName);

   if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
   {
      LkiItems[0].buf_len = LOCK_VALUE_BLOCK_64;
      LkiItems[0].buf_addr = &LockSb.lksb$b_valblk;
      LkiItems[0].item = LKI$_XVALBLK;
      LkiItems[1].buf_len = sizeof(Lki_XVALNOTVALID);
      LkiItems[1].buf_addr = &Lki_XVALNOTVALID;
      LkiItems[1].item = LKI$_XVALNOTVALID;
   }
   else
   {
      LkiItems[0].buf_len = LOCK_VALUE_BLOCK_16;
      LkiItems[0].buf_addr = &LockSb.lksb$b_valblk;
      LkiItems[0].item = LKI$_VALBLK;
      /* in this case this terminates the item list */
      LkiItems[1].buf_len = 0;
      LkiItems[1].buf_addr = 0;
      LkiItems[1].item = 0;
      Lki_XVALNOTVALID = 0;
   }

   /* build the (binary) resource name the admin lock */
   zptr = (sptr = LockName) + sizeof(LockName)-1;
   for (cptr = HTTPD_NAME; *cptr && sptr < zptr; *sptr++ = *cptr++);
   if (sptr < zptr) *sptr++ = (char)InstanceLockNameMagic;
   for (cptr = ProcessName; *cptr && sptr < zptr; *sptr++ = *cptr++);
   LockNameDsc.dsc$w_length = sptr - LockName; 

   sys$setprv (1, &SysLckMask, 0, 0);
   status = sys$enqw (EfnWait, LCK$K_NLMODE, &LockSb, LCK$M_SYSTEM,
                      &LockNameDsc, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = LockSb.lksb$w_status;
   if (VMSok (status))
   {
      status = sys$getlkiw (EfnWait, &LockSb.lksb$l_lkid, &LkiItems,
                            &IOsb, 0, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
   }
   sys$deq (LockSb.lksb$l_lkid, 0, 0, 0);
   sys$setprv (0, &SysLckMask, 0, 0);
   if (VMSnok (status)) return (status);

   if (Lki_XVALNOTVALID)
   {
      /* hmmm, change in cluster composition? whatever! go back to 16 bytes */
      SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
      ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
   }

   if (IpPortPtr) *IpPortPtr = atoi(&LockSb.lksb$b_valblk);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
This function controls the creation of bound sockets, and distribution of the
BG: device names, amongst per-node instances of the server.

This function is called one or two times to do it's job, which is to create a 
per-node lock resource name containing a BINARY representation of a service IP
address and port.  Binary is necessary to be able to contain the 16 byte
address + 2 byte port of IPv6.  The resource name becomes the 5 character
resource name prefix (see above), the (up to) 6 character node name the finally
the 18 byte socket address, a total of 29 characters (out of a possible 31).

The first call checks if this instance already has a channel to the requested
socket (address/port combination).  If it does (stored in a local table) it
returns the BG device name with a leading underscore.  If not it checks The
first (and possibly second) call has 'BgDevName' as NULL and creates the
resource name, enqueues  a CR lock then converts it to NL which causes the lock
value block to be returned.  This can be checked for a string with the BG:
device name (e.g. "_BG206:") of any previously created listening socket for the
address and port.  If such a string is found then a pointer to it is returned
and it can be used to assign another channel to it.

If the lock value block is empty a NULL is returned, the calling routine then
creates and binds a socket, then calls this function again.  This time with the
'BgDeviceName' is non-NULL and points to a string containing the device name
(e.g. "_BG206:").  This is copied to the lock value block, an EX mode lock
enqueued then converted back to NL to write the lock value, making it available
for use by other processes.

This function assumes some other overall lock prevents other processes from
using this function while it is called two times (i.e. the service creation
process is locked).
*/

char* InstanceSocket
(
IPADDRESS *ipaptr,
short IpPort,
char *BgDevName
)
{
   static char  DeviceName [LOCK_VALUE_BLOCK_64];

   int  cnt, enqfl, status,
        SocketNameLength;
   char  *cptr, *sptr, *zptr;
   char  SocketName [31+1];
   INSTANCE_SOCKET_LOCK  *islptr;
   $DESCRIPTOR (NameDsc, "");

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceSocket()");

   if (BgDevName)
   {
      /*************************************/
      /* socket created, store device name */
      /*************************************/

      islptr = &InstanceSocketTable[InstanceSocketCount];

      /* store the BG device name in the lock value block */
      sptr = islptr->Lksb.lksb$b_valblk;
      zptr = sptr + sizeof(islptr->Lksb.lksb$b_valblk)-1;
      for (cptr = BgDevName; *cptr && sptr < zptr; *sptr++ = *cptr++);
      *sptr = '\0';

      enqfl = LCK$M_VALBLK | LCK$M_CONVERT | LCK$M_SYSTEM;
      if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
         enqfl |= LCK$M_XVALBLK;

      sys$setprv (1, &SysLckMask, 0, 0);

      /* convert NL to EX then back to NL, lock value block is written */
      status = sys$enqw (EfnWait, LCK$K_EXMODE, &islptr->Lksb,
                         LCK$M_CONVERT | LCK$M_SYSTEM,
                         0, 0, 0, 0, 0, 0, 2, 0);
      if (VMSok (status)) status = islptr->Lksb.lksb$w_status;
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

      status = sys$enqw (EfnWait, LCK$K_NLMODE, &islptr->Lksb, enqfl,
                         0, 0, 0, 0, 0, 0, 2, 0);
      if (VMSok (status)) status = islptr->Lksb.lksb$w_status;
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

      sys$setprv (0, &SysLckMask, 0, 0);

      if (status == SS$_XVALNOTVALID)
      {
         /* hmmm, change in cluster composition? whatever! back to 16 bytes */
         SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
         ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
      }

      InstanceSocketCount++;
      return (NULL);
   }

   /***************************************/
   /* build the socket lock resource name */
   /***************************************/

   zptr = (sptr = SocketName) + sizeof(SocketName)-1;
   for (cptr = HTTPD_NAME; *cptr && sptr < zptr; *sptr++ = *cptr++);
   if (sptr < zptr) *sptr++ = (char)InstanceLockNameMagic;
   cptr = SysInfo.NodeName;
   while (*cptr && sptr < zptr) *sptr++ = *cptr++;
   if (sptr < zptr)
   {
      if (IPADDRESS_IS_V4(ipaptr))
         *sptr++ = (char)INSTANCE_NODE_SOCKIP4;
      else
         *sptr++ = (char)INSTANCE_NODE_SOCKIP6;
   }
   cnt = IPADDRESS_SIZE(ipaptr);
   cptr = IPADDRESS_ADR46(ipaptr);
   while (cnt-- && sptr < zptr) *sptr++ = *cptr++;
   cnt = sizeof(short);
   cptr = (char*)&IpPort;
   while (cnt-- && sptr < zptr) *sptr++ = *cptr++;
   if (sptr >= zptr) ErrorExitVmsStatus (0, ErrorSanityCheck, FI_LI);
   *sptr = '\0';  /* not really necessary */
   SocketNameLength = sptr - SocketName;

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchDataDump (SocketName, SocketNameLength);

   /**************************************************************/
   /* check if this instance already has a channel to the socket */
   /**************************************************************/

   for (cnt = 0; cnt < InstanceSocketCount; cnt++)
   {
      islptr = &InstanceSocketTable[cnt];
      if (MATCH0 (islptr->Name, SocketName, SocketNameLength)) break;
   }
   if (cnt >= InstanceSocketCount) islptr = NULL;

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!&?YES\rNO\r", islptr);

   if (islptr)
   {
      /* yes it has! */
      zptr = (sptr = DeviceName) + sizeof(DeviceName)-1;
      cptr = &islptr->Lksb.lksb$b_valblk;
      if (*cptr == '_') cptr++;
      *sptr++ = '_';
      while (*cptr && sptr < zptr) *sptr++ = *cptr++;
      *sptr = '\0';
      /* return with a leading underscore */
      return (DeviceName);
   }

   /**************************************************/
   /* check if another instance has bound the socket */
   /**************************************************/

   islptr = &InstanceSocketTable[InstanceSocketCount];

   memcpy (islptr->Name, SocketName, SocketNameLength+1);
   NameDsc.dsc$w_length = SocketNameLength;
   NameDsc.dsc$a_pointer = islptr->Name;

   sys$setprv (1, &SysLckMask, 0, 0);

   /* this is the basic place-holding, resource instantiating lock */
   status = sys$enqw (EfnWait, LCK$K_NLMODE, &islptr->Lksb,
                      LCK$M_EXPEDITE | LCK$M_SYSTEM,
                      &NameDsc, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = islptr->Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   enqfl = LCK$M_VALBLK | LCK$M_CONVERT | LCK$M_SYSTEM;
   if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
      enqfl |= LCK$M_XVALBLK;

   /* convert NL to CR then back to NL, the lock value block is returned */
   status = sys$enqw (EfnWait, LCK$K_CRMODE, &islptr->Lksb, enqfl,
                      0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = islptr->Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   if (status == SS$_XVALNOTVALID)
   {
      /* hmmm, change in cluster composition? whatever! go back to 16 bytes */
      SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
      ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
   }

   /* back to NL mode */
   status = sys$enqw (EfnWait, LCK$K_NLMODE, &islptr->Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM,
                      0, 0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status)) status = islptr->Lksb.lksb$w_status;
   if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);

   sys$setprv (0, &SysLckMask, 0, 0);

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!&Z",
                 islptr->Lksb.lksb$b_valblk);

   if (islptr->Lksb.lksb$b_valblk[0])
   {
      /* yes it has! lock value block contains a BG: device name string */
      InstanceSocketCount++;
      /* return without a leading underscore */
      return (islptr->Lksb.lksb$b_valblk);
   }

   /* no BG: device name string, socket will need to be created */
   return (NULL);
}

/*****************************************************************************/
/*
Lock the server notification functionality against any concurrent usage. 
Write the PID of the initiating process into the value block of the CONTROL
lock.  This can be used for log and audit purposes on other nodes, etc.
*/

int InstanceLockNotify ()

{
   static int  LockIndex = INSTANCE_CLUSTER_NOTIFY;
   static ulong  JpiPid = 0;
   static char  PidBuf [8+1];
   static VMS_ITEM_LIST3 JpiItems [] =
   {
      { 0,0,0,0 }
   };

   int  enqfl, status;
   IO_SB  IOsb;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceLockNotify() !UL !&Z",
                 LockIndex, InstanceLockTable[LockIndex].Name);

   if (InstanceLockTable[LockIndex].InUse) return (SS$_NOTQUEUED);

   if (!JpiPid)
   {
      status = sys$getjpiw (EfnWait, &JpiPid, 0, &JpiItems, &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSnok (status)) return (status);
      status = FaoToBuffer (PidBuf, sizeof(PidBuf), NULL, "!8XL", JpiPid);
      if (VMSnok (status)) return (status);
   }

   /* store the PID in the lock status block */
   memcpy (&InstanceLockTable[LockIndex].Lksb.lksb$b_valblk,
           PidBuf, sizeof(PidBuf));

   enqfl = LCK$M_VALBLK | LCK$M_CONVERT | LCK$M_SYSTEM;
   if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
      enqfl |= LCK$M_XVALBLK;

   sys$setprv (1, &SysLckMask, 0, 0);

   /* convert to EX then to PW causing lock value block to be written */
   status = sys$enqw (EfnWait, LCK$K_EXMODE,
                      &InstanceLockTable[LockIndex].Lksb,
                      LCK$M_NOQUEUE | LCK$M_CONVERT | LCK$M_SYSTEM,
                      0, 0, 0, 0, 0, 2, 0);
   if (VMSok (status))
      status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
   if (VMSok (status))
   {
      status = sys$enqw (EfnWait, LCK$K_PWMODE,
                         &InstanceLockTable[LockIndex].Lksb, enqfl,
                         0, 0, 0, 0, 0, 0, 2, 0);
      if (VMSok (status))
      {
         status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
         if (VMSok (status))
            InstanceLockTable[LockIndex].InUse = true;
         else
            ErrorNoticed (NULL, status, "sys$enqw", FI_LI);
      }
      else
      {
         ErrorNoticed (NULL, status, "sys$enqw", FI_LI);
         status = sys$enqw (EfnWait, LCK$K_NLMODE,
                            &InstanceLockTable[LockIndex].Lksb,
                            LCK$M_CONVERT | LCK$M_SYSTEM,
                            0, 0, 0, 0, 0, 0, 2, 0);
         if (VMSok (status))
            status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
         else
            ErrorNoticed (NULL, status, "sys$enqw", FI_LI);
      }
   }
   else
   if (status != SS$_NOTQUEUED)
      ErrorNoticed (NULL, status, "sys$enqw", FI_LI);

   sys$setprv (0, &SysLckMask, 0, 0);

   if (status == SS$_XVALNOTVALID)
   {
      /* hmmm, change in cluster composition? whatever! go back to 16 bytes */
      SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
      ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
   }

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!&S", status);

   return (status);
}

/*****************************************************************************/
/*
Take out an EX lock on the specified resource.  If it cannot be immediately
granted then do not queue, immediately return with an indicative status.
*/

int InstanceLockNoWait (int LockIndex)

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceLockNoWait() !&B !UL !31&H",
                 LockIndex <= INSTANCE_CLUSTER_LOCK_COUNT ||
                    InstanceNodeConfig > 1,
                 LockIndex, InstanceLockTable[LockIndex].Name);

   if (InstanceLockTable[LockIndex].InUse) return (SS$_NOTQUEUED);

   if (LockIndex > INSTANCE_CLUSTER_LOCK_COUNT && InstanceNodeConfig <= 1)
   {
      /* a node-only lock is being requested and not multiple instances */
      InstanceLockTable[LockIndex].InUse = true;
      return (SS$_NORMAL);
   }

   sys$setprv (1, &SysLckMask, 0, 0);

   status = sys$enqw (EfnWait, LCK$K_EXMODE,
                      &InstanceLockTable[LockIndex].Lksb,
                      LCK$M_NOQUEUE | LCK$M_CONVERT | LCK$M_SYSTEM,
                      0, 0, 0, 0, 0, 0, 2, 0);

   sys$setprv (0, &SysLckMask, 0, 0);

   if (VMSok (status)) status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
   if (VMSok (status)) InstanceLockTable[LockIndex].InUse = true;

   return (status);
}

/*****************************************************************************/
/*
Unlock the server control functionality.
*/

InstanceUnLockNotify ()

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceUnLockNotify()");

   if (VMSnok (status = InstanceUnLock (INSTANCE_CLUSTER_NOTIFY)))
      ErrorExitVmsStatus (status, "InstanceUnLockNotify()", FI_LI);
}

/*****************************************************************************/
/*
Take out a EX lock on the specified resource.  Wait until it is granted.
InstanceLock(), InstanceLockNoWait() and InstanceUnLock() attempt to improve
performance by avoiding the use of the DLM where possible.  The DLM does not
need to be used when its a node-only lock (not for a cluster-wide resource) and
when there is only the one instance executing on a node.  When this is the case
the serialization is performed by AST deliver level and the '.InUse' flags.
*/

int InstanceLock (int LockIndex)

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceLock() !&B !UL !31&H",
                 LockIndex <= INSTANCE_CLUSTER_LOCK_COUNT ||
                    InstanceNodeConfig > 1,
                 LockIndex, InstanceLockTable[LockIndex].Name);

   if (InstanceLockTable[LockIndex].InUse) return (SS$_BUGCHECK);

   if (LockIndex > INSTANCE_CLUSTER_LOCK_COUNT && InstanceNodeConfig <= 1)
   {
      /* a node-only lock is being requested and not multiple instances */
      InstanceLockTable[LockIndex].InUse = true;
      return (SS$_NORMAL);
   }

   sys$setprv (1, &SysLckMask, 0, 0);

   status = sys$enqw (EfnWait, LCK$K_EXMODE,
                      &InstanceLockTable[LockIndex].Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);

   sys$setprv (0, &SysLckMask, 0, 0);

   if (VMSok (status)) status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
   if (VMSok (status)) InstanceLockTable[LockIndex].InUse = true;

   return (status);
}

/*****************************************************************************/
/*
Return the specified lock to NL mode.
*/

int InstanceUnLock (int LockIndex)

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceUnLock() !&B !UL !31&H",
                 LockIndex <= INSTANCE_CLUSTER_LOCK_COUNT ||
                    InstanceNodeConfig > 1,
                 LockIndex, InstanceLockTable[LockIndex].Name);

   if (!InstanceLockTable[LockIndex].InUse) return (SS$_BUGCHECK);

   if (LockIndex > INSTANCE_CLUSTER_LOCK_COUNT && InstanceNodeConfig <= 1)
   {
      /* a node-only lock is being requested and not multiple instances */
      InstanceLockTable[LockIndex].InUse = false;
      return (SS$_NORMAL);
   }

   sys$setprv (1, &SysLckMask, 0, 0);

   status = sys$enqw (EfnWait, LCK$K_NLMODE,
                      &InstanceLockTable[LockIndex].Lksb,
                      LCK$M_CONVERT | LCK$M_SYSTEM, 0, 0, 0, 0, 0, 0, 2, 0);

   sys$setprv (0, &SysLckMask, 0, 0);

   if (VMSok (status)) status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
   if (VMSok (status)) InstanceLockTable[LockIndex].InUse = false;

   return (status);
}

/*****************************************************************************/
/*
Set the longword in the shared global section pointed to by the supplied
parameter.  Lock global section structure if multiple per-node instances
possible.  This function avoids the overhead of InstanceMutexLock()/UnLock()
with it's required set privilege calls, etc., for the very common action of
accounting structure longword increment.  See InstanceMutexLock() for a
description of mutex operation.
*/

InstanceGblSecSetLong
(
long *longptr,
long value
)
{
   int  WaitCount,
        WaitHttpdTickSecond;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceGblSecSetLong()");

   /* if multiple per-node instances not possible */
   if (InstanceNodeConfig <= 1)
   {
      *longptr = value;
      return;
   }

   if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   WaitCount = 0;
   InstanceMutexCount[INSTANCE_MUTEX_HTTPD]++;
   for (;;)
   {
      InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] =
         !_BBSSI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
      if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      {
         *longptr = value;
         _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
         InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] = 0;
         return;
      }
      if (!WaitCount++)
      {
         InstanceMutexWaitCount[INSTANCE_MUTEX_HTTPD]++;
         WaitHttpdTickSecond = HttpdTickSecond + INSTANCE_MUTEX_WAIT;
      }
      if (SysInfo.AvailCpuCnt == 1) sys$resched ();
      if (HttpdGetTickSecond() > WaitHttpdTickSecond) break;
   }
   /* something's drastically amiss, clear the mutex peremptorily */
   _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
   ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
}

/*****************************************************************************/
/*
Increment the longword in the shared global section pointed to by the supplied
parameter.  Lock global section structure if multiple per-node instances
possible.  This function avoids the overhead of InstanceMutexLock()/UnLock()
with it's required set privilege calls, etc., for the very common action of
accounting structure longword increment.  See InstanceMutexLock() for a
description of mutex operation.
*/

InstanceGblSecIncrLong (long *longptr)

{
   int  WaitCount,
        WaitHttpdTickSecond;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceGblSecIncrLong()");

   /* if multiple per-node instances not possible */
   if (InstanceNodeConfig <= 1)
   {
      *longptr = *longptr + 1;
      return;
   }

   if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   WaitCount = 0;
   InstanceMutexCount[INSTANCE_MUTEX_HTTPD]++;
   for (;;)
   {
      InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] =
         !_BBSSI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
      if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      {
         *longptr = *longptr + 1;
         _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
         InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] = 0;
         return;
      }
      if (!WaitCount++)
      {
         InstanceMutexWaitCount[INSTANCE_MUTEX_HTTPD]++;
         WaitHttpdTickSecond = HttpdTickSecond + INSTANCE_MUTEX_WAIT;
      }
      if (SysInfo.AvailCpuCnt == 1) sys$resched ();
      if (HttpdGetTickSecond() > WaitHttpdTickSecond) break;
   }
   /* something's drastically amiss, clear the mutex peremptorily */
   _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
   ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
}

/*****************************************************************************/
/*
Same as InstanceGblSecIncrLong() except it decrements the longword if non-zero.
See InstanceMutexLock() for a description of mutex operation.
*/

InstanceGblSecDecrLong (long *longptr)

{
   int  WaitCount,
        WaitHttpdTickSecond;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceGblSecDecrLong()");

   /* if multiple per-node instances not possible */
   if (InstanceNodeConfig <= 1)
   {
      if (*longptr) *longptr = *longptr - 1;
      return;
   }

   if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   WaitCount = 0;
   InstanceMutexCount[INSTANCE_MUTEX_HTTPD]++;
   for (;;)
   {
      InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] =
         !_BBSSI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
      if (InstanceMutexHeld[INSTANCE_MUTEX_HTTPD])
      {
         if (*longptr) *longptr = *longptr - 1;
         _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
         InstanceMutexHeld[INSTANCE_MUTEX_HTTPD] = 0;
         return;
      }
      if (!WaitCount++)
      {
         InstanceMutexWaitCount[INSTANCE_MUTEX_HTTPD]++;
         WaitHttpdTickSecond = HttpdTickSecond + INSTANCE_MUTEX_WAIT;
      }
      if (SysInfo.AvailCpuCnt == 1) sys$resched ();
      if (HttpdGetTickSecond() > WaitHttpdTickSecond) break;
   }
   /* something's drastically amiss, clear the mutex peremptorily */
   _BBCCI (0, &HttpdGblSecPtr->Mutex[INSTANCE_MUTEX_HTTPD]);
   ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
}

/*****************************************************************************/
/*
Take out a mutex (lock) on the global section.  There is a small chance that an
instance will crash or be stopped while the mutex is held.  InstanceExit()
should reset the mutex if currently held.  A sanity checks causes the instance
to exit if the mutex is held for more than the defined period.  Of course as
with all indeterminate shared access there are small critical code sections and
chances of race conditions here.   

Worst-case is a mutex being taken out and not released because of process
STOPing, though there should not be infinite loops or waits, the sanity check
should cause an exit.  There is also a small chance that the instance may have
released the mutex but still have the flag set that it holds it.  This might
result in the mutex being "released" (zeroed) while some other instance
legitimately holds it.  All-in-all such uncoordinated access to the global
section might result in minor data corruption (accounting accumulators), but
nothing disasterous.

On a multi-CPU system this algorithm might cause the waiting instance to "spin"
a little (i.e. uselessly consume CPU cycles). It assumes the blocking instance
will be scheduled and processing on some other CPU :^)

If this "hangs" at AST delivery level then 'HttpdTickSecond' will have stopped
ticking.  Generate our own ticks here.

I'm assuming that this mutex approach is more light-weight than using the DLM.
*/

InstanceMutexLock (int MutexNumber)

{
   int  WaitCount,
        WaitHttpdTickSecond;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceMutexLock() !UL", MutexNumber);

   if (InstanceNodeConfig <= 1) return;

   if (MutexNumber <= 0 ||
       MutexNumber > INSTANCE_MUTEX_COUNT ||
       InstanceMutexHeld[MutexNumber])
   {
      char  String [256];
      sprintf (String, "%s (mutex %d)", ErrorSanityCheck, MutexNumber);
      ErrorExitVmsStatus (SS$_BUGCHECK, String, FI_LI);
   }

   WaitCount = 0;
   InstanceMutexCount[MutexNumber]++;
   for (;;)
   {
      InstanceMutexHeld[MutexNumber] =
         !_BBSSI (0, &HttpdGblSecPtr->Mutex[MutexNumber]);
      if (InstanceMutexHeld[MutexNumber]) return;
      if (!WaitCount++)
      {
         InstanceMutexWaitCount[MutexNumber]++;
         WaitHttpdTickSecond = HttpdTickSecond + INSTANCE_MUTEX_WAIT;
      }
      if (SysInfo.AvailCpuCnt == 1) sys$resched ();
      if (HttpdGetTickSecond() > WaitHttpdTickSecond) break;
   }
   /* something's drastically amiss, clear the mutex peremptorily */
   _BBCCI (0, &HttpdGblSecPtr->Mutex[MutexNumber]);
   ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
}

/*****************************************************************************/
/*
Reset the mutex taken out on the global section.
See InstanceMutexLock() for a description of mutex operation.
*/

InstanceMutexUnLock (int MutexNumber)

{
   int  status;
   char  String [256];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceMutexUnLock() !UL", MutexNumber);

   if (InstanceNodeConfig <= 1) return;

   if (MutexNumber >= 1 &&
       MutexNumber <= INSTANCE_MUTEX_COUNT &&
       InstanceMutexHeld[MutexNumber])
   {
      _BBCCI (0, &HttpdGblSecPtr->Mutex[MutexNumber]);
      InstanceMutexHeld[MutexNumber] = 0;
      return;
   }

   /* something's drastically amiss, clear the mutex peremptorily */
   if (InstanceMutexHeld[MutexNumber])
      _BBCCI (0, &HttpdGblSecPtr->Mutex[MutexNumber]);
   sprintf (String, "%s (mutex %d)", ErrorSanityCheck, MutexNumber);
   ErrorExitVmsStatus (SS$_BUGCHECK, String, FI_LI);
}

/*****************************************************************************/
/*
This function establishes a DLM based mechanism for registering interest in
receiving notifications of "events" across all node and/or cluster instances
(depending on the resource name involved) of servers.  When called it
"registers interest" in the associated resource name and when
InstanceNotifyNow() is used the callback AST is activated and the lock status
value block used to transfer data to that AST.

Enqueues a CR (concurrent read) lock on the specified resource.  This allows a
"blocking" AST to be delivered (back to this function, the two states are
differentiated by setting the most significant bit of 'LockIndex' for the AST
call), indicating another instance somewhere (using InstanceNotifyNow()) is
wishing to initiate a distributed action, by enqueing an EX (exclusive) lock
for the same resource.  Release the CR lock then immediately enqueue another CR
so that the lock value block subsequently written to by the initiating EX mode
lock is read via the specified AST function.  (Note the 'AstFunction' parameter
is only accessed during non-AST processing and so is not a *real* issue -
except for purists ;^)  The '..' lock status block and AST function are
used here because it is being set up to last the life of the server.
*/

int InstanceNotifySet
(
int LockIndex,
CALL_BACK AstFunction
)
{
   int  enqfl, status;
   char  *cptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceNotifySet() !&F !&X !&Z !&A",
                 &InstanceNotifySet, LockIndex,
                 InstanceLockTable[LockIndex&0x7fffffff].Name,
                 LockIndex&0x80000000 ? 0 : AstFunction);

   sys$setprv (1, &SysLckMask, 0, 0);

   if (LockIndex & 0x80000000)
   {
      enqfl = LCK$M_VALBLK | LCK$M_QUECVT | LCK$M_CONVERT | LCK$M_SYSTEM;
      if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
         enqfl |= LCK$M_XVALBLK;

      /* mask out the bit that indicates it's an AST */
      LockIndex &= 0x7fffffff;
      if (!InstanceLockTable[LockIndex].InUse)
         ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
      /* convert (wait) current CR mode lock to NL unblocking the queue */
      status = sys$enqw (EfnWait, LCK$K_NLMODE,
                         &InstanceLockTable[LockIndex].Lksb,
                         LCK$M_CONVERT | LCK$M_SYSTEM,
                         0, 0, 0, LockIndex|0x80000000, 0, 0, 2, 0);
      if (VMSok (status))
         status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);
      /* convert (nowait) back to CR to block queue against next EX mode */
      status = sys$enq (EfnNoWait, LCK$K_CRMODE,
                        &InstanceLockTable[LockIndex].Lksb, enqfl,
                        0, 0, &InstanceNotifySetAst, LockIndex|0x80000000,
                        &InstanceNotifySet, 0, 2, 0);
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enq()", FI_LI);

      if (status == SS$_XVALNOTVALID)
      {
         /* hmmm, change in cluster composition? whatever! back to 16 bytes */
         SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
         ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
      }
   }
   else
   if (AstFunction)
   {
      /* initial call */
      if (InstanceLockTable[LockIndex].InUse)
         ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
      InstanceLockTable[LockIndex].InUse = true;
      InstanceLockTable[LockIndex].AstFunction = AstFunction;
      /* convert (wait) to CR to block the queue against EX mode */
      status = sys$enqw (EfnWait, LCK$K_CRMODE,
                         &InstanceLockTable[LockIndex].Lksb,
                         LCK$M_CONVERT | LCK$M_SYSTEM,
                         0, 0, 0, LockIndex|0x80000000,
                         &InstanceNotifySet, 0, 2, 0);
      if (VMSok (status))
         status = InstanceLockTable[LockIndex].Lksb.lksb$w_status;
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$enqw()", FI_LI);
   }
   else
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   sys$setprv (0, &SysLckMask, 0, 0);

   return (status);
}

/*****************************************************************************/
/*
This function abstracts away the actual lock status block containing the data
being delivered by calling the AST with a pointer to the one in use.
*/

InstanceNotifySetAst (int LockIndex)

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceNotifySetAst() !&F !&X !&Z",
                 &InstanceNotifySetAst, LockIndex,
                 InstanceLockTable[LockIndex].Name);

   /* mask out the bit that indicates it's an AST */
   LockIndex &= 0x7fffffff;

   /* invoke the AST function, address of the lock status block parameter */
   (*InstanceLockTable[LockIndex].AstFunction)
      (&InstanceLockTable[LockIndex].Lksb);
}

/*****************************************************************************/
/*
After InstanceNotifySet() has "registered interest" in a particular
resource name this function may be used to notify and deliver either, 15 (16)
bytes for pre-V8.2 VMS, or 63 (64) bytes for non-VAX V8.2 and later VMS, of 
data in the lock status block to the callback AST function specified when
InstanceNotifySet() was originally called.  The 15/63 bytes of data can be
anything including a null-terminated string, only the first 15/63 bytes are
used of any parameter supplied.  As it's generally assumed to be a string the
16/64th byte is always set to a null character (for when the string has been
truncated).

This function is explicitly called to initiate the notify, queuing an EXMODE
lock containing a lock value block, and is also called by itself as an AST to
dequeue the EXMODE lock causing the lock value block to be written to all
participating in the resource.  The two states are differentiated by setting
the most significant bit of 'LockIndex' for the AST call.

There is a third behaviour performed.  If 'LockIndex' is zero the value of the
lock ID is returned as a boolean.  If zero then the enqueuing has concluded. 
If non-zero (i.e. a lock ID) then the enqueuing is not complete.  Used by
polling from ControlCommand().

This function uses it's own internal, static lock status block, and is used
infrequently enough that the full enqueue/dequeue does not pose any performance
issue.  The "queue" then "convert" is required due to the conversion queue
(and the locks being converted in InstanceNotifySet()) having priority
over the waiting queue (VMS Progamming Concepts diagram).
*/

int InstanceNotifyNow
(
int LockIndex,
void *ValuePtr
)
{
   static uchar  ValueBlock [LOCK_VALUE_BLOCK_64];
   static struct lksb  NotifyLksb;

   int  deqfl, status;
   char  *cptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE,
                 "InstanceNotifyNow() !&F !&X !&B !&Z !&Z",
                 &InstanceNotifyNow, LockIndex,
                 NotifyLksb.lksb$l_lkid,
                 InstanceLockTable[LockIndex&0x7fffffff].Name,
                 LockIndex&0x80000000 ? ValueBlock : ValuePtr);

   /* just polling the progress of the lock enqueue */
   if (!LockIndex) return (NotifyLksb.lksb$l_lkid);

   sys$setprv (1, &SysLckMask, 0, 0);

   if (LockIndex & 0x80000000)
   {
      if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
         deqfl = LCK$M_XVALBLK;
      else
         deqfl = 0;

      /* dequeue the EX mode lock writing the value block */
      status = sys$deq (NotifyLksb.lksb$l_lkid, ValueBlock, 0, deqfl);
      if (VMSnok (status)) ErrorExitVmsStatus (status, "sys$deq()", FI_LI);

      memset (&NotifyLksb, 0, sizeof(struct lksb));
      memset (ValueBlock, 0, sizeof(ValueBlock));

      InstanceUnLockNotify ();
   }
   else
   if (!NotifyLksb.lksb$l_lkid)
   {
      if (ValuePtr) memcpy (ValueBlock, ValuePtr, SysInfo.LockValueBlockSize);

      /* queue (wait) a CR mode lock */
      status = sys$enqw (EfnWait, LCK$K_CRMODE, &NotifyLksb,
                         LCK$M_SYSTEM, &InstanceLockTable[LockIndex].NameDsc,
                         0, 0, LockIndex|0x80000000, 0, 0, 2, 0);
      if (VMSok (status))
      {
         status = NotifyLksb.lksb$w_status;
         if (VMSok (status))
         {
            /* convert (nowait) to EX mode */
            status = sys$enq (EfnNoWait, LCK$K_EXMODE, &NotifyLksb,
                              LCK$M_CONVERT | LCK$M_SYSTEM,
                              0, 0, &InstanceNotifyNow, LockIndex|0x80000000,
                              0, 0, 2, 0);
            if (VMSnok (status))
               ErrorExitVmsStatus (status, "sys$enq()", FI_LI);
         }
         else
            ErrorNoticed (NULL, status, "sys$enqw()", FI_LI);
      }
   }
   else
      ErrorNoticed (NULL, status = SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   sys$setprv (0, &SysLckMask, 0, 0);

   return (status);
}

/*****************************************************************************/
/*
Waiting up to |Seconds| gain the notification lock and then send the
notification.  After notification wait |Seconds| for it to complete.
Where an AST is not in progress (/DO=..) the notification is waited on to
completion or timeout.  Where an AST is in progress (Server Admin) there is not
wait to completion.  Note that InstanceNotifyNow() actually releases the lock
gained in this function as the value block is delivered.  Returns a VMS status.
*/

int InstanceNotifyWait
(
int LockIndex,
void *ValuePtr,
int Seconds
)
{
   static ulong  JpiAstAct;
   static VMS_ITEM_LIST3  JpiItems [] =
   {
      { sizeof(JpiAstAct), JPI$_ASTACT, &JpiAstAct, 0 },
      { 0,0,0,0 }
   };

   int  cnt, status;
   IO_SB  IOsb;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceNotifyWait()");

   /* wait for the lock */
   if (!Seconds)
      status = InstanceLockNotify ();
   else
   for (cnt = Seconds * 10; cnt; cnt--)
   {
      if (VMSok (status = InstanceLockNotify ())) break;
      if (status != SS$_NOTQUEUED) break;
      usleep (100 * 1000);  /* 100 mS */
   }

   /* if the lock was not available */
   if (VMSnok (status)) return (status);

   status = InstanceNotifyNow (LockIndex, ValuePtr);

   if (VMSok (status))
   {
      status = sys$getjpiw (EfnWait, 0, 0, &JpiItems, &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSok (status))
      {
         if (!(JpiAstAct & 0x08))
         {
            /* user mode AST not active so wait for completion */
            for (cnt = Seconds * 10; cnt; cnt--)
            {
               if (!InstanceNotifyNow (0, NULL)) break;
               usleep (100 * 1000);  /* 100 mS */
            }
            if (!cnt) status = SS$_TIMEOUT;
         }
      }
   }
   else
      InstanceUnLockNotify ();

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!&S", status);

   return (status);
}

/*****************************************************************************/
/*
NL locks indicate any other utility, etc., (e.g. HTTPDMON) that may have an
interest in the server locks.  Non-NL locks indicate active server interest in
the resource.   This function gets all locks associated with the specified lock
resource and then goes through them noting each non-NL lock.  From these it can
set the count of the number of servers (number of CR locks) and/or create a
list of the processes with these non-NL locks.  It returns a pointer to a
dynamically allocated string containing list of processes.  THIS MUST BE FREED.
*/

int InstanceLockList
(
int LockIndex,
char *Separator,
char **ListPtrPtr
)
{
   static ushort  JpiNodeNameLen,
                  JpiPrcNamLen;
   static char  JpiNodeName [7],
                JpiPrcNam [16];

   static struct
   {
      ushort  tot_len,  /* bits 0..15 */
              lck_len;  /* bits 16..30 */
   } LkiLocksLength;

   static VMS_ITEM_LIST3  JpiItems [] =
   {
      { sizeof(JpiNodeName)-1, JPI$_NODENAME, &JpiNodeName, &JpiNodeNameLen },
      { sizeof(JpiPrcNam)-1, JPI$_PRCNAM, &JpiPrcNam, &JpiPrcNamLen },
      { 0,0,0,0 }
   };
   static VMS_ITEM_LIST3  LkiItems [] =
   {
      /* careful, values are dynamically assigned in code below! */
      { 0, LKI$_LOCKS, 0, &LkiLocksLength },
      {0,0,0,0}
   };

   int  cnt, status,
        ListBytes,
        LockCount,
        NonNlLockCount;
   char  *aptr, *sptr;
   IO_SB  IOsb;
   LKIDEF  *lkiptr;
   LKIDEF  LkiLocks [INSTANCE_REPORT_LOCK_MAX];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
   {
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceLockList()");
      WatchDataDump (InstanceLockTable[LockIndex].Name,
                     InstanceLockTable[LockIndex].NameLength);
   }

   NonNlLockCount = 0;
   if (ListPtrPtr) *ListPtrPtr = NULL;

   LkiItems[0].buf_addr = LkiLocks;
   LkiItems[0].buf_len = sizeof(LkiLocks);

   sys$setprv (1, &SysLckMask, 0, 0);
   status = sys$getlkiw (EfnWait,
                         &InstanceLockTable[LockIndex].Lksb.lksb$l_lkid,
                         &LkiItems, &IOsb, 0, 0, 0);
   sys$setprv (0, &SysLckMask, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSnok (status))
   {
      ErrorNoticed (NULL, status, NULL, FI_LI);
      return (-1);
   }

   if (LkiLocksLength.tot_len)
   {
      if (LkiLocksLength.tot_len & 0x8000)
      {
         ErrorNoticed (NULL, SS$_BADPARAM, NULL, FI_LI);
         return (NULL);
      }
      LockCount = LkiLocksLength.tot_len / LkiLocksLength.lck_len;
   }
   else
      LockCount = 0;

   cnt = LockCount;
   for (lkiptr = &LkiLocks; cnt--; lkiptr++)
   {
      /* only interested in CR locks when not looking at supervisor */
      if (LockIndex != INSTANCE_NODE_SUPERVISOR)
         if (lkiptr->lki$b_grmode == LCK$K_NLMODE)
            continue;
      NonNlLockCount++;
   }
   /* if not interested in generating a list */
   if (!(Separator && ListPtrPtr && NonNlLockCount))
   {
      if (WATCH_MODULE(WATCH_MOD_INSTANCE))
         WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "!UL", NonNlLockCount);
      return (NonNlLockCount);
   }

   ListBytes = sizeof(JpiNodeName) + sizeof(JpiPrcNam) + strlen(Separator);
   ListBytes *= LockCount;
   aptr = sptr = VmGet (ListBytes);

   /* use WORLD to allow access to other processes */
   sys$setprv (1, &WorldMask, 0, 0);

   cnt = LockCount;
   for (lkiptr = &LkiLocks; cnt--; lkiptr++)
   {
      /* only interested in CR locks when not looking at supervisor */
      if (LockIndex != INSTANCE_NODE_SUPERVISOR)
         if (lkiptr->lki$b_grmode == LCK$K_NLMODE)
            continue;

      status = sys$getjpiw (EfnWait, &lkiptr->lki$l_pid, 0, &JpiItems,
                            &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSnok (status))
      {
         ErrorNoticed (NULL, status, NULL, FI_LI);
         continue;
      }
      JpiNodeName[JpiNodeNameLen] = '\0';
      JpiPrcNam[JpiPrcNamLen] = '\0';
      if (aptr[0]) strcpy (sptr, Separator);
      while (*sptr) sptr++;
      strcpy (sptr, JpiNodeName);
      while (*sptr) sptr++;
      strcpy (sptr, "::");
      while (*sptr) sptr++;
      strcpy (sptr, JpiPrcNam);
      while (*sptr) sptr++;
   }

   sys$setprv (0, &WorldMask, 0, 0);

   *ListPtrPtr = aptr;
   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchDataFormatted ("!UL !&Z\n", NonNlLockCount, aptr);

   return (NonNlLockCount);
}

/*****************************************************************************/
/*
Using the lock IDs in the general and socket lock tables produce a report that
lists all of the related locks, showing process PIDs, cluster nodes, etc.  This
is mainly intended as a debugging, development and trouble-shooting tool.
*/ 

InstanceLockReport (REQUEST_STRUCT *rqptr)

{
   static char  BeginPage [] =
"<p><table class=\"ctgry\">\n\
<tr><td style=\"padding:0.5em 1em 0.5em 1.3em;\"><pre>\n\
<b>!#*   MSTLKID  MSTCSID  RQ GR QU LKID     \
CSID     PRCNAM          PID      VALBLK(!UL)</b>\n";

   static char  MutexFao [] = "\n<b>!18AZ</b>  !11&L / !&L (!UL%)";

   static char  EndPage [] =
"</pre></td></tr>\n\
</table>\n\
!AZ\
</div>\n\
</body>\n\
</html>\n";

   int  idx, status,
        ResNameLength;
   ulong  *vecptr;
   ulong  FaoVector [32];

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_INSTANCE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_INSTANCE, "InstanceLockReport()");

   InstanceLockReportNameWidth = 0;
   /* the socket locks index from zero!! */
   for (idx = 0; idx < InstanceSocketCount; idx++)
   {
      ResNameLength =
         strlen(InstanceParseLockName(InstanceSocketTable[idx].Name));
      if (ResNameLength > InstanceLockReportNameWidth)
         InstanceLockReportNameWidth = ResNameLength;
   }

   AdminPageTitle (rqptr, "Lock Report", BeginPage,
                   InstanceLockReportNameWidth, SysInfo.LockValueBlockSize);

   /* use WORLD to allow access to other process' PID process names */
   sys$setprv (1, &WorldMask, 0, 0);
   sys$setprv (1, &SysLckMask, 0, 0);

   /* the general locks index from one!! */
   for (idx = 1; idx <= INSTANCE_LOCK_COUNT; idx++)
      InstanceLockReportData (rqptr, &InstanceLockTable[idx].Lksb.lksb$l_lkid);

   /* the socket locks index from zero!! */
   for (idx = 0; idx < InstanceSocketCount; idx++)
      InstanceLockReportData (rqptr, &InstanceSocketTable[idx].Lksb.lksb$l_lkid);

   if (InstanceLockAdmin.Lksb.lksb$l_lkid)
      InstanceLockReportData (rqptr, &InstanceLockAdmin. Lksb.lksb$l_lkid);

   sys$setprv (0, &SysLckMask, 0, 0);
   sys$setprv (0, &WorldMask, 0, 0);

   InstanceMutexLock (INSTANCE_MUTEX_HTTPD);

   for (idx = 1; idx <= INSTANCE_MUTEX_COUNT; idx++)
   {
      vecptr = FaoVector;
      *vecptr++ = InstanceMutexDescr[idx];
      *vecptr++ = HttpdGblSecPtr->MutexCount[idx];
      *vecptr++ = HttpdGblSecPtr->MutexWaitCount[idx];
      *vecptr++ = PercentOf32 (HttpdGblSecPtr->MutexWaitCount[idx],
                             HttpdGblSecPtr->MutexCount[idx]);

      status = FaolToNet (rqptr, MutexFao, &FaoVector);
      if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);
   }

   InstanceMutexUnLock (INSTANCE_MUTEX_HTTPD);

   vecptr = FaoVector;
   *vecptr++ = AdminRefresh();

   status = FaolToNet (rqptr, EndPage, &FaoVector);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);

   rqptr->rqResponse.PreExpired = PRE_EXPIRE_ADMIN;
   ResponseHeader200 (rqptr, "text/html", &rqptr->NetWriteBufferDsc);

   AdminEnd (rqptr);
}

/*****************************************************************************/
/*
Report on a single lock.
*/ 

InstanceLockReportData
(
REQUEST_STRUCT *rqptr,
ulong *LockIdPtr
)
{
   static char  LockDataFao [] =
"<b>!#AZ</b>  !8XL !8AZ !AZ !AZ !&@ !8XL !8AZ \
!15AZ <a href=\"!AZ?pid=!8XL&puser=!AZ\">!8XL</a> !AZ\n";

   static char  *LockMode [] = { "NL","CR","CW","PR","PW","EX" },
                /* lock state seems to range from -8 (RSPRESEND) to +1 (GR) */
                *LockState [] = { "??","??","-8","-7","-6","-5","-4",
                                  "-3","-2","WT","CV","GR","??","??" };

   static ulong  JpiPrcNamLen,
                 LkiResNamLen,
                 LkiValBlkLen,
                 Lki_XVALNOTVALID;
   static char  NodeName [16],
                JpiPrcNam [16],
                JpiUserName [13],
                LkiResNam [31+1],
                LkiValBlk [LOCK_VALUE_BLOCK_64+1];

   static struct
   {
      ushort  tot_len,  /* bits 0..15 */
              lck_len;  /* bits 16..30 */
   } *lksptr,
     LkiLocksLen;

   static VMS_ITEM_LIST3  LkiItems [] =
   {
      /* careful, values are dynamically assigned in code below! */
      { 0, 0, 0, 0 },  /* reserved for LKI$_LOCKS item */
      { 0, 0, 0, 0 },  /* reserved for LKI$_RESNAM item */
      { 0, 0, 0, 0 },  /* reserved for LKI$_[X]VALBLK item */
      { 0, 0, 0, 0 },  /* reserved for LKI$_XVALNOTVALID item */
      {0,0,0,0}
   };
   static VMS_ITEM_LIST3  JpiItems [] =
   {
      { sizeof(JpiPrcNam)-1, JPI$_PRCNAM, &JpiPrcNam, &JpiPrcNamLen },
      { sizeof(JpiUserName), JPI$_USERNAME, &JpiUserName, 0 },
      { 0,0,0,0 }
   };
   static VMS_ITEM_LIST3  SyiItems [] =
   {
      { sizeof(NodeName)-1, SYI$_NODENAME, &NodeName, 0 },
      {0,0,0,0}
   };

   int  cnt, idx, status,
        LockCount,
        LockTotal;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr;
   char  CsidNodeName [16],
         MstCsidNodeName [16],
         PidPrcNam [16],
         String [256];
   IO_SB  IOsb;
   LKIDEF  *lkiptr;
   LKIDEF  LkiLocks [INSTANCE_REPORT_LOCK_MAX];

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_INSTANCE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_INSTANCE,
                 "InstanceLockReportData() !8XL", *LockIdPtr);

   memset (LkiValBlk, 0, sizeof(LkiValBlk));

   LkiItems[0].buf_len = sizeof(LkiLocks);
   LkiItems[0].buf_addr = &LkiLocks;
   LkiItems[0].item = LKI$_LOCKS;
   LkiItems[0].ret_len = &LkiLocksLen;
   LkiItems[1].buf_len = sizeof(LkiResNam);
   LkiItems[1].buf_addr = &LkiResNam;
   LkiItems[1].item = LKI$_RESNAM;
   LkiItems[1].ret_len = &LkiResNamLen;
   if (SysInfo.LockValueBlockSize == LOCK_VALUE_BLOCK_64)
   {
      LkiItems[2].buf_len = LOCK_VALUE_BLOCK_64;
      LkiItems[2].buf_addr = &LkiValBlk;
      LkiItems[2].item = LKI$_XVALBLK;
      LkiItems[2].ret_len = &LkiValBlkLen;
      LkiItems[3].buf_len = sizeof(Lki_XVALNOTVALID);
      LkiItems[3].buf_addr = &Lki_XVALNOTVALID;
      LkiItems[3].item = LKI$_XVALNOTVALID;
   }
   else
   {
      LkiItems[2].buf_len = LOCK_VALUE_BLOCK_16;
      LkiItems[2].buf_addr = &LkiValBlk;
      LkiItems[2].item = LKI$_VALBLK;
      LkiItems[2].ret_len = &LkiValBlkLen;
      /* in this case this terminates the item list */
      LkiItems[3].buf_len = 0;
      LkiItems[3].buf_addr = 0;
      LkiItems[3].item = 0;
      Lki_XVALNOTVALID = 0;
   }

   status = sys$getlkiw (EfnWait, LockIdPtr, &LkiItems, &IOsb, 0, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSnok (status))
   {
      ErrorNoticed (rqptr, status, NULL, FI_LI);
      return (status);
   }

   if (Lki_XVALNOTVALID)
   {
      /* hmmm, change in cluster composition? whatever! go back to 16 bytes */
      SysInfo.LockValueBlockSize = LOCK_VALUE_BLOCK_16;
      ErrorNoticed (NULL, SS$_XVALNOTVALID, ErrorXvalNotValid, FI_LI);
      return (SS$_XVALNOTVALID);
   }

   LkiResNam[LkiResNamLen] = '\0';
   LkiValBlk[LkiValBlkLen] = '\0';

   lkiptr = LkiItems[0].buf_addr;
   lksptr = LkiItems[0].ret_len;

   if (lksptr->tot_len & 0x8000)
   {
      ErrorNoticed (rqptr, SS$_BADPARAM, NULL, FI_LI);
      return (SS$_BADPARAM);
   }
   if (lksptr->lck_len)
      LockCount = lksptr->tot_len / lksptr->lck_len;
   else
      LockCount = 0;

   if (!LockCount) 
   {
      ErrorNoticed (NULL, status, ErrorSanityCheck, FI_LI);
      return (status);
   }

   for (cnt = 0; cnt < LockCount; cnt++, lkiptr++)
   {
      memset (NodeName, 0, sizeof(NodeName));
      status = sys$getsyiw (EfnWait, &lkiptr->lki$l_mstcsid,
                            0, &SyiItems, &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSnok (status))
      {
         ErrorNoticed (rqptr, status, NULL, FI_LI);
         continue;
      }
      strcpy (MstCsidNodeName, NodeName);
      memset (NodeName, 0, sizeof(NodeName));
      status = sys$getsyiw (EfnWait, &lkiptr->lki$l_csid, 0,
                            &SyiItems, &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSnok (status))
      {
         ErrorNoticed (rqptr, status, NULL, FI_LI);
         continue;
      }
      strcpy (CsidNodeName, NodeName);
      memset (JpiPrcNam, 0, sizeof(JpiPrcNam));
      status = sys$getjpiw (EfnWait, &lkiptr->lki$l_pid, 0,
                            &JpiItems, &IOsb, 0, 0);
      if (VMSok (status)) status = IOsb.Status;
      if (VMSnok (status))
      {
         ErrorNoticed (rqptr, status, NULL, FI_LI);
         continue;
      }

      JpiPrcNam[15] = JpiUserName[12] = '\0';
      for (cptr = JpiUserName; *cptr && *cptr != ' '; cptr++);
      *cptr = '\0';

      if (strsame (LkiValBlk, CONTROL_AUTH_SKELKEY,
                       sizeof(CONTROL_AUTH_SKELKEY)-1) &&
          !isdigit(LkiValBlk[sizeof(CONTROL_AUTH_SKELKEY)-1]))
      {
         /* mask any credentials */
         LkiValBlk[sizeof(CONTROL_AUTH_SKELKEY)-1] = '*';
         LkiValBlk[sizeof(CONTROL_AUTH_SKELKEY)] = '\0';
      }

      vecptr = FaoVector;
      *vecptr++ = InstanceLockReportNameWidth;
      if (cnt)
         *vecptr++ = "";
      else
         *vecptr++ = InstanceParseLockName(LkiResNam);
      *vecptr++ = lkiptr->lki$l_mstlkid;
      *vecptr++ = MstCsidNodeName;
      *vecptr++ = LockMode[lkiptr->lki$b_rqmode];
      *vecptr++ = LockMode[lkiptr->lki$b_grmode];
      if (lkiptr->lki$b_queue == 1)
         *vecptr++ = "!AZ";
      else
         *vecptr++ = "<u>!AZ</u>";
      *vecptr++ = LockState[lkiptr->lki$b_queue+10];
      *vecptr++ = lkiptr->lki$l_lkid;
      *vecptr++ = CsidNodeName;
      *vecptr++ = JpiPrcNam;
      *vecptr++ = ADMIN_REPORT_SHOW_PROCESS;
      *vecptr++ = lkiptr->lki$l_pid;
      *vecptr++ = JpiUserName;
      *vecptr++ = lkiptr->lki$l_pid;
      *vecptr++ = LkiValBlk;
      status = FaolToNet (rqptr, LockDataFao, &FaoVector);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
         ErrorNoticed (rqptr, status, NULL, FI_LI);
   }

   return (status);
}

/*****************************************************************************/
/*
Parse the binary lock resource name into readable format suitable for display.
Return a pointer to a static buffer containing that description.
*/

char* InstanceParseLockName (char *LockName)

{
   static char  String [128];
   static char  *LockUses [] = { INSTANCE_LOCK_USES };
   static char  ErrorOverflow [] = "***OVERFLOW***";

   int  cnt;
   uint  Ip4Address;
   ushort  IpPort;
   uchar  Ip6Address [16];
   char  *cptr, *sptr, *zptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_INSTANCE))
      WatchThis (WATCHALL, WATCH_MOD_INSTANCE, "InstanceParseLockName()");

   zptr = (sptr = String) + sizeof(String)-16;

   cptr = LockName;
   cnt = sizeof(HTTPD_NAME)-1;
   while (cnt-- && sptr < zptr) *sptr++ = *cptr++;
   /* version and environment number */
   sptr += sprintf (sptr, "|%d|%d", (*cptr & 0xf0) >> 4, *cptr & 0x0f);
   if (sptr >= zptr) return (ErrorOverflow);
   cptr++;
   if (*cptr > INSTANCE_LOCK_PRINTABLE)
   {
      /* node name */
      if (sptr < zptr) *sptr++ = '|';
      while (*cptr > INSTANCE_LOCK_PRINTABLE && sptr < zptr) *sptr++ = *cptr++;
   }
   if (*cptr <= INSTANCE_LOCK_COUNT)
   {
      sptr += sprintf (sptr, "|%s", LockUses[*cptr]);
      if (sptr >= zptr) return (ErrorOverflow);
      *sptr = '\0';
      return (String);
   }
   if (*cptr == INSTANCE_NODE_SOCKIP4)
   {
      cptr++;
      Ip4Address = *(UINTPTR)cptr;
      cptr += sizeof(uint);
      IpPort = *(USHORTPTR)cptr;
      sptr += sprintf (sptr, "|%s,%d",
                       TcpIpAddressToString(Ip4Address,4), IpPort);
   }
   else
   if (*cptr == INSTANCE_NODE_SOCKIP6)
   {
      cptr++;
      memcpy (&Ip6Address, cptr, sizeof(Ip6Address));
      cptr += sizeof(Ip6Address);
      IpPort = *(USHORTPTR)cptr;
      sptr += sprintf (sptr, "|%s,%d",
                       TcpIpAddressToString(&Ip6Address,6), IpPort);
   }
   else
      *sptr++ = '?';

   if (sptr >= zptr) return (ErrorOverflow);
   *sptr = '\0';
   return (String);
}

/*****************************************************************************/