summaryrefslogtreecommitdiffstats
path: root/private/os2/server/concreat.c
blob: 8c1ee23df3ef4135511789c3c9a0af70751abd16 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    concreat.c

Abstract:

    This module handles the request to create a session form OS2SES.

Author:

    Avi Nathan (avin) 17-Jul-1991

Revision History:

--*/

#define INCL_OS2V20_TASKING
#define INCL_OS2V20_ERRORS
#include "os2srv.h"

#define NTOS2_ONLY
#include "sesport.h"

extern ULONG Os2GlobalInfoSeg;
extern HANDLE Os2GlobalInfoSegHandle;

extern HANDLE FirstOs2ProcessHandle;
extern CLIENT_ID    FirstOs2ProcessClientId;

NTSTATUS
Os2CreateConSession(
    IN OUT PVOID RequestMsg
    )
{
    PSCREQ_CREATE   Create = & ((POS2SESREQUESTMSG)RequestMsg)->d.Create;
    OS2_DOSEXECPGM_MSG ExecInfo;
    OS2_DOSSTARTSESSION_INFO SessionInfo;
    POS2_THREAD     NewThread = NULL;
    POS2_PROCESS    Process;
    POS2_SESSION    Session, SessionForCreate;
    APIRET          Rc = 0;
    NTSTATUS        Status = STATUS_SUCCESS;
    BOOLEAN         NewSession = FALSE;
    ULONG           RegionSize = 0, Length;

    RtlZeroMemory(&ExecInfo, sizeof( ExecInfo ) );
    RtlZeroMemory(&SessionInfo, sizeof( SessionInfo ) );

    try
    {
        Session = (POS2_SESSION)((POS2SESREQUESTMSG)RequestMsg)->Session;

        /*
         * Set ExecPgm information
         */

        ExecInfo.Flags = EXEC_ASYNC;

        Length = 1 + strlen(Create->d.In.ApplName);
        RtlMoveMemory(
                &ExecInfo.ApplName[0],
                &Create->d.In.ApplName[0],
                Length
               );
        ExecInfo.ApplNameLength = Length;

        RtlInitAnsiString( &ExecInfo.ErrorText, "Default error text");

        /*
         * Set StartSession information
         */
        /* USHORT */  // SessionInfo.FgBg;
        /* USHORT */  // SessionInfo.InheritOpt;
        /* USHORT */  // SessionInfo.PgmControl;

        NewThread = Os2LocateThreadByClientId( NULL, &((POS2SESREQUESTMSG)RequestMsg)->h.ClientId );
        if (NewThread == NULL)
        {
           if( ((POS2SESREQUESTMSG)RequestMsg)->Request != SesCheckPortAndConCreate)
           {

              //
              // Not a root process of a session, still NewThread is NULL - return
              // failure
              //
#if DBG
                KdPrint(("Os2CreateConSession: Process already killed by signal\n"));
#endif
                return( STATUS_UNSUCCESSFUL );
           }

            /*
             * create a process for the new session. OS2SS is the parent
             * of this process.
             */

           ASSERT(Create->d.In.IsNewSession == OS2SS_NEW_SESSION);
            SessionForCreate = Session;
            NewSession = TRUE;

        } else if (OS2SS_IS_PROCESS( Create->d.In.IsNewSession ))
        {
            /*
             * a child process
             */

            SessionForCreate = NULL;
        } else
        {
            ASSERT(Create->d.In.IsNewSession == OS2SS_CHILD_SESSION);

            /*
             * a child session
             */

            SessionForCreate = Session;
        }

        Rc = Os2CreateProcess(
                           RequestMsg,
                           NULL, //  ParentThread or (POS2_THREAD)Session->Thread,
                           &ExecInfo,
                           SessionForCreate,
                           &NewThread
                          );

        if (FirstOs2ProcessHandle == 0 && Rc == 0) {
            //
            // First OS/2 application - remember for logoff/shutdown
            //
            FirstOs2ProcessHandle = NewThread->Process->ProcessHandle;
            FirstOs2ProcessClientId = NewThread->Process->ClientId;

        }
    } except ( EXCEPTION_EXECUTE_HANDLER ){
        Rc = (APIRET)STATUS_UNSUCCESSFUL; // BUGBUG!
        /*
         * fall thru to close the section.
         */
    }

    if ( Rc == NO_ERROR )
    {
        Process = NewThread->Process;
        if (( Process == NULL ) ||
            ( Create->d.In.ExitListDispatcher == NULL ) ||
            ( Create->d.In.InfiniteSleep == NULL) ||
            ( Create->d.In.SignalDeliverer == NULL ) ||
            ( Create->d.In.FreezeThread == NULL ) ||
            ( Create->d.In.UnfreezeThread == NULL ) ||
            ( Create->d.In.ClientPib == NULL ) ||
            ( Create->d.In.InitialPebOs2Length !=
               Process->InitialPebOs2Data.Length ))
        {
            Status = STATUS_UNSUCCESSFUL;
        } else
        {
            Process->SignalDeliverer = Create->d.In.SignalDeliverer;
            Process->ExitListDispatcher = Create->d.In.ExitListDispatcher;
            Process->InfiniteSleep = Create->d.In.InfiniteSleep;
            Process->FreezeThread = Create->d.In.FreezeThread;
            Process->UnfreezeThread = Create->d.In.UnfreezeThread;
            Process->VectorHandler = Create->d.In.VectorHandler;
            Process->CritSectionAddr = Create->d.In.CritSectionAddr;

            Process->ClientPib = Create->d.In.ClientPib;
            NewThread->ClientOs2Tib = Create->d.In.ClientOs2Tib;

            Create->d.Out.PibProcessId = (HANDLE)Process->ProcessId;
            Create->d.Out.PibParentProcessId = (HANDLE)Process->Parent->ProcessId;
            Create->d.Out.PibImageFileHandle = (HANDLE)-1;
            Create->d.Out.PibStatus = 0;
            if (Process->Flags & OS2_PROCESS_BACKGROUND)
            {
                Create->d.Out.PibType = PT_DETACHED;
            }
            else
                Create->d.Out.PibType = PT_PM;


            Create->d.Out.Os2TibThreadId = (ULONG)NewThread->ThreadId;
            Create->d.Out.Os2TibVersion = OS2_VERSION;

            Os2SetThreadPriority( NewThread, NewThread->Os2Class, NewThread->Os2Level );

            *((PPEB_OS2_DATA)&Create->d.Out.InitialPebOs2Data[0]) = Process->InitialPebOs2Data;
            Create->d.Out.BootDrive = Os2BootDrive;
            Create->d.Out.SystemDrive = Os2DefaultDrive;
            Create->d.Out.SessionNumber = Process->Session->SessionId;
            Create->d.Out.GInfoAddr = (PVOID)Os2GlobalInfoSeg;

            while (NT_SUCCESS(Status))
            {
                Status = NtDuplicateObject( NtCurrentProcess(),
                                            Os2DevicesDirectory,
                                            Process->ProcessHandle,
                                            &Create->d.Out.DeviceDirectory,
                                            0,
                                            0,
                                            DUPLICATE_SAME_ACCESS |
                                            DUPLICATE_SAME_ATTRIBUTES
                                          );
                if (!NT_SUCCESS(Status) )
                {
#if DBG
                    KdPrint(("Os2CreateConSession: NtDuplicateObject-1 Failed %lx\n",Status));
#endif
                    break;
                }

                Status = NtDuplicateObject( NtCurrentProcess(),
                                            Process->Session->ConsolePort,
                                            Process->ProcessHandle,
                                            &Create->d.Out.CtrlPortHandle,
                                            0,
                                            0,
                                            DUPLICATE_SAME_ACCESS |
                                            DUPLICATE_SAME_ATTRIBUTES
                                            );
                if (!NT_SUCCESS(Status) )
                {
#if DBG
                    KdPrint(("Os2CreateConSession: NtDuplicateObject-2 Failed %lx\n",Status));
#endif
                    break;
                }

                //
                // Map view the global info seg into the new client
                //

                Status = NtMapViewOfSection( Os2GlobalInfoSegHandle,
                                             Process->ProcessHandle,
                                             (PVOID) &Os2GlobalInfoSeg,
                                             0,
                                             0,
                                             NULL,
                                             &RegionSize,
                                             ViewUnmap,
                                             0,
                                             PAGE_READONLY
                                            );
                if (!NT_SUCCESS(Status) )
                {
#if DBG
                    KdPrint(("Os2CreateConSession: NtMapViewOfSection Failed %lx\n",Status));
#endif
                }
                break;
            }
        }

        if (!NT_SUCCESS(Status) )
        {
            ASSERT( FALSE );

            // fall into Os2DereferenceSession

        } else if (SessionForCreate)
        {
            /*
             * now that the session is allocated set the console port
             * and reply the session handle to OS2SES
             */

            OS2SESREQUESTMSG    FocusMsg;
            ASSERT(NewThread->Process->Session == Session);

                //
                // Set the new session to be foreground
                //
            FocusMsg.d.FocusSet = TRUE;
            FocusMsg.Session = Session;
            Os2SessionFocusSet(&FocusMsg);

            return(Status);
        } else
        {
            //
            // os2.exe that was created by DosExecPgm
            //
            return(Status);
        }
    }

    Os2DereferenceSession(Session, NULL, (BOOLEAN)TRUE);
    return( STATUS_UNSUCCESSFUL );   // BUGBUG!
}


NTSTATUS
Os2TerminateConSession (
                            IN POS2_SESSION Session,
                            IN POS2_TERMINATEPROCESS_MSG a
                       )
{
    HANDLE              SessionPort;
    NTSTATUS            Status;
    SCREQUESTMSG        Request;
    OS2SESREQUESTMSG    FocusMsg;


    Request.Request = TaskManRequest;
    Request.d.Tm.Request = TmExit;
    Request.d.Tm.ExitResults = a->ExitResult;
    strcpy(&Request.d.Tm.ErrorText[0], &a->ErrorText[0]);

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

    SessionPort = Session->ConsolePort;

    Status = NtRequestPort(
                            SessionPort,
                            (PPORT_MESSAGE) &Request
                          );

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

        return( Status );
    }

        //
        // Reset foreground session (nop if the terminating session is not in foreground)
        //
    FocusMsg.d.FocusSet = FALSE;
    FocusMsg.Session = Session;
    Os2SessionFocusSet(&FocusMsg);

    return( STATUS_SUCCESS );

}