From: Ben Pfaff Date: Thu, 16 Oct 2008 23:16:36 +0000 (-0700) Subject: Centralize daemon option processing and usage. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b50bbdf27daa89240b164dd65bcfa21962c2072a;p=openvswitch Centralize daemon option processing and usage. --- diff --git a/controller/controller.c b/controller/controller.c index e761f436..df8ca16b 100644 --- a/controller/controller.c +++ b/controller/controller.c @@ -242,15 +242,13 @@ parse_options(int argc, char *argv[]) OPT_PEER_CA_CERT }; static struct option long_options[] = { - {"detach", no_argument, 0, 'D'}, - {"pidfile", optional_argument, 0, 'P'}, - {"force", no_argument, 0, 'f'}, {"hub", no_argument, 0, 'H'}, {"noflow", no_argument, 0, 'n'}, {"max-idle", required_argument, 0, OPT_MAX_IDLE}, {"verbose", optional_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, + DAEMON_LONG_OPTIONS, #ifdef HAVE_OPENSSL VCONN_SSL_LONG_OPTIONS {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT}, @@ -269,18 +267,6 @@ parse_options(int argc, char *argv[]) } switch (c) { - case 'D': - set_detach(); - break; - - case 'P': - set_pidfile(optarg); - break; - - case 'f': - ignore_existing_pidfile(); - break; - case 'H': learn_macs = false; break; @@ -312,6 +298,8 @@ parse_options(int argc, char *argv[]) vlog_set_verbosity(optarg); break; + DAEMON_OPTION_HANDLERS + #ifdef HAVE_OPENSSL VCONN_SSL_OPTION_HANDLERS @@ -338,17 +326,14 @@ usage(void) "where METHOD is any OpenFlow connection method.\n", program_name, program_name); vconn_usage(true, true, false); + daemon_usage(); printf("\nOther options:\n" - " -D, --detach run in background as daemon\n" - " -P, --pidfile[=FILE] create pidfile (default: %s/controller.pid)\n" - " -f, --force with -P, start even if already running\n" " -H, --hub act as hub instead of learning switch\n" " -n, --noflow pass traffic, but don't add flows\n" " --max-idle=SECS max idle time for new flows\n" " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n" " -v, --verbose set maximum verbosity level\n" " -h, --help display this help message\n" - " -V, --version display version information\n", - RUNDIR); + " -V, --version display version information\n"); exit(EXIT_SUCCESS); } diff --git a/include/daemon.h b/include/daemon.h index ee6b0f0f..94d369aa 100644 --- a/include/daemon.h +++ b/include/daemon.h @@ -36,6 +36,24 @@ #include +#define DAEMON_LONG_OPTIONS \ + {"detach", no_argument, 0, 'D'}, \ + {"force", no_argument, 0, 'f'}, \ + {"pidfile", optional_argument, 0, 'P'} + +#define DAEMON_OPTION_HANDLERS \ + case 'D': \ + set_detach(); \ + break; \ + \ + case 'P': \ + set_pidfile(optarg); \ + break; \ + \ + case 'f': \ + ignore_existing_pidfile(); \ + break; + char *make_pidfile_name(const char *name); void set_pidfile(const char *name); const char *get_pidfile(void); @@ -43,5 +61,6 @@ void set_detach(void); void daemonize(void); void die_if_already_running(void); void ignore_existing_pidfile(void); +void daemon_usage(void); #endif /* daemon.h */ diff --git a/lib/daemon.c b/lib/daemon.c index c0b42bc6..f9bf5cef 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -235,3 +235,13 @@ daemonize(void) } } +void +daemon_usage(void) +{ + printf( + "\nDaemon options:\n" + " -D, --detach run in background as daemon\n" + " -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n" + " -f, --force with -P, start even if already running\n", + RUNDIR, program_name); +} diff --git a/secchan/secchan.c b/secchan/secchan.c index 5037cec2..8311d13c 100644 --- a/secchan/secchan.c +++ b/secchan/secchan.c @@ -2531,12 +2531,10 @@ parse_options(int argc, char *argv[], struct settings *s) {"no-stp", no_argument, 0, OPT_NO_STP}, {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND}, {"in-band", no_argument, 0, OPT_IN_BAND}, - {"detach", no_argument, 0, 'D'}, - {"force", no_argument, 0, 'f'}, - {"pidfile", optional_argument, 0, 'P'}, {"verbose", optional_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, + DAEMON_LONG_OPTIONS, #ifdef HAVE_OPENSSL VCONN_SSL_LONG_OPTIONS {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT}, @@ -2649,18 +2647,6 @@ parse_options(int argc, char *argv[], struct settings *s) s->in_band = true; break; - case 'D': - set_detach(); - break; - - case 'P': - set_pidfile(optarg); - break; - - case 'f': - ignore_existing_pidfile(); - break; - case 'l': if (s->n_listeners >= MAX_MGMT) { ofp_fatal(0, @@ -2688,6 +2674,8 @@ parse_options(int argc, char *argv[], struct settings *s) vlog_set_verbosity(optarg); break; + DAEMON_OPTION_HANDLERS + #ifdef HAVE_OPENSSL VCONN_SSL_OPTION_HANDLERS @@ -2780,15 +2768,12 @@ usage(void) " --no-stp disable 802.1D Spanning Tree Protocol\n" "\nRate-limiting of \"packet-in\" messages to the controller:\n" " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n" - " --burst-limit=BURST limit on packet credit for idle time\n" - "\nOther options:\n" - " -D, --detach run in background as daemon\n" - " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n" - " -f, --force with -P, start even if already running\n" + " --burst-limit=BURST limit on packet credit for idle time\n"); + daemon_usage(); + printf("\nOther options:\n" " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n" " -v, --verbose set maximum verbosity level\n" " -h, --help display this help message\n" - " -V, --version display version information\n", - RUNDIR); + " -V, --version display version information\n"); exit(EXIT_SUCCESS); } diff --git a/switch/switch.c b/switch/switch.c index a7624126..e24ea671 100644 --- a/switch/switch.c +++ b/switch/switch.c @@ -171,9 +171,6 @@ parse_options(int argc, char *argv[]) {"datapath-id", required_argument, 0, 'd'}, {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF}, {"listen", required_argument, 0, 'l'}, - {"detach", no_argument, 0, 'D'}, - {"pidfile", optional_argument, 0, 'P'}, - {"force", no_argument, 0, 'f'}, {"verbose", optional_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, @@ -181,6 +178,7 @@ parse_options(int argc, char *argv[]) {"hw-desc", required_argument, 0, OPT_HW_DESC}, {"sw-desc", required_argument, 0, OPT_SW_DESC}, {"serial_num", required_argument, 0, OPT_SERIAL_NUM}, + DAEMON_LONG_OPTIONS, #ifdef HAVE_OPENSSL VCONN_SSL_LONG_OPTIONS {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT}, @@ -219,18 +217,6 @@ parse_options(int argc, char *argv[]) printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]); exit(EXIT_SUCCESS); - case 'D': - set_detach(); - break; - - case 'P': - set_pidfile(optarg); - break; - - case 'f': - ignore_existing_pidfile(); - break; - case 'v': vlog_set_verbosity(optarg); break; @@ -275,6 +261,8 @@ parse_options(int argc, char *argv[]) listen_pvconn_name = optarg; break; + DAEMON_OPTION_HANDLERS + #ifdef HAVE_OPENSSL VCONN_SSL_OPTION_HANDLERS @@ -309,15 +297,12 @@ usage(void) " --max-backoff=SECS max time between controller connection\n" " attempts (default: 15 seconds)\n" " -l, --listen=METHOD allow management connections on METHOD\n" - " (a passive OpenFlow connection method)\n" - "\nOther options:\n" - " -D, --detach run in background as daemon\n" - " -P, --pidfile[=FILE] create pidfile (default: %s/switch.pid)\n" - " -f, --force with -P, start even if already running\n" + " (a passive OpenFlow connection method)\n"); + daemon_usage(); + printf("\nOther options:\n" " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n" " -v, --verbose set maximum verbosity level\n" " -h, --help display this help message\n" - " -V, --version display version information\n", - RUNDIR); + " -V, --version display version information\n"); exit(EXIT_SUCCESS); }