ovn-northd: Ensure that flows are added to correct types of datapaths.
[cascardo/ovs.git] / lib / daemon.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
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:
7  *
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.
15  */
16
17 #ifndef DAEMON_H
18 #define DAEMON_H 1
19
20 #include <limits.h>
21 #include <stdbool.h>
22 #include <sys/types.h>
23
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-unix.c (for POSIX platforms) and daemon-windows.c (for Windows).
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.
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.
37  */
38
39 #ifndef _WIN32
40 #define DAEMON_OPTION_ENUMS                     \
41     OPT_DETACH,                                 \
42     OPT_NO_SELF_CONFINEMENT,                    \
43     OPT_NO_CHDIR,                               \
44     OPT_OVERWRITE_PIDFILE,                      \
45     OPT_PIDFILE,                                \
46     OPT_MONITOR,                                \
47     OPT_USER_GROUP
48
49 #define DAEMON_LONG_OPTIONS                                              \
50         {"detach",            no_argument, NULL, OPT_DETACH},            \
51         {"no-self-confinement", no_argument, NULL, OPT_NO_SELF_CONFINEMENT}, \
52         {"no-chdir",          no_argument, NULL, OPT_NO_CHDIR},          \
53         {"pidfile",           optional_argument, NULL, OPT_PIDFILE},     \
54         {"overwrite-pidfile", no_argument, NULL, OPT_OVERWRITE_PIDFILE}, \
55         {"monitor",           no_argument, NULL, OPT_MONITOR},           \
56         {"user",              required_argument, NULL, OPT_USER_GROUP}
57
58 #define DAEMON_OPTION_HANDLERS                  \
59         case OPT_DETACH:                        \
60             set_detach();                       \
61             break;                              \
62                                                 \
63         case OPT_NO_SELF_CONFINEMENT:           \
64             daemon_disable_self_confinement();  \
65             break;                              \
66                                                 \
67         case OPT_NO_CHDIR:                      \
68             set_no_chdir();                     \
69             break;                              \
70                                                 \
71         case OPT_PIDFILE:                       \
72             set_pidfile(optarg);                \
73             break;                              \
74                                                 \
75         case OPT_OVERWRITE_PIDFILE:             \
76             ignore_existing_pidfile();          \
77             break;                              \
78                                                 \
79         case OPT_MONITOR:                       \
80             daemon_set_monitor();               \
81             break;                              \
82                                                 \
83         case OPT_USER_GROUP:                    \
84             daemon_set_new_user(optarg);        \
85             break;
86
87 void set_detach(void);
88 void daemon_set_monitor(void);
89 void set_no_chdir(void);
90 void ignore_existing_pidfile(void);
91 pid_t read_pidfile(const char *name);
92 #else
93 #define DAEMON_OPTION_ENUMS                    \
94     OPT_DETACH,                                \
95     OPT_NO_SELF_CONFINEMENT,                   \
96     OPT_NO_CHDIR,                              \
97     OPT_PIDFILE,                               \
98     OPT_PIPE_HANDLE,                           \
99     OPT_SERVICE,                               \
100     OPT_SERVICE_MONITOR,                       \
101     OPT_USER_GROUP
102
103 #define DAEMON_LONG_OPTIONS                                               \
104         {"detach",             no_argument, NULL, OPT_DETACH},            \
105         {"no-self-confinement", no_argument, NULL, OPT_NO_SELF_CONFINEMENT}, \
106         {"no-chdir",           no_argument, NULL, OPT_NO_CHDIR},          \
107         {"pidfile",            optional_argument, NULL, OPT_PIDFILE},     \
108         {"pipe-handle",        required_argument, NULL, OPT_PIPE_HANDLE}, \
109         {"service",            no_argument, NULL, OPT_SERVICE},           \
110         {"service-monitor",    no_argument, NULL, OPT_SERVICE_MONITOR},   \
111         {"user",               required_argument, NULL, OPT_USER_GROUP}
112
113 #define DAEMON_OPTION_HANDLERS                  \
114         case OPT_DETACH:                        \
115             break;                              \
116                                                 \
117         case OPT_NO_SELF_CONFINEMENT:           \
118             daemon_disable_self_confinement();  \
119             break;                              \
120                                                 \
121         case OPT_NO_CHDIR:                      \
122             break;                              \
123                                                 \
124         case OPT_PIDFILE:                       \
125             set_pidfile(optarg);                \
126             break;                              \
127                                                 \
128         case OPT_PIPE_HANDLE:                   \
129             set_pipe_handle(optarg);            \
130             break;                              \
131                                                 \
132         case OPT_SERVICE:                       \
133             break;                              \
134                                                 \
135         case OPT_SERVICE_MONITOR:               \
136             break;                              \
137                                                 \
138         case OPT_USER_GROUP:                    \
139             daemon_set_new_user(optarg);
140
141 void control_handler(DWORD request);
142 void set_pipe_handle(const char *pipe_handle);
143 #endif /* _WIN32 */
144
145 bool get_detach(void);
146 void daemon_save_fd(int fd);
147 void daemonize(void);
148 void daemonize_start(bool access_datapath);
149 void daemonize_complete(void);
150 void daemon_set_new_user(const char * user_spec);
151 void daemon_become_new_user(bool access_datapath);
152 void daemon_usage(void);
153 void daemon_disable_self_confinement(void);
154 bool daemon_should_self_confine(void);
155 void service_start(int *argcp, char **argvp[]);
156 void service_stop(void);
157 bool should_service_stop(void);
158 void set_pidfile(const char *name);
159 void close_standard_fds(void);
160
161 #endif /* daemon.h */