odp-util: Format and scan multiple MPLS labels.
[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_CHDIR,                               \
43     OPT_OVERWRITE_PIDFILE,                      \
44     OPT_PIDFILE,                                \
45     OPT_MONITOR,                                \
46     OPT_USER_GROUP
47
48 #define DAEMON_LONG_OPTIONS                                              \
49         {"detach",            no_argument, NULL, OPT_DETACH},            \
50         {"no-chdir",          no_argument, NULL, OPT_NO_CHDIR},          \
51         {"pidfile",           optional_argument, NULL, OPT_PIDFILE},     \
52         {"overwrite-pidfile", no_argument, NULL, OPT_OVERWRITE_PIDFILE}, \
53         {"monitor",           no_argument, NULL, OPT_MONITOR},           \
54         {"user",              required_argument, NULL, OPT_USER_GROUP}
55
56 #define DAEMON_OPTION_HANDLERS                  \
57         case OPT_DETACH:                        \
58             set_detach();                       \
59             break;                              \
60                                                 \
61         case OPT_NO_CHDIR:                      \
62             set_no_chdir();                     \
63             break;                              \
64                                                 \
65         case OPT_PIDFILE:                       \
66             set_pidfile(optarg);                \
67             break;                              \
68                                                 \
69         case OPT_OVERWRITE_PIDFILE:             \
70             ignore_existing_pidfile();          \
71             break;                              \
72                                                 \
73         case OPT_MONITOR:                       \
74             daemon_set_monitor();               \
75             break;                              \
76                                                 \
77         case OPT_USER_GROUP:                    \
78             daemon_set_new_user(optarg);        \
79             break;
80
81 void set_detach(void);
82 void daemon_set_monitor(void);
83 void set_no_chdir(void);
84 void ignore_existing_pidfile(void);
85 pid_t read_pidfile(const char *name);
86 #else
87 #define DAEMON_OPTION_ENUMS                    \
88     OPT_DETACH,                                \
89     OPT_NO_CHDIR,                              \
90     OPT_PIDFILE,                               \
91     OPT_PIPE_HANDLE,                           \
92     OPT_SERVICE,                               \
93     OPT_SERVICE_MONITOR,                       \
94     OPT_USER_GROUP
95
96 #define DAEMON_LONG_OPTIONS                                               \
97         {"detach",             no_argument, NULL, OPT_DETACH},            \
98         {"no-chdir",           no_argument, NULL, OPT_NO_CHDIR},          \
99         {"pidfile",            optional_argument, NULL, OPT_PIDFILE},     \
100         {"pipe-handle",        required_argument, NULL, OPT_PIPE_HANDLE}, \
101         {"service",            no_argument, NULL, OPT_SERVICE},           \
102         {"service-monitor",    no_argument, NULL, OPT_SERVICE_MONITOR},   \
103         {"user",               required_argument, NULL, OPT_USER_GROUP}
104
105 #define DAEMON_OPTION_HANDLERS                  \
106         case OPT_DETACH:                        \
107             break;                              \
108                                                 \
109         case OPT_NO_CHDIR:                      \
110             break;                              \
111                                                 \
112         case OPT_PIDFILE:                       \
113             set_pidfile(optarg);                \
114             break;                              \
115                                                 \
116         case OPT_PIPE_HANDLE:                   \
117             set_pipe_handle(optarg);            \
118             break;                              \
119                                                 \
120         case OPT_SERVICE:                       \
121             break;                              \
122                                                 \
123         case OPT_SERVICE_MONITOR:               \
124             break;                              \
125                                                 \
126         case OPT_USER_GROUP:                    \
127             daemon_set_new_user(optarg);
128
129 void control_handler(DWORD request);
130 void set_pipe_handle(const char *pipe_handle);
131 #endif /* _WIN32 */
132
133 bool get_detach(void);
134 void daemon_save_fd(int fd);
135 void daemonize(void);
136 void daemonize_start(bool access_datapath);
137 void daemonize_complete(void);
138 void daemon_set_new_user(const char * user_spec);
139 void daemon_become_new_user(bool access_datapath);
140 void daemon_usage(void);
141 void service_start(int *argcp, char **argvp[]);
142 void service_stop(void);
143 bool should_service_stop(void);
144 void set_pidfile(const char *name);
145 void close_standard_fds(void);
146
147 #endif /* daemon.h */