Centralize daemon option processing and usage.
authorBen Pfaff <blp@nicira.com>
Thu, 16 Oct 2008 23:16:36 +0000 (16:16 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 16 Oct 2008 23:16:36 +0000 (16:16 -0700)
controller/controller.c
include/daemon.h
lib/daemon.c
secchan/secchan.c
switch/switch.c

index e761f43677f32d0ae4d6312609b41ae71957c3fb..df8ca16bbeac6cf8406dab5172111bca0c10023c 100644 (file)
@@ -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);
 }
index ee6b0f0fadca4566a00348e6455467313270c3d3..94d369aaafb98205b8e7831a64f74127574b166a 100644 (file)
 
 #include <stdbool.h>
 
+#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 */
index c0b42bc6730f88581d09d18b4a0de46426b91083..f9bf5cef9be8dd35eb48444c6310e21346a533d5 100644 (file)
@@ -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);
+}
index 5037cec27f406940ec85ec6315f030b0c0472a9a..8311d13cd5e188c0c06f03d292500d2e27d75ca3 100644 (file)
@@ -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);
 }
index a7624126cd51c92f07375845a774cc14410910ee..e24ea671cd9b9128d67da98577d361df6e70d9da 100644 (file)
@@ -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);
 }