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.
25 unsigned int debug_flags
= 0;
27 struct thread_master
*master
;
28 struct timeval current_time
;
29 static const char *pid_file
= PATH_NHRPD_PID
;
30 static char config_default
[] = SYSCONFDIR NHRP_DEFAULT_CONFIG
;
31 static char *config_file
= NULL
;
32 static char *vty_addr
= NULL
;
33 static int vty_port
= NHRP_VTY_PORT
;
34 static int do_daemonise
= 0;
37 struct option longopts
[] = {
38 { "daemon", no_argument
, NULL
, 'd'},
39 { "config_file", required_argument
, NULL
, 'f'},
40 { "pid_file", required_argument
, NULL
, 'i'},
41 { "socket", required_argument
, NULL
, 'z'},
42 { "help", no_argument
, NULL
, 'h'},
43 { "vty_addr", required_argument
, NULL
, 'A'},
44 { "vty_port", required_argument
, NULL
, 'P'},
45 { "user", required_argument
, NULL
, 'u'},
46 { "group", required_argument
, NULL
, 'g'},
47 { "version", no_argument
, NULL
, 'v'},
51 /* nhrpd privileges */
52 static zebra_capabilities_t _caps_p
[] = {
55 ZCAP_DAC_OVERRIDE
, /* for now needed to write to /proc/sys/net/ipv4/<if>/send_redirect */
58 static struct zebra_privs_t nhrpd_privs
= {
63 .group
= QUAGGA_GROUP
,
66 .vty_group
= VTY_GROUP
,
69 .cap_num_p
= ZEBRA_NUM_OF(_caps_p
),
72 static void usage(const char *progname
, int status
)
75 fprintf(stderr
, "Try `%s --help' for more information.\n", progname
);
78 "Usage : %s [OPTION...]\n\
79 Daemon which manages NHRP protocol.\n\n\
80 -d, --daemon Runs in daemon mode\n\
81 -f, --config_file Set configuration file name\n\
82 -i, --pid_file Set process identifier file name\n\
83 -z, --socket Set path of zebra socket\n\
84 -A, --vty_addr Set vty's bind address\n\
85 -P, --vty_port Set vty's port number\n\
86 -u, --user User to run as\n\
87 -g, --group Group to run as\n\
88 -v, --version Print program version\n\
89 -h, --help Display this help and exit\n\
91 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
96 static void parse_arguments(const char *progname
, int argc
, char **argv
)
101 opt
= getopt_long(argc
, argv
, "df:i:z:hA:P:u:g:v", longopts
, 0);
111 config_file
= optarg
;
117 zclient_serv_path_set(optarg
);
123 vty_port
= atoi (optarg
);
124 if (vty_port
<= 0 || vty_port
> 0xffff)
125 vty_port
= NHRP_VTY_PORT
;
128 nhrpd_privs
.user
= optarg
;
131 nhrpd_privs
.group
= optarg
;
134 print_version(progname
);
147 static void nhrp_sigusr1(void)
152 static void nhrp_request_stop(void)
154 debugf(NHRP_DEBUG_COMMON
, "Exiting...");
156 nhrp_shortcut_terminate();
157 nhrp_nhs_terminate();
158 nhrp_zebra_terminate();
163 /* memory_terminate(); */
164 /* vty_terminate(); */
166 /* signal_terminate(); */
167 zprivs_terminate(&nhrpd_privs
);
169 debugf(NHRP_DEBUG_COMMON
, "Remove pid file.");
170 if (pid_file
) unlink(pid_file
);
171 debugf(NHRP_DEBUG_COMMON
, "Done.");
173 closezlog(zlog_default
);
178 static struct quagga_signal_t sighandlers
[] = {
179 { .signal
= SIGUSR1
, .handler
= &nhrp_sigusr1
, },
180 { .signal
= SIGINT
, .handler
= &nhrp_request_stop
, },
181 { .signal
= SIGTERM
, .handler
= &nhrp_request_stop
, },
184 int main(int argc
, char **argv
)
186 struct thread thread
;
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 while (thread_fetch(master
, &thread
))
243 thread_call(&thread
);