-void
-daemon_usage(void)
-{
- printf(
- "\nDaemon options:\n"
- " -D, --detach run in background as daemon\n"
- " --no-chdir do not chdir to '/'\n"
- " -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
- " -f, --force with -P, start even if already running\n",
- ovs_rundir, program_name);
-}
-
-/* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
- * successful, otherwise a negative errno value. */
-pid_t
-read_pidfile(const char *pidfile)
-{
- char line[128];
- struct flock lck;
- FILE *file;
- int error;
-
- file = fopen(pidfile, "r");
- if (!file) {
- error = errno;
- VLOG_WARN("%s: open: %s", pidfile, strerror(error));
- goto error;
- }
-
- lck.l_type = F_WRLCK;
- lck.l_whence = SEEK_SET;
- lck.l_start = 0;
- lck.l_len = 0;
- if (fcntl(fileno(file), F_GETLK, &lck)) {
- error = errno;
- VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
- goto error;
- }
- if (lck.l_type == F_UNLCK) {
- error = ESRCH;
- VLOG_WARN("%s: pid file is not locked", pidfile);
- goto error;
- }
-
- if (!fgets(line, sizeof line, file)) {
- if (ferror(file)) {
- error = errno;
- VLOG_WARN("%s: read: %s", pidfile, strerror(error));
- } else {
- error = ESRCH;
- VLOG_WARN("%s: read: unexpected end of file", pidfile);
- }
- goto error;
- }
-
- if (lck.l_pid != strtoul(line, NULL, 10)) {
- error = ESRCH;
- VLOG_WARN("l_pid (%ld) != %s pid (%s)",
- (long int) lck.l_pid, pidfile, line);
- goto error;
- }
-
- fclose(file);
- return lck.l_pid;
-
-error:
- if (file) {
- fclose(file);
- }
- return -error;