2 * Copyright (c) 2008, 2009 Nicira Networks.
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.
25 #include "fatal-signal.h"
31 #define THIS_MODULE VLM_daemon
34 /* Should we run in the background? */
37 /* Name of pidfile (null if none). */
40 /* Create pidfile even if one already exists and is locked? */
41 static bool overwrite_pidfile;
43 /* Should we chdir to "/"? */
44 static bool chdir_ = true;
46 /* File descriptors used by daemonize_start() and daemonize_complete(). */
47 static int daemonize_fds[2];
49 /* Returns the file name that would be used for a pidfile if 'name' were
50 * provided to set_pidfile(). The caller must free the returned string. */
52 make_pidfile_name(const char *name)
54 return (!name ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
55 : *name == '/' ? xstrdup(name)
56 : xasprintf("%s/%s", ovs_rundir, name));
59 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
60 * If 'name' begins with '/', then it is treated as an absolute path.
61 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
64 * If 'name' is null, then program_name followed by ".pid" is used. */
66 set_pidfile(const char *name)
69 pidfile = make_pidfile_name(name);
72 /* Returns an absolute path to the configured pidfile, or a null pointer if no
73 * pidfile is configured. The caller must not modify or free the returned
81 /* Sets that we do not chdir to "/". */
88 /* Will we chdir to "/" as part of daemonizing? */
90 is_chdir_enabled(void)
95 /* Normally, die_if_already_running() will terminate the program with a message
96 * if a locked pidfile already exists. If this function is called,
97 * die_if_already_running() will merely log a warning. */
99 ignore_existing_pidfile(void)
101 overwrite_pidfile = true;
104 /* Sets up a following call to daemonize() to detach from the foreground
105 * session, running this process in the background. */
112 /* Will daemonize() really detach? */
119 /* If a pidfile has been configured and that pidfile already exists and is
120 * locked by a running process, returns the pid of the running process.
121 * Otherwise, returns 0. */
123 already_running(void)
127 int fd = open(pidfile, O_RDWR);
130 lck.l_type = F_WRLCK;
131 lck.l_whence = SEEK_SET;
134 if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
143 /* If a locked pidfile exists, issue a warning message and, unless
144 * ignore_existing_pidfile() has been called, terminate the program. */
146 die_if_already_running(void)
148 pid_t pid = already_running();
150 if (!overwrite_pidfile) {
151 ovs_fatal(0, "%s: already running as pid %ld",
152 get_pidfile(), (long int) pid);
154 VLOG_WARN("%s: %s already running as pid %ld",
155 get_pidfile(), program_name, (long int) pid);
160 /* If a pidfile has been configured, creates it and stores the running process'
161 * pid init. Ensures that the pidfile will be deleted when the process
167 /* Create pidfile via temporary file, so that observers never see an
168 * empty pidfile or an unlocked pidfile. */
169 long int pid = getpid();
173 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
174 fatal_signal_add_file_to_unlink(tmpfile);
175 fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
178 lck.l_type = F_WRLCK;
179 lck.l_whence = SEEK_SET;
182 if (fcntl(fd, F_SETLK, &lck) != -1) {
183 char *text = xasprintf("%ld\n", pid);
184 if (write(fd, text, strlen(text)) == strlen(text)) {
185 fatal_signal_add_file_to_unlink(pidfile);
186 if (rename(tmpfile, pidfile) < 0) {
187 VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
188 tmpfile, pidfile, strerror(errno));
189 fatal_signal_remove_file_to_unlink(pidfile);
192 /* Keep 'fd' open to retain the lock. */
196 VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
200 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
204 VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
206 fatal_signal_remove_file_to_unlink(tmpfile);
213 /* If configured with set_pidfile() or set_detach(), creates the pid file and
214 * detaches from the foreground session. */
219 daemonize_complete();
222 /* If daemonization is configured, then starts daemonization, by forking and
223 * returning in the child process. The parent process hangs around until the
224 * child lets it know either that it completed startup successfully (by calling
225 * daemon_complete()) or that it failed to start up (by exiting with a nonzero
228 daemonize_start(void)
233 if (pipe(daemonize_fds) < 0) {
234 ovs_fatal(errno, "pipe failed");
239 /* Running in parent process. */
242 close(daemonize_fds[1]);
244 if (read(daemonize_fds[0], &c, 1) != 1) {
249 retval = waitpid(pid, &status, 0);
250 } while (retval == -1 && errno == EINTR);
254 && WEXITSTATUS(status)) {
255 /* Child exited with an error. Convey the same error to
256 * our parent process as a courtesy. */
257 exit(WEXITSTATUS(status));
260 ovs_fatal(errno, "daemon child failed to signal startup");
264 /* Running in child process. */
265 close(daemonize_fds[0]);
270 ovs_fatal(errno, "could not fork");
277 /* If daemonization is configured, then this function notifies the parent
278 * process that the child process has completed startup successfully. */
280 daemonize_complete(void)
285 ignore(write(daemonize_fds[1], &c, 1));
286 close(daemonize_fds[1]);
298 "\nDaemon options:\n"
299 " --detach run in background as daemon\n"
300 " --no-chdir do not chdir to '/'\n"
301 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
302 " --overwrite-pidfile with --pidfile, start even if already "
304 ovs_rundir, program_name);
307 /* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
308 * successful, otherwise a negative errno value. */
310 read_pidfile(const char *pidfile)
317 file = fopen(pidfile, "r");
320 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
324 lck.l_type = F_WRLCK;
325 lck.l_whence = SEEK_SET;
328 if (fcntl(fileno(file), F_GETLK, &lck)) {
330 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
333 if (lck.l_type == F_UNLCK) {
335 VLOG_WARN("%s: pid file is not locked", pidfile);
339 if (!fgets(line, sizeof line, file)) {
342 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
345 VLOG_WARN("%s: read: unexpected end of file", pidfile);
350 if (lck.l_pid != strtoul(line, NULL, 10)) {
352 VLOG_WARN("l_pid (%ld) != %s pid (%s)",
353 (long int) lck.l_pid, pidfile, line);