summaryrefslogtreecommitdiffstats
path: root/private/os2/server/os2srv.c
blob: 3aed1aca6037cbd58b7789f9320867f4009a0c73 (plain) (blame)
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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    os2srv.c

Abstract:

    This is the main startup module for the OS/2 Emulation Subsystem Server

Author:

    Steve Wood (stevewo) 22-Aug-1989

Environment:

    User Mode Only

Revision History:

--*/

#include "os2srv.h"
#include "os2win.h"
#define NTOS2_ONLY
#include "sesport.h"
#include <winerror.h>

ULONG
GetKeyboardRegistryChange(
    VOID
    );

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

BOOL
SetEvent(
    HANDLE hEvent
    );

// 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;

// Flag to let OS2SRV know whether or not to ignore LOGOFF (when started as a service)
BOOLEAN fService = FALSE;

int __cdecl
main(
    IN ULONG argc,
    IN PCH argv[],
    IN PCH envp[],
    IN ULONG DebugFlag OPTIONAL
    )
{
    LARGE_INTEGER   TimeOut;
    PLARGE_INTEGER  pTimeOut;
    NTSTATUS        Status;
    HANDLE          SmPort;
    UNICODE_STRING  Os2Name;
    SCREQUESTMSG    Request;
    ULONG           Rc, i;
    HANDLE          InitialEventHandle;
    SECURITY_ATTRIBUTES Sa;
    CHAR localSecurityDescriptor[SECURITY_DESCRIPTOR_MIN_LENGTH];

    if ((argc > 1) && (!_stricmp(argv[1], "/S")))
    {
        fService = TRUE;
    }

    // Check whether Os2SSService environment variable is set
    if (!fService)
    {
        char TmpBuffer[256];

        if (GetEnvironmentVariableA(
            "Os2SSService",
            &TmpBuffer[0],
            256))
        {
            // non-zero return code means variable was found
            fService = TRUE;
        }
    }
    else
    {
        if (!SetEnvironmentVariableA(
            "Os2SSService",
            "1"))
        {
#if DBG
            KdPrint(("OS2SRV: failed to SetEnvironment variable Os2SSService, error=%x\n",
                GetLastError()));
#endif
        }
    }

    UNREFERENCED_PARAMETER(DebugFlag);

    environ = envp;

    //
    // Create Win32 event to ensure that there is only one os2srv in system.
    //

    if (CreateEventW(
            NULL,
            TRUE,   // Notification event
            FALSE,  // Nonsignaled
            L"OS2SRVONLY1EVENT"
            )) {
        if (GetLastError() == ERROR_ALREADY_EXISTS) {
#if DBG
            KdPrint(( "OS2SRV: Unable to initialize server.  Another server exists\n"));
#endif
            ExitProcess(1);
        }
    }
    else {
#if DBG
            KdPrint(( "OS2SRV: Unable to initialize server - failed to create first event, error=%d\n",
                GetLastError()));
#endif
            ExitProcess(1);
    }

    // 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(("OS2SRV: failed at RtlCreateSecurityDescriptor %x\n", Status));
        ASSERT(FALSE);
#endif
        ExitProcess(1);
    }

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

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

    //
    // Try to open handle to event that was created by the 1st client. If the
    // server was invoked before clients it will create the event.
    //
    InitialEventHandle = CreateEventW(
                            &Sa,
                            TRUE,   // Notification event
                            FALSE,  // Nonsignaled
                            U_OS2_SS_INITIALIZATION_EVENT
                            );

    if (!InitialEventHandle) {
#if DBG
        KdPrint(("OS2SRV: Fail to open initialization event, error %d\n", GetLastError()));
#endif
        ExitProcess(1);
    }

    Status = SmConnectToSm(NULL,NULL,0,&SmPort);
    if ( NT_SUCCESS(Status) ) {
        RtlInitUnicodeString(&Os2Name,L"Os2");
        SmLoadDeferedSubsystem(SmPort,&Os2Name);
        }

    Status = Os2Initialize();

    //
    // Notify all clients that server is up and running.
    //

    if (!SetEvent(InitialEventHandle)) {
#if DBG
        KdPrint(("OS2SRV: Fail to set initialization event, error %d\n", GetLastError()));
#endif
    }

    if (!NT_SUCCESS( Status )) {
#if DBG
        KdPrint(( "OS2SRV: Unable to initialize server.  Status == %X\n",
                  Status
                ));
#endif

        NtTerminateProcess( NtCurrentProcess(), Status );
    }

    Request.Request = KbdRequest;
    Request.d.Kbd.Request = KBDNewCountry;

    PORT_MSG_TOTAL_LENGTH(Request) = sizeof(SCREQUESTMSG);
    PORT_MSG_DATA_LENGTH(Request) = sizeof(SCREQUESTMSG) - sizeof(PORT_MESSAGE);
    PORT_MSG_ZERO_INIT(Request) = 0L;

    while ( TRUE )
    {
        Rc = GetKeyboardRegistryChange();

        if (Rc == 0)
        {
            break;
        }

        Request.d.Kbd.d.CodePage = Rc;

        for ( i = 1 ; (i < OS2_MAX_SESSION) ; i++ )
        {
            if (SessionTable[i].Session)
            {
                NtRequestPort(
                              ((POS2_SESSION)SessionTable[i].Session)->ConsolePort,
                              (PPORT_MESSAGE) &Request
                             );
            }
        }
    }

    TimeOut.LowPart = 0x0;
    TimeOut.HighPart = 0x80000000;
    pTimeOut = &TimeOut;

rewait:
    NtDelayExecution(TRUE, pTimeOut);
    goto rewait;

    return( 0 );
}