summaryrefslogtreecommitdiffstats
path: root/private/os2/server/srvwin.c
blob: 6e76ea9f2469bab8d4cf8f828f45d954e0788593 (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
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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    srvwin.c

Abstract:

    This module contains routines for direct os2srv->win32 interface.

Author:

    Yaron Shamir (yarons) 2-Nov-1992

Environment:

    User Mode Only

Revision History:

--*/

#include <windows.h>
#define WIN32_ONLY
#include "sesport.h"
#include "..\os2ses\os2ses.h"
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
#include <ctype.h>
#include <os2win.h>
#include "os2res.h"

HANDLE Os2hReadPipe;
HANDLE Os2hWritePipe;
HANDLE Os2hOs2SrvInstance;

    //
    // Termination commands - communication to Os2TerminationThread (srvwin.c)
    // BUGBUG - this is defined in os2srv.h as well, for non-windows files
    //
typedef enum _OS2_TERMCMD_TYPE {
    Os2TerminateProcess = 1,
    Os2TerminateThread,
    Os2MaxTermCmd
} OS2_TERMCMD_TYPE;

typedef struct _OS2_TERMCMD {
    OS2_TERMCMD_TYPE op;
    HANDLE  Handle;
    PVOID   Param1;
    PVOID   Param2;
} OS2_TERMCMD, *POS2_TERMCMD;


VOID
Os2NotifyDeathOfProcess(
    IN PVOID    m,
    IN PVOID    Proc);

VOID
Os2SwitchContextToExitListDispatcher(
    IN PVOID Thread
    );

#define  CAP_BUFFER_SIZE    64
#define  TEXT_BUFFER_SIZE   512
#define  REG_TEXT_BUFFER_SIZE   640

CHAR    DefaultAccessApiGPCap[] = "%s.EXE - General Protection";
CHAR    DefaultAccessGPText[] = "An OS/2 program caused a protection violation.\
\n\nCS\t=   0x%04lx\
\nIP\t=   0x%04lx\
\nAX\t=   0x%04lx\
\nBX\t=   0x%04lx\
\nCX\t=   0x%04lx\
\nDX\t=   0x%04lx\
\nSI\t=   0x%04lx\
\nDI\t=   0x%04lx\
\nBP\t=   0x%04lx\
\nSP\t=   0x%04lx\
\nSS\t=   0x%04lx\
\nDS\t=   0x%04lx\
\nES\t=   0x%04lx\
\n\nThe program will be terminated.";
CHAR    DefaultApiGPText[] = "An OS/2 program called %s()\nwith a bad pointer argument.\n\nThe application will be terminated.\n";


DWORD
Os2AccessGPPopup(
    IN  ULONG   CS,
    IN  ULONG   IP,
    IN  ULONG   AX,
    IN  ULONG   BX,
    IN  ULONG   CX,
    IN  ULONG   DX,
    IN  ULONG   SI,
    IN  ULONG   DI,
    IN  ULONG   BP,
    IN  ULONG   SP,
    IN  ULONG   SS,
    IN  ULONG   DS,
    IN  ULONG   ES,
    IN  PUCHAR  AppName
    )
{
    char    MessageCaption[CAP_BUFFER_SIZE];
    char    MessageText[TEXT_BUFFER_SIZE];
    CHAR    TextString[TEXT_BUFFER_SIZE];
    CHAR    CapString[CAP_BUFFER_SIZE];

    if ((Os2hOs2SrvInstance == NULL) &&
        ((Os2hOs2SrvInstance = GetModuleHandle(NULL)) == NULL))
    {
#if DBG
        KdPrint(("Os2AccessGPPopup: error %lu on GetModuleHandle\n",
                GetLastError()));
#endif
    }

    if (( Os2hOs2SrvInstance == NULL) ||
          !LoadString(Os2hOs2SrvInstance,
                   IDS_OS2SRV_ACCESS_GP_TXT,
                   TextString,
                   TEXT_BUFFER_SIZE))
    {
#if DBG
        if ( Os2hOs2SrvInstance == NULL )
        {
            KdPrint(("Os2AccessGPPopup: error %lu on LoadString1\n",
                        GetLastError()));
        }
#endif
        strncpy(TextString, DefaultAccessGPText, TEXT_BUFFER_SIZE - 1);
    }

    if (( Os2hOs2SrvInstance == NULL) ||
          !LoadString(Os2hOs2SrvInstance,
                   IDS_OS2SRV_ACCESS_API_GP_CAP,
                   CapString,
                   CAP_BUFFER_SIZE))
    {
#if DBG
        if ( Os2hOs2SrvInstance == NULL )
        {
            KdPrint(("Os2AccessGPPopup: error %lu on LoadString2\n",
                        GetLastError()));
        }
#endif
        strncpy(CapString, DefaultAccessApiGPCap, CAP_BUFFER_SIZE - 1);
    }

    sprintf(MessageCaption, CapString, AppName);
    sprintf(MessageText, TextString, CS, IP, AX, BX, CX, DX, SI, DI, BP, SP, SS, DS, ES);
    MessageBox(GetActiveWindow(), MessageText, MessageCaption, MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL | MB_SETFOREGROUND);
    return (0L);
}


DWORD
Os2ApiGPPopup(
    IN  PUCHAR  AppName,
    IN  PUCHAR  Text
    )
{
    char    MessageCaption[CAP_BUFFER_SIZE];
    char    MessageText[TEXT_BUFFER_SIZE];
    CHAR    TextString[TEXT_BUFFER_SIZE];
    CHAR    CapString[CAP_BUFFER_SIZE];

    if ((Os2hOs2SrvInstance == NULL) &&
        ((Os2hOs2SrvInstance = GetModuleHandle(NULL)) == NULL))
    {
#if DBG
        KdPrint(("Os2ApiGPPopup: error %lu on GetModuleHandle\n",
                GetLastError()));
#endif
    }

    if (( Os2hOs2SrvInstance == NULL) ||
          !LoadString(Os2hOs2SrvInstance,
                   IDS_OS2SRV_API_GP_TXT,
                   TextString,
                   TEXT_BUFFER_SIZE))
    {
#if DBG
        if ( Os2hOs2SrvInstance == NULL )
        {
            KdPrint(("Os2ApiGPPopup: error %lu on LoadString1\n",
                        GetLastError()));
        }
#endif
        strncpy(TextString, DefaultApiGPText, TEXT_BUFFER_SIZE - 1);
    }

    if (( Os2hOs2SrvInstance == NULL) ||
          !LoadString(Os2hOs2SrvInstance,
                   IDS_OS2SRV_ACCESS_API_GP_CAP,
                   CapString,
                   CAP_BUFFER_SIZE))
    {
#if DBG
        if ( Os2hOs2SrvInstance == NULL )
        {
            KdPrint(("Os2ApiGPPopup: error %lu on LoadString2\n",
                        GetLastError()));
        }
#endif
        strncpy(CapString, DefaultAccessApiGPCap, CAP_BUFFER_SIZE - 1);
    }

    sprintf(MessageCaption, CapString, AppName);
    sprintf(MessageText, TextString, Text);
    MessageBox(GetActiveWindow(), MessageText, MessageCaption, MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL | MB_SETFOREGROUND);
    return (0L);
}


LPTHREAD_START_ROUTINE
Os2TerminationThread(
            LPVOID lpThreadParameter)

{
    OS2_TERMCMD Buffer;
    DWORD nNumberOfBytesRead;

    UNREFERENCED_PARAMETER(lpThreadParameter);

    for (; ; ){
       if (!ReadFile(
                Os2hReadPipe,
                (LPVOID)&Buffer,
                sizeof(Buffer),
                &nNumberOfBytesRead,
                NULL)){

            ASSERT( FALSE );
#if DBG
            DbgPrint("Os2erminateThread - fail to Read pipe, %d\n, ignore",GetLastError());
#endif
       }
       else {
          //
          // Read succesfully a command to execute
          //
          switch (Buffer.op){
            case Os2TerminateProcess:
                TerminateProcess( Buffer.Handle, 0L );
                WaitForSingleObject( Buffer.Handle, 1000 );
                CloseHandle( Buffer.Handle );
                Os2NotifyDeathOfProcess(Buffer.Param1, Buffer.Param2);
                break;

            case Os2TerminateThread:

                if (Buffer.Param1 == (PVOID)1) {
                    //
                    // This message means
                    // resume thread1 and alert it, to process exit list
                    //
                    // Switch thread1 context to ExitListDispatcher.
                    Os2SwitchContextToExitListDispatcher(Buffer.Param2);
                }
                else {
                    TerminateThread( Buffer.Handle, 0L );
                    WaitForSingleObject( Buffer.Handle, 1000 );
                    CloseHandle( Buffer.Handle );
                }

                break;

            default:
                ASSERT(FALSE);
#if DBG
                DbgPrint("Os2erminateThread - Unknown Termination Cmd, %d\n",
                            Buffer.op);
#endif
                break;
          }
       }
    }
    //
    // will never get here
    //
    return(0); // for the compiler to shutdown his warnings.
}


BOOL
Os2TerminationThreadInitialize( VOID )
{
    HANDLE  hThreadHandle;
    ULONG Tid;

    if (!CreatePipe(
            &Os2hReadPipe,
            &Os2hWritePipe,
            NULL,
            0)) {
        ASSERT( FALSE );
#if DBG
        DbgPrint("Os2erminateThreadInitialize - fail at win32 CreatePipe, %d\n",GetLastError());
#endif
        return( FALSE );
    }

    //
    // use same port for exception handling and debugger
    //

    hThreadHandle = CreateThread( NULL,
                            0,
                            (LPTHREAD_START_ROUTINE )Os2TerminationThread,
                            NULL,
                            0,
                            &Tid);
    if (!hThreadHandle){
        ASSERT( FALSE );
#if DBG
        DbgPrint("Os2erminateThreadInitialize - fail at win32 CreateThread, %d\n",GetLastError());
#endif
        return( FALSE );
    }

    return( TRUE );
}