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.
24 #include "fatal-signal.h"
28 #define THIS_MODULE VLM_daemon
31 /* Should we run in the background? */
34 /* Name of pidfile (null if none). */
37 /* Create pidfile even if one already exists and is locked? */
40 /* Returns the file name that would be used for a pidfile if 'name' were
41 * provided to set_pidfile(). The caller must free the returned string. */
43 make_pidfile_name(const char *name)
45 return (!name ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
46 : *name == '/' ? xstrdup(name)
47 : xasprintf("%s/%s", ovs_rundir, name));
50 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
51 * If 'name' begins with '/', then it is treated as an absolute path.
52 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
55 * If 'name' is null, then program_name followed by ".pid" is used. */
57 set_pidfile(const char *name)
60 pidfile = make_pidfile_name(name);
63 /* Returns an absolute path to the configured pidfile, or a null pointer if no
64 * pidfile is configured. The caller must not modify or free the returned
72 /* Normally, die_if_already_running() will terminate the program with a message
73 * if a locked pidfile already exists. If this function is called,
74 * die_if_already_running() will merely log a warning. */
76 ignore_existing_pidfile(void)
81 /* Sets up a following call to daemonize() to detach from the foreground
82 * session, running this process in the background. */
89 /* If a pidfile has been configured and that pidfile already exists and is
90 * locked by a running process, returns the pid of the running process.
91 * Otherwise, returns 0. */
97 int fd = open(pidfile, O_RDWR);
100 lck.l_type = F_WRLCK;
101 lck.l_whence = SEEK_SET;
104 if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
113 /* If a locked pidfile exists, issue a warning message and, unless
114 * ignore_existing_pidfile() has been called, terminate the program. */
116 die_if_already_running(void)
118 pid_t pid = already_running();
121 ovs_fatal(0, "%s: already running as pid %ld",
122 get_pidfile(), (long int) pid);
124 VLOG_WARN("%s: %s already running as pid %ld",
125 get_pidfile(), program_name, (long int) pid);
130 /* If a pidfile has been configured, creates it and stores the running process'
131 * pid init. Ensures that the pidfile will be deleted when the process
137 /* Create pidfile via temporary file, so that observers never see an
138 * empty pidfile or an unlocked pidfile. */
139 long int pid = getpid();
143 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
144 fatal_signal_add_file_to_unlink(tmpfile);
145 fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
148 lck.l_type = F_WRLCK;
149 lck.l_whence = SEEK_SET;
152 if (fcntl(fd, F_SETLK, &lck) != -1) {
153 char *text = xasprintf("%ld\n", pid);
154 if (write(fd, text, strlen(text)) == strlen(text)) {
155 fatal_signal_add_file_to_unlink(pidfile);
156 if (rename(tmpfile, pidfile) < 0) {
157 VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
158 tmpfile, pidfile, strerror(errno));
159 fatal_signal_remove_file_to_unlink(pidfile);
162 /* Keep 'fd' open to retain the lock. */
166 VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
170 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
174 VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
176 fatal_signal_remove_file_to_unlink(tmpfile);
183 /* If configured with set_pidfile() or set_detach(), creates the pid file and
184 * detaches from the foreground session. */
192 ovs_fatal(errno, "pipe failed");
197 /* Parent process: wait for child to create pidfile, then exit. */
200 if (read(fds[0], &c, 1) != 1) {
201 ovs_fatal(errno, "daemon child failed to signal startup");
209 write(fds[1], &c, 1);
217 ovs_fatal(errno, "could not fork");
229 "\nDaemon options:\n"
230 " -D, --detach run in background as daemon\n"
231 " -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
232 " -f, --force with -P, start even if already running\n",
233 ovs_rundir, program_name);
236 /* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
237 * successful, otherwise a negative errno value. */
239 read_pidfile(const char *pidfile)
246 file = fopen(pidfile, "r");
249 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
253 lck.l_type = F_WRLCK;
254 lck.l_whence = SEEK_SET;
257 if (fcntl(fileno(file), F_GETLK, &lck)) {
259 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
262 if (lck.l_type == F_UNLCK) {
264 VLOG_WARN("%s: pid file is not locked", pidfile);
268 if (!fgets(line, sizeof line, file)) {
271 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
274 VLOG_WARN("%s: read: unexpected end of file", pidfile);
279 if (lck.l_pid != strtoul(line, NULL, 10)) {
281 VLOG_WARN("l_pid (%ld) != %s pid (%s)",
282 (long int) lck.l_pid, pidfile, line);