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

Copyright (c) 1989  Microsoft Corporation

Module Name:

    srvfile.c

Abstract:

    Support for copying file system variables during exec

Author:

    Therese Stowell (thereses) 6-Nov-1989

Revision History:

--*/

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

NTSTATUS
DeviceAddShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    );

NTSTATUS
DeviceDupShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    );

NTSTATUS
DeviceRemoveShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    );

SHARE_ACCESS DeviceSharer[NUMBER_OF_DEVICES];


APIRET
DupHandleTable(
    IN PFILE_HANDLE ParentHandleTable,
    IN ULONG ParentTableLength,
    IN HANDLE ParentProcess,    // NT process handle
    IN HANDLE ChildProcess,     // NT process handle
    POS2_PROCESS Process,       // OS/2 child process
    IN POS2_DOSEXECPGM_MSG pExecPgmMsg
    )

/*++

Routine Description:

    This routine is called during an exec.  It reads the handle table of
    the parent and then creates the handle table for the child by duping
    any inheritable handles and clearing some flags (as OS/2 does).  The
    resulting table will be read by the child process.

Arguments:

    ParentHandleTable - pointer to parent handle table

    ParentTableLength - length of parent handle table

    ParentProcess - NT process handle of parent

    ChildProcess - NT process handle of child

    Process - OS/2 child process

    pExecPgmMsg - DosExecPgm message:

        CmdLineFlag - flag to process the file handle

        1 - change StdOut to NULL
        2 - change StdOut to RedirectedFileName

        RedirectedFileName - file name for STD_OUT (1)

Return Value:

    ??

--*/

{
    NTSTATUS Status;
    ULONG i, CmdLineFlag = pExecPgmMsg->CmdLineFlag;
    ULONG BytesRead;

    Process->HandleTableLength = ParentTableLength;
    Process->HandleTable = (PFILE_HANDLE)RtlAllocateHeap( Os2Heap, 0,
                     ParentTableLength * sizeof(FILE_HANDLE)
                                         );
    if (Process->HandleTable == NULL) {
#if DBG
        KdPrint(( "Os2DupHandleTable, no memory in Os2Heap\n" ));
#endif
        return(ERROR_NOT_ENOUGH_MEMORY);
    }

    Status = NtReadVirtualMemory( ParentProcess,
                  ParentHandleTable,
                  Process->HandleTable,
                  ParentTableLength * sizeof(FILE_HANDLE),
                  &BytesRead
                );
    if ((!NT_SUCCESS(Status)) || (BytesRead != ParentTableLength * sizeof(FILE_HANDLE))) {
        RtlFreeHeap(Os2Heap,0,Process->HandleTable);
        return ERROR_INVALID_HANDLE;
    }

    for (i=0;i<ParentTableLength;i++)
    {
        if ((i == 1) && CmdLineFlag )
        {
            if (CmdLineFlag & REDIR_NUL)
            {
                Process->HandleTable[1].IoVectorType = NulVectorType;
                Process->HandleTable[1].FileType = FILE_TYPE_DEV;
                Process->HandleTable[1].DeviceAttribute = DEVICE_ATTRIBUTE_NUL | DEVICE_ATTRIBUTE_CHAR | 0x80;
                /* 0x80 stands for LEVEL 1 which makes it OS/2 1.x compatible */
                //Process->HandleTable[1].NtHandle =
                Process->HandleTable[1].Flags = FILE_HANDLE_ALLOCATED | FILE_HANDLE_VALID |
                    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE;
            } else if (CmdLineFlag & REDIR_FILE)
            {

                Process->HandleTable[1].IoVectorType = FileVectorType;
                Process->HandleTable[1].FileType = pExecPgmMsg->RedirectedFileType;
                Process->HandleTable[1].DeviceAttribute = 0;
                Process->HandleTable[1].NtHandle = pExecPgmMsg->hRedirectedFile;
                Process->HandleTable[1].Flags = FILE_HANDLE_ALLOCATED | FILE_HANDLE_VALID |
                    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE;
            }
        } else

        if ((Process->HandleTable[i].Flags & FILE_HANDLE_ALLOCATED) &&
            (!(Process->HandleTable[i].Flags & OPEN_FLAGS_NOINHERIT))) {
        //
        // all NT handles are non-inherited.  to inherit OS/2 handles, dup handles to
        // child.
        //
            if (Process->HandleTable[i].IoVectorType == MonitorVectorType)
            {
                continue;
            }

        // For VIO-devices, NtHandle field holds an OS2.EXE handle.
        // Od2InitializeFileSystemForExec(fileinit.c) will handle it.

            if ((Process->HandleTable[i].IoVectorType != ConVectorType) &&
                (Process->HandleTable[i].IoVectorType != KbdVectorType) &&
                (Process->HandleTable[i].IoVectorType != MouseVectorType) &&
                (Process->HandleTable[i].IoVectorType != ScreenVectorType) &&
                (Process->HandleTable[i].IoVectorType != RemoteVectorType))
            {

                Status = NtDuplicateObject(ParentProcess,
                         Process->HandleTable[i].NtHandle,
                         ChildProcess,
                         &(Process->HandleTable[i].NtHandle),
                         (ACCESS_MASK) NULL,
                         OBJ_CASE_INSENSITIVE,
                         DUPLICATE_SAME_ACCESS
                        );
            // if dup fails, the child process will die and the system will release all
            // the already-duped handles.

//          if (!NT_SUCCESS(Status)) {
//          RtlFreeHeap(Os2Heap,0,Process->HandleTable);
//          return ERROR_INVALID_HANDLE;    // BUGBUG bogus error value
//          }

            }
            // os2 clears the write-through flag when duping the handle table

            Process->HandleTable[i].Flags &= ~(OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_NO_CACHE);
        }
        else {
        Process->HandleTable[i].Flags = FILE_HANDLE_FREE;
        }
    }
    return( NO_ERROR );
}


APIRET
InitializeFileSystemForExec(
    IN POS2_FILE_SYSTEM_PARAMETERS FileSystemParameters,
    IN HANDLE ParentProcessHandle,  // NT process handle
    IN HANDLE ChildProcessHandle,   // NT process handle
    POS2_PROCESS ParentProcess,     // OS/2 parent process
    POS2_PROCESS ChildProcess,       // OS/2 child process
    IN POS2_DOSEXECPGM_MSG pExecPgmMsg
    )

/*++

Routine Description:

    This routine is called during an exec.  It calls the routines to
    initialize various components of the filesystem.

Arguments:

    FileSystemParameters - values needed to initialize the file system

    ParentProcessHandle - NT process handle to parent process

    ChildProcessHandle - NT process handle to child process

    ParentProcess - OS/2 parent process

    ChildProcess - OS/2 child process

    pExecPgmMsg - the message passed from the client

Return Value:

    ??

--*/

{
    APIRET RetCode;

    RetCode = DupHandleTable(FileSystemParameters->ParentHandleTable,
                 FileSystemParameters->ParentTableLength,
                 ParentProcessHandle,
                 ChildProcessHandle,
                 ChildProcess,
                 pExecPgmMsg
                );
    return RetCode;
}


BOOLEAN
Os2InternalCopyHandleTable(
    IN POS2_THREAD t,
    IN POS2_API_MSG m
    )

/*++

Routine Description:

    This routine is called during an exec by the child process.  It copies
    the handle table into the child's context.

Arguments:

    t - calling thread

    m - message

Return Value:

    TRUE - create a return message

--*/

{
    POS2_COPYHANDLETABLE_MSG a = &m->u.CopyHandleTable;
    NTSTATUS Status;
    ULONG BytesWritten;

//    DbgBreakPoint();
    Status = NtWriteVirtualMemory( t->Process->ProcessHandle,
                   a->ChildHandleTable,
                   t->Process->HandleTable,
                   a->ChildTableLength * sizeof(FILE_HANDLE),
                   &BytesWritten
                 );

    RtlFreeHeap(Os2Heap,0,t->Process->HandleTable);
    if ((!NT_SUCCESS(Status)) || (BytesWritten != a->ChildTableLength * sizeof(FILE_HANDLE))) {
    m->ReturnedErrorValue = ERROR_INVALID_HANDLE;    // BUGBUG bogus error value
    return( TRUE );
    }
    m->ReturnedErrorValue = NO_ERROR;
    return( TRUE );
}


VOID
InitializeFileSystemForSesMgr(
    IN POS2_PROCESS Process
    )

/*++

Routine Description:

    This routine is called during an create session. It allocates space
    for the current directory table and initializes each entry to the
    root directory.

Arguments:

    Process - process being created

Return Value:

    none.

--*/

{
    int i;

    for (i=0;i<NUMBER_OF_DEVICES;i++) {
        DeviceSharer[i].OpenCount = 0;
        DeviceSharer[i].Readers = 0;
        DeviceSharer[i].Writers = 0;
        DeviceSharer[i].Deleters = 0;
        DeviceSharer[i].SharedRead = 0;
        DeviceSharer[i].SharedWrite = 0;
        DeviceSharer[i].SharedDelete = 0;
    }
}


APIRET
InitializeFileSystemForChildSesMgr(
    IN POS2_FILE_SYSTEM_PARAMETERS FileSystemParameters,
    IN HANDLE ParentProcessHandle,  // NT process handle
    IN HANDLE ChildProcessHandle,   // NT process handle
    POS2_PROCESS ParentProcess,     // OS/2 parent process
    POS2_PROCESS ChildProcess,      // OS/2 child process
    IN POS2_DOSEXECPGM_MSG pExecPgmMsg
    )

/*++

Routine Description:

    This routine is called during an create session of a child session.
    It calls the routines to initialize various components of the filesystem.

Arguments:

    FileSystemParameters - values needed to initialize the file system

    ParentProcessHandle - NT process handle to parent process

    ChildProcessHandle - NT process handle to child process

    ParentProcess - OS/2 parent process

    ChildProcess - OS/2 child process

    pExecPgmMsg - the message passed from the client

Return Value:

    ??

--*/

{
    APIRET RetCode;
    int    i;

    RetCode = DupHandleTable(FileSystemParameters->ParentHandleTable,
                 FileSystemParameters->ParentTableLength,
                 ParentProcessHandle,
                 ChildProcessHandle,
                 ChildProcess,
                 pExecPgmMsg
                );

    if (RetCode == NO_ERROR) {

        for (i=0;i<NUMBER_OF_DEVICES;i++) {
            DeviceSharer[i].OpenCount = 0;
            DeviceSharer[i].Readers = 0;
            DeviceSharer[i].Writers = 0;
            DeviceSharer[i].Deleters = 0;
            DeviceSharer[i].SharedRead = 0;
            DeviceSharer[i].SharedWrite = 0;
            DeviceSharer[i].SharedDelete = 0;
        }
    }
    return RetCode;
}


BOOLEAN
Os2InternalDeviceShare(
    IN POS2_THREAD t,
    IN POS2_API_MSG m
    )

/*++

Routine Description:

    This routine is called to add, dup, or remove the sharing of a particular
    device.

Arguments:

    t - calling thread

    m - message

Return Value:

    TRUE - create a return message

--*/

{
    POS2_SHARE_MSG a = &m->u.DeviceShare;
    PSHARE_ACCESS ShareRecord;

    UNREFERENCED_PARAMETER(t);
    if (a->VectorType > MAXIMUM_DEVICE_VECTOR_TYPE) {
        ASSERT (FALSE);
        m->ReturnedErrorValue = (ULONG)STATUS_INVALID_PARAMETER;
        return TRUE;
    }
    ShareRecord = &DeviceSharer[a->VectorType];

    switch (a->Operation) {
        case AddShare:
            m->ReturnedErrorValue = DeviceAddShare(a->DesiredAccess,
                                                   a->ShareAccess,
                                                   ShareRecord
                                                  );
            break;
        case DupShare:
            m->ReturnedErrorValue = DeviceDupShare(a->DesiredAccess,
                                                   a->ShareAccess,
                                                   ShareRecord
                                                  );
            break;
        case RemoveShare:
            m->ReturnedErrorValue = DeviceRemoveShare(a->DesiredAccess,
                                                      a->ShareAccess,
                                                      ShareRecord
                                                     );
            break;
        default:
            ASSERT (FALSE);
            m->ReturnedErrorValue = (ULONG)STATUS_INVALID_PARAMETER;
            break;
    }
    return TRUE;
}

NTSTATUS
DeviceAddShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    )

{
    ULONG Ocount;
    ULONG ReadAccess;
    ULONG WriteAccess;
    ULONG DeleteAccess;
    ULONG SharedRead;
    ULONG SharedWrite;
    ULONG SharedDelete;

    //
    // Set the access type in the file object for the current accessor.
    // Note that reading and writing attributes are not included in the
    // access check.
    //

    ReadAccess = (DesiredAccess & (READ_CONTROL | FILE_EXECUTE
        | FILE_READ_DATA | FILE_READ_EA)) != 0;
    WriteAccess = (DesiredAccess & (WRITE_DAC | WRITE_OWNER
        | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA)) != 0;
    DeleteAccess = (DesiredAccess & DELETE) != 0;

    SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
    SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
    SharedDelete = (DesiredShareAccess & FILE_SHARE_DELETE) != 0;

    //
    // Now check to see whether or not the desired accesses are compatible
    // with the way that the file is currently open.
    //

    Ocount = ShareAccess->OpenCount;

    if ( (ReadAccess && (ShareAccess->SharedRead < Ocount))
         ||
         (WriteAccess && (ShareAccess->SharedWrite < Ocount))
         ||
         (DeleteAccess && (ShareAccess->SharedDelete < Ocount))
         ||
         ((ShareAccess->Readers != 0) && !SharedRead)
         ||
         ((ShareAccess->Writers != 0) && !SharedWrite)
         ||
         ((ShareAccess->Deleters != 0) && !SharedDelete)
       ) {

        //
        // The check failed.  Simply return to the caller indicating that the
        // current open cannot access the file.
        //

        return STATUS_SHARING_VIOLATION;

    } else {

        //
        // The check was successful.  Update the counter information in the
        // shared access structure for this open request if the caller
        // specified that it should be updated.
        //

        ShareAccess->OpenCount++;

        ShareAccess->Readers += ReadAccess;
        ShareAccess->Writers += WriteAccess;
        ShareAccess->Deleters += DeleteAccess;

        ShareAccess->SharedRead += SharedRead;
        ShareAccess->SharedWrite += SharedWrite;
        ShareAccess->SharedDelete += SharedDelete;

        return STATUS_SUCCESS;
    }
}

NTSTATUS
DeviceDupShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    )

{
    ULONG ReadAccess;
    ULONG WriteAccess;
    ULONG DeleteAccess;
    ULONG SharedRead;
    ULONG SharedWrite;
    ULONG SharedDelete;

    //
    // Set the access type in the file object for the current accessor.
    // Note that reading and writing attributes are not included in the
    // access check.
    //

    ReadAccess = (DesiredAccess & (READ_CONTROL | FILE_EXECUTE
        | FILE_READ_DATA | FILE_READ_EA)) != 0;
    WriteAccess = (DesiredAccess & (WRITE_DAC | WRITE_OWNER
        | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA)) != 0;
    DeleteAccess = (DesiredAccess & DELETE) != 0;

    SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
    SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
    SharedDelete = (DesiredShareAccess & FILE_SHARE_DELETE) != 0;

#if DBG
    if (ShareAccess->OpenCount == 0)
        ASSERT (FALSE);
#endif
    ShareAccess->OpenCount++;

    ShareAccess->Readers += ReadAccess;
    ShareAccess->Writers += WriteAccess;
    ShareAccess->Deleters += DeleteAccess;

    ShareAccess->SharedRead += SharedRead;
    ShareAccess->SharedWrite += SharedWrite;
    ShareAccess->SharedDelete += SharedDelete;

    return STATUS_SUCCESS;
}

NTSTATUS
DeviceRemoveShare(
    IN ULONG DesiredAccess,
    IN ULONG DesiredShareAccess,
    IN OUT PSHARE_ACCESS ShareAccess
    )

{
    ULONG ReadAccess;
    ULONG WriteAccess;
    ULONG DeleteAccess;
    ULONG SharedRead;
    ULONG SharedWrite;
    ULONG SharedDelete;

    //
    // Set the access type in the file object for the current accessor.
    // Note that reading and writing attributes are not included in the
    // access check.
    //

    ReadAccess = (DesiredAccess & (READ_CONTROL | FILE_EXECUTE
        | FILE_READ_DATA | FILE_READ_EA)) != 0;
    WriteAccess = (DesiredAccess & (WRITE_DAC | WRITE_OWNER
        | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA)) != 0;
    DeleteAccess = (DesiredAccess & DELETE) != 0;

    SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
    SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
    SharedDelete = (DesiredShareAccess & FILE_SHARE_DELETE) != 0;

//#if DBG
//    if (ShareAccess->OpenCount == 0)
//       ASSERT (FALSE);
//#endif
    ShareAccess->OpenCount--;

    ShareAccess->Readers -= ReadAccess;
    ShareAccess->Writers -= WriteAccess;
    ShareAccess->Deleters -= DeleteAccess;

    ShareAccess->SharedRead -= SharedRead;
    ShareAccess->SharedWrite -= SharedWrite;
    ShareAccess->SharedDelete -= SharedDelete;

    return STATUS_SUCCESS;
}