summaryrefslogblamecommitdiffstats
path: root/private/ntos/npfs/waitsup.c
blob: fa03790e26e8a612d3e8c14b06d10d2bd05daf70 (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

















































































































































































































































































































































































































































































































































































































































                                                                                                     
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    WaitSup.c

Abstract:

    This module implements the Wait for Named Pipe support routines.

Author:

    Gary Kimura     [GaryKi]    30-Aug-1990

Revision History:

--*/

#include "NpProcs.h"

//
//  The debug trace level
//

#define Dbg                              (DEBUG_TRACE_WAITSUP)

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NpInitializeWaitQueue)
#pragma alloc_text(PAGE, NpUninitializeWaitQueue)
#endif


//
//  Local procedures and structures
//

typedef struct _WAIT_CONTEXT {
    KDPC Dpc;
    KTIMER Timer;
    PWAIT_QUEUE WaitQueue;
} WAIT_CONTEXT;
typedef WAIT_CONTEXT *PWAIT_CONTEXT;

VOID
NpTimerDispatch(
    IN PKDPC Dpc,
    IN PVOID Contxt,
    IN PVOID SystemArgument1,
    IN PVOID SystemArgument2
    );

VOID
NpCancelWaitQueueIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );


VOID
NpInitializeWaitQueue (
    IN PWAIT_QUEUE WaitQueue
    )

/*++

Routine Description:

    This routine initializes the wait for named pipe queue.

Arguments:

    WaitQueue - Supplies a pointer to the list head being initialized

Return Value:

    None.

--*/

{
    PAGED_CODE();

    DebugTrace(+1, Dbg, "NpInitializeWaitQueue, WaitQueue = %08lx\n", WaitQueue);

    //
    //  Initialize the List head
    //

    InitializeListHead( &WaitQueue->Queue );

    //
    //  Initialize the Wait Queue's spinlock
    //

    KeInitializeSpinLock( &WaitQueue->SpinLock );

    //
    //  and return to our caller
    //

    DebugTrace(-1, Dbg, "NpInitializeWaitQueue -> VOID\n", 0);

    return;
}


VOID
NpUninitializeWaitQueue (
    IN PWAIT_QUEUE WaitQueue
    )

/*++

Routine Description:

    This routine uninitializes the wait for named pipe queue.

Arguments:

    WaitQueue - Supplies a pointer to the list head being uninitialized

Return Value:

    None.

--*/

{
    PAGED_CODE();

    DebugTrace(+1, Dbg, "NpInitializeWaitQueue, WaitQueue = %08lx\n", WaitQueue);

    //
    //  And return to our caller
    //

    DebugTrace(-1, Dbg, "NpInitializeWaitQueue -> VOID\n", 0);

    return;
}


VOID
NpAddWaiter (
    IN PWAIT_QUEUE WaitQueue,
    IN LARGE_INTEGER DefaultTimeOut,
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine adds a new "wait for named pipe" IRP to the wait queue.
    After calling this function the caller nolonger can access the IRP

Arguments:

    WaitQueue - Supplies the wait queue being used

    DefaultTimeOut - Supplies the default time out to use if one is
        not supplied in the Irp

    Irp - Supplies a pointer to the wait Irp

Return Value:

    None.

--*/

{
    KIRQL OldIrql;
    PWAIT_CONTEXT Context;
    PFILE_PIPE_WAIT_FOR_BUFFER WaitForBuffer;
    LARGE_INTEGER Timeout;
    ULONG i;

    DebugTrace(+1, Dbg, "NpAddWaiter, WaitQueue = %08lx\n", WaitQueue);

    //
    //  Allocate a dpc and timer structure and initialize them
    //

    Context = FsRtlAllocatePool( NonPagedPool, sizeof(WAIT_CONTEXT) );

    KeInitializeDpc( &Context->Dpc, NpTimerDispatch, Irp );

    KeInitializeTimer( &Context->Timer );

    Context->WaitQueue = WaitQueue;

    //
    //  Have the information of the irp point to the context buffer
    //

    Irp->IoStatus.Information = (ULONG)Context;

    //
    //  Figure out our timeout value
    //

    WaitForBuffer = (PFILE_PIPE_WAIT_FOR_BUFFER)Irp->AssociatedIrp.SystemBuffer;

    if (WaitForBuffer->TimeoutSpecified) {

        Timeout = WaitForBuffer->Timeout;

    } else {

        Timeout = DefaultTimeOut;
    }

    //
    //  Upcase the name of the pipe we are waiting for
    //

    for (i = 0; i < WaitForBuffer->NameLength/sizeof(WCHAR); i += 1) {

        WaitForBuffer->Name[i] = RtlUpcaseUnicodeChar(WaitForBuffer->Name[i]);
    }

    //
    //  Acquire the spinlock
    //

    KeAcquireSpinLock( &WaitQueue->SpinLock, &OldIrql );

    try {

        //
        //  Now insert this new entry into the Wait Queue
        //

        InsertTailList( &WaitQueue->Queue, &Irp->Tail.Overlay.ListEntry );

        //
        //  And set the timer to go off
        //

        (VOID)KeSetTimer( &Context->Timer, Timeout, &Context->Dpc );

        //
        //  Now set the cancel routine for the irp and check if it has been cancelled.
        //

        IoAcquireCancelSpinLock( &Irp->CancelIrql );
        Irp->IoStatus.Status = (ULONG)WaitQueue;

        if (Irp->Cancel) {

            NpCancelWaitQueueIrp( ((PVOID)0x1), Irp );

        } else {

            IoSetCancelRoutine( Irp, NpCancelWaitQueueIrp );
            IoReleaseCancelSpinLock( Irp->CancelIrql );
        }

    } finally {

        //
        //  Release the spinlock
        //

        KeReleaseSpinLock( &WaitQueue->SpinLock, OldIrql );
    }

    //
    //  And now return to our caller
    //

    DebugTrace(-1, Dbg, "NpAddWaiter -> VOID\n", 0);

    return;
}


VOID
NpCancelWaiter (
    IN PWAIT_QUEUE WaitQueue,
    IN PUNICODE_STRING NameOfPipe
    )

/*++

Routine Description:

    This procedure cancels all waiters that are waiting for the named
    pipe to reach the listening state.  The corresponding IRPs are completed
    with STATUS_SUCCESS.

Arguments:

    WaitQueue - Supplies the wait queue being modified

    NameOfPipe - Supplies the name of the named pipe (device relative)
        that has just reached the listening state.

Return Value:

    None.

--*/

{
    KIRQL OldIrql;
    PLIST_ENTRY Links;
    PIRP Irp;
    PFILE_PIPE_WAIT_FOR_BUFFER WaitForBuffer;
    PWAIT_CONTEXT Context;
    ULONG i;

    UNICODE_STRING NonPagedNameOfPipe;

    DebugTrace(+1, Dbg, "NpCancelWaiter, WaitQueue = %08lx\n", WaitQueue);

    //
    //  Capture the name of pipe before we grab the spinlock, and upcase it
    //

    NonPagedNameOfPipe.Buffer = FsRtlAllocatePool( NonPagedPool, NameOfPipe->Length );
    NonPagedNameOfPipe.Length = 0;
    NonPagedNameOfPipe.MaximumLength = NameOfPipe->Length;

    (VOID) RtlUpcaseUnicodeString( &NonPagedNameOfPipe, NameOfPipe, FALSE );

    //
    //  Acquire the spinlock
    //

    KeAcquireSpinLock( &WaitQueue->SpinLock, &OldIrql );

    try {

        //
        //  For each waiting irp check if the name matches
        //

        for (Links = WaitQueue->Queue.Flink;
             Links != &WaitQueue->Queue;
             Links = Links->Flink) {

            Irp = CONTAINING_RECORD( Links, IRP, Tail.Overlay.ListEntry );
            WaitForBuffer = (PFILE_PIPE_WAIT_FOR_BUFFER)Irp->AssociatedIrp.SystemBuffer;
            Context = (PWAIT_CONTEXT)Irp->IoStatus.Information;

            //
            //  Check if this Irp matches the one we've been waiting for
            //  First check the lengths for equality, and then compare
            //  the strings.  They match if we exit the inner loop with
            //  i >= name length.
            //

            if (((USHORT)(WaitForBuffer->NameLength + sizeof(WCHAR))) == NonPagedNameOfPipe.Length) {

                for (i = 0; i < WaitForBuffer->NameLength/sizeof(WCHAR); i += 1) {

                    if (WaitForBuffer->Name[i] != NonPagedNameOfPipe.Buffer[i+1]) {

                        break;
                    }
                }

                if (i >= WaitForBuffer->NameLength/sizeof(WCHAR)) {

                    //
                    //  We need to complete this irp so we first
                    //  stop the timer, dequeue it from the wait queue
                    //  (be sure to keep links correct), disable the cancel routine
                    //  and then complete the Irp.
                    //

                    if (KeCancelTimer( &Context->Timer )) {

                        Links = Links->Blink;

                        RemoveEntryList( &Irp->Tail.Overlay.ListEntry );

                        IoAcquireCancelSpinLock( &Irp->CancelIrql );
                        IoSetCancelRoutine( Irp, NULL );
                        Irp->IoStatus.Information = 0;
                        IoReleaseCancelSpinLock( Irp->CancelIrql );

                        NpCompleteRequest( Irp, STATUS_SUCCESS );

                        ExFreePool( Context );
                    }
                }
            }
        }

    } finally {

        //
        //  Release the spinlock
        //

        KeReleaseSpinLock( &WaitQueue->SpinLock, OldIrql );

        ExFreePool( NonPagedNameOfPipe.Buffer );

        DebugTrace(-1, Dbg, "NpCancelWaiter -> VOID\n", 0);
    }

    //
    //  And now return to our caller
    //

    return;
}


//
//  Local support routine
//

VOID
NpTimerDispatch(
    IN PKDPC Dpc,
    IN PVOID Contxt,
    IN PVOID SystemArgument1,
    IN PVOID SystemArgument2
    )

/*++

Routine Description:

    This routine is called whenever a timer on a wait queue Irp goes off

Arguments:

    Dpc - Ignored

    Contxt - Supplies a pointer to the irp whose timer went off

    SystemArgument1 - Ignored

    SystemArgument2 - Ignored

Return Value:

    none.

--*/

{
    PIRP Irp = Contxt;
    KIRQL OldIrql;
    PLIST_ENTRY Links;
    PWAIT_CONTEXT Context;
    PWAIT_QUEUE WaitQueue;

    UNREFERENCED_PARAMETER( Dpc );
    UNREFERENCED_PARAMETER( SystemArgument1 );
    UNREFERENCED_PARAMETER( SystemArgument2 );

    Context = (PWAIT_CONTEXT)Irp->IoStatus.Information;
    WaitQueue = Context->WaitQueue;

    KeAcquireSpinLock( &WaitQueue->SpinLock, &OldIrql );

    try {

        //
        //  Check if the Irp is still in the waiting queue.  We need to do
        //  this because we might be in the middle of canceling the entry
        //  when the timer went off.
        //

        for (Links = WaitQueue->Queue.Flink;
             Links != &WaitQueue->Queue;
             Links = Links->Flink) {

            if (Irp == CONTAINING_RECORD( Links, IRP, Tail.Overlay.ListEntry )) {

                //
                //  Remove the IRP, and complete it with a result of timeout
                //

                RemoveEntryList( &Irp->Tail.Overlay.ListEntry );

                NpCompleteRequest( Irp, STATUS_IO_TIMEOUT );

                //
                //  Deallocate the context
                //

                ExFreePool( Context );

                //
                //  And exit from the loop because we found our match
                //

                break;
            }
        }

    } finally {

        //
        //  Release the spinlock
        //

        KeReleaseSpinLock( &WaitQueue->SpinLock, OldIrql );
    }

    //
    //  And now return to our caller
    //

    return;
}


//
//  Local Support routine
//

VOID
NpCancelWaitQueueIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine is called to cancel a wait queue irp

Arguments:

    DeviceObject - Ignored

    Irp - Supplies the Irp being cancelled.  The Iosb.Status field in the irp
        points to the wait queue

Return Value:

    none.

--*/

{
    PWAIT_QUEUE WaitQueue;
    KIRQL OldIrql;
    PLIST_ENTRY Links;

    UNREFERENCED_PARAMETER( DeviceObject );

    //
    //  The status field is used to store a pointer to the wait queue
    //  containing this irp
    //

    WaitQueue = (PWAIT_QUEUE)Irp->IoStatus.Status;

    //
    //  We now need to void the cancel routine and release the io cancel spinlock
    //

    IoSetCancelRoutine( Irp, NULL );
    IoReleaseCancelSpinLock( Irp->CancelIrql );

    //
    //  Get the spinlock proctecting the wait queue
    //

    if (DeviceObject != (PVOID)0x1) { KeAcquireSpinLock( &WaitQueue->SpinLock, &OldIrql ); }

    try {

        //
        //  For each waiting irp check if it has been cancelled
        //

        for (Links = WaitQueue->Queue.Flink;
             Links != &WaitQueue->Queue;
             Links = Links->Flink) {

            PIRP LocalIrp;
            PWAIT_CONTEXT Context;

            LocalIrp = CONTAINING_RECORD( Links, IRP, Tail.Overlay.ListEntry );
            Context = (PWAIT_CONTEXT)LocalIrp->IoStatus.Information;

            if (LocalIrp->Cancel) {

                //
                //  We need to complete this irp so we first
                //  stop the timer, dequeue it from the wait queue
                //  (be sure to keep links correct), and then complete the Irp.
                //

                if (KeCancelTimer( &Context->Timer )) {

                    Links = Links->Blink;

                    RemoveEntryList( &LocalIrp->Tail.Overlay.ListEntry );

                    LocalIrp->IoStatus.Information = 0;

                    NpCompleteRequest( LocalIrp, STATUS_CANCELLED );
                    ExFreePool( Context );
                }
            }
        }

    } finally {

        if (DeviceObject != (PVOID)0x1) { KeReleaseSpinLock( &WaitQueue->SpinLock, OldIrql ); }
    }

    //
    //  And return to our caller
    //

    return;



}