]> git.proxmox.com Git - mirror_ovs.git/blob - lib/daemon.h
dpif-netdev: Store actions data and size contiguously.
[mirror_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
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}
53
54 #define DAEMON_OPTION_HANDLERS \
55 case OPT_DETACH: \
56 set_detach(); \
57 break; \
58 \
59 case OPT_NO_CHDIR: \
60 set_no_chdir(); \
61 break; \
62 \
63 case OPT_PIDFILE: \
64 set_pidfile(optarg); \
65 break; \
66 \
67 case OPT_OVERWRITE_PIDFILE: \
68 ignore_existing_pidfile(); \
69 break; \
70 \
71 case OPT_MONITOR: \
72 daemon_set_monitor(); \
73 break;
74
75 void set_detach(void);
76 void daemon_set_monitor(void);
77 void set_no_chdir(void);
78 void ignore_existing_pidfile(void);
79 pid_t read_pidfile(const char *name);
80 #else
81 #define DAEMON_OPTION_ENUMS \
82 OPT_DETACH, \
83 OPT_NO_CHDIR, \
84 OPT_PIDFILE, \
85 OPT_PIPE_HANDLE, \
86 OPT_SERVICE, \
87 OPT_SERVICE_MONITOR
88
89 #define DAEMON_LONG_OPTIONS \
90 {"detach", no_argument, NULL, OPT_DETACH}, \
91 {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \
92 {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \
93 {"pipe-handle", required_argument, NULL, OPT_PIPE_HANDLE}, \
94 {"service", no_argument, NULL, OPT_SERVICE}, \
95 {"service-monitor", no_argument, NULL, OPT_SERVICE_MONITOR}
96
97 #define DAEMON_OPTION_HANDLERS \
98 case OPT_DETACH: \
99 break; \
100 \
101 case OPT_NO_CHDIR: \
102 break; \
103 \
104 case OPT_PIDFILE: \
105 set_pidfile(optarg); \
106 break; \
107 \
108 case OPT_PIPE_HANDLE: \
109 set_pipe_handle(optarg); \
110 break; \
111 \
112 case OPT_SERVICE: \
113 break; \
114 \
115 case OPT_SERVICE_MONITOR: \
116 break;
117
118 void control_handler(DWORD request);
119 void set_pipe_handle(const char *pipe_handle);
120 #endif /* _WIN32 */
121
122 bool get_detach(void);
123 void daemon_save_fd(int fd);
124 void daemonize(void);
125 void daemonize_start(void);
126 void daemonize_complete(void);
127 void daemon_usage(void);
128 void service_start(int *argcp, char **argvp[]);
129 void service_stop(void);
130 bool should_service_stop(void);
131 void set_pidfile(const char *name);
132 void close_standard_fds(void);
133
134 #endif /* daemon.h */