daemon: Write "already running" message to log also.
[sliver-openvswitch.git] / lib / daemon.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/resource.h>
25 #include <sys/wait.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include "command-line.h"
29 #include "fatal-signal.h"
30 #include "dirs.h"
31 #include "lockfile.h"
32 #include "process.h"
33 #include "socket-util.h"
34 #include "timeval.h"
35 #include "util.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(daemon);
39
40 /* --detach: Should we run in the background? */
41 static bool detach;
42
43 /* --pidfile: Name of pidfile (null if none). */
44 static char *pidfile;
45
46 /* Device and inode of pidfile, so we can avoid reopening it. */
47 static dev_t pidfile_dev;
48 static ino_t pidfile_ino;
49
50 /* --overwrite-pidfile: Create pidfile even if one already exists and is
51    locked? */
52 static bool overwrite_pidfile;
53
54 /* --no-chdir: Should we chdir to "/"? */
55 static bool chdir_ = true;
56
57 /* File descriptor used by daemonize_start() and daemonize_complete(). */
58 static int daemonize_fd = -1;
59
60 /* --monitor: Should a supervisory process monitor the daemon and restart it if
61  * it dies due to an error signal? */
62 static bool monitor;
63
64 /* Returns the file name that would be used for a pidfile if 'name' were
65  * provided to set_pidfile().  The caller must free the returned string. */
66 char *
67 make_pidfile_name(const char *name)
68 {
69     return (!name
70             ? xasprintf("%s/%s.pid", ovs_rundir(), program_name)
71             : abs_file_name(ovs_rundir(), name));
72 }
73
74 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
75  * If 'name' begins with '/', then it is treated as an absolute path.
76  * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
77  * default.
78  *
79  * If 'name' is null, then program_name followed by ".pid" is used. */
80 void
81 set_pidfile(const char *name)
82 {
83     free(pidfile);
84     pidfile = make_pidfile_name(name);
85 }
86
87 /* Returns an absolute path to the configured pidfile, or a null pointer if no
88  * pidfile is configured.  The caller must not modify or free the returned
89  * string. */
90 const char *
91 get_pidfile(void)
92 {
93     return pidfile;
94 }
95
96 /* Sets that we do not chdir to "/". */
97 void
98 set_no_chdir(void)
99 {
100     chdir_ = false;
101 }
102
103 /* Will we chdir to "/" as part of daemonizing? */
104 bool
105 is_chdir_enabled(void)
106 {
107     return chdir_;
108 }
109
110 /* Normally, die_if_already_running() will terminate the program with a message
111  * if a locked pidfile already exists.  If this function is called,
112  * die_if_already_running() will merely log a warning. */
113 void
114 ignore_existing_pidfile(void)
115 {
116     overwrite_pidfile = true;
117 }
118
119 /* Sets up a following call to daemonize() to detach from the foreground
120  * session, running this process in the background.  */
121 void
122 set_detach(void)
123 {
124     detach = true;
125 }
126
127 /* Will daemonize() really detach? */
128 bool
129 get_detach(void)
130 {
131     return detach;
132 }
133
134 /* Sets up a following call to daemonize() to fork a supervisory process to
135  * monitor the daemon and restart it if it dies due to an error signal.  */
136 void
137 daemon_set_monitor(void)
138 {
139     monitor = true;
140 }
141
142 /* If a pidfile has been configured and that pidfile already exists and is
143  * locked by a running process, returns the pid of the running process.
144  * Otherwise, returns 0. */
145 static pid_t
146 already_running(void)
147 {
148     pid_t pid = 0;
149     if (pidfile) {
150         int fd = open(pidfile, O_RDWR);
151         if (fd >= 0) {
152             struct flock lck;
153             lck.l_type = F_WRLCK;
154             lck.l_whence = SEEK_SET;
155             lck.l_start = 0;
156             lck.l_len = 0;
157             if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
158                 pid = lck.l_pid;
159             }
160             close(fd);
161         }
162     }
163     return pid;
164 }
165
166 /* If a locked pidfile exists, issue a warning message and, unless
167  * ignore_existing_pidfile() has been called, terminate the program. */
168 void
169 die_if_already_running(void)
170 {
171     pid_t pid = already_running();
172     if (pid) {
173         if (!overwrite_pidfile) {
174             VLOG_ERR("%s: %s already running as pid %ld, aborting",
175                       get_pidfile(), program_name, (long int) pid);
176             ovs_fatal(0, "%s: already running as pid %ld",
177                       get_pidfile(), (long int) pid);
178         } else {
179             VLOG_WARN("%s: %s already running as pid %ld",
180                       get_pidfile(), program_name, (long int) pid);
181         }
182     }
183 }
184
185 /* If a pidfile has been configured, creates it and stores the running
186  * process's pid in it.  Ensures that the pidfile will be deleted when the
187  * process exits. */
188 static void
189 make_pidfile(void)
190 {
191     if (pidfile) {
192         /* Create pidfile via temporary file, so that observers never see an
193          * empty pidfile or an unlocked pidfile. */
194         long int pid = getpid();
195         char *tmpfile;
196         int fd;
197
198         tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
199         fatal_signal_add_file_to_unlink(tmpfile);
200         fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
201         if (fd >= 0) {
202             struct flock lck;
203             lck.l_type = F_WRLCK;
204             lck.l_whence = SEEK_SET;
205             lck.l_start = 0;
206             lck.l_len = 0;
207             if (fcntl(fd, F_SETLK, &lck) != -1) {
208                 char *text = xasprintf("%ld\n", pid);
209                 if (write(fd, text, strlen(text)) == strlen(text)) {
210                     fatal_signal_add_file_to_unlink(pidfile);
211                     if (rename(tmpfile, pidfile) < 0) {
212                         VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
213                                  tmpfile, pidfile, strerror(errno));
214                         fatal_signal_remove_file_to_unlink(pidfile);
215                         close(fd);
216                     } else {
217                         /* Keep 'fd' open to retain the lock. */
218                         struct stat s;
219
220                         if (!fstat(fd, &s)) {
221                             pidfile_dev = s.st_dev;
222                             pidfile_ino = s.st_ino;
223                         } else {
224                             VLOG_ERR("%s: fstat failed: %s",
225                                      pidfile, strerror(errno));
226                         }
227                     }
228                 } else {
229                     VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
230                     close(fd);
231                 }
232                 free(text);
233             } else {
234                 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
235                 close(fd);
236             }
237         } else {
238             VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
239         }
240         fatal_signal_remove_file_to_unlink(tmpfile);
241         free(tmpfile);
242     }
243     free(pidfile);
244     pidfile = NULL;
245 }
246
247 /* If configured with set_pidfile() or set_detach(), creates the pid file and
248  * detaches from the foreground session.  */
249 void
250 daemonize(void)
251 {
252     daemonize_start();
253     daemonize_complete();
254 }
255
256 static pid_t
257 fork_and_wait_for_startup(int *fdp)
258 {
259     int fds[2];
260     pid_t pid;
261
262     if (pipe(fds) < 0) {
263         ovs_fatal(errno, "pipe failed");
264     }
265
266     pid = fork();
267     if (pid > 0) {
268         /* Running in parent process. */
269         char c;
270
271         close(fds[1]);
272         fatal_signal_fork();
273         if (read(fds[0], &c, 1) != 1) {
274             int retval;
275             int status;
276
277             do {
278                 retval = waitpid(pid, &status, 0);
279             } while (retval == -1 && errno == EINTR);
280
281             if (retval == pid
282                 && WIFEXITED(status)
283                 && WEXITSTATUS(status)) {
284                 /* Child exited with an error.  Convey the same error to
285                  * our parent process as a courtesy. */
286                 exit(WEXITSTATUS(status));
287             }
288
289             ovs_fatal(errno, "fork child failed to signal startup");
290         }
291         close(fds[0]);
292         *fdp = -1;
293     } else if (!pid) {
294         /* Running in child process. */
295         close(fds[0]);
296         time_postfork();
297         lockfile_postfork();
298         *fdp = fds[1];
299     } else {
300         ovs_fatal(errno, "could not fork");
301     }
302
303     return pid;
304 }
305
306 static void
307 fork_notify_startup(int fd)
308 {
309     if (fd != -1) {
310         size_t bytes_written;
311         int error;
312
313         error = write_fully(fd, "", 1, &bytes_written);
314         if (error) {
315             ovs_fatal(error, "could not write to pipe");
316         }
317
318         close(fd);
319     }
320 }
321
322 static bool
323 should_restart(int status)
324 {
325     if (WIFSIGNALED(status)) {
326         static const int error_signals[] = {
327             SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGILL, SIGPIPE, SIGSEGV,
328             SIGXCPU, SIGXFSZ
329         };
330
331         size_t i;
332
333         for (i = 0; i < ARRAY_SIZE(error_signals); i++) {
334             if (error_signals[i] == WTERMSIG(status)) {
335                 return true;
336             }
337         }
338     }
339     return false;
340 }
341
342 static void
343 monitor_daemon(pid_t daemon_pid)
344 {
345     /* XXX Should log daemon's stderr output at startup time. */
346     const char *saved_program_name;
347     time_t last_restart;
348     char *status_msg;
349     int crashes;
350
351     saved_program_name = program_name;
352     program_name = xasprintf("monitor(%s)", program_name);
353     status_msg = xstrdup("healthy");
354     last_restart = TIME_MIN;
355     crashes = 0;
356     for (;;) {
357         int retval;
358         int status;
359
360         proctitle_set("%s: monitoring pid %lu (%s)",
361                       saved_program_name, (unsigned long int) daemon_pid,
362                       status_msg);
363
364         do {
365             retval = waitpid(daemon_pid, &status, 0);
366         } while (retval == -1 && errno == EINTR);
367
368         if (retval == -1) {
369             ovs_fatal(errno, "waitpid failed");
370         } else if (retval == daemon_pid) {
371             char *s = process_status_msg(status);
372             if (should_restart(status)) {
373                 free(status_msg);
374                 status_msg = xasprintf("%d crashes: pid %lu died, %s",
375                                        ++crashes,
376                                        (unsigned long int) daemon_pid, s);
377                 free(s);
378
379                 if (WCOREDUMP(status)) {
380                     /* Disable further core dumps to save disk space. */
381                     struct rlimit r;
382
383                     r.rlim_cur = 0;
384                     r.rlim_max = 0;
385                     if (setrlimit(RLIMIT_CORE, &r) == -1) {
386                         VLOG_WARN("failed to disable core dumps: %s",
387                                   strerror(errno));
388                     }
389                 }
390
391                 /* Throttle restarts to no more than once every 10 seconds. */
392                 if (time(NULL) < last_restart + 10) {
393                     VLOG_WARN("%s, waiting until 10 seconds since last "
394                               "restart", status_msg);
395                     for (;;) {
396                         time_t now = time(NULL);
397                         time_t wakeup = last_restart + 10;
398                         if (now >= wakeup) {
399                             break;
400                         }
401                         sleep(wakeup - now);
402                     }
403                 }
404                 last_restart = time(NULL);
405
406                 VLOG_ERR("%s, restarting", status_msg);
407                 daemon_pid = fork_and_wait_for_startup(&daemonize_fd);
408                 if (!daemon_pid) {
409                     break;
410                 }
411             } else {
412                 VLOG_INFO("pid %lu died, %s, exiting",
413                           (unsigned long int) daemon_pid, s);
414                 free(s);
415                 exit(0);
416             }
417         }
418     }
419     free(status_msg);
420
421     /* Running in new daemon process. */
422     proctitle_restore();
423     free((char *) program_name);
424     program_name = saved_program_name;
425 }
426
427 /* Close stdin, stdout, stderr.  If we're started from e.g. an SSH session,
428  * then this keeps us from holding that session open artificially. */
429 static void
430 close_standard_fds(void)
431 {
432     int null_fd = get_null_fd();
433     if (null_fd >= 0) {
434         dup2(null_fd, STDIN_FILENO);
435         dup2(null_fd, STDOUT_FILENO);
436         dup2(null_fd, STDERR_FILENO);
437     }
438 }
439
440 /* If daemonization is configured, then starts daemonization, by forking and
441  * returning in the child process.  The parent process hangs around until the
442  * child lets it know either that it completed startup successfully (by calling
443  * daemon_complete()) or that it failed to start up (by exiting with a nonzero
444  * exit code). */
445 void
446 daemonize_start(void)
447 {
448     daemonize_fd = -1;
449
450     if (detach) {
451         if (fork_and_wait_for_startup(&daemonize_fd) > 0) {
452             /* Running in parent process. */
453             exit(0);
454         }
455         /* Running in daemon or monitor process. */
456     }
457
458     if (monitor) {
459         int saved_daemonize_fd = daemonize_fd;
460         pid_t daemon_pid;
461
462         daemon_pid = fork_and_wait_for_startup(&daemonize_fd);
463         if (daemon_pid > 0) {
464             /* Running in monitor process. */
465             fork_notify_startup(saved_daemonize_fd);
466             close_standard_fds();
467             monitor_daemon(daemon_pid);
468         }
469         /* Running in daemon process. */
470     }
471
472     make_pidfile();
473
474     /* Make sure that the unixctl commands for vlog get registered in a
475      * daemon, even before the first log message. */
476     vlog_init();
477 }
478
479 /* If daemonization is configured, then this function notifies the parent
480  * process that the child process has completed startup successfully.
481  *
482  * Calling this function more than once has no additional effect. */
483 void
484 daemonize_complete(void)
485 {
486     fork_notify_startup(daemonize_fd);
487     daemonize_fd = -1;
488
489     if (detach) {
490         setsid();
491         if (chdir_) {
492             ignore(chdir("/"));
493         }
494         close_standard_fds();
495         detach = false;
496     }
497 }
498
499 void
500 daemon_usage(void)
501 {
502     printf(
503         "\nDaemon options:\n"
504         "  --detach                run in background as daemon\n"
505         "  --no-chdir              do not chdir to '/'\n"
506         "  --pidfile[=FILE]        create pidfile (default: %s/%s.pid)\n"
507         "  --overwrite-pidfile     with --pidfile, start even if already "
508                                    "running\n",
509         ovs_rundir(), program_name);
510 }
511
512 /* Opens and reads a PID from 'pidfile'.  Returns the nonnegative PID if
513  * successful, otherwise a negative errno value. */
514 pid_t
515 read_pidfile(const char *pidfile)
516 {
517     char line[128];
518     struct flock lck;
519     struct stat s;
520     FILE *file;
521     int error;
522
523     if ((pidfile_ino || pidfile_dev)
524         && !stat(pidfile, &s)
525         && s.st_ino == pidfile_ino && s.st_dev == pidfile_dev) {
526         /* It's our own pidfile.  We can't afford to open it, because closing
527          * *any* fd for a file that a process has locked also releases all the
528          * locks on that file.
529          *
530          * Fortunately, we know the associated pid anyhow: */
531         return getpid();
532     }
533
534     file = fopen(pidfile, "r");
535     if (!file) {
536         error = errno;
537         VLOG_WARN("%s: open: %s", pidfile, strerror(error));
538         goto error;
539     }
540
541     lck.l_type = F_WRLCK;
542     lck.l_whence = SEEK_SET;
543     lck.l_start = 0;
544     lck.l_len = 0;
545     lck.l_pid = 0;
546     if (fcntl(fileno(file), F_GETLK, &lck)) {
547         error = errno;
548         VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
549         goto error;
550     }
551     if (lck.l_type == F_UNLCK) {
552         error = ESRCH;
553         VLOG_WARN("%s: pid file is not locked", pidfile);
554         goto error;
555     }
556
557     if (!fgets(line, sizeof line, file)) {
558         if (ferror(file)) {
559             error = errno;
560             VLOG_WARN("%s: read: %s", pidfile, strerror(error));
561         } else {
562             error = ESRCH;
563             VLOG_WARN("%s: read: unexpected end of file", pidfile);
564         }
565         goto error;
566     }
567
568     if (lck.l_pid != strtoul(line, NULL, 10)) {
569         error = ESRCH;
570         VLOG_WARN("l_pid (%ld) != %s pid (%s)",
571                    (long int) lck.l_pid, pidfile, line);
572         goto error;
573     }
574
575     fclose(file);
576     return lck.l_pid;
577
578 error:
579     if (file) {
580         fclose(file);
581     }
582     return -error;
583 }