daemon: Move some common code to daemon.c
[sliver-openvswitch.git] / lib / daemon.c
1 /*
2  * Copyright (c) 2014 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 #include <config.h>
17 #include "daemon.h"
18 #include "daemon-private.h"
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include "vlog.h"
23
24 VLOG_DEFINE_THIS_MODULE(daemon);
25
26 /* For each of the standard file descriptors, whether to replace it by
27  * /dev/null (if false) or keep it for the daemon to use (if true). */
28 static bool save_fds[3];
29
30 /* Will daemonize() really detach? */
31 bool
32 get_detach(void)
33 {
34     return detach;
35 }
36
37 /* If configured with set_pidfile() or set_detach(), creates the pid file and
38  * detaches from the foreground session.  */
39 void
40 daemonize(void)
41 {
42     daemonize_start();
43     daemonize_complete();
44 }
45
46 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
47  * If 'name' begins with '/' (or contains ':' in windows), then it is treated
48  * as an absolute path. Otherwise, it is taken relative to RUNDIR,
49  * which is $(prefix)/var/run by default.
50  *
51  * If 'name' is null, then program_name followed by ".pid" is used. */
52 void
53 set_pidfile(const char *name)
54 {
55     assert_single_threaded();
56     free(pidfile);
57     pidfile = make_pidfile_name(name);
58 }
59
60 /* A daemon doesn't normally have any use for the file descriptors for stdin,
61  * stdout, and stderr after it detaches.  To keep these file descriptors from
62  * e.g. holding an SSH session open, by default detaching replaces each of
63  * these file descriptors by /dev/null.  But a few daemons expect the user to
64  * redirect stdout or stderr to a file, in which case it is desirable to keep
65  * these file descriptors.  This function, therefore, disables replacing 'fd'
66  * by /dev/null when the daemon detaches. */
67 void
68 daemon_save_fd(int fd)
69 {
70     ovs_assert(fd == STDIN_FILENO ||
71                fd == STDOUT_FILENO ||
72                fd == STDERR_FILENO);
73     save_fds[fd] = true;
74 }
75
76 /* Returns a readable and writable fd for /dev/null, if successful, otherwise
77  * a negative errno value.  The caller must not close the returned fd (because
78  * the same fd will be handed out to subsequent callers). */
79 static int
80 get_null_fd(void)
81 {
82     static int null_fd;
83 #ifndef _WIN32
84     char *device = "/dev/null";
85 #else
86     char *device = "nul";
87 #endif
88
89     if (!null_fd) {
90         null_fd = open(device, O_RDWR);
91         if (null_fd < 0) {
92             int error = errno;
93             VLOG_ERR("could not open %s: %s", device, ovs_strerror(error));
94             null_fd = -error;
95         }
96     }
97
98     return null_fd;
99 }
100
101 /* Close standard file descriptors (except any that the client has requested we
102  * leave open by calling daemon_save_fd()).  If we're started from e.g. an SSH
103  * session, then this keeps us from holding that session open artificially. */
104 void
105 close_standard_fds(void)
106 {
107     int null_fd = get_null_fd();
108     if (null_fd >= 0) {
109         int fd;
110
111         for (fd = 0; fd < 3; fd++) {
112             if (!save_fds[fd]) {
113                 dup2(null_fd, fd);
114             }
115         }
116     }
117
118     /* Disable logging to stderr to avoid wasting CPU time. */
119     vlog_set_levels(NULL, VLF_CONSOLE, VLL_OFF);
120 }