summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2015-12-31 18:48:44 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2015-12-31 19:58:34 -0500
commita40fd1bb6c4769683c11b3e308279c17a0acbc5a (patch)
tree76c91d86542bcdbb3b8b10e7ab9656488f2faca2
parent8c812f534175d43d93e022af49f0441cfca0ea0d (diff)
Add ability to specify multiple command line arguments
Needed in order to specify which log backend to use. Init scripts need to be updated to contain --config before the path to the config file.
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c128
-rw-r--r--other/bootstrap_daemon/tox-bootstrapd.service2
-rw-r--r--other/bootstrap_daemon/tox-bootstrapd.sh2
3 files changed, 121 insertions, 11 deletions
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index 9a4ccf9f..e2bf7ade 100644
--- a/other/bootstrap_daemon/src/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c
@@ -23,6 +23,7 @@
23 23
24// system provided 24// system provided
25#include <arpa/inet.h> 25#include <arpa/inet.h>
26#include <getopt.h>
26#include <syslog.h> 27#include <syslog.h>
27#include <sys/stat.h> 28#include <sys/stat.h>
28#include <sys/types.h> 29#include <sys/types.h>
@@ -516,18 +517,125 @@ void print_public_key(const uint8_t *public_key)
516 return; 517 return;
517} 518}
518 519
519int main(int argc, char *argv[]) 520// Prints --help message
521
522bool print_help()
520{ 523{
521 open_log(LOGGER_BACKEND_SYSLOG); 524 // 2 space ident
525 // make sure all lines fit into 80 columns
526 write_log(LOG_LEVEL_INFO,
527 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
528 "\n"
529 "Options:\n"
530 " --config=FILE_PATH Specify path to the config file.\n"
531 " This is a required option.\n"
532 " Set FILE_PATH to a path to an empty file in order to\n"
533 " use default settings.\n"
534 " --help Print this help message.\n"
535 " --log-backend=BACKEND Specify which logging backend to use.\n"
536 " Valid BACKEND values (case sensetive):\n"
537 " syslog Writes log messages to syslog.\n"
538 " Default option when no --log-backend is\n"
539 " specified.\n"
540 " stdout Writes log messages to stdout/stderr.\n"
541 " --version Print version information.\n");
542}
522 543
523 write_log(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); 544// Handels command line arguments, setting cfg_file_path and log_backend.
545// Terminates the application if incorrect arguments are specified.
524 546
547void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOGGER_BACKEND *log_backend)
548{
525 if (argc < 2) { 549 if (argc < 2) {
526 write_log(LOG_LEVEL_ERROR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); 550 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
527 return 1; 551 print_help();
552 exit(1);
553 }
554
555 opterr = 0;
556
557 static struct option long_options[] = {
558 {"help", no_argument, 0, 'h'},
559 {"config", required_argument, 0, 'c'}, // required option
560 {"log-backend", required_argument, 0, 'l'}, // optional, defaults to syslog
561 {"version", no_argument, 0, 'v'},
562 {0, 0, 0, 0 }
563 };
564
565 bool cfg_file_path_set = false;
566 bool log_backend_set = false;
567
568 int opt;
569
570 while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
571
572 switch (opt) {
573 case 'h':
574 print_help();
575 exit(0);
576
577 case 'c':
578 *cfg_file_path = optarg;
579 cfg_file_path_set = true;
580 break;
581
582 case 'l':
583 if (strcmp(optarg, "syslog") == 0) {
584 *log_backend = LOGGER_BACKEND_SYSLOG;
585 log_backend_set = true;
586 } else if (strcmp(optarg, "stdout") == 0) {
587 *log_backend = LOGGER_BACKEND_STDOUT;
588 log_backend_set = true;
589 } else {
590 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
591 print_help();
592 exit(1);
593 }
594 break;
595
596 case 'v':
597 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
598 exit(0);
599
600 case '?':
601 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
602 print_help();
603 exit(1);
604
605 case ':':
606 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
607 print_help();
608 exit(1);
609 }
610 }
611
612 if (!log_backend_set) {
613 *log_backend = LOGGER_BACKEND_SYSLOG;
528 } 614 }
529 615
530 const char *cfg_file_path = argv[1]; 616 if (!cfg_file_path_set) {
617 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
618 print_help();
619 exit(1);
620 }
621}
622
623int main(int argc, char *argv[])
624{
625 char *cfg_file_path;
626 LOGGER_BACKEND log_backend;
627
628 // choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal
629 log_backend = isatty(STDOUT_FILENO) ? LOGGER_BACKEND_STDOUT : LOGGER_BACKEND_SYSLOG;
630
631 open_log(log_backend);
632 handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend);
633 close_log();
634
635 open_log(log_backend);
636
637 write_log(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER);
638
531 char *pid_file_path, *keys_file_path; 639 char *pid_file_path, *keys_file_path;
532 int port; 640 int port;
533 int enable_ipv6; 641 int enable_ipv6;
@@ -690,9 +798,11 @@ int main(int argc, char *argv[])
690 } 798 }
691 799
692 // Go quiet 800 // Go quiet
693 close(STDOUT_FILENO); 801 if (log_backend != LOGGER_BACKEND_STDOUT) {
694 close(STDIN_FILENO); 802 close(STDOUT_FILENO);
695 close(STDERR_FILENO); 803 close(STDIN_FILENO);
804 close(STDERR_FILENO);
805 }
696 806
697 uint64_t last_LANdiscovery = 0; 807 uint64_t last_LANdiscovery = 0;
698 const uint16_t htons_port = htons(port); 808 const uint16_t htons_port = htons(port);
diff --git a/other/bootstrap_daemon/tox-bootstrapd.service b/other/bootstrap_daemon/tox-bootstrapd.service
index db54cc41..20f698d2 100644
--- a/other/bootstrap_daemon/tox-bootstrapd.service
+++ b/other/bootstrap_daemon/tox-bootstrapd.service
@@ -8,7 +8,7 @@ RuntimeDirectory=tox-bootstrapd
8RuntimeDirectoryMode=750 8RuntimeDirectoryMode=750
9PIDFile=/var/run/tox-bootstrapd/tox-bootstrapd.pid 9PIDFile=/var/run/tox-bootstrapd/tox-bootstrapd.pid
10WorkingDirectory=/var/lib/tox-bootstrapd 10WorkingDirectory=/var/lib/tox-bootstrapd
11ExecStart=/usr/local/bin/tox-bootstrapd /etc/tox-bootstrapd.conf 11ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf
12User=tox-bootstrapd 12User=tox-bootstrapd
13Group=tox-bootstrapd 13Group=tox-bootstrapd
14#CapabilityBoundingSet=CAP_NET_BIND_SERVICE 14#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
diff --git a/other/bootstrap_daemon/tox-bootstrapd.sh b/other/bootstrap_daemon/tox-bootstrapd.sh
index 1431bde0..d33c38da 100644
--- a/other/bootstrap_daemon/tox-bootstrapd.sh
+++ b/other/bootstrap_daemon/tox-bootstrapd.sh
@@ -15,7 +15,7 @@ DESC="Tox DHT bootstrap daemon"
15NAME=tox-bootstrapd 15NAME=tox-bootstrapd
16DAEMON=/usr/local/bin/$NAME 16DAEMON=/usr/local/bin/$NAME
17CFGFILE=/etc/$NAME.conf 17CFGFILE=/etc/$NAME.conf
18DAEMON_ARGS="$CFGFILE" 18DAEMON_ARGS="--config $CFGFILE"
19PIDDIR=/var/run/$NAME 19PIDDIR=/var/run/$NAME
20PIDFILE=$PIDDIR/$NAME.pid 20PIDFILE=$PIDDIR/$NAME.pid
21SCRIPTNAME=/etc/init.d/$NAME 21SCRIPTNAME=/etc/init.d/$NAME