daemon: Close standard file descriptors after detach for windows.
[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 <errno.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "vlog.h"
22
23 VLOG_DEFINE_THIS_MODULE(daemon);
24
25 /* For each of the standard file descriptors, whether to replace it by
26  * /dev/null (if false) or keep it for the daemon to use (if true). */
27 static bool save_fds[3];
28
29 /* A daemon doesn't normally have any use for the file descriptors for stdin,
30  * stdout, and stderr after it detaches.  To keep these file descriptors from
31  * e.g. holding an SSH session open, by default detaching replaces each of
32  * these file descriptors by /dev/null.  But a few daemons expect the user to
33  * redirect stdout or stderr to a file, in which case it is desirable to keep
34  * these file descriptors.  This function, therefore, disables replacing 'fd'
35  * by /dev/null when the daemon detaches. */
36 void
37 daemon_save_fd(int fd)
38 {
39     ovs_assert(fd == STDIN_FILENO ||
40                fd == STDOUT_FILENO ||
41                fd == STDERR_FILENO);
42     save_fds[fd] = true;
43 }
44
45 /* Returns a readable and writable fd for /dev/null, if successful, otherwise
46  * a negative errno value.  The caller must not close the returned fd (because
47  * the same fd will be handed out to subsequent callers). */
48 static int
49 get_null_fd(void)
50 {
51     static int null_fd;
52 #ifndef _WIN32
53     char *device = "/dev/null";
54 #else
55     char *device = "nul";
56 #endif
57
58     if (!null_fd) {
59         null_fd = open(device, O_RDWR);
60         if (null_fd < 0) {
61             int error = errno;
62             VLOG_ERR("could not open %s: %s", device, ovs_strerror(error));
63             null_fd = -error;
64         }
65     }
66
67     return null_fd;
68 }
69
70 /* Close standard file descriptors (except any that the client has requested we
71  * leave open by calling daemon_save_fd()).  If we're started from e.g. an SSH
72  * session, then this keeps us from holding that session open artificially. */
73 void
74 close_standard_fds(void)
75 {
76     int null_fd = get_null_fd();
77     if (null_fd >= 0) {
78         int fd;
79
80         for (fd = 0; fd < 3; fd++) {
81             if (!save_fds[fd]) {
82                 dup2(null_fd, fd);
83             }
84         }
85     }
86
87     /* Disable logging to stderr to avoid wasting CPU time. */
88     vlog_set_levels(NULL, VLF_CONSOLE, VLL_OFF);
89 }