Add --detach and --pidfile options to daemons.
[openvswitch] / secchan / secchan.c
index d1e619c29e36ab66b272766697e63f5db61c0317..75d46ebccf4223876e82d54079f5e373c9833068 100644 (file)
@@ -44,6 +44,7 @@
 #include "buffer.h"
 #include "command-line.h"
 #include "compiler.h"
+#include "daemon.h"
 #include "fault.h"
 #include "flow.h"
 #include "learning-switch.h"
@@ -100,7 +101,7 @@ static uint8_t local_mac[ETH_ADDR_LEN];
 static struct mac_learning *local_ml;
 
 /* -f, --fail: Behavior when the connection to the controller fails. */
-static enum fail_mode fail_mode;
+static enum fail_mode fail_mode = FAIL_OPEN;
 
 /* -d, --fail-open-delay: Number of seconds after which to fail open, when
  * fail_mode is FAIL_OPEN. */
@@ -187,6 +188,8 @@ main(int argc, char *argv[])
         fatal(retval, "Could not listen for vlog connections");
     }
 
+    daemonize();
+
     relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1),
                  false);
     for (;;) {
@@ -466,6 +469,8 @@ parse_options(int argc, char *argv[])
         {"fail",        required_argument, 0, 'f'},
         {"fail-open-delay", required_argument, 0, 'd'},
         {"listen",      required_argument, 0, 'l'},
+        {"detach",      no_argument, 0, 'D'},
+        {"pidfile",     optional_argument, 0, 'P'},
         {"verbose",     optional_argument, 0, 'v'},
         {"help",        no_argument, 0, 'h'},
         {"version",     no_argument, 0, 'V'},
@@ -502,6 +507,14 @@ parse_options(int argc, char *argv[])
             }
             break;
 
+        case 'D':
+            set_detach();
+            break;
+
+        case 'P':
+            set_pidfile(optarg ? optarg : "secchan.pid");
+            break;
+
         case 'l':
             if (listen_vconn_name) {
                 fatal(0, "-l or --listen may be only specified once");
@@ -542,15 +555,18 @@ usage(void)
     vconn_usage(true, true);
     printf("\nNetworking options:\n"
            "  -f, --fail=open|closed  when controller connection fails:\n"
-           "                            closed (default): drop all packets\n"
-           "                            open: act as learning switch\n"
+           "                            closed: drop all packets\n"
+           "                            open (default): act as learning switch\n"
            "  -d, --fail-open-delay=SECS  number of seconds after which to\n"
            "                          fail open if --fail=open (default: 30)\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/secchan.pid)\n"
            "  -v, --verbose           set maximum verbosity level\n"
            "  -h, --help              display this help message\n"
-           "  -V, --version           display version information\n");
+           "  -V, --version           display version information\n",
+           RUNDIR);
     exit(EXIT_SUCCESS);
 }