]> git.proxmox.com Git - ovs.git/blob - lib/daemon.c
ofproto: Make current packet counts more accurate.
[ovs.git] / lib / daemon.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
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 #include <config.h>
18 #include "daemon.h"
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "fatal-signal.h"
25 #include "dirs.h"
26 #include "util.h"
27
28 #define THIS_MODULE VLM_daemon
29 #include "vlog.h"
30
31 /* Should we run in the background? */
32 static bool detach;
33
34 /* Name of pidfile (null if none). */
35 static char *pidfile;
36
37 /* Create pidfile even if one already exists and is locked? */
38 static bool overwrite_pidfile;
39
40 /* Should we chdir to "/". */
41 static bool chdir_ = true;
42
43 /* Returns the file name that would be used for a pidfile if 'name' were
44 * provided to set_pidfile(). The caller must free the returned string. */
45 char *
46 make_pidfile_name(const char *name)
47 {
48 return (!name ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
49 : *name == '/' ? xstrdup(name)
50 : xasprintf("%s/%s", ovs_rundir, name));
51 }
52
53 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
54 * If 'name' begins with '/', then it is treated as an absolute path.
55 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
56 * default.
57 *
58 * If 'name' is null, then program_name followed by ".pid" is used. */
59 void
60 set_pidfile(const char *name)
61 {
62 free(pidfile);
63 pidfile = make_pidfile_name(name);
64 }
65
66 /* Returns an absolute path to the configured pidfile, or a null pointer if no
67 * pidfile is configured. The caller must not modify or free the returned
68 * string. */
69 const char *
70 get_pidfile(void)
71 {
72 return pidfile;
73 }
74
75 /* Sets that we do not chdir to "/". */
76 void
77 set_no_chdir(void)
78 {
79 chdir_ = false;
80 }
81
82 /* Normally, die_if_already_running() will terminate the program with a message
83 * if a locked pidfile already exists. If this function is called,
84 * die_if_already_running() will merely log a warning. */
85 void
86 ignore_existing_pidfile(void)
87 {
88 overwrite_pidfile = true;
89 }
90
91 /* Sets up a following call to daemonize() to detach from the foreground
92 * session, running this process in the background. */
93 void
94 set_detach(void)
95 {
96 detach = true;
97 }
98
99 /* If a pidfile has been configured and that pidfile already exists and is
100 * locked by a running process, returns the pid of the running process.
101 * Otherwise, returns 0. */
102 static pid_t
103 already_running(void)
104 {
105 pid_t pid = 0;
106 if (pidfile) {
107 int fd = open(pidfile, O_RDWR);
108 if (fd >= 0) {
109 struct flock lck;
110 lck.l_type = F_WRLCK;
111 lck.l_whence = SEEK_SET;
112 lck.l_start = 0;
113 lck.l_len = 0;
114 if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
115 pid = lck.l_pid;
116 }
117 close(fd);
118 }
119 }
120 return pid;
121 }
122
123 /* If a locked pidfile exists, issue a warning message and, unless
124 * ignore_existing_pidfile() has been called, terminate the program. */
125 void
126 die_if_already_running(void)
127 {
128 pid_t pid = already_running();
129 if (pid) {
130 if (!overwrite_pidfile) {
131 ovs_fatal(0, "%s: already running as pid %ld",
132 get_pidfile(), (long int) pid);
133 } else {
134 VLOG_WARN("%s: %s already running as pid %ld",
135 get_pidfile(), program_name, (long int) pid);
136 }
137 }
138 }
139
140 /* If a pidfile has been configured, creates it and stores the running process'
141 * pid init. Ensures that the pidfile will be deleted when the process
142 * exits. */
143 static void
144 make_pidfile(void)
145 {
146 if (pidfile) {
147 /* Create pidfile via temporary file, so that observers never see an
148 * empty pidfile or an unlocked pidfile. */
149 long int pid = getpid();
150 char *tmpfile;
151 int fd;
152
153 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
154 fatal_signal_add_file_to_unlink(tmpfile);
155 fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
156 if (fd >= 0) {
157 struct flock lck;
158 lck.l_type = F_WRLCK;
159 lck.l_whence = SEEK_SET;
160 lck.l_start = 0;
161 lck.l_len = 0;
162 if (fcntl(fd, F_SETLK, &lck) != -1) {
163 char *text = xasprintf("%ld\n", pid);
164 if (write(fd, text, strlen(text)) == strlen(text)) {
165 fatal_signal_add_file_to_unlink(pidfile);
166 if (rename(tmpfile, pidfile) < 0) {
167 VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
168 tmpfile, pidfile, strerror(errno));
169 fatal_signal_remove_file_to_unlink(pidfile);
170 close(fd);
171 } else {
172 /* Keep 'fd' open to retain the lock. */
173 }
174 free(text);
175 } else {
176 VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
177 close(fd);
178 }
179 } else {
180 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
181 close(fd);
182 }
183 } else {
184 VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
185 }
186 fatal_signal_remove_file_to_unlink(tmpfile);
187 free(tmpfile);
188 }
189 free(pidfile);
190 pidfile = NULL;
191 }
192
193 /* If configured with set_pidfile() or set_detach(), creates the pid file and
194 * detaches from the foreground session. */
195 void
196 daemonize(void)
197 {
198 if (detach) {
199 char c = 0;
200 int fds[2];
201 if (pipe(fds) < 0) {
202 ovs_fatal(errno, "pipe failed");
203 }
204
205 switch (fork()) {
206 default:
207 /* Parent process: wait for child to create pidfile, then exit. */
208 close(fds[1]);
209 fatal_signal_fork();
210 if (read(fds[0], &c, 1) != 1) {
211 ovs_fatal(errno, "daemon child failed to signal startup");
212 }
213 exit(0);
214
215 case 0:
216 /* Child process. */
217 close(fds[0]);
218 make_pidfile();
219 write(fds[1], &c, 1);
220 close(fds[1]);
221 setsid();
222 if (chdir_) {
223 chdir("/");
224 }
225 break;
226
227 case -1:
228 /* Error. */
229 ovs_fatal(errno, "could not fork");
230 break;
231 }
232 } else {
233 make_pidfile();
234 }
235 }
236
237 void
238 daemon_usage(void)
239 {
240 printf(
241 "\nDaemon options:\n"
242 " --detach run in background as daemon\n"
243 " --no-chdir do not chdir to '/'\n"
244 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
245 " --overwrite-pidfile with --pidfile, start even if already "
246 "running\n",
247 ovs_rundir, program_name);
248 }
249
250 /* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
251 * successful, otherwise a negative errno value. */
252 pid_t
253 read_pidfile(const char *pidfile)
254 {
255 char line[128];
256 struct flock lck;
257 FILE *file;
258 int error;
259
260 file = fopen(pidfile, "r");
261 if (!file) {
262 error = errno;
263 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
264 goto error;
265 }
266
267 lck.l_type = F_WRLCK;
268 lck.l_whence = SEEK_SET;
269 lck.l_start = 0;
270 lck.l_len = 0;
271 if (fcntl(fileno(file), F_GETLK, &lck)) {
272 error = errno;
273 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
274 goto error;
275 }
276 if (lck.l_type == F_UNLCK) {
277 error = ESRCH;
278 VLOG_WARN("%s: pid file is not locked", pidfile);
279 goto error;
280 }
281
282 if (!fgets(line, sizeof line, file)) {
283 if (ferror(file)) {
284 error = errno;
285 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
286 } else {
287 error = ESRCH;
288 VLOG_WARN("%s: read: unexpected end of file", pidfile);
289 }
290 goto error;
291 }
292
293 if (lck.l_pid != strtoul(line, NULL, 10)) {
294 error = ESRCH;
295 VLOG_WARN("l_pid (%ld) != %s pid (%s)",
296 (long int) lck.l_pid, pidfile, line);
297 goto error;
298 }
299
300 fclose(file);
301 return lck.l_pid;
302
303 error:
304 if (file) {
305 fclose(file);
306 }
307 return -error;
308 }