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"
29 #define THIS_MODULE VLM_daemon
32 /* Should we run in the background? */
35 /* Name of pidfile (null if none). */
38 /* Create pidfile even if one already exists and is locked? */
39 static bool overwrite_pidfile;
41 /* Should we chdir to "/". */
42 static bool chdir_ = true;
44 /* Returns the file name that would be used for a pidfile if 'name' were
45 * provided to set_pidfile(). The caller must free the returned string. */
47 make_pidfile_name(const char *name)
49 return (!name ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
50 : *name == '/' ? xstrdup(name)
51 : xasprintf("%s/%s", ovs_rundir, name));
54 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
55 * If 'name' begins with '/', then it is treated as an absolute path.
56 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
59 * If 'name' is null, then program_name followed by ".pid" is used. */
61 set_pidfile(const char *name)
64 pidfile = make_pidfile_name(name);
67 /* Returns an absolute path to the configured pidfile, or a null pointer if no
68 * pidfile is configured. The caller must not modify or free the returned
76 /* Sets that we do not chdir to "/". */
83 /* Normally, die_if_already_running() will terminate the program with a message
84 * if a locked pidfile already exists. If this function is called,
85 * die_if_already_running() will merely log a warning. */
87 ignore_existing_pidfile(void)
89 overwrite_pidfile = true;
92 /* Sets up a following call to daemonize() to detach from the foreground
93 * session, running this process in the background. */
100 /* If a pidfile has been configured and that pidfile already exists and is
101 * locked by a running process, returns the pid of the running process.
102 * Otherwise, returns 0. */
104 already_running(void)
108 int fd = open(pidfile, O_RDWR);
111 lck.l_type = F_WRLCK;
112 lck.l_whence = SEEK_SET;
115 if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
124 /* If a locked pidfile exists, issue a warning message and, unless
125 * ignore_existing_pidfile() has been called, terminate the program. */
127 die_if_already_running(void)
129 pid_t pid = already_running();
131 if (!overwrite_pidfile) {
132 ovs_fatal(0, "%s: already running as pid %ld",
133 get_pidfile(), (long int) pid);
135 VLOG_WARN("%s: %s already running as pid %ld",
136 get_pidfile(), program_name, (long int) pid);
141 /* If a pidfile has been configured, creates it and stores the running process'
142 * pid init. Ensures that the pidfile will be deleted when the process
148 /* Create pidfile via temporary file, so that observers never see an
149 * empty pidfile or an unlocked pidfile. */
150 long int pid = getpid();
154 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
155 fatal_signal_add_file_to_unlink(tmpfile);
156 fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
159 lck.l_type = F_WRLCK;
160 lck.l_whence = SEEK_SET;
163 if (fcntl(fd, F_SETLK, &lck) != -1) {
164 char *text = xasprintf("%ld\n", pid);
165 if (write(fd, text, strlen(text)) == strlen(text)) {
166 fatal_signal_add_file_to_unlink(pidfile);
167 if (rename(tmpfile, pidfile) < 0) {
168 VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
169 tmpfile, pidfile, strerror(errno));
170 fatal_signal_remove_file_to_unlink(pidfile);
173 /* Keep 'fd' open to retain the lock. */
177 VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
181 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
185 VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
187 fatal_signal_remove_file_to_unlink(tmpfile);
194 /* If configured with set_pidfile() or set_detach(), creates the pid file and
195 * detaches from the foreground session. */
203 ovs_fatal(errno, "pipe failed");
208 /* Parent process: wait for child to create pidfile, then exit. */
211 if (read(fds[0], &c, 1) != 1) {
212 ovs_fatal(errno, "daemon child failed to signal startup");
220 write(fds[1], &c, 1);
231 ovs_fatal(errno, "could not fork");
243 "\nDaemon options:\n"
244 " --detach run in background as daemon\n"
245 " --no-chdir do not chdir to '/'\n"
246 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
247 " --overwrite-pidfile with --pidfile, start even if already "
249 ovs_rundir, program_name);
252 /* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
253 * successful, otherwise a negative errno value. */
255 read_pidfile(const char *pidfile)
262 file = fopen(pidfile, "r");
265 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
269 lck.l_type = F_WRLCK;
270 lck.l_whence = SEEK_SET;
273 if (fcntl(fileno(file), F_GETLK, &lck)) {
275 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
278 if (lck.l_type == F_UNLCK) {
280 VLOG_WARN("%s: pid file is not locked", pidfile);
284 if (!fgets(line, sizeof line, file)) {
287 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
290 VLOG_WARN("%s: read: unexpected end of file", pidfile);
295 if (lck.l_pid != strtoul(line, NULL, 10)) {
297 VLOG_WARN("l_pid (%ld) != %s pid (%s)",
298 (long int) lck.l_pid, pidfile, line);