summaryrefslogtreecommitdiffstats
path: root/ldpd/ldpd.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2016-11-13 08:02:23 +0100
committerDavid Lamparter <equinox@opensourcerouting.org>2017-03-07 23:07:20 +0100
commit4f04a76b717fc4881a547f582a02a4d06d356c47 (patch)
tree90951be570b6de3889da7c5358b1fe2f998d4acd /ldpd/ldpd.c
parentMerge pull request #257 from opensourcerouting/nhrpd (diff)
downloadfrr-4f04a76b717fc4881a547f582a02a4d06d356c47.tar.xz
frr-4f04a76b717fc4881a547f582a02a4d06d356c47.zip
*: add frr_init() infrastructure
Start centralising startup & option parsing into the library. FRR_DAEMON_INFO is a bit weird, but it will become useful later (e.g. for killing the ZLOG_* enum, and having the daemon name available) Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'ldpd/ldpd.c')
-rw-r--r--ldpd/ldpd.c127
1 files changed, 31 insertions, 96 deletions
diff --git a/ldpd/ldpd.c b/ldpd/ldpd.c
index 4e79f8b8a..4fd40c7bb 100644
--- a/ldpd/ldpd.c
+++ b/ldpd/ldpd.c
@@ -40,8 +40,8 @@
#include "zclient.h"
#include "vrf.h"
#include "filter.h"
-#include "sockopt.h"
#include "qobj.h"
+#include "libfrr.h"
static void ldpd_shutdown(void);
static pid_t start_child(enum ldpd_process, char *, int, int,
@@ -117,14 +117,10 @@ struct zebra_privs_t ldpd_privs =
.cap_num_i = 0
};
-/* VTY Socket prefix */
-char vty_sock_path[MAXPATHLEN] = LDP_VTYSH_PATH;
-
/* CTL Socket path */
char ctl_sock_path[MAXPATHLEN] = LDPD_SOCKET;
/* LDPd options. */
-#define OPTION_VTYSOCK 1000
#define OPTION_CTLSOCK 1001
static struct option longopts[] =
{
@@ -133,47 +129,10 @@ static struct option longopts[] =
{ "pid_file", required_argument, NULL, 'i'},
{ "socket", required_argument, NULL, 'z'},
{ "dryrun", no_argument, NULL, 'C'},
- { "help", no_argument, NULL, 'h'},
- { "vty_addr", required_argument, NULL, 'A'},
- { "vty_port", required_argument, NULL, 'P'},
- { "vty_socket", required_argument, NULL, OPTION_VTYSOCK},
{ "ctl_socket", required_argument, NULL, OPTION_CTLSOCK},
- { "user", required_argument, NULL, 'u'},
- { "group", required_argument, NULL, 'g'},
- { "version", no_argument, NULL, 'v'},
{ 0 }
};
-/* Help information display. */
-static void __attribute__ ((noreturn))
-usage(char *progname, int status)
-{
- if (status != 0)
- fprintf(stderr, "Try `%s --help' for more information.\n",
- progname);
- else {
- printf("Usage : %s [OPTION...]\n\
-Daemon which manages LDP.\n\n\
--d, --daemon Runs in daemon mode\n\
--f, --config_file Set configuration file name\n\
--i, --pid_file Set process identifier file name\n\
--z, --socket Set path of zebra socket\n\
--A, --vty_addr Set vty's bind address\n\
--P, --vty_port Set vty's port number\n\
- --vty_socket Override vty socket path\n\
- --ctl_socket Override ctl socket path\n\
--u, --user User to run as\n\
--g, --group Group to run as\n\
--v, --version Print program version\n\
--C, --dryrun Check configuration for validity and exit\n\
--h, --help Display this help and exit\n\
-\n\
-Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
- }
-
- exit(status);
-}
-
/* SIGHUP handler. */
static void
sighup(void)
@@ -216,6 +175,17 @@ static struct quagga_signal_t ldp_signals[] =
}
};
+FRR_DAEMON_INFO(ldpd, LDP,
+ .vty_port = LDP_VTY_PORT,
+
+ .proghelp = "Implementation of the LDP protocol.",
+
+ .signals = ldp_signals,
+ .n_signals = array_size(ldp_signals),
+
+ .privs = &ldpd_privs,
+)
+
int
main(int argc, char *argv[])
{
@@ -223,36 +193,34 @@ main(int argc, char *argv[])
int lflag = 0, eflag = 0;
int pipe_parent2ldpe[2], pipe_parent2ldpe_sync[2];
int pipe_parent2lde[2], pipe_parent2lde_sync[2];
- char *p;
- char *vty_addr = NULL;
- int vty_port = LDP_VTY_PORT;
char *ctl_sock_custom_path = NULL;
char *ctl_sock_name;
int daemon_mode = 0;
const char *user = NULL;
const char *group = NULL;
char *config_file = NULL;
- char *progname;
struct thread thread;
int dryrun = 0;
ldpd_process = PROC_MAIN;
- /* Set umask before anything for security */
- umask(0027);
-
- /* get program name */
- progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
-
saved_argv0 = argv[0];
if (saved_argv0 == NULL)
saved_argv0 = (char *)"ldpd";
+ frr_preinit(&ldpd_di, argc, argv);
+ frr_opt_add("df:i:z:CLE", longopts,
+ " -d, --daemon Runs in daemon mode\n"
+ " -f, --config_file Set configuration file name\n"
+ " -i, --pid_file Set process identifier file name\n"
+ " -z, --socket Set path of zebra socket\n"
+ " --ctl_socket Override ctl socket path\n"
+ " -C, --dryrun Check configuration for validity and exit\n");
+
while (1) {
int opt;
- opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:vCLE",
- longopts, 0);
+ opt = frr_getopt(argc, argv, NULL);
if (opt == EOF)
break;
@@ -266,31 +234,12 @@ main(int argc, char *argv[])
case 'f':
config_file = optarg;
break;
- case 'A':
- vty_addr = optarg;
- break;
case 'i':
pid_file = optarg;
break;
case 'z':
zclient_serv_path_set(optarg);
break;
- case 'P':
- /*
- * Deal with atoi() returning 0 on failure, and ldpd
- * not listening on ldpd port.
- */
- if (strcmp(optarg, "0") == 0) {
- vty_port = 0;
- break;
- }
- vty_port = atoi(optarg);
- if (vty_port <= 0 || vty_port > 0xffff)
- vty_port = LDP_VTY_PORT;
- break;
- case OPTION_VTYSOCK:
- set_socket_path(vty_sock_path, LDP_VTYSH_PATH, optarg, sizeof (vty_sock_path));
- break;
case OPTION_CTLSOCK:
ctl_sock_name = strrchr(LDPD_SOCKET, '/');
if (ctl_sock_name)
@@ -310,22 +259,9 @@ main(int argc, char *argv[])
strlcat(ctl_sock_path, ctl_sock_name,
sizeof(ctl_sock_path));
break;
- case 'u':
- user = optarg;
- break;
- case 'g':
- group = optarg;
- break;
- case 'v':
- print_version(progname);
- exit(0);
- break;
case 'C':
dryrun = 1;
break;
- case 'h':
- usage(progname, 0);
- break;
case 'L':
lflag = 1;
break;
@@ -333,24 +269,27 @@ main(int argc, char *argv[])
eflag = 1;
break;
default:
- usage(progname, 1);
+ frr_help_exit(1);
break;
}
}
+ user = ldpd_privs.user;
+ group = ldpd_privs.group;
+
argc -= optind;
argv += optind;
if (argc > 0 || (lflag && eflag))
- usage(progname, 1);
+ frr_help_exit(1);
/* check for root privileges */
if (geteuid() != 0) {
errno = EPERM;
- perror(progname);
+ perror(ldpd_di.progname);
exit(1);
}
- zlog_default = openzlog(progname, ZLOG_LDP, 0,
+ zlog_default = openzlog(ldpd_di.progname, ZLOG_LDP, 0,
LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
if (lflag)
@@ -418,10 +357,6 @@ main(int argc, char *argv[])
user, group, ctl_sock_custom_path);
/* drop privileges */
- if (user)
- ldpd_privs.user = user;
- if (group)
- ldpd_privs.group = group;
zprivs_init(&ldpd_privs);
/* setup signal handler */
@@ -475,10 +410,10 @@ main(int argc, char *argv[])
pid_output(pid_file);
/* Create VTY socket */
- vty_serv_sock(vty_addr, vty_port, vty_sock_path);
+ frr_vty_serv(LDP_VTYSH_PATH);
/* Print banner. */
- log_notice("LDPd %s starting: vty@%d", FRR_VERSION, vty_port);
+ log_notice("LDPd %s starting: vty@%d", FRR_VERSION, ldpd_di.vty_port);
/* Fetch next active thread. */
while (thread_fetch(master, &thread))