summaryrefslogblamecommitdiffstats
path: root/private/os2/os2ses/ntinitss.c
blob: 449c81c34a0cc7800b455d047a89bc0c8e9d129a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650

















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                      
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    ntinitss.c

Abstract:

    This module contains the code to establish the connection between
    the session console process and the OS2 Emulation Subsystem.

Author:

    Avi Nathan (avin) 17-Jul-1991

Environment:

    User Mode Only

Revision History:

--*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define NTOS2_ONLY
#include "os2ses.h"
#include "os2tile.h"
#if PMNT
#define INCL_32BIT
#include "pmnt.h"
#endif
#include <winerror.h>

#define OD2_PORT_MEMORY_SIZE 0x10000

extern PVOID Od2PortHeap;
extern ULONG Od2PortMemoryRemoteDelta;
extern PSZ   Od2PgmFilePath;
extern HANDLE Od2PortHandle;

#if PMNT
extern ULONG PMSubprocSem32;
extern BOOLEAN Ow2WriteBackCloseEvent();
extern APIRET DosSemClear(ULONG hsem);
#endif //PMNT

HANDLE
CreateEventW(
    PVOID lpEventAttributes,
    BOOL bManualReset,
    BOOL bInitialState,
    LPCWSTR lpName
    );

ULONG
KbdInitAfterSesGrp(IN VOID);

APIRET
Od2WaitForSingleObject(
    IN HANDLE   Handle,
    IN BOOLEAN  Alertable,
    IN PLARGE_INTEGER Timeout OPTIONAL
    );

BOOLEAN
Od2InitCreateProcessMessage(
    OUT PSCREQ_CREATE   pCreate
    );

VOID
Od2HandleCreateProcessRespond(
    IN  PSCREQ_CREATE   pCreate
    );

VOID
ExitThread(
    ULONG dwExitCode
    );

APIRET
OpenLVBsection(VOID);

DWORD
GetCurrentDirectoryW(
    DWORD nBufferLength,
    LPWSTR lpBuffer
    );

BOOLEAN
InitializeSecurityDescriptor (
    PSECURITY_DESCRIPTOR pSecurityDescriptor,
    DWORD dwRevision
    );

BOOLEAN
SetSecurityDescriptorDacl (
    PSECURITY_DESCRIPTOR pSecurityDescriptor,
    BOOLEAN bDaclPresent,
    PACL pDacl,
    BOOLEAN bDaclDefaulted
    );

// Defined in <winbase.h> but we can't include it in this file
typedef struct _SECURITY_ATTRIBUTES {
    DWORD nLength;
    PVOID lpSecurityDescriptor;
    BOOL bInheritHandle;
} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;

BOOLEAN     Ow2ExitInProcess = (BOOLEAN)FALSE;
HANDLE      hOs2Srv;
ULONG       Ow2bNewSession = 0;
OEM_STRING  Ow2CommandLineString;

HANDLE      CtrlDataSemaphore;
HANDLE      KbdDataSemaphore;
HANDLE      FocusSemaphore;
HANDLE      MouDataSemaphore;
HANDLE      PopUpSemaphore;
HANDLE      ScreenLockSemaphore;
HANDLE      Od2StdHandleLockHandle;
HANDLE      Od2VioWriteSemHandle;

BOOLEAN     Od2ReceivedSignalAtInit = FALSE;
ULONG       Od2InitSignalType;

BYTE    NtInitssFail[] = "OS2SES(ntinitss) - error %X at %s\n";

/*
 *  This is the table of Secions to initialize.
 *
 *  Section
 *      SectionSize - the size of the section (0 - no section, -1 - for LVB)
 *      DataNamePrefix - the prefix added to get section name
 *      SectionDataBaseAddress - where to put the base address of the section
 *      SectionDataHandle - where to put the handle of the section
 */

struct
{
    ULONG    SectionSize;
    WCHAR    DataNamePrefix;
    PVOID    *SectionDataBaseAddress;
    HANDLE   *SectionDataHandle;
} PORT_TABLE[] =
    {
        {
            OS2SES_CTRL_SECTION_SIZE,
            U_OS2_SES_BASE_DATA_PREFIX,
            &Os2SessionCtrlDataBaseAddress,
            &Os2SessionCtrlDataSectionHandle
        },
        {
            OS2SES_GROUP_SECTION_SIZE,
            U_OS2_SES_GROUP_PREFIX,
            &Os2SessionDataBaseAddress,
            &Os2SessionSesGrpDataSectionHandle
        },
        {
            //  Warning: MUST BE THE LAST ENTRY OF THE TABLE

            (ULONG)(-1L),
            U_OS2_SES_BASE_LVB_PREFIX,
            &(PVOID)(LVBBuffer),
            &LVBHandle
        },
        {
            0,              // End of Table
            0,
            NULL,
            NULL
        }
    };

/*
 *  This is a table of semaphores to create (and close).
 *  All (but PauseEvent which is Event) are Semaphores.
 */

struct
{
    WCHAR       NamePrefix;
    HANDLE *    SemaphoreHandle;
} SEMAPHORE_TABLE [] =
{
    {
        U_OS2_SES_CTRL_PORT_SEMAPHORE_PREFIX,
        &CtrlDataSemaphore,
    },
    {
        U_OS2_SES_KBD_PORT_SEMAPHORE_PREFIX,
        &KbdDataSemaphore,
    },
    {
        U_OS2_SES_KBD_FOCUS_SEMAPHORE_PREFIX,
        &FocusSemaphore,
    },
    {
        U_OS2_SES_MOU_PORT_SEMAPHORE_PREFIX,
        &MouDataSemaphore,
    },
    {
        U_OS2_SES_POPUP_SEMAPHORE_PREFIX,
        &PopUpSemaphore,
    },
    {
        U_OS2_SES_SLOCK_SEMAPHORE_PREFIX,
        &ScreenLockSemaphore,
    },
    {
        U_OS2_SES_STD_HANDLE_LOCK_PREFIX,
        &Od2StdHandleLockHandle,
    },
    {
        U_OS2_SES_PAUSE_EVENT_PREFIX,
        &PauseEvent,
    },
    {
        U_OS2_SES_VIOWRITE_SEMAPHORE_PREFIX,
        &Od2VioWriteSemHandle,
    },
    {
        0,
        NULL
    }
};

#if DBG
PSZ SEMAPHORE_NAME_TABLE[] =
    {
        "CtrlDataSemaphore",
        "KbdDataSemaphore",
        "FocusSemaphore",
        "MouDataSemaphore",
        "PopUpSemaphore",
        "ScreenLockSemaphore",
        "PauseEvent",
        "VioWriteSemaphore",
        NULL
    };
#endif

    //
    // A routine that wait for os2srv to connect
    //

NTSTATUS
CtrlListen()
{
    NTSTATUS Status;
    PSCCONNECTINFO ConnectionInfo;
    SCREQUESTMSG ConnectionRequest;
    HANDLE CommPortHandle;


    /*
     * Listen to the os2ss connection and then too all
     * processes created in this session.
     */

    // Non-alertable, indefinite wait listen
    Status = NtListenPort( Ow2hOs2sesPort,
                           (PPORT_MESSAGE) &ConnectionRequest);
    if (!NT_SUCCESS( Status ))
    {
        KdPrint(( NtInitssFail,  Status, "NtListenPort"));
        return Status;
    } else
    {

        // ??? Any reply
        ConnectionInfo = &ConnectionRequest.ConnectionRequest;
        ConnectionInfo->dummy = 0;

        // BUGBUG!
        // ServerView.Length = sizeof(ServerView);
        // ServerView.SectionOffset = 0L;
        // ServerView.ViewSize = 0L;

        Status = NtAcceptConnectPort(
                             & CommPortHandle,
                             NULL,
                             (PPORT_MESSAGE) &ConnectionRequest,
                             (BOOLEAN)TRUE,
                             NULL, // &ServerView,
                             NULL);

        if ( !NT_SUCCESS(Status) )
        {
#if DBG
            KdPrint(( NtInitssFail,  Status, "NtAcceptConnectPort"));
#endif
            return Status;
        } else
        {
            /*
             * Record the view section address in a global variable.
             */
            // BUGBUG! Os2SesConPortBaseAddress = ServerView.ViewBase;
            Status = NtCompleteConnectPort( CommPortHandle );
            ASSERT( NT_SUCCESS( Status) );

            return Status;
        }
    }
}


DWORD
SessionRequestThread(IN PVOID Parameter)
{
    NTSTATUS    Status;

    UNREFERENCED_PARAMETER(Parameter);

    try {
        //
        // Listen and accept session request port
        //

        Status = CtrlListen();
        if ( !NT_SUCCESS( Status ))
        {
#if DBG
            KdPrint(( NtInitssFail,  Status, "CtrlListen"));
            ASSERT( FALSE );
#endif
            Ow2Exit( 0, NULL, 1);
        }

        ServeSessionRequests();
    }
        //
        // if Os2Debug is on, and ntsd is attached, it will get the second chance
        //
#if DBG
    except( (Os2Debug ? Ow2FaultFilter(EXCEPTION_CONTINUE_SEARCH, GetExceptionInformation()):

                        Ow2FaultFilter(EXCEPTION_EXECUTE_HANDLER, GetExceptionInformation())) ) {
#else
    except( Ow2FaultFilter(EXCEPTION_EXECUTE_HANDLER, GetExceptionInformation()) ) {
#endif

#if DBG
        KdPrint(("OS2SES: Internal error - Exception occured in EventServerThread\n"));
#endif
        Ow2DisplayExceptionInfo();
        ExitThread(1L);
    }
    ExitThread(0L);
    return(0L);
}

CONST WCHAR OS2SSInitializationEvent[] = U_OS2_SS_INITIALIZATION_EVENT;

/*
 *  OS2 Session console - protocol with OS2SS
 *
 *               Connect
 *  1. OS2SES  ----------> OS2SRV
 *
 *
 *                Accept
 *  2. OS2SES  <---------- OS2SRV
 *                session
 *
 *
 *            Create process
 *        (also checkport for root process)
 *  3. OS2SES  ----------> OS2SRV
 *              session
 *
 *              /                Connect
 *              |   4. OS2SES <----------  OS2SRV
 * root only by |
 * ServerRequest|
 *              |                Accept
 *              |   5. OS2SES  ----------> OS2SRV
 *              \
 *
 *  Returns:
 *            0L - problem with resources, like memory
 *            -1L - problem connecting to os2srv
 *            01L - OK
 *
 */

DWORD
InitOs2ssSessionPort()
{
    NTSTATUS            Status;
    ULONG               ConnectionInfoLen, i, j, ViewSize = 0L;
    HANDLE              SessionUniqueId;
    OS2SESCONNECTINFO   ConnectionInfo;
    OBJECT_ATTRIBUTES   ObjectAttributes;
    LARGE_INTEGER       SectionSize;
    UNICODE_STRING      Name_U;
    HANDLE              SectionHandle;
    HANDLE              hCurrentProcess;
    HANDLE              hOs2srvProcess = NULL;
    PWSTR               BasePrefix;
    PVOID               PhyKbd;
    REMOTE_PORT_VIEW    ServerView;
    PORT_VIEW           ClientView;
    OS2SESREQUESTMSG    RequestMsg, ReplyMsg;
    WCHAR               SessionName_U[U_OS2_SES_BASE_PORT_NAME_LENGTH];
    SECURITY_QUALITY_OF_SERVICE DynamicQos;
    ULONG       Length;
#if DBG
    BOOLEAN             DebugOnStartup = FALSE;
#endif
    HANDLE              InitialEventHandle;
    DWORD               NonFirstClient;
    DWORD               WaitState;
    SECURITY_ATTRIBUTES Sa;
    CHAR localSecurityDescriptor[SECURITY_DESCRIPTOR_MIN_LENGTH];

    //
    // Create a section to contain the Port Memory.  Port Memory is private
    // memory that is shared between the OS/2 client and server processes.
    // This allows data that is too large to fit into an API request message
    // to be passed to the OS/2 server.
    //
    // Create the client section now, to pass is to the server on the
    // connection request.
    //

    SectionSize.HighPart = 0L;
    SectionSize.LowPart = OD2_PORT_MEMORY_SIZE;
    Status = NtCreateSection( &SectionHandle,
                              SECTION_ALL_ACCESS,
                              NULL,
                              &SectionSize,
                              PAGE_READWRITE,
                              SEC_RESERVE,
                              NULL
                            );
    if (!NT_SUCCESS( Status ))
    {
#if DBG
        KdPrint(( NtInitssFail,  Status, "NtCreateSection"));
        ASSERT( FALSE );
#endif
        return( 0L );
    }

    /*
     * connect to OS2SS and notify of the new session and the port associated
     * with it.
     */

#if DBG
    if ( fBrkOnStart )
    {
        ConnectionInfo.In.SessionDbg = TRUE;
    } else
    {
        ConnectionInfo.In.SessionDbg = FALSE;
    }
#else
    ConnectionInfo.In.SessionDbg = FALSE;
#endif
    ConnectionInfo.In.ExpectedVersion = OS2_SS_VERSION;
    ConnectionInfo.In.Win32ForegroundWindow = Ow2ForegroundWindow;
    ConnectionInfoLen = sizeof(ConnectionInfo);

    RtlInitUnicodeString( &Name_U, U_OS2_SS_SESSION_PORT_NAME );

    //
    // Set up the security quality of service parameters to use over the
    // port.  Use the most efficient (least overhead) - which is dynamic
    // rather than static tracking.
    //

    DynamicQos.ImpersonationLevel = SecurityImpersonation;
    DynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
    DynamicQos.EffectiveOnly = TRUE;

    ClientView.Length = sizeof( ClientView );
    ClientView.SectionHandle = SectionHandle;
    ClientView.SectionOffset = 0;
    ClientView.ViewSize = OD2_PORT_MEMORY_SIZE;
    ClientView.ViewBase = 0;
    ClientView.ViewRemoteBase = 0;

    ServerView.Length = sizeof( ServerView );
    ServerView.ViewSize = 0;
    ServerView.ViewBase = 0;

    // Create security attribute record granting access to all
    Sa.nLength = sizeof(Sa);
    Sa.bInheritHandle = TRUE;

    Status = RtlCreateSecurityDescriptor( (PSECURITY_DESCRIPTOR)
                                          &localSecurityDescriptor,
                                          SECURITY_DESCRIPTOR_REVISION );
    if (!NT_SUCCESS( Status ))
    {
#if DBG
            KdPrint(("OS2SES: failed at RtlCreateSecurityDescriptor %x\n", Status));
        ASSERT(FALSE);
#endif
        return 0L;
    }

    Status = RtlSetDaclSecurityDescriptor( (PSECURITY_DESCRIPTOR)
                                           &localSecurityDescriptor,
                                           (BOOLEAN)TRUE,
                                           (PACL) NULL,
                                           (BOOLEAN)FALSE );

    if (!NT_SUCCESS( Status ))
    {
#if DBG
            KdPrint(("OS2SES: failed at RtlSetDaclSecurityDescriptor %x\n", Status));
        ASSERT(FALSE);
#endif
        return 0L;
    }
    Sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR) &localSecurityDescriptor;

    InitialEventHandle = CreateEventW(
                            &Sa,
                            TRUE,   // Notification event
                            FALSE,  // Nonsignaled
                            OS2SSInitializationEvent
                            );
    if ((NonFirstClient = GetLastError()) || !InitialEventHandle) {
        if (NonFirstClient != ERROR_ALREADY_EXISTS) {
#if DBG
            KdPrint(("OS2SES: Cannot create initialization event, error %d\n", NonFirstClient));
#endif
            return 0L;
        }
    }

    if (!NonFirstClient) {
        Status = (NTSTATUS) CreateOS2SRV(&hOs2srvProcess);
        if (!NT_SUCCESS(Status) || !hOs2srvProcess) {
#if DBG
            KdPrint(("OS2SES: Fail to start server process, status %x\n", Status));
#endif
            return 0L;
        }
        NtClose(hOs2srvProcess);
    }

    //
    // Wait for server. We want to see messages printed in debugger in the case that
    // client waits too much (or may be it will wait forever).
    //

    while (TRUE) {
        WaitState = WaitForSingleObject(
                        InitialEventHandle,
                        (DWORD) 4900L
                        );
        if (WaitState == STATUS_TIMEOUT) {
#ifdef DBG
            KdPrint(("OS2SES: Waiting for server\n"));
#endif
        }
        else {
            break;
        }
    }

    if (WaitState != WAIT_OBJECT_0) {
#if DBG
        KdPrint(("OS2SES: Initialization event wasn't set by the server, wait_state %d\n", WaitState));
#endif
        return (DWORD) -1L;
    }

    CloseHandle(InitialEventHandle);

    Status = NtConnectPort(
                &Ow2hOs2srvPort,
                &Name_U,
                &DynamicQos,              // Security Quality
                &ClientView,
                &ServerView,
                NULL,                     // MaxMessageLength,
                (PVOID) &ConnectionInfo,
                &ConnectionInfoLen
                );

    if (!NT_SUCCESS(Status)) {
#if DBG
        KdPrint(("OS2SES: Fail to connect to port, status %x\n", Status));
#endif
        return (DWORD) -1L;
    }

    NtClose( SectionHandle );

    //
    // Now capture the fact if we were exec'd with the DEBUG command
    //
#if DBG
    if (ConnectionInfo.Out.Od2Debug)
    {
        Os2Debug |= ConnectionInfo.Out.Od2Debug;
        DebugOnStartup = TRUE;
    }
#endif

    Od2PortMemoryRemoteDelta = (ULONG)ClientView.ViewRemoteBase -
                               (ULONG)ClientView.ViewBase;

#if DBG
    IF_OD2_DEBUG( LPC )
    {
        KdPrint(( "OS2: ClientView: Base=%lX  RemoteBase=%lX  Delta: %lX  Size=%lX\n",
                  (ULONG)ClientView.ViewBase, (ULONG)ClientView.ViewRemoteBase,
                  Od2PortMemoryRemoteDelta, (ULONG)ClientView.ViewSize
                ));
    }
#endif

    SessionUniqueId = (HANDLE)(ConnectionInfo.Out.SessionUniqueID);
    Ow2bNewSession = ConnectionInfo.Out.IsNewSession;

    Od2PortHeap = RtlCreateHeap( 0,
                                 ClientView.ViewBase,
                                 ClientView.ViewSize,
                                 0,
                                 0,
                                 0
                               );
    if (Od2PortHeap == NULL)
    {
#if DBG
        KdPrint(( NtInitssFail,  Status, "RtlCreateHeap"));
        ASSERT( FALSE );
#endif
        return 0L;
    }

    hOs2Srv = OpenProcess(
                        PROCESS_DUP_HANDLE,
                        FALSE, // no inherit
                        (DWORD)(ConnectionInfo.Out.Os2SrvId));
    if (!hOs2Srv){
#if DBG
        KdPrint(( NtInitssFail,  GetLastError(), "OpenProcess"));
#endif
        return((DWORD)-1L);
    }

#if DBG
    if ( fVerbose )
    {
        KdPrint(("OS2SES: id =%d, Unique id=%d\n",
                ConnectionInfo.Out.ProcessUniqueID, (ULONG)SessionUniqueId));
    }
#endif

    CONSTRUCT_U_OS2_SES_NAME(SessionName_U, U_OS2_SES_BASE_PORT_PREFIX, (ULONG)SessionUniqueId);
    RtlInitUnicodeString( &Name_U, SessionName_U );

    /*
     * used later to fix to the data section/port name
     */

    BasePrefix = &Name_U.Buffer[sizeof(U_OS2_SES_BASE_PORT_NAME) / 2];

#if DBG
    IF_OD2_DEBUG( OS2_EXE )
    {
        PRTL_USER_PROCESS_PARAMETERS ProcessParameters =
            (NtCurrentPeb())->ProcessParameters;

        KdPrint(( "OS2SES(Win-Handles): hConsole %lx, StdIn %lx, StdOut %lx, StdErr %lx\n",
            ProcessParameters->ConsoleHandle,
            ProcessParameters->StandardInput,
            ProcessParameters->StandardOutput,
            ProcessParameters->StandardError ));
    }
#endif

    /*
     *  Create 7 NT Semaphore and 1 Event that will be used to:
     *
     *  1. mutex use of CtrlDataSection
     *  2. mutex use of KbdDataSection
     *  3. focus the Kbd-handle
     *  4. mutex enable PopUp
     *  5. mutex screen lock
     *  6. Pause event
     *  7. mutex read Mouse Event
     */

    for ( i = 0, j = 0 ; SEMAPHORE_TABLE[i].NamePrefix ;)
    {
        *BasePrefix = SEMAPHORE_TABLE[i].NamePrefix;

        InitializeObjectAttributes( &ObjectAttributes,
                                    &Name_U,
                                    OBJ_CASE_INSENSITIVE,
                                    NULL,
                                    NULL);

        if ( OS2SS_IS_SESSION( Ow2bNewSession ))
        {
            if (*BasePrefix == U_OS2_SES_PAUSE_EVENT_PREFIX)
            {
                Status = NtCreateEvent( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                        EVENT_ALL_ACCESS,
                                        &ObjectAttributes,
                                        1,
                                        (BOOLEAN)1);
            } else if ((*BasePrefix == U_OS2_SES_VIOWRITE_SEMAPHORE_PREFIX) ||
                       (*BasePrefix == U_OS2_SES_KBD_PORT_SEMAPHORE_PREFIX) ||
                       (*BasePrefix == U_OS2_SES_MOU_PORT_SEMAPHORE_PREFIX))
            {
                Status = NtCreateMutant( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                         MUTANT_ALL_ACCESS,
                                         &ObjectAttributes,
                                         FALSE);    // not owned
            } else
            {
                Status = NtCreateSemaphore( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                            SEMAPHORE_ALL_ACCESS,
                                            &ObjectAttributes,
                                            1,
                                            1);
            }
        } else
        {
            if (*BasePrefix == U_OS2_SES_PAUSE_EVENT_PREFIX)
            {
                Status = NtOpenEvent( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                      EVENT_ALL_ACCESS,
                                      &ObjectAttributes);
            } else if ((*BasePrefix == U_OS2_SES_VIOWRITE_SEMAPHORE_PREFIX) ||
                       (*BasePrefix == U_OS2_SES_KBD_PORT_SEMAPHORE_PREFIX) ||
                       (*BasePrefix == U_OS2_SES_MOU_PORT_SEMAPHORE_PREFIX))
            {
                Status = NtOpenMutant( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                       MUTANT_ALL_ACCESS,
                                       &ObjectAttributes);
            } else
            {
                Status = NtOpenSemaphore( SEMAPHORE_TABLE[i].SemaphoreHandle,
                                          SEMAPHORE_ALL_ACCESS,
                                          &ObjectAttributes);
            }
        }

        if ( ! NT_SUCCESS( Status ) )
        {
            if (i==0 && OS2SS_IS_SESSION( Ow2bNewSession ) &&
                Status == STATUS_OBJECT_NAME_COLLISION )
            {
                //
                // A previous sesssion with the same cookie is being cleaned up - wait
                // 30 seconds (600 times 50 ms is 30 seconds)
                //
                j++;
                if (j<600)
                {
#if DBG
                    if (j == 1)
                    {
                        KdPrint(( "OS2SES(ntinitss) - waiting for previous os2 session cleanup\n"));
                    }
                    else {
                        KdPrint(( "."));

                    }
#endif
                    Sleep(50L);
                    continue;
                }

            }
#if DBG
            KdPrint(( "OS2SES(ntinitss) - error %X at NtCreate/OpenSemaphore/Event #%u (%c-%s)\n",
                    Status, i, SEMAPHORE_TABLE[i].NamePrefix, SEMAPHORE_NAME_TABLE[i]));
            ASSERT( FALSE );
#endif
            return(0L);
        }
            //
            // increment i only if the create/open above was successful
            //
        i++;
    }

    /*
     * Map the the whole section to virtual address.
     * Let MM locate the view.
     *
     * The Vio case is different, since it has to be mapped
     * into app space below 512M
     */

    Os2SessionCtrlDataBaseAddress = 0L;
    Os2SessionDataBaseAddress = 0L;
    LVBBuffer = (PUCHAR)(VIOSECTION_BASE);

    for ( i = 0 ; PORT_TABLE[i].SectionSize ; i++ )
    {

        /*
         * create 3 sections to be shared by all client processes runing in this
         * session: Ctrl, SesGrp & LVB.
         */

        //
        //   Only for LVB the section size is unknon until OS2 is started.
        //   For the LVB< there is -1 at the SectionSize field in the table
        //

        if ( PORT_TABLE[i].SectionSize != -1L )
        {
            SectionSize.LowPart = PORT_TABLE[i].SectionSize;
        } else
        {
            //
            //   This is the time to continue initiliziation: SesGrp
            //   is available.
            //

            SesGrp = (POS2_SES_GROUP_PARMS)Os2SessionDataBaseAddress;
            PortMessageHeaderSize = sizeof(PORT_MESSAGE);

            if (OS2SS_IS_PROCESS( Ow2bNewSession ))
            {
                if ((PhyKbd = StartEventHandler()) == NULL)
                {
                    return(0);
                }
            } else
            {
                RtlZeroMemory(SesGrp, sizeof(OS2_SES_GROUP_PARMS));
                if ((PhyKbd = StartEventHandlerForSession()) == NULL)
                {
                    return(0);
                }
                SesGrp->PhyKbd = PhyKbd;
            }
            SectionSize.LowPart = SesGrp->MaxLVBsize;         // LVB Buffer
        }

        ViewSize = 0L;

        /*
         * Get a private ID for the session data.
         */

        *BasePrefix = PORT_TABLE[i].DataNamePrefix;

        /*
         * create a 64k section.
         * BUGBUG! - cruiser apis allow io of more then 64k
         */

        if (OS2SS_IS_SESSION( Ow2bNewSession ))
        {
            SECURITY_DESCRIPTOR SecurityDescriptor;

            Status = RtlCreateSecurityDescriptor( &SecurityDescriptor,
                                                  SECURITY_DESCRIPTOR_REVISION );
            if (!NT_SUCCESS(Status)) {
#if DBG
                KdPrint(( NtInitssFail,  Status, "RtlCreateSecurityDescriptor"));
                ASSERT( FALSE );
#endif
                return(0L);
            }

            Status = RtlSetDaclSecurityDescriptor( &SecurityDescriptor,
                                                   TRUE,
                                                   NULL,
                                                   FALSE );
            if (!NT_SUCCESS(Status)) {
#if DBG
                KdPrint(( NtInitssFail,  Status, "RtlSetDaclSecurityDescriptor"));
                ASSERT(FALSE);
#endif
                return(0L);
            }

            InitializeObjectAttributes(&ObjectAttributes,
                                       &Name_U,
                                       OBJ_CASE_INSENSITIVE,
                                       NULL,
                                       &SecurityDescriptor);

            Status = NtCreateSection ( &SectionHandle,
                                       /* BUGBUG! SECTION_ALL_ACCESS, */ SECTION_MAP_WRITE,
                                       &ObjectAttributes,
                                       &SectionSize,
                                       PAGE_READWRITE,
                                       SEC_COMMIT,
                                       NULL);
        } else
        {
            InitializeObjectAttributes(&ObjectAttributes,
                                       &Name_U,
                                       OBJ_CASE_INSENSITIVE,
                                       NULL,
                                       NULL);

            Status = NtOpenSection (   &SectionHandle,
                                       SECTION_MAP_WRITE,
                                       &ObjectAttributes);

        }

        if ( !NT_SUCCESS( Status ) )
        {
#if DBG
            KdPrint(( NtInitssFail,  Status, "NtCreate/OpenSection"));
            ASSERT( FALSE );
#endif
            return(0L);
        }

        //
        // Map the the whole section to virtual address.
        //

        Status = NtMapViewOfSection( SectionHandle,
                                     NtCurrentProcess(),
                                     PORT_TABLE[i].SectionDataBaseAddress,
                                     0L,
                                     0L,
                                     NULL,
                                     &ViewSize,
                                     ViewUnmap,
                                     0L,
                                     PAGE_READWRITE);

        if ( !NT_SUCCESS( Status ) )
        {
#if DBG
            KdPrint(( NtInitssFail,  Status, "NtMapViewOfSection"));
            ASSERT( FALSE );
#endif
            return(0L);
        }

        *(PORT_TABLE[i].SectionDataHandle) = SectionHandle;
    }

    OpenLVBsection();

    Ow2hOs2sesPort = NULL;

    //
    // Connect to the OS/2 Emulation Subsystem server. Also pass information
    // the OS/2 server needs in the connection information structure.
    //

    /*
     * Set Header info
     */

    PORT_MSG_DATA_LENGTH(RequestMsg) = sizeof(RequestMsg) - sizeof(PORT_MESSAGE);
    PORT_MSG_TOTAL_LENGTH(RequestMsg) = sizeof(RequestMsg);   // BUGBUG! too much
    PORT_MSG_ZERO_INIT(RequestMsg) = 0L;

    RequestMsg.PortType = 1;
    RequestMsg.Request = SesConCreate;
    RequestMsg.d.Create.d.In.IsNewSession = Ow2bNewSession;

    if ( !Od2InitCreateProcessMessage(&RequestMsg.d.Create) )
    {
#if DBG
        KdPrint(( NtInitssFail,  Status, "Od2InitCreateProcessMessage"));
#endif
        return(0L);
    }

    /*
     * for all request pass the session handle
     */

    RequestMsg.Session = SessionUniqueId;

    strncpy(&RequestMsg.d.Create.d.In.ApplName[0],
            Od2PgmFilePath,
            OS2_MAX_APPL_NAME
           );
    RequestMsg.d.Create.d.In.ApplName[OS2_MAX_APPL_NAME - 1] = '\0';

    //  server doesn't need the LPC, except for
    //  the root process in the session, so we'll create port only for it.

    if (OS2SS_IS_SESSION( Ow2bNewSession ))
    {
        CHAR sd[SECURITY_DESCRIPTOR_MIN_LENGTH];
        PSECURITY_DESCRIPTOR psd = (PSECURITY_DESCRIPTOR)&sd;

        *BasePrefix = U_OS2_SES_BASE_PORT_PREFIX;

        /*
         * create (session) LPC port to be connected to all client processes
         * runing in this session for Kbd, Mou, Mon, Tm and Prt.
         */

        InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
        SetSecurityDescriptorDacl(psd, TRUE, NULL, FALSE); // NULL DACL means access free to all

        InitializeObjectAttributes(&ObjectAttributes,
                                   &Name_U,
                                   OBJ_CASE_INSENSITIVE,
                                   NULL,
                                   psd);

        Status = NtCreatePort( &Ow2hOs2sesPort,
                               &ObjectAttributes,
                               sizeof( SCCONNECTINFO ),
                               U_OS2_SES_BASE_PORT_PREFIX,
                               32 * U_OS2_SES_BASE_PORT_PREFIX);

        if ( ! NT_SUCCESS( Status ) )
        {
#if DBG
            KdPrint(( NtInitssFail,  Status, "NtCreatePort"));
            ASSERT( FALSE );
#endif
            return(0L);
        }

        //
        // create 2 threads: one to read input from console
        // and second to server requsets thru LPC
        //

        if (timing)
        {
            printf("Os2 time before CreateServerThreads is %d\n", (GetTickCount()) - timing);
        }
        if (CreateServerThreads())
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "CreateServerThread"));
            ASSERT( FALSE );
#endif
            return(0L);
        }

        /*
         * Set request info
         */

        RequestMsg.Request = SesCheckPortAndConCreate;

        //
        //  duplicate handles for os2srv,
        //

        if (timing)
        {
            printf("Os2 time before DuplicateHandle is %d\n", (GetTickCount()) - timing);
        }
        hCurrentProcess = GetCurrentProcess();
            //
            // duplicate process and thread handles for os2srv
            //

        if (OS2SS_IS_NEW_SESSION( Ow2bNewSession ))
        {
            /*
             * a new session, which isn't a child session
             */

            if (!DuplicateHandle(
                        hCurrentProcess,
                        hCurrentProcess,
                        hOs2Srv,
                        &RequestMsg.d.Create.d.In.hProcess,
                        0,
                        FALSE,
                        DUPLICATE_SAME_ACCESS
                           ))
            {
#if DBG
                KdPrint(( NtInitssFail,  GetLastError(), "DuplicateHandle(Process)"));
#endif
            }
            if (!DuplicateHandle(
                        hCurrentProcess,
                        GetCurrentThread(),
                        hOs2Srv,
                        &RequestMsg.d.Create.d.In.hThread,
                        0,
                        FALSE,
                        DUPLICATE_SAME_ACCESS
                           ))
            {
#if DBG
                KdPrint(( NtInitssFail,  GetLastError(), "DuplicateHandle(Thread)"));
#endif
            }
        }
            //
            // Duplicate the event thread handle, so os2srv can debug it
            //
        if (!DuplicateHandle(
                    hCurrentProcess,
                    EventServerThreadHandle,
                    hOs2Srv,
                    &RequestMsg.d.Create.d.In.hEventThread,
                    0,
                    FALSE,
                    DUPLICATE_SAME_ACCESS
                       ))
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "DuplicateHandle(EventServerThread)"));
#endif
        }
            //
            // Duplicate the session request thread handle, so os2srv can debug it
            //
        if (!DuplicateHandle(
                    hCurrentProcess,
                    Ow2hSessionRequestThread,
                    hOs2Srv,
                    &RequestMsg.d.Create.d.In.hSessionRequestThread,
                    0,
                    FALSE,
                    DUPLICATE_SAME_ACCESS
                       ))
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "DuplicateHandle(SessionRequestTread)"));
#endif
        }
    }

    Status = NtRequestWaitReplyPort( Ow2hOs2srvPort,
                                     (PPORT_MESSAGE) &RequestMsg,
                                     (PPORT_MESSAGE) &ReplyMsg);

    if ( !NT_SUCCESS( Status ))
    {
#if DBG
        KdPrint(( NtInitssFail,  Status, "NtRequestWaitReplyPort"));
        ASSERT( FALSE );
#endif
        return((DWORD)-1L );
    }

    if (!NT_SUCCESS(ReplyMsg.Status)){
       //
       // Could not initiate with os2srv - exit
       //
#if DBG
       KdPrint(( NtInitssFail,  ReplyMsg.Status, "Os2ConCreate"));
#endif
       return( (DWORD)-1L );
    }

    //
    // Now capture the fact if we were exec'd with the DEBUG command
    //
#if DBG
    if (DebugOnStartup)
    {
        DbgBreakPoint();
    }
#endif

    Od2HandleCreateProcessRespond(&ReplyMsg.d.Create);

    Ow2hSession = SessionUniqueId;

    if (OS2SS_IS_SESSION( Ow2bNewSession ))
    {
        /*
         *  Complete all initializations that are depend on SesGrp from os2srv
         */

        if (timing)
        {
            printf("Os2 time before KbdInitAfterSesGrp is %d\n", (GetTickCount()) - timing);
        }
        if( KbdInitAfterSesGrp() )
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "KbdInitAfterSesGrp"));
            ASSERT( FALSE );
#endif
            return( 0L );
        }

        /*
         *  Complete NLS initialization
         */

        if (timing)
        {
            printf("Os2 time before NlsInit is %d\n", (GetTickCount()) - timing);
        }
        if( NLSInit() )
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "NlsInit"));
            ASSERT( FALSE );
#endif
            return( 0L );
        }

        //
        // resume remaining threads
        //

        if (timing)
        {
            printf("Os2 time before ResumeServerThreads is %d\n", (GetTickCount()) - timing);
        }
        if (ResumeServerThreads())
        {
#if DBG
            KdPrint(( NtInitssFail,  GetLastError(), "ReleaseServerThreads"));
            ASSERT( FALSE );
#endif
            return( 0L );
        }
    }

    Length = SesGrp->LVBsize;
    Ow2VioGetLVBBuf(&Length);

    /*
     *  Don't put parameters into SesGrp before this point
     *  (since it's clear by the server when coping the NLS definitions).
     */

    return ( 1L );
}


VOID TerminateSession(VOID)
{
    //NTSTATUS Status;

    Ow2ExitInProcess = (BOOLEAN)TRUE;
    if (timing)
    {
        printf("Os2 main->Exit time is %d\n", (GetTickCount()) - timing);
    }

    /*
     * remove event handler so we don't try to send a message
     * for a dying session.
     */

    SetEventHandlers( FALSE );

    if (OS2SS_IS_SESSION( Ow2bNewSession ))
    {
        RestoreWin32ParmsBeforeTermination();
    }

    // Close the named objects: port, section

    NtClose(Ow2hOs2srvPort);
    NtClose(Os2SessionCtrlDataSectionHandle);
    NtClose(Os2SessionSesGrpDataSectionHandle);
    NtClose(Ow2hOs2sesPort);
    NtClose(hOs2Srv);
    // Jul-2-1995 YosefD:
    // Od2PortHandle is the same value as Ow2hOs2srvPort. This handle is already closed. An
    // attemt to close it once more is a bug. On build 1096 this cause exception and
    // process termination. The return value of the terminated process isn't right in this
    // case.
    //NtClose(Od2PortHandle);

    /* =>? what about the following handles:

HANDLE  hConsoleInput;
HANDLE  hConsoleOutput;
HANDLE  hConsoleStdIn;      if diff from hConsoleInput
HANDLE  hConsoleStdOut;     if diff from hConsoleOutput
HANDLE  hConsoleStdErr;
HANDLE  hPopUpOutput;       if not NULL
HANDLE  LVBHandle;
HANDLE  PauseEvent;
HANDLE  CtrlDataSemaphore;
HANDLE  KbdDataSemaphore;
HANDLE  FocusSemaphore;
HANDLE  MouDataSemaphore;
HANDLE  PopUpSemaphore;
HANDLE  ScreenLockSemaphore;
HANDLE  EventServerThreadHandle;
HANDLE  Ow2hSessionRequestThread;

    */
    // notify OS2SS

    // Cleanup TaskMan stuff

}


BOOL
SendSignalToOs2Srv(
    IN int SignalType
    )
{
    OS2SESREQUESTMSG  RequestMsg;
    OS2SESREQUESTMSG  ReplyMsg;
    NTSTATUS Status;

#if DBG
    IF_OD2_DEBUG2( OS2_EXE, SIG )
    {
        KdPrint(("OS2SES(SendSignalToOs2Srv):  Signal %u\n", SignalType));
    }
#endif
    /*
     * Set Header info
     */
    PORT_MSG_DATA_LENGTH(RequestMsg) = sizeof(RequestMsg) - sizeof(PORT_MESSAGE);
    PORT_MSG_TOTAL_LENGTH(RequestMsg) = sizeof(RequestMsg);   // BUGBUG! too much
    PORT_MSG_ZERO_INIT(RequestMsg) = 0L;
    RequestMsg.PortType = 1;

    RequestMsg.Request = SesConSignal;
    RequestMsg.Session = Ow2hSession;
    RequestMsg.d.Signal.Type = SignalType;

    Status = NtRequestWaitReplyPort( Ow2hOs2srvPort,
                                     (PPORT_MESSAGE) &RequestMsg,
                                     (PPORT_MESSAGE) &ReplyMsg);

    if ( !NT_SUCCESS( Status ))
    {
#if DBG
            KdPrint(( "OS2SES: Unable to send signal - Status == %X\n",
                      Status));
#endif

        TerminateSession();
        Ow2Exit(0, NULL, 15);
    }

    ASSERT ( PORT_MSG_TYPE(ReplyMsg) == LPC_REPLY );

    if ( Ow2ExitInProcess )
    {
        return FALSE;
    }

    return TRUE;
}

BOOL
EventHandlerRoutine (IN ULONG CtrlType)
{
    int     SignalType;
    BOOL    Rc;
    ULONG   i=0;

        //
        // If Ow2hSession is null - os2srv is not ready yet to kill,
        // wait so we don't loose the signal
        //

    if (!Od2SignalEnabled)
    {
#if DBG
        IF_OD2_DEBUG2( OS2_EXE, SIG )
        {
            KdPrint(("OS2SES(EventHandlerRoutine):  Ctr-Type %u before loading completed, handle later\n", CtrlType));
        }
#endif
        Od2ReceivedSignalAtInit = TRUE;
        Od2InitSignalType = CtrlType;
        return(TRUE);
    }

#if DBG
    IF_OD2_DEBUG2( OS2_EXE, SIG )
    {
        KdPrint(("OS2SES(EventHandlerRoutine):  Ctr-Type %u\n", CtrlType));
    }
#endif

    switch (CtrlType) {
        case CTRL_C_EVENT:
            if ((SesGrp->ModeFlag & 1 ) ||            // ^C  in binary mode
                SesGrp->WinProcessNumberInSession )   // There is a child Win process
            {
                return (TRUE);
            }
            SignalType = XCPT_SIGNAL_INTR;
            break;
        case CTRL_BREAK_EVENT:
            if (SesGrp->WinProcessNumberInSession )   // There is a child Win process
            {
                return (TRUE);
            }
            SignalType = XCPT_SIGNAL_BREAK;
            break;
        case CTRL_CLOSE_EVENT:
            SignalType = XCPT_SIGNAL_KILLPROC;
#if PMNT
CloseApp:
            //
            // PM apps handling
            //
            if (ProcessIsPMProcess())
            {
                // Regular app (i.e. not PMShell)
                if (!ProcessIsPMShell())
                {
                    if (Ow2WriteBackCloseEvent())
                    {
                        Sleep(7900L);
                        if (Ow2ExitInProcess)
                            return(FALSE);
                        else
                            return(TRUE);
                    }
                    else
                    {
                        // We failed to write-back a close event:
                        //  must be DosExecPgm proc; Pass event through semaphore
                        DosSemClear(PMSubprocSem32);
                        Sleep(7900L);
                        if (Ow2ExitInProcess)
                            return(FALSE);
                        else
                            return(TRUE);
                    }
                    return(TRUE);
                }
                else // PMSHELL
                {
                    if (Ow2WriteBackCloseEvent())
                    {
                        return(TRUE);
                    }
                }
           }
#endif // PMNT
            break;
        case CTRL_LOGOFF_EVENT:
            if (fService) // Are we running as a service ?
            {
#if DBG
                DbgPrint("OS2: service - ignoring CTRL_LOGOFF_EVENT !\n");
#endif
                return FALSE;
            }
            SignalType = XCPT_SIGNAL_KILLPROC;
#if PMNT
            // Jump to CloseApp only for PM apps !
            if (ProcessIsPMProcess())
                goto CloseApp;
#endif // PMNT
            break;
        case CTRL_SHUTDOWN_EVENT:
            SignalType = XCPT_SIGNAL_KILLPROC;
#if PMNT
            // Jump to CloseApp only for PM apps !
            if (ProcessIsPMProcess())
                goto CloseApp;
#endif // PMNT
            break;
        default:
#if DBG
            IF_OD2_DEBUG2( OS2_EXE, SIG )
            {
                KdPrint(("OS2SES(EventHandlerRoutine): Unknown CtrlType %lu\n", CtrlType));
            }
#endif
            SignalType = CtrlType;
            break;
    }
    //if (OS2SS_IS_SESSION( Ow2bNewSession )){
    if (SesGrp->InTermination == 0){

        //
        // The root process of a session handles singals with os2srv
        // always. child processes in the session only need to send
        // the signal if they where in initialization when a CtrlC/CtrlBrk
        // happened (server ignore signal in this case)
        //

        Rc = SendSignalToOs2Srv(SignalType);
    } else{
        Rc = TRUE;
    }

    if (Rc && ((CtrlType == CTRL_CLOSE_EVENT) ||
               (CtrlType == CTRL_LOGOFF_EVENT) ||
               (CtrlType == CTRL_SHUTDOWN_EVENT)))
    {
        //
        //  Cmd wait 10 sec before it popup a time-out fail message
        //  We are waiting 5 sec to give the application (client)
        //  time to clean-up before we return TRUE.

        Sleep(5000);
        if ( Ow2ExitInProcess )
        {
            Rc = FALSE;
        }
    } else if (Rc && SesGrp->InTermination && Od2SignalEnabled) {
        Rc = SendSignalToOs2Srv(SignalType);
    }

    return (Rc);
}


ULONG
Ow2GetProcessIdFromLPCMessage(
    IN  PVOID   LPCMessage
    )
{
    return((ULONG) ((PPORT_MESSAGE)LPCMessage)->ClientId.UniqueProcess);
}


DWORD
Ow2CommandLineWToCommandLineA(
    IN  LPWSTR  CommandLineW,
    OUT PSZ     *CommandLineA
    )
{
    UNICODE_STRING  CommandLine_U;
    UNICODE_STRING  CommandLineUpcase;
    UNICODE_STRING  CurrentDir_U;
    UNICODE_STRING  CurrentDirUpcase;
    WCHAR           CurrentDirectory[MAX_PATH];
    WCHAR           *pWchar;
    NTSTATUS        Status;

    RtlInitUnicodeString(
                         &CommandLine_U,
                         CommandLineW
                        );

    if (GetCurrentDirectoryW((DWORD)MAX_PATH, CurrentDirectory))
    {
        RtlInitUnicodeString(
                             &CurrentDir_U,
                             CurrentDirectory
                            );

        //
        // Prepare the structures for upcase strings and then upcase.
        //
        if (NULL == (CommandLineUpcase.Buffer =
                RtlAllocateHeap(RtlProcessHeap(), 0, CommandLine_U.MaximumLength))) {
            KdPrint(( NtInitssFail,  0, "RtlAllocateHeap(CommandLine)"));
            return ((DWORD)-1L);
        }
        if (NULL == (CurrentDirUpcase.Buffer =
                RtlAllocateHeap(RtlProcessHeap(), 0, CurrentDir_U.MaximumLength))) {
            KdPrint(( NtInitssFail,  0, "RtlAllocateHeap(CurrentDir)"));
            RtlFreeHeap(RtlProcessHeap(), 0, CommandLineUpcase.Buffer);
            return ((DWORD)-1L);
        }

        CurrentDirUpcase.Length = CurrentDir_U.Length;
        CurrentDirUpcase.MaximumLength = CurrentDir_U.MaximumLength;
        Status = RtlUpcaseUnicodeString(&CurrentDirUpcase, &CurrentDir_U, FALSE);
        if (!NT_SUCCESS(Status)) {
            KdPrint(( NtInitssFail, 0, "RtlUpcaseUnicodeString(CurrentDirUpcase)"));
            RtlFreeHeap(RtlProcessHeap(), 0, CommandLineUpcase.Buffer);
            RtlFreeHeap(RtlProcessHeap(), 0, CurrentDirUpcase.Buffer);
            return ((DWORD)-1L);
        }

        CommandLineUpcase.Length = CommandLine_U.Length;
        CommandLineUpcase.MaximumLength = CommandLine_U.MaximumLength;
        Status = RtlUpcaseUnicodeString(&CommandLineUpcase, &CommandLine_U, FALSE);
        if (!NT_SUCCESS(Status)) {
            KdPrint(( NtInitssFail, 0, "RtlUpcaseUnicodeString(CommandLineUpcase)"));
            RtlFreeHeap(RtlProcessHeap(), 0, CommandLineUpcase.Buffer);
            RtlFreeHeap(RtlProcessHeap(), 0, CurrentDirUpcase.Buffer);
            return ((DWORD)-1L);
        }

        //
        // Initialize pWchar and append NULL to the end of the upcased strings
        // for the search of wsstr().
        //
        pWchar = CommandLineUpcase.Buffer;
        CurrentDirUpcase.Buffer[CurrentDirUpcase.Length / 2] = (WCHAR)NULL;
        CommandLineUpcase.Buffer[CommandLineUpcase.Length / 2] = (WCHAR)NULL;

        //
        // Replace every prefix of the current directory in the command
        // line with what GetCurrentDirectoryW() returned. This resolves
        // problems caused by apps transfering the command line in
        // apper/lower case.
        //
        while ((pWchar < &CommandLineUpcase.Buffer[CommandLineUpcase.Length / 2]) &&
               (pWchar = wcsstr(pWchar, CurrentDirUpcase.Buffer))) {
            wcsncpy(&CommandLine_U.Buffer[pWchar - CommandLineUpcase.Buffer],
                      CurrentDirectory,
                    CurrentDirUpcase.Length / 2);
            pWchar += CurrentDirUpcase.Length / 2;
        }

        //
        // Free the allocated buffers.
        //
        RtlFreeHeap(RtlProcessHeap(), 0, CommandLineUpcase.Buffer);
        RtlFreeHeap(RtlProcessHeap(), 0, CurrentDirUpcase.Buffer);
    }

    Status = RtlUnicodeStringToOemString(
                                          &Ow2CommandLineString,
                                          &CommandLine_U,
                                          TRUE
                                         );
    if (!NT_SUCCESS( Status ))
    {
        KdPrint(( NtInitssFail,  Status, "CommandLine-UnicodeToOemString"));
        return ((DWORD)-1L);
    }

    *CommandLineA = Ow2CommandLineString.Buffer;

    return(0L);
}
#if PMNT


// Returns true if process is the root process of a session
// otherwise returns false
// This procedure was written to enable 16 Bit dll init routine (PMWIN)
// to check if the it is executed from the root process of a session.
APIRET PMNTIsSessionRoot()
{

    if ( OS2SS_IS_SESSION( Ow2bNewSession ))
        return(TRUE);
    else
        return(FALSE);
}

#endif // PMNT