2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/types.h>
24 /* This file provides an interface for utilities to run in the background
25 * as daemons on POSIX platforms like Linux or as services on Windows platform.
26 * Some of the functionalities defined in this file are only applicable to
27 * POSIX platforms and some are applicable only on Windows. As such, the
28 * function definitions unique to each platform are separated out with
29 * ifdef macros. More descriptive comments on individual functions are provided
30 * in daemon.c (for Linux) and daemon-windows.c (for Windows).
32 * The DAEMON_OPTION_ENUMS, DAEMON_LONG_OPTIONS and DAEMON_OPTION_HANDLERS
33 * macros are useful for parsing command-line options in individual utilities.
34 * For e.g., the command-line option "--detach" is recognized on Linux
35 * and results in calling the set_detach() function. The same option is not
36 * recognized on Windows platform.
40 #define DAEMON_OPTION_ENUMS \
43 OPT_OVERWRITE_PIDFILE, \
47 #define DAEMON_LONG_OPTIONS \
48 {"detach", no_argument, NULL, OPT_DETACH}, \
49 {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \
50 {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \
51 {"overwrite-pidfile", no_argument, NULL, OPT_OVERWRITE_PIDFILE}, \
52 {"monitor", no_argument, NULL, OPT_MONITOR}
54 #define DAEMON_OPTION_HANDLERS \
64 set_pidfile(optarg); \
67 case OPT_OVERWRITE_PIDFILE: \
68 ignore_existing_pidfile(); \
72 daemon_set_monitor(); \
75 void set_detach(void);
76 void daemon_set_monitor(void);
77 void set_pidfile(const char *name);
78 void set_no_chdir(void);
79 void ignore_existing_pidfile(void);
80 pid_t read_pidfile(const char *name);
82 #define DAEMON_OPTION_ENUMS \
86 #define DAEMON_LONG_OPTIONS \
87 {"service", no_argument, NULL, OPT_SERVICE}, \
88 {"service-monitor", no_argument, NULL, OPT_SERVICE_MONITOR}
90 #define DAEMON_OPTION_HANDLERS \
94 case OPT_SERVICE_MONITOR: \
97 void control_handler(DWORD request);
100 bool get_detach(void);
101 void daemon_save_fd(int fd);
102 void daemonize(void);
103 void daemonize_start(void);
104 void daemonize_complete(void);
105 void daemon_usage(void);
106 void service_start(int *argcp, char **argvp[]);
107 void service_stop(void);
108 bool should_service_stop(void);
110 #endif /* daemon.h */