Skip to content

Commit

Permalink
daemon: Remove short options from daemon library
Browse files Browse the repository at this point in the history
The daemon library provides a few short options, but these then take
away their availability from programs that wish to use the library.
Since the daemon options are generally going to be called from a script
(which doesn't care how much typing is involved), we'll only provide
long options.
  • Loading branch information
Justin Pettit committed Aug 7, 2009
1 parent 8093d64 commit e7bd7d7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 47 deletions.
13 changes: 7 additions & 6 deletions lib/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static bool detach;
static char *pidfile;

/* Create pidfile even if one already exists and is locked? */
static bool force;
static bool overwrite_pidfile;

/* Should we chdir to "/". */
static bool chdir_ = true;
Expand Down Expand Up @@ -85,7 +85,7 @@ set_no_chdir(void)
void
ignore_existing_pidfile(void)
{
force = true;
overwrite_pidfile = true;
}

/* Sets up a following call to daemonize() to detach from the foreground
Expand Down Expand Up @@ -127,7 +127,7 @@ die_if_already_running(void)
{
pid_t pid = already_running();
if (pid) {
if (!force) {
if (!overwrite_pidfile) {
ovs_fatal(0, "%s: already running as pid %ld",
get_pidfile(), (long int) pid);
} else {
Expand Down Expand Up @@ -239,10 +239,11 @@ daemon_usage(void)
{
printf(
"\nDaemon options:\n"
" -D, --detach run in background as daemon\n"
" --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",
" --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
" --overwrite-pidfile with --pidfile, start even if already "
"running\n",
ovs_rundir, program_name);
}

Expand Down
21 changes: 12 additions & 9 deletions lib/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,32 @@
#include <sys/types.h>

enum {
OPT_NO_CHDIR = UCHAR_MAX + 2048
OPT_DETACH = UCHAR_MAX + 2048,
OPT_NO_CHDIR,
OPT_OVERWRITE_PIDFILE,
OPT_PIDFILE,
};

#define DAEMON_LONG_OPTIONS \
{"detach", no_argument, 0, 'D'}, \
{"no-chdir", no_argument, 0, OPT_NO_CHDIR}, \
{"force", no_argument, 0, 'f'}, \
{"pidfile", optional_argument, 0, 'P'}
#define DAEMON_LONG_OPTIONS \
{"detach", no_argument, 0, OPT_DETACH}, \
{"no-chdir", no_argument, 0, OPT_NO_CHDIR}, \
{"pidfile", optional_argument, 0, OPT_PIDFILE}, \
{"overwrite-pidfile", no_argument, 0, OPT_OVERWRITE_PIDFILE}

#define DAEMON_OPTION_HANDLERS \
case 'D': \
case OPT_DETACH: \
set_detach(); \
break; \
\
case OPT_NO_CHDIR: \
set_no_chdir(); \
break; \
\
case 'P': \
case OPT_PIDFILE: \
set_pidfile(optarg); \
break; \
\
case 'f': \
case OPT_OVERWRITE_PIDFILE: \
ignore_existing_pidfile(); \
break;

Expand Down
22 changes: 10 additions & 12 deletions lib/daemon.man
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
.TP
\fB-P\fR[\fIpidfile\fR], \fB--pidfile\fR[\fB=\fIpidfile\fR]
\fB--pidfile\fR[\fB=\fIpidfile\fR]
Causes a file (by default, \fB\*(PN.pid\fR) to be created indicating
the PID of the running process. If \fIpidfile\fR is not specified, or
if it does not begin with \fB/\fR, then it is created in
\fB@RUNDIR@\fR.

.TP
\fB-f\fR, \fB--force\fR
By default, when \fB-P\fR or \fB--pidfile\fR is specified and the
specified pidfile already exists and is locked by a running process,
\fB\*(PN\fR refuses to start. Specify \fB-f\fR or \fB--force\fR
to cause it to instead overwrite the pidfile.
\fB--overwrite-pidfile\fR
By default, when \fB--pidfile\fR is specified and the specified pidfile
already exists and is locked by a running process, \fB\*(PN\fR refuses
to start. Specify \fB--overwrite-pidfile\fR to cause it to instead
overwrite the pidfile.

When \fB-P\fR or \fB--pidfile\fR is not specified, this option has no
effect.
When \fB--pidfile\fR is not specified, this option has no effect.

.TP
\fB-D\fR, \fB--detach\fR
\fB--detach\fR
Causes \fB\*(PN\fR to detach itself from the foreground session and
run as a background process.

.TP
\fB--no-chdir\fR
By default, when \fB-D\fR or \fB--detach\fR is specified, \fB\*(PN\fR
By default, when \fB--detach\fR is specified, \fB\*(PN\fR
changes its current working directory to the root directory after it
detaches. Otherwise, invoking \fB\*(PN\fR from a carelessly chosen
directory would prevent the administrator from unmounting the file
Expand All @@ -34,5 +33,4 @@ useful for collecting core files, since it is common behavior to write
core dumps into the current working directory and the root directory
is not a good directory to use.
.IP
This option has no effect when neither \fB-D\fR nor \fB--detach\fR is
specified.
This option has no effect when \fB--detach\fR is not specified.
17 changes: 8 additions & 9 deletions utilities/ovs-discover.8.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ This option is mutually exclusive with \fB--exit-without-bind\fR and
\fB--exit-after-bind\fR.

.TP
\fB-P\fR[\fIpidfile\fR], \fB--pidfile\fR[\fB=\fIpidfile\fR]
\fB--pidfile\fR[\fB=\fIpidfile\fR]
Causes a file (by default, \fBovs\-discover.pid\fR) to be created indicating
the PID of the running process. If \fIpidfile\fR is not specified, or
if it does not begin with \fB/\fR, then it is created in
Expand All @@ -85,14 +85,13 @@ this this option has no effect when one of \fB--exit-without-bind\fR,
\fB--exit-after-bind\fR, or \fB--no-detach\fR is also given.

.TP
\fB-f\fR, \fB--force\fR
By default, when \fB-P\fR or \fB--pidfile\fR is specified and the
specified pidfile already exists and is locked by a running process,
\fBcontroller\fR refuses to start. Specify \fB-f\fR or \fB--force\fR
to cause it to instead overwrite the pidfile.

When \fB-P\fR or \fB--pidfile\fR is not specified, this option has no
effect.
\fB--overwrite-pidfile\fR
By default, when \fB--pidfile\fR is specified and the specified pidfile
already exists and is locked by a running process, \fBcontroller\fR refuses
to start. Specify \fB--overwrite-pidfile\fR to cause it to instead
overwrite the pidfile.

When \fB--pidfile\fR is not specified, this option has no effect.

.so lib/vlog.man
.so lib/common.man
Expand Down
15 changes: 8 additions & 7 deletions utilities/ovs-discover.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ parse_options(int argc, char *argv[])
OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
OPT_EXIT_WITHOUT_BIND,
OPT_EXIT_AFTER_BIND,
OPT_NO_DETACH,
OPT_NO_DETACH
};
static struct option long_options[] = {
{"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
{"exit-without-bind", no_argument, 0, OPT_EXIT_WITHOUT_BIND},
{"exit-after-bind", no_argument, 0, OPT_EXIT_AFTER_BIND},
{"no-detach", no_argument, 0, OPT_NO_DETACH},
{"timeout", required_argument, 0, 't'},
{"pidfile", optional_argument, 0, 'P'},
{"force", no_argument, 0, 'f'},
{"pidfile", optional_argument, 0, OPT_PIDFILE},
{"overwrite-pidfile", no_argument, 0, OPT_OVERWRITE_PIDFILE},
{"verbose", optional_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
Expand Down Expand Up @@ -328,11 +328,11 @@ parse_options(int argc, char *argv[])
detach_after_bind = false;
break;

case 'P':
case OPT_PIDFILE:
set_pidfile(optarg);
break;

case 'f':
case OPT_OVERWRITE_PIDFILE:
ignore_existing_pidfile();
break;

Expand Down Expand Up @@ -396,8 +396,9 @@ usage(void)
vlog_usage();
printf("\nOther options:\n"
" -t, --timeout=SECS give up discovery after SECS seconds\n"
" -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
" -f, --force with -P, start even if already running\n"
" --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
" --overwrite-pidfile with --pidfile, start even if already "
"running\n"
" -h, --help display this help message\n"
" -V, --version display version information\n",
ovs_rundir, program_name);
Expand Down
8 changes: 4 additions & 4 deletions xenserver/etc_init.d_vswitch
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ function start_vswitchd {
if [ "$daemonize" != "y" ]; then
# Start in background and force a "success" message
action "Starting ovs-vswitchd ($strace_opt$valgrind_opt)" true
(nice -n "$VSWITCHD_PRIORITY" $strace_opt $valgrind_opt "$vswitchd" -P"$VSWITCHD_PIDFILE" -D $fake_proc_net_opt -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF") &
(nice -n "$VSWITCHD_PRIORITY" $strace_opt $valgrind_opt "$vswitchd" --pidfile="$VSWITCHD_PIDFILE" --detach $fake_proc_net_opt -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF") &
else
action "Starting ovs-vswitchd" nice -n "$VSWITCHD_PRIORITY" "$vswitchd" -P"$VSWITCHD_PIDFILE" -D $fake_proc_net_opt -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF"
action "Starting ovs-vswitchd" nice -n "$VSWITCHD_PRIORITY" "$vswitchd" --pidfile="$VSWITCHD_PIDFILE" --detach $fake_proc_net_opt -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF"
fi
}

Expand Down Expand Up @@ -191,9 +191,9 @@ function start_brcompatd {
if [ "$daemonize" != "y" ]; then
# Start in background and force a "success" message
action "Starting ovs-brcompatd ($strace_opt$valgrind_opt)" true
(nice -n "$VSWITCHD_PRIORITY" $strace_opt $valgrind_opt "$brcompatd" --appctl-command="$appctl_cmd" -P$BRCOMPATD_PIDFILE -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF") &
(nice -n "$VSWITCHD_PRIORITY" $strace_opt $valgrind_opt "$brcompatd" --appctl-command="$appctl_cmd" --pidfile=$BRCOMPATD_PIDFILE -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF") &
else
action "Starting ovs-brcompatd" nice -n "$BRCOMPATD_PRIORITY" $strace_opt $valgrind_opt "$brcompatd" --appctl-command="$appctl_cmd" -P$BRCOMPATD_PIDFILE -D -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF"
action "Starting ovs-brcompatd" nice -n "$BRCOMPATD_PRIORITY" $strace_opt $valgrind_opt "$brcompatd" --appctl-command="$appctl_cmd" --pidfile=$BRCOMPATD_PIDFILE --detach -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt $leak_opt "$VSWITCHD_CONF"
fi
}

Expand Down

0 comments on commit e7bd7d7

Please sign in to comment.