1 /* NHRP daemon main functions
2 * Copyright (c) 2014-2015 Timo Teräs
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
26 unsigned int debug_flags = 0;
28 struct thread_master *master;
29 struct timeval current_time;
30 static const char *pid_file = PATH_NHRPD_PID;
31 static char config_default[] = SYSCONFDIR NHRP_DEFAULT_CONFIG;
32 static char *config_file = NULL;
33 static char *vty_addr = NULL;
34 static int vty_port = NHRP_VTY_PORT;
35 static int do_daemonise = 0;
38 struct option longopts[] = {
39 { "daemon", no_argument, NULL, 'd'},
40 { "config_file", required_argument, NULL, 'f'},
41 { "pid_file", required_argument, NULL, 'i'},
42 { "socket", required_argument, NULL, 'z'},
43 { "help", no_argument, NULL, 'h'},
44 { "vty_addr", required_argument, NULL, 'A'},
45 { "vty_port", required_argument, NULL, 'P'},
46 { "user", required_argument, NULL, 'u'},
47 { "group", required_argument, NULL, 'g'},
48 { "version", no_argument, NULL, 'v'},
52 /* nhrpd privileges */
53 static zebra_capabilities_t _caps_p [] = {
56 ZCAP_DAC_OVERRIDE, /* for now needed to write to /proc/sys/net/ipv4/<if>/send_redirect */
59 static struct zebra_privs_t nhrpd_privs = {
64 .group = QUAGGA_GROUP,
67 .vty_group = VTY_GROUP,
70 .cap_num_p = ZEBRA_NUM_OF(_caps_p),
73 static void usage(const char *progname, int status)
76 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
79 "Usage : %s [OPTION...]\n\
80 Daemon which manages NHRP protocol.\n\n\
81 -d, --daemon Runs in daemon mode\n\
82 -f, --config_file Set configuration file name\n\
83 -i, --pid_file Set process identifier file name\n\
84 -z, --socket Set path of zebra socket\n\
85 -A, --vty_addr Set vty's bind address\n\
86 -P, --vty_port Set vty's port number\n\
87 -u, --user User to run as\n\
88 -g, --group Group to run as\n\
89 -v, --version Print program version\n\
90 -h, --help Display this help and exit\n\
92 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
97 static void parse_arguments(const char *progname, int argc, char **argv)
102 opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0);
112 config_file = optarg;
118 zclient_serv_path_set(optarg);
124 vty_port = atoi (optarg);
125 if (vty_port <= 0 || vty_port > 0xffff)
126 vty_port = NHRP_VTY_PORT;
129 nhrpd_privs.user = optarg;
132 nhrpd_privs.group = optarg;
135 print_version(progname);
148 static void nhrp_sigusr1(void)
153 static void nhrp_request_stop(void)
155 debugf(NHRP_DEBUG_COMMON, "Exiting...");
157 nhrp_shortcut_terminate();
158 nhrp_nhs_terminate();
159 nhrp_zebra_terminate();
164 /* memory_terminate(); */
165 /* vty_terminate(); */
167 /* signal_terminate(); */
168 zprivs_terminate(&nhrpd_privs);
170 debugf(NHRP_DEBUG_COMMON, "Remove pid file.");
171 if (pid_file) unlink(pid_file);
172 debugf(NHRP_DEBUG_COMMON, "Done.");
174 closezlog(zlog_default);
179 static struct quagga_signal_t sighandlers[] = {
180 { .signal = SIGUSR1, .handler = &nhrp_sigusr1, },
181 { .signal = SIGINT, .handler = &nhrp_request_stop, },
182 { .signal = SIGTERM, .handler = &nhrp_request_stop, },
185 int main(int argc, char **argv)
187 const char *progname;
189 /* Set umask before anything for security */
191 progname = basename(argv[0]);
192 zlog_default = openzlog(progname, ZLOG_NHRP, LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
193 zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
195 parse_arguments(progname, argc, argv);
198 master = thread_master_create();
199 zprivs_init(&nhrpd_privs);
200 signal_init(master, array_size(sighandlers), sighandlers);
204 nhrp_interface_init();
208 /* Run with elevated capabilities, as for all netlink activity
209 * we need privileges anyway. */
210 nhrpd_privs.change(ZPRIVS_RAISE);
218 nhrp_shortcut_init();
222 /* Get zebra configuration file. */
223 zlog_set_level(NULL, ZLOG_DEST_STDOUT, do_daemonise ? ZLOG_DISABLED : LOG_DEBUG);
224 vty_read_config(config_file, config_default);
226 if (do_daemonise && daemon(0, 0) < 0) {
227 zlog_err("daemonise: %s", safe_strerror(errno));
232 if (pid_output(pid_file) < 0) {
233 zlog_err("error while writing pidfile");
237 /* Create VTY socket */
238 vty_serv_sock(vty_addr, vty_port, NHRP_VTYSH_PATH);
239 zlog_notice("nhrpd starting: vty@%d", vty_port);
242 thread_main (master);