summaryrefslogblamecommitdiffstats
path: root/private/eventlog/server/control.c
blob: 6f937f51005e641f319667c15c35a740aa2d61c0 (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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699


























































































































































































































































































































































































































































































































































































































































































































                                                                                      

/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    CONTROL.C

Abstract:

    This file contains the control handler for the eventlog service.

Author:

    Rajen Shah  (rajens)    16-Jul-1991

Revision History:


--*/

//
// INCLUDES
//

#include <eventp.h>


//
// GLOBALS
//

    CRITICAL_SECTION StatusCriticalSection={0};
    SERVICE_STATUS   ElStatus={0};
    DWORD            HintCount=0;
    DWORD            ElUninstallCode=0;  // reason for uninstalling
    DWORD            ElSpecificCode=0;
    DWORD            ElState=STARTING;


VOID
ElfPrepareForPause()

/*++

Routine Description:

    This routine prepares the eventlog service for pausing. Since the
    caller took the global resource for exclusive access, we know that
    there is no activity on any of the files.

    We just have to note somewhere that the service is paused
    so that any threads that start running will know that and will not
    perform any further operations until the service is CONTINUEd.

Arguments:

    NONE

Return Value:

    NONE

Note:


--*/
{
    NTSTATUS    Status;

    ElfDbgPrint(("[ELF] Control: Prepare for service pause\n"));

    //
    // Flush all files to disk.
    //
    Status = ElfpFlushFiles ();

    return;
}



VOID
ElfPrepareForContinue()

/*++

Routine Description:

    This routine restarts the eventlog service after a pause operation.
    It will signal the relevant event(s) for all the threads to start
    running.

    The caller ensures that it has exclusive access to the global resource
    so that there is no thread inside the service that is doing operations
    on the log file(s) or the data structures while the control request
    is being handled.

Arguments:


Return Value:

    NONE

Note:


--*/
{
    ElfDbgPrint(("[ELF] Control: Prepare for service continue\n"));

    //
    // Signal the "continue" event.
    //


    return;
}



VOID
ElfControlResponse(
        DWORD   opCode
        )

{
    DWORD   state;

    ElfDbgPrint(("[ELF] Inside control handler. Control = %ld\n", opCode));

    //
    // Determine the type of service control message and modify the
    // service status, if necessary.
    //
    switch(opCode) {

        case SERVICE_CONTROL_SHUTDOWN:
        case SERVICE_CONTROL_STOP:


            //
            // If the service is installed, shut it down and exit.
            //

            ElfStatusUpdate(STOPPING);

            GetGlobalResource (ELF_GLOBAL_EXCLUSIVE);

            //
            // Log an event that says we're stopping
            //

#if DBG
            ElfpCreateElfEvent(EVENT_EventlogStopped,
                               EVENTLOG_INFORMATION_TYPE,
                               0,                    // EventCategory
                               0,                    // NumberOfStrings
                               NULL,                 // Strings
                               NULL,                 // Data
                               0,                    // Datalength
                               0                     // flags
                               );

            //
            // Now force it to be written before we shut down
            //

            WriteQueuedEvents();
#endif
            ReleaseGlobalResource();
            //
            // If the RegistryMonitor is started, wakeup that
            // worker thread and have it handle the rest of the
            // shutdown.
            //
            // Otherwise The main thread should pick up the
            // fact that a shutdown during startup is occuring.
            //
            if (EventFlags & ELF_STARTED_REGISTRY_MONITOR) {
                StopRegistryMonitor();
            }

            break ;

        case SERVICE_CONTROL_PAUSE:

            //
            // If the service is not already paused, pause it.
            //
            state = GetElState();
            if ((state != PAUSED) && (state != PAUSING)) {

                GetGlobalResource (ELF_GLOBAL_EXCLUSIVE);

                // NOTE:    If there was any service-related pause cleanup, we'd
                //          set the status to PAUSE_PENDING, announce it, and
                //          then do the cleanup.
                //

                // Announce that the service is about to be paused
                ElfStatusUpdate(PAUSING);

                // Get into a decent state to pause the service.

                ElfPrepareForPause();


                // Set the status and announce that the service is paused
                ElfStatusUpdate(PAUSED);

                ReleaseGlobalResource();
            }

            break ;

        case SERVICE_CONTROL_CONTINUE:

            //
            // If the service is not already running, un-pause it.
            //

            state = GetElState();
            if ((state != RUNNING) && (state != CONTINUING)) {

                GetGlobalResource (ELF_GLOBAL_EXCLUSIVE);

                // NOTE:    If there was any service-related continue cleanup, we'd
                //          set the status to CONTINUE_PENDING, announce it, and
                //          then do the cleanup.
                //

                // Announce that the service is about to be continued

                ElfStatusUpdate(CONTINUING);

                // Start up the service.

                ElfPrepareForContinue();


                // Set the status and announce that the service is no longer
                // paused
                //
                ElfStatusUpdate(RUNNING);

                ReleaseGlobalResource();
            }

            break ;

        case SERVICE_CONTROL_INTERROGATE:

            // Do nothing; the status gets announced below

        default:
            // WARNING: This should never happen.
            ElfStatusUpdate(UPDATE_ONLY);
            break ;
        }

    return ;

}

DWORD
ElfBeginForcedShutdown(
    IN BOOL     PendingCode,
    IN DWORD    ExitCode,
    IN DWORD    ServiceSpecificCode
    )

/*++

Routine Description:


Arguments:


Return Value:


--*/
{
    DWORD  status;

    EnterCriticalSection(&StatusCriticalSection);

    ElfDbgPrint(("BeginForcedShutdown: Errors - %d  0x%lx\n",
         ExitCode, ServiceSpecificCode));
    //
    // See if the eventlog is already stopping for some reason.
    // It could be that the ControlHandler thread received a control to
    // stop the eventlog just as we decided to stop ourselves.
    //
    if ((ElState != STOPPING) || (ElState != STOPPED)) {
        if (PendingCode == PENDING) {
            ElStatus.dwCurrentState = SERVICE_STOP_PENDING;
            ElState = STOPPING;
        }
        else {
            //
            // The shutdown is to take immediate effect.
            //
            ElStatus.dwCurrentState = SERVICE_STOPPED;
            ElStatus.dwControlsAccepted = 0;
            ElStatus.dwCheckPoint = 0;
            ElStatus.dwWaitHint = 0;
            ElState = STOPPED;
        }

        ElUninstallCode = ExitCode;
        ElSpecificCode = ServiceSpecificCode;

        ElStatus.dwWin32ExitCode = ExitCode;
        ElStatus.dwServiceSpecificExitCode = ServiceSpecificCode;
    }

    //
    // Send the new status to the service controller.
    //
    if (ElfServiceStatusHandle == (SERVICE_STATUS_HANDLE) NULL) {
        ElfDbgPrint(("ElfBeginForcedShutdown, no handle to call SetServiceStatus\n"));

    }
    else if (! SetServiceStatus( ElfServiceStatusHandle, &ElStatus )) {

        status = GetLastError();

        if (status != NERR_Success) {
            ElfDbgPrint(("ElfBeginForcedShutdown,SetServiceStatus Failed %X\n",
                status));
        }
    }

    status = ElState;
    LeaveCriticalSection(&StatusCriticalSection);
    return(status);
}


DWORD
ElfStatusUpdate(
    IN DWORD    NewState
    )

/*++

Routine Description:

    Sends a status to the Service Controller via SetServiceStatus.

    The contents of the status message is controlled by this routine.
    The caller simply passes in the desired state, and this routine does
    the rest.  For instance, if the Eventlog passes in a STARTING state,
    This routine will update the hint count that it maintains, and send
    the appropriate information in the SetServiceStatus call.

    This routine uses transitions in state to send determine which status
    to send.  For instance if the status was STARTING, and has changed
    to RUNNING, this routine sends out an INSTALLED to the Service
    Controller.

Arguments:

    NewState - Can be any of the state flags:
                UPDATE_ONLY - Simply send out the current status
                STARTING - The Eventlog is in the process of initializing
                RUNNING - The Eventlog has finished with initialization
                STOPPING - The Eventlog is in the process of shutting down
                STOPPED - The Eventlog has completed the shutdown.
                PAUSING -
                CONTINUING -
                PAUSED -


Return Value:

    CurrentState - This may not be the same as the NewState that was
        passed in.  It could be that the main thread is sending in a new
        install state just after the Control Handler set the state to
        STOPPING.  In this case, the STOPPING state will be returned so as
        to inform the main thread that a shut-down is in process.

Note:


--*/

{
    DWORD       status;
    BOOL        inhibit = FALSE;    // Used to inhibit sending the status
                                    // to the service controller.

    EnterCriticalSection(&StatusCriticalSection);

    ElfDbgPrint(("ElfStatusUpdate (entry) NewState = %d, OldState = %d\n",
        NewState,ElState));

    if (NewState == STOPPED) {
        if (ElState == STOPPED) {
            //
            // It was already stopped, don't send another SetServiceStatus.
            //
            inhibit = TRUE;
        }
        else {
            //
            // The shut down is complete, indicate that the eventlog
            // has stopped.
            //
            ElStatus.dwCurrentState =  SERVICE_STOPPED;
            ElStatus.dwControlsAccepted = 0;
            ElStatus.dwCheckPoint = 0;
            ElStatus.dwWaitHint = 0;

            ElStatus.dwWin32ExitCode = ElUninstallCode;
            ElStatus.dwServiceSpecificExitCode = ElSpecificCode;

        }
        ElState = NewState;
    }
    else if (NewState == UPDATE_ONLY) {
        inhibit = FALSE;
    }
    else {
        //
        // We are not being asked to change to the STOPPED state.
        //
        switch(ElState) {

        case STARTING:
            if (NewState == STOPPING) {

                ElStatus.dwCurrentState =  SERVICE_STOP_PENDING;
                ElStatus.dwControlsAccepted = 0;
                ElStatus.dwCheckPoint = HintCount++;
                ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
                ElState = NewState;

                EventlogShutdown = TRUE;
            }

            else if (NewState == RUNNING) {

                //
                // The Eventlog Service has completed installation.
                //
                ElStatus.dwCurrentState =  SERVICE_RUNNING;
                ElStatus.dwCheckPoint = 0;
                ElStatus.dwWaitHint = 0;
                //
                // Only stoppable/pausable if developer's build.
                //
#if DBG
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
                                              SERVICE_ACCEPT_PAUSE_CONTINUE |
                                              SERVICE_ACCEPT_SHUTDOWN;
#else
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
#endif
                ElState = NewState;
            }

            else {
                //
                // The NewState must be STARTING.  So update the pending
                // count
                //

                ElStatus.dwCurrentState =  SERVICE_START_PENDING;
                ElStatus.dwControlsAccepted = 0;
                ElStatus.dwCheckPoint = HintCount++;
                ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
            }
            break;

        case RUNNING:
            if (NewState == STOPPING) {

                ElStatus.dwCurrentState =  SERVICE_STOP_PENDING;
                ElStatus.dwControlsAccepted = 0;

                EventlogShutdown = TRUE;
            }
            else if (NewState == PAUSING) {
                ElStatus.dwCurrentState =  SERVICE_PAUSE_PENDING;
            }
            else if (NewState == PAUSED) {
                ElStatus.dwCurrentState =  SERVICE_PAUSED;
            }
            ElStatus.dwCheckPoint = HintCount++;
            ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
            ElState = NewState;

            break;

        case STOPPING:
            //
            // No matter what else was passed in, force the status to
            // indicate that a shutdown is pending.
            //
            ElStatus.dwCurrentState =  SERVICE_STOP_PENDING;
            ElStatus.dwControlsAccepted = 0;
            ElStatus.dwCheckPoint = HintCount++;
            ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
            EventlogShutdown = TRUE;

            break;

        case STOPPED:
            //
            // We're already stopped.  Therefore, an uninstalled status
            // has already been sent.  Do nothing.
            //
            inhibit = TRUE;
            break;
        case PAUSING:
            if (NewState == PAUSED) {
                ElStatus.dwCurrentState =  SERVICE_PAUSED;
                ElStatus.dwCheckPoint = 0;
                ElStatus.dwWaitHint = 0;
            }
            else {
                ElStatus.dwCheckPoint = HintCount++;
                ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
            }
            ElState = NewState;
            break;
        case CONTINUING:
            if (NewState == RUNNING) {
                //
                // The Eventlog Service has completed installation.
                //
                ElStatus.dwCurrentState =  SERVICE_RUNNING;
                ElStatus.dwCheckPoint = 0;
                ElStatus.dwWaitHint = 0;
                //
                // Only stoppable/pausable if developer's build.
                //
#if DBG
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
                                              SERVICE_ACCEPT_PAUSE_CONTINUE |
                                              SERVICE_ACCEPT_SHUTDOWN;
#else
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
#endif
                ElState = NewState;
            }
            else {
                ElStatus.dwCheckPoint = HintCount++;
            }
            break;
        case PAUSED:
            if (NewState == CONTINUING) {
                ElStatus.dwCheckPoint = HintCount++;
                ElStatus.dwWaitHint = ELF_WAIT_HINT_TIME;
                ElStatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
            }
            else if (NewState == RUNNING) {
                //
                // The Eventlog Service has completed installation.
                //
                ElStatus.dwCurrentState =  SERVICE_RUNNING;
                ElStatus.dwCheckPoint = 0;
                ElStatus.dwWaitHint = 0;
                //
                // Only stoppable/pausable if developer's build.
                //
#if DBG
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
                                              SERVICE_ACCEPT_PAUSE_CONTINUE |
                                              SERVICE_ACCEPT_SHUTDOWN;
#else
                ElStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
#endif
                ElState = NewState;
            }
            break;
        }
    }

    if (!inhibit) {
        if (ElfServiceStatusHandle == (SERVICE_STATUS_HANDLE) NULL) {
            ElfDbgPrint(("ElfStatusUpdate, no handle to call SetServiceStatus\n"));

        }
        else if (! SetServiceStatus( ElfServiceStatusHandle, &ElStatus )) {

            status = GetLastError();

            if (status != NERR_Success) {
                ElfDbgPrint(("ElfStatusUpdate, SetServiceStatus Failed %d\n",
                    status));
            }
        }
    }

    ElfDbgPrint(("ElfStatusUpdate (exit) State = %d\n",ElState));
    status = ElState;
    LeaveCriticalSection(&StatusCriticalSection);
    return(status);
}


DWORD
GetElState (
    VOID
    )

/*++

Routine Description:

    Obtains the state of the Eventlog Service.  This state information
    is protected as a critical section such that only one thread can
    modify or read it at a time.

Arguments:

    none

Return Value:

    The Eventlog State is returned as the return value.

--*/
{
    DWORD   status;

    EnterCriticalSection(&StatusCriticalSection);
    status = ElState;
    LeaveCriticalSection(&StatusCriticalSection);

    return(status);
}


VOID
ElInitStatus(VOID)

/*++

Routine Description:

    Initializes the critical section that is used to guard access to the
    status database.

Arguments:

    none

Return Value:

    none

Note:


--*/
{
    InitializeCriticalSection(&StatusCriticalSection);

    ElStatus.dwCurrentState =  SERVICE_START_PENDING;
    ElStatus.dwServiceType = SERVICE_WIN32;

}


VOID
ElCleanupStatus(VOID)

/*++

Routine Description:

    Deletes the critical section used to control access to the thread and
    status database.

Arguments:

    none

Return Value:

    none

Note:


--*/
{
    DeleteCriticalSection(&StatusCriticalSection);
}