]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #ifndef DAEMON_H | |
18 | #define DAEMON_H 1 | |
19 | ||
91a1e24d | 20 | #include <limits.h> |
064af421 BP |
21 | #include <stdbool.h> |
22 | #include <sys/types.h> | |
23 | ||
fda546bd GS |
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 | |
3834bcf2 | 30 | * in daemon-unix.c (for POSIX platforms) and daemon-windows.c (for Windows). |
fda546bd GS |
31 | |
32 | * The DAEMON_OPTION_ENUMS, DAEMON_LONG_OPTIONS and DAEMON_OPTION_HANDLERS | |
33 | * macros are useful for parsing command-line options in individual utilities. | |
4ec4776c GS |
34 | * For e.g., the command-line option "--monitor" is recognized on Linux |
35 | * and results in calling the daemon_set_monitor() function. The same option is | |
36 | * not recognized on Windows platform. | |
fda546bd GS |
37 | */ |
38 | ||
39 | #ifndef _WIN32 | |
8274ae95 BP |
40 | #define DAEMON_OPTION_ENUMS \ |
41 | OPT_DETACH, \ | |
81d2f75c | 42 | OPT_NO_SELF_CONFINEMENT, \ |
8274ae95 BP |
43 | OPT_NO_CHDIR, \ |
44 | OPT_OVERWRITE_PIDFILE, \ | |
45 | OPT_PIDFILE, \ | |
e91b927d AZ |
46 | OPT_MONITOR, \ |
47 | OPT_USER_GROUP | |
91a1e24d | 48 | |
e91b927d AZ |
49 | #define DAEMON_LONG_OPTIONS \ |
50 | {"detach", no_argument, NULL, OPT_DETACH}, \ | |
81d2f75c | 51 | {"no-self-confinement", no_argument, NULL, OPT_NO_SELF_CONFINEMENT}, \ |
e91b927d AZ |
52 | {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \ |
53 | {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \ | |
e3c17733 | 54 | {"overwrite-pidfile", no_argument, NULL, OPT_OVERWRITE_PIDFILE}, \ |
e91b927d AZ |
55 | {"monitor", no_argument, NULL, OPT_MONITOR}, \ |
56 | {"user", required_argument, NULL, OPT_USER_GROUP} | |
064af421 BP |
57 | |
58 | #define DAEMON_OPTION_HANDLERS \ | |
e7bd7d78 | 59 | case OPT_DETACH: \ |
064af421 BP |
60 | set_detach(); \ |
61 | break; \ | |
62 | \ | |
81d2f75c AA |
63 | case OPT_NO_SELF_CONFINEMENT: \ |
64 | daemon_disable_self_confinement(); \ | |
65 | break; \ | |
66 | \ | |
91a1e24d JP |
67 | case OPT_NO_CHDIR: \ |
68 | set_no_chdir(); \ | |
69 | break; \ | |
70 | \ | |
e7bd7d78 | 71 | case OPT_PIDFILE: \ |
064af421 BP |
72 | set_pidfile(optarg); \ |
73 | break; \ | |
74 | \ | |
e7bd7d78 | 75 | case OPT_OVERWRITE_PIDFILE: \ |
064af421 | 76 | ignore_existing_pidfile(); \ |
ff8decf1 BP |
77 | break; \ |
78 | \ | |
79 | case OPT_MONITOR: \ | |
80 | daemon_set_monitor(); \ | |
e91b927d AZ |
81 | break; \ |
82 | \ | |
83 | case OPT_USER_GROUP: \ | |
84 | daemon_set_new_user(optarg); \ | |
064af421 BP |
85 | break; |
86 | ||
7e5f6b1e BP |
87 | #define DAEMON_OPTION_CASES \ |
88 | case OPT_DETACH: \ | |
89 | case OPT_NO_SELF_CONFINEMENT: \ | |
90 | case OPT_NO_CHDIR: \ | |
91 | case OPT_PIDFILE: \ | |
92 | case OPT_OVERWRITE_PIDFILE: \ | |
93 | case OPT_MONITOR: \ | |
94 | case OPT_USER_GROUP: | |
95 | ||
fda546bd GS |
96 | void set_detach(void); |
97 | void daemon_set_monitor(void); | |
91a1e24d | 98 | void set_no_chdir(void); |
fda546bd GS |
99 | void ignore_existing_pidfile(void); |
100 | pid_t read_pidfile(const char *name); | |
101 | #else | |
4ec4776c GS |
102 | #define DAEMON_OPTION_ENUMS \ |
103 | OPT_DETACH, \ | |
81d2f75c | 104 | OPT_NO_SELF_CONFINEMENT, \ |
fd9164cd | 105 | OPT_NO_CHDIR, \ |
a9e9db79 | 106 | OPT_PIDFILE, \ |
4ec4776c GS |
107 | OPT_PIPE_HANDLE, \ |
108 | OPT_SERVICE, \ | |
f9280ca7 GS |
109 | OPT_SERVICE_MONITOR, \ |
110 | OPT_USER_GROUP | |
fda546bd | 111 | |
4ec4776c GS |
112 | #define DAEMON_LONG_OPTIONS \ |
113 | {"detach", no_argument, NULL, OPT_DETACH}, \ | |
117ec423 | 114 | {"no-self-confinement", no_argument, NULL, OPT_NO_SELF_CONFINEMENT}, \ |
fd9164cd | 115 | {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \ |
a9e9db79 | 116 | {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \ |
4ec4776c GS |
117 | {"pipe-handle", required_argument, NULL, OPT_PIPE_HANDLE}, \ |
118 | {"service", no_argument, NULL, OPT_SERVICE}, \ | |
f9280ca7 | 119 | {"service-monitor", no_argument, NULL, OPT_SERVICE_MONITOR}, \ |
e91b927d | 120 | {"user", required_argument, NULL, OPT_USER_GROUP} |
fda546bd GS |
121 | |
122 | #define DAEMON_OPTION_HANDLERS \ | |
4ec4776c GS |
123 | case OPT_DETACH: \ |
124 | break; \ | |
125 | \ | |
81d2f75c AA |
126 | case OPT_NO_SELF_CONFINEMENT: \ |
127 | daemon_disable_self_confinement(); \ | |
128 | break; \ | |
129 | \ | |
fd9164cd GS |
130 | case OPT_NO_CHDIR: \ |
131 | break; \ | |
132 | \ | |
a9e9db79 GS |
133 | case OPT_PIDFILE: \ |
134 | set_pidfile(optarg); \ | |
135 | break; \ | |
136 | \ | |
4ec4776c GS |
137 | case OPT_PIPE_HANDLE: \ |
138 | set_pipe_handle(optarg); \ | |
139 | break; \ | |
140 | \ | |
fda546bd GS |
141 | case OPT_SERVICE: \ |
142 | break; \ | |
143 | \ | |
144 | case OPT_SERVICE_MONITOR: \ | |
e91b927d AZ |
145 | break; \ |
146 | \ | |
147 | case OPT_USER_GROUP: \ | |
f9280ca7 | 148 | daemon_set_new_user(optarg); |
fda546bd | 149 | |
7e5f6b1e BP |
150 | #define DAEMON_OPTION_CASES \ |
151 | case OPT_DETACH: \ | |
152 | case OPT_NO_SELF_CONFINEMENT: \ | |
153 | case OPT_NO_CHDIR: \ | |
154 | case OPT_PIDFILE: \ | |
155 | case OPT_PIPE_HANDLE: \ | |
156 | case OPT_SERVICE: \ | |
157 | case OPT_SERVICE_MONITOR: \ | |
158 | case OPT_USER_GROUP: | |
159 | ||
fda546bd | 160 | void control_handler(DWORD request); |
4ec4776c | 161 | void set_pipe_handle(const char *pipe_handle); |
fda546bd GS |
162 | #endif /* _WIN32 */ |
163 | ||
eb077b26 | 164 | bool get_detach(void); |
7d0c5973 | 165 | void daemon_save_fd(int fd); |
064af421 | 166 | void daemonize(void); |
e91b927d | 167 | void daemonize_start(bool access_datapath); |
95440284 | 168 | void daemonize_complete(void); |
e91b927d AZ |
169 | void daemon_set_new_user(const char * user_spec); |
170 | void daemon_become_new_user(bool access_datapath); | |
064af421 | 171 | void daemon_usage(void); |
81d2f75c AA |
172 | void daemon_disable_self_confinement(void); |
173 | bool daemon_should_self_confine(void); | |
fda546bd GS |
174 | void service_start(int *argcp, char **argvp[]); |
175 | void service_stop(void); | |
176 | bool should_service_stop(void); | |
a9e9db79 | 177 | void set_pidfile(const char *name); |
d6bc33f3 | 178 | void close_standard_fds(void); |
8aee05cc | 179 | |
064af421 | 180 | #endif /* daemon.h */ |