]>
git.proxmox.com Git - ovs.git/blob - lib/daemon.c
2 * Copyright (c) 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "daemon-private.h"
23 #include "ovs-thread.h"
24 #include "openvswitch/vlog.h"
26 VLOG_DEFINE_THIS_MODULE(daemon
);
28 /* For each of the standard file descriptors, whether to replace it by
29 * /dev/null (if false) or keep it for the daemon to use (if true). */
30 static bool save_fds
[3];
32 /* Will daemonize() really detach? */
39 /* If configured with set_pidfile() or set_detach(), creates the pid file and
40 * detaches from the foreground session. */
48 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
49 * If 'name' begins with '/' (or contains ':' in windows), then it is treated
50 * as an absolute path. Otherwise, it is taken relative to RUNDIR,
51 * which is $(prefix)/var/run by default.
53 * If 'name' is null, then program_name followed by ".pid" is used. */
55 set_pidfile(const char *name
)
57 assert_single_threaded();
59 pidfile
= make_pidfile_name(name
);
62 /* A daemon doesn't normally have any use for the file descriptors for stdin,
63 * stdout, and stderr after it detaches. To keep these file descriptors from
64 * e.g. holding an SSH session open, by default detaching replaces each of
65 * these file descriptors by /dev/null. But a few daemons expect the user to
66 * redirect stdout or stderr to a file, in which case it is desirable to keep
67 * these file descriptors. This function, therefore, disables replacing 'fd'
68 * by /dev/null when the daemon detaches. */
70 daemon_save_fd(int fd
)
72 ovs_assert(fd
== STDIN_FILENO
||
73 fd
== STDOUT_FILENO
||
78 /* Returns a readable and writable fd for /dev/null, if successful, otherwise
79 * a negative errno value. The caller must not close the returned fd (because
80 * the same fd will be handed out to subsequent callers). */
86 char *device
= "/dev/null";
92 null_fd
= open(device
, O_RDWR
);
95 VLOG_ERR("could not open %s: %s", device
, ovs_strerror(error
));
103 /* Close standard file descriptors (except any that the client has requested we
104 * leave open by calling daemon_save_fd()). If we're started from e.g. an SSH
105 * session, then this keeps us from holding that session open artificially. */
107 close_standard_fds(void)
109 int null_fd
= get_null_fd();
113 for (fd
= 0; fd
< 3; fd
++) {
120 /* Disable logging to stderr to avoid wasting CPU time. */
121 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_OFF
);