summaryrefslogtreecommitdiff
path: root/lib/daemon-windows.c
blob: 4e6bbe0f040b85f81106127bdaa2e88ae36078a0 (plain)
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
/*
 * Copyright (c) 2014, 2017 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#include "daemon.h"
#include "daemon-private.h"
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <unistd.h>
#include "dirs.h"
#include "fatal-signal.h"
#include "ovs-thread.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(daemon_windows);

/* Constants for flock function */
#define	LOCK_SHARED	0x0                     /* Shared lock. */
#define	LOCK_UNLOCK	0x80000000              /* Unlock. Custom value. */

static bool service_create;          /* Was --service specified? */
static bool service_started;         /* Have we dispatched service to start? */

/* --service-monitor: Should the service be restarted if it dies
 * unexpectedly? */
static bool monitor;

bool detach;                 /* Was --detach specified? */
static bool detached;        /* Running as the child process. */
static HANDLE write_handle;  /* End of pipe to write to parent. */

char *pidfile;                 /* --pidfile: Name of pidfile (null if none). */
static FILE *filep_pidfile;    /* File pointer to access the pidfile. */

/* Handle to the Services Manager and the created service. */
static SC_HANDLE manager, service;

/* Handle to the status information structure for the current service. */
static SERVICE_STATUS_HANDLE hstatus;

/* Hold the service's current status. */
static SERVICE_STATUS service_status;

/* Handle to an event object used to wakeup from poll_block(). */
static HANDLE wevent;

/* Hold the arguments sent to the main function. */
static int sargc;
static char ***sargvp;

static void check_service(void);
static void handle_scm_callback(void);
static void init_service_status(void);
static void set_config_failure_actions(void);

static bool detach_process(int argc, char *argv[]);

extern int main(int argc, char *argv[]);

void
daemon_usage(void)
{
    printf(
        "\nService options:\n"
        "  --service               run in background as a service.\n"
        "  --service-monitor       restart the service in case of an "
                                   "unexpected failure. \n");
}

/* Sets up a following call to service_start() to detach from the foreground
 * session, running this process in the background.  */
void
set_detach(void)
{
    detach = true;
}

/* Registers the call-back and configures the actions in case of a failure
 * with the Windows services manager. */
void
service_start(int *argcp, char **argvp[])
{
    int argc = *argcp;
    char **argv = *argvp;
    int i;
    SERVICE_TABLE_ENTRY service_table[] = {
        {(LPTSTR)program_name, (LPSERVICE_MAIN_FUNCTION)main},
        {NULL, NULL}
    };

    /* If one of the command line option is "--detach", we create
     * a new process in case of parent, wait for child to start and exit.
     * In case of the child, we just return. We should not be creating a
     * service in either case. */
    if (detach_process(argc, argv)) {
        return;
    }

    /* 'service_started' is 'false' when service_start() is called the first
     * time.  It is 'true', when it is called the second time by the Windows
     * services manager. */
    if (service_started) {
        init_service_status();

        wevent = CreateEvent(NULL, TRUE, FALSE, NULL);
        if (!wevent) {
            char *msg_buf = ovs_lasterror_to_string();
            VLOG_FATAL("Failed to create a event (%s).", msg_buf);
        }

        poll_wevent_wait(wevent);

        /* Register the control handler. This function is called by the service
         * manager to stop the service. */
        hstatus = RegisterServiceCtrlHandler(program_name,
                                         (LPHANDLER_FUNCTION)control_handler);
        if (!hstatus) {
            char *msg_buf = ovs_lasterror_to_string();
            VLOG_FATAL("Failed to register the service control handler (%s).",
                        msg_buf);
        }

        if (monitor) {
            set_config_failure_actions();
        }

        /* When the service control manager does the call back, it does not
         * send the same arguments as sent to the main function during the
         * service start. So, use the arguments passed over during the first
         * time. */
        *argcp = sargc;
        *argvp = *sargvp;

        /* Enable default error mode so we can take advantage of WER
         * (Windows Error Reporting) crash dumps.
         * Being a service it does not allow for WER window pop-up.
         * XXX implement our on crash dump collection mechanism. */
        SetErrorMode(0);

        return;
    }

    assert_single_threaded();

    /* A reference to arguments passed to the main function the first time.
     * We need it after the call-back from service control manager. */
    sargc = argc;
    sargvp = argvp;

    /* We are only interested in the '--service' and '--service-monitor'
     * options before the call-back from the service control manager. */
    for (i = 0; i < argc; i ++) {
        if (!strcmp(argv[i], "--service")) {
            service_create = true;
        } else if (!strcmp(argv[i], "--service-monitor")) {
            monitor = true;
        }
    }

    /* If '--service' is not a command line option, run in foreground. */
    if (!service_create) {
        return;
    }

    /* If we have been configured to run as a service, then that service
     * should already have been created either manually or through a start up
     * script. */
    check_service();

    service_started = true;

    /* StartServiceCtrlDispatcher blocks and returns after the service is
     * stopped. */
    if (!StartServiceCtrlDispatcher(service_table)) {
        char *msg_buf = ovs_lasterror_to_string();
        VLOG_FATAL("Failed at StartServiceCtrlDispatcher (%s)", msg_buf);
    }
    exit(0);
}

/* This function is registered with the Windows services manager through
 * a call to RegisterServiceCtrlHandler() and will be called by the Windows
 * services manager asynchronously to stop the service. */
void
control_handler(DWORD request)
{
    switch (request) {
    case SERVICE_CONTROL_STOP:
    case SERVICE_CONTROL_SHUTDOWN:
        service_status.dwCurrentState = SERVICE_STOPPED;
        service_status.dwWin32ExitCode = NO_ERROR;
        SetEvent(wevent);
        SetServiceStatus(hstatus, &service_status);
        break;

    default:
        break;
    }
}

/* Return 'true' if the Windows services manager has called the
 * control_handler() and asked the program to terminate. */
bool
should_service_stop(void)
{
    if (service_started) {
        if (service_status.dwCurrentState != SERVICE_RUNNING) {
            return true;
        } else {
            poll_wevent_wait(wevent);
        }
    }
    return false;
}

/* Set the service as stopped. The control manager will terminate the
 * service soon after this call. Hence, this should ideally be the last
 * call before termination. */
void
service_stop()
{
    if (!service_started) {
        return;
    }
    fatal_signal_atexit_handler();

    ResetEvent(wevent);
    CloseHandle(wevent);

    service_status.dwCurrentState = SERVICE_STOPPED;
    service_status.dwWin32ExitCode = NO_ERROR;
    SetServiceStatus(hstatus, &service_status);
}

/* Call this function to signal that the daemon is ready. init_service()
 * or control_handler() has already initalized/set the
 * service_status.dwCurrentState .*/
static void
service_complete(void)
{
    if (hstatus) {
        SetServiceStatus(hstatus, &service_status);
    }
}

/* Check whether 'program_name' has been created as a service. */
static void
check_service()
{
    /* Establish a connection to the local service control manager. */
    manager = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
    if (!manager) {
        char *msg_buf = ovs_lasterror_to_string();
        VLOG_FATAL("Failed to open the service control manager (%s).",
                   msg_buf);
    }

    service = OpenService(manager, program_name, SERVICE_ALL_ACCESS);
    if (!service) {
        char *msg_buf = ovs_lasterror_to_string();
        VLOG_FATAL("Failed to open service (%s).", msg_buf);
    }
}

/* Service status of a service can be checked asynchronously through
 * tools like 'sc' or through Windows services manager and is set
 * through a call to SetServiceStatus(). */
static void
init_service_status()
{
    /* The service runs in its own process. */
    service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

    /* The control codes the service accepts. */
    service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
                                            SERVICE_ACCEPT_SHUTDOWN;

    /* Initialize the current state as SERVICE_RUNNING. */
    service_status.dwCurrentState = SERVICE_RUNNING;

    /* The exit code to indicate if there was an error. */
    service_status.dwWin32ExitCode = NO_ERROR;

    /* The checkpoint value the service increments periodically. Set as 0
     * as we do not plan to periodically increment the value. */
    service_status.dwCheckPoint = 0;

    /* The estimated time required for the stop operation in ms. */
    service_status.dwWaitHint = 1000;
}

/* In case of an unexpected termination, configure the action to be
 * taken. */
static void
set_config_failure_actions()
{
    /* In case of a failure, restart the process the first two times
     * After 'dwResetPeriod', the failure count is reset. */
    SC_ACTION fail_action[3] = {
        {SC_ACTION_RESTART, 0},
        {SC_ACTION_RESTART, 0},
        {SC_ACTION_NONE, 0}
    };
    SERVICE_FAILURE_ACTIONS service_fail_action;

    /* Reset failure count after (in seconds). */
    service_fail_action.dwResetPeriod = 10;

    /* Reboot message. */
    service_fail_action.lpRebootMsg = NULL;

    /* The command line of the process. */
    service_fail_action.lpCommand = NULL;

    /* Number of elements in 'fail_actions'. */
    service_fail_action.cActions = sizeof(fail_action)/sizeof(fail_action[0]);

    /* A pointer to an array of SC_ACTION structures. */
    service_fail_action.lpsaActions = fail_action;

    if (!ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS,
                              &service_fail_action)) {
        char *msg_buf = ovs_lasterror_to_string();
        VLOG_FATAL("Failed to configure service fail actions (%s).", msg_buf);
    }
}

/* When a daemon is passed the --detach option, we create a new
 * process and pass an additional non-documented option called --pipe-handle.
 * Through this option, the parent passes one end of a pipe handle. */
void
set_pipe_handle(const char *pipe_handle)
{
    write_handle = (HANDLE) atoi(pipe_handle);
}

/* If one of the command line option is "--detach", creates
 * a new process in case of parent, waits for child to start and exits.
 * In case of the child, returns. */
static bool
detach_process(int argc, char *argv[])
{
    SECURITY_ATTRIBUTES sa;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    HANDLE read_pipe, write_pipe;
    char *buffer;
    int error, i;
    char ch;

    /* We are only interested in the '--detach' and '--pipe-handle'. */
    for (i = 0; i < argc; i ++) {
        if (!detach && !strcmp(argv[i], "--detach")) {
            detach = true;
        } else if (!strncmp(argv[i], "--pipe-handle", 13)) {
            /* If running as a child, return. */
            detached = true;
            return true;
        }
    }

    /* Nothing to do if the option --detach is not set. */
    if (!detach) {
        return false;
    }

    /* Set the security attribute such that a process created will
     * inherit the pipe handles. */
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;

    /* Create an anonymous pipe to communicate with the child. */
    error = CreatePipe(&read_pipe, &write_pipe, &sa, 0);
    if (!error) {
        VLOG_FATAL("CreatePipe failed (%s)", ovs_lasterror_to_string());
    }

    GetStartupInfo(&si);

    /* To the child, we pass an extra argument '--pipe-handle=write_pipe' */
    buffer = xasprintf("%s %s=%ld", GetCommandLine(), "--pipe-handle",
                       write_pipe);

    /* Create a detached child */
    error = CreateProcess(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
                          NULL, NULL, &si, &pi);
    if (!error) {
        VLOG_FATAL("CreateProcess failed (%s)", ovs_lasterror_to_string());
    }

    /* Close one end of the pipe in the parent. */
    CloseHandle(write_pipe);

    /* Block and wait for child to say it is ready. */
    error = ReadFile(read_pipe, &ch, 1, NULL, NULL);
    if (!error) {
        VLOG_FATAL("Failed to read from child (%s)",
                   ovs_lasterror_to_string());
    }
    /* The child has successfully started and is ready. */
    exit(0);
}

static void
flock(FILE* fd, int operation)
{
    HANDLE hFile;
    OVERLAPPED ov = {0};

    hFile = (HANDLE)_get_osfhandle(fileno(fd));
    if (hFile == INVALID_HANDLE_VALUE) {
        VLOG_FATAL("Failed to get PID file handle (%s).",
                   ovs_strerror(errno));
    }

    if (operation & LOCK_UNLOCK) {
        if (UnlockFileEx(hFile, 0, 1, 0, &ov) == 0) {
            VLOG_FATAL("Failed to unlock PID file (%s).",
                       ovs_lasterror_to_string());
        }
    } else {
       /* Use LOCKFILE_FAIL_IMMEDIATELY flag to avoid hang of another daemon that tries to
           acquire exclusive lock over the same PID file */
        if (LockFileEx(hFile, operation | LOCKFILE_FAIL_IMMEDIATELY,
                       0, 1, 0, &ov) == FALSE) {
            VLOG_FATAL("Failed to lock PID file (%s).",
                       ovs_lasterror_to_string());
        }
    }
}

static void
unlink_pidfile(void)
{
    if (filep_pidfile) {
        /* Remove the shared lock on file */
        flock(filep_pidfile, LOCK_UNLOCK);
        fclose(filep_pidfile);
    }
    if (pidfile) {
        unlink(pidfile);
    }
}

/* If a pidfile has been configured, creates it and stores the running
 * process's pid in it.  Ensures that the pidfile will be deleted when the
 * process exits. */
static void
make_pidfile(void)
{
    int error;

    error = GetFileAttributes(pidfile);
    if (error != INVALID_FILE_ATTRIBUTES) {
        /* pidfile exists. Try to unlink() it. */
        error = unlink(pidfile);
        if (error) {
            VLOG_FATAL("Failed to delete existing pidfile %s (%s)", pidfile,
                       ovs_strerror(errno));
        }
    }

    filep_pidfile = fopen(pidfile, "w");
    if (filep_pidfile == NULL) {
        VLOG_FATAL("failed to open %s (%s)", pidfile, ovs_strerror(errno));
    }

    flock(filep_pidfile, LOCKFILE_EXCLUSIVE_LOCK);

    fatal_signal_add_hook(unlink_pidfile, NULL, NULL, true);

    fprintf(filep_pidfile, "%ld\n", (long int) getpid());
    if (fflush(filep_pidfile) == EOF) {
        VLOG_FATAL("Failed to write into the pidfile %s", pidfile);
    }

    flock(filep_pidfile, LOCK_SHARED);
    /* This will remove the exclusive lock. The shared lock will remain */
    flock(filep_pidfile, LOCK_UNLOCK);

    /* Don't close the pidfile till the process exits. */
}

void
daemonize_start(bool access_datapath OVS_UNUSED,
                bool access_hardware_ports OVS_UNUSED)
{
    if (pidfile) {
        make_pidfile();
    }
}

void
daemonize_complete(void)
{
    /* If running as a child because '--detach' option was specified,
     * communicate with the parent to inform that the child is ready. */
    if (detached) {
        int error;

        close_standard_fds();

        error = WriteFile(write_handle, "a", 1, NULL, NULL);
        if (!error) {
            VLOG_FATAL("Failed to communicate with the parent (%s)",
                       ovs_lasterror_to_string());
        }
    }

    service_complete();
}

void
daemon_become_new_user(bool access_datapath OVS_UNUSED,
                       bool access_hardware_ports OVS_UNUSED)
{
}

/* Returns the file name that would be used for a pidfile if 'name' were
 * provided to set_pidfile().  The caller must free the returned string. */
char *
make_pidfile_name(const char *name)
{
    if (name) {
        if (strchr(name, ':')) {
            return xstrdup(name);
        } else {
            return xasprintf("%s/%s", ovs_rundir(), name);
        }
    } else {
        return xasprintf("%s/%s.pid", ovs_rundir(), program_name);
    }
}

void
daemon_set_new_user(const char *user_spec OVS_UNUSED)
{
    VLOG_FATAL("--user options is not currently supported.");
}