]>
Commit | Line | Data |
---|---|---|
4f04a76b DL |
1 | /* |
2 | * libfrr overall management functions | |
3 | * | |
4 | * Copyright (C) 2016 David Lamparter for NetDEF, Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
4f04a76b DL |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
689f5a8c | 22 | #include <sys/un.h> |
4f04a76b | 23 | |
f43fbf83 DL |
24 | #include <sys/types.h> |
25 | #include <sys/wait.h> | |
26 | ||
4f04a76b DL |
27 | #include "libfrr.h" |
28 | #include "getopt.h" | |
beaa5470 | 29 | #include "privs.h" |
4f04a76b DL |
30 | #include "vty.h" |
31 | #include "command.h" | |
32 | #include "version.h" | |
857b5446 | 33 | #include "memory_vty.h" |
eb05883f | 34 | #include "zclient.h" |
cf7466ac | 35 | #include "log_int.h" |
30771d65 | 36 | #include "module.h" |
f43fbf83 | 37 | #include "network.h" |
eb05883f | 38 | |
d62a17ae | 39 | DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm)) |
03951374 DL |
40 | DEFINE_KOOH(frr_early_fini, (), ()) |
41 | DEFINE_KOOH(frr_fini, (), ()) | |
a5b38c5b | 42 | |
eb05883f DL |
43 | const char frr_sysconfdir[] = SYSCONFDIR; |
44 | const char frr_vtydir[] = DAEMON_VTY_DIR; | |
80b4df3b | 45 | const char frr_moduledir[] = MODULE_PATH; |
eb05883f | 46 | |
4f138a3e DL |
47 | char frr_protoname[256] = "NONE"; |
48 | char frr_protonameinst[256] = "NONE"; | |
b85120bc | 49 | |
ff44f570 | 50 | char config_default[512]; |
689f5a8c | 51 | char frr_zclientpath[256]; |
ff44f570 | 52 | static char pidfile_default[512]; |
eb05883f | 53 | static char vtypath_default[256]; |
4f04a76b | 54 | |
9eed278b DL |
55 | bool debug_memstats_at_exit = 0; |
56 | ||
4f04a76b DL |
57 | static char comb_optstr[256]; |
58 | static struct option comb_lo[64]; | |
59 | static struct option *comb_next_lo = &comb_lo[0]; | |
60 | static char comb_helpstr[4096]; | |
61 | ||
62 | struct optspec { | |
63 | const char *optstr; | |
64 | const char *helpstr; | |
65 | const struct option *longopts; | |
66 | }; | |
67 | ||
68 | static void opt_extend(const struct optspec *os) | |
69 | { | |
70 | const struct option *lo; | |
71 | ||
72 | strcat(comb_optstr, os->optstr); | |
73 | strcat(comb_helpstr, os->helpstr); | |
74 | for (lo = os->longopts; lo->name; lo++) | |
75 | memcpy(comb_next_lo++, lo, sizeof(*lo)); | |
76 | } | |
77 | ||
78 | ||
80b4df3b MW |
79 | #define OPTION_VTYSOCK 1000 |
80 | #define OPTION_MODULEDIR 1002 | |
e9b4e74a DS |
81 | #define OPTION_LOG 1003 |
82 | #define OPTION_LOGLEVEL 1004 | |
4f04a76b DL |
83 | |
84 | static const struct option lo_always[] = { | |
d62a17ae | 85 | {"help", no_argument, NULL, 'h'}, |
86 | {"version", no_argument, NULL, 'v'}, | |
87 | {"daemon", no_argument, NULL, 'd'}, | |
88 | {"module", no_argument, NULL, 'M'}, | |
89 | {"vty_socket", required_argument, NULL, OPTION_VTYSOCK}, | |
90 | {"moduledir", required_argument, NULL, OPTION_MODULEDIR}, | |
f8507817 | 91 | {"log", required_argument, NULL, OPTION_LOG}, |
e9b4e74a | 92 | {"log-level", required_argument, NULL, OPTION_LOGLEVEL}, |
d62a17ae | 93 | {NULL}}; |
4f04a76b | 94 | static const struct optspec os_always = { |
30771d65 | 95 | "hvdM:", |
4f04a76b DL |
96 | " -h, --help Display this help and exit\n" |
97 | " -v, --version Print program version\n" | |
eb05883f | 98 | " -d, --daemon Runs in daemon mode\n" |
30771d65 | 99 | " -M, --module Load specified module\n" |
80b4df3b | 100 | " --vty_socket Override vty socket path\n" |
f8507817 | 101 | " --moduledir Override modules directory\n" |
e9b4e74a DS |
102 | " --log Set Logging to stdout, syslog, or file:<name>\n" |
103 | " --log-level Set Logging Level to use, debug, info, warn, etc\n", | |
d62a17ae | 104 | lo_always}; |
4f04a76b DL |
105 | |
106 | ||
eb05883f | 107 | static const struct option lo_cfg_pid_dry[] = { |
d62a17ae | 108 | {"pid_file", required_argument, NULL, 'i'}, |
109 | {"config_file", required_argument, NULL, 'f'}, | |
d1b4fc1f | 110 | {"pathspace", required_argument, NULL, 'N'}, |
d62a17ae | 111 | {"dryrun", no_argument, NULL, 'C'}, |
cff2b211 | 112 | {"terminal", no_argument, NULL, 't'}, |
d62a17ae | 113 | {NULL}}; |
eb05883f | 114 | static const struct optspec os_cfg_pid_dry = { |
d1b4fc1f | 115 | "f:i:CtN:", |
eb05883f DL |
116 | " -f, --config_file Set configuration file name\n" |
117 | " -i, --pid_file Set process identifier file name\n" | |
d1b4fc1f | 118 | " -N, --pathspace Insert prefix into config & socket paths\n" |
cff2b211 DL |
119 | " -C, --dryrun Check configuration for validity and exit\n" |
120 | " -t, --terminal Open terminal session on stdio\n" | |
121 | " -d -t Daemonize after terminal session ends\n", | |
d62a17ae | 122 | lo_cfg_pid_dry}; |
eb05883f DL |
123 | |
124 | ||
125 | static const struct option lo_zclient[] = { | |
d62a17ae | 126 | {"socket", required_argument, NULL, 'z'}, |
127 | {NULL}}; | |
eb05883f | 128 | static const struct optspec os_zclient = { |
d62a17ae | 129 | "z:", " -z, --socket Set path of zebra socket\n", lo_zclient}; |
eb05883f DL |
130 | |
131 | ||
4f04a76b | 132 | static const struct option lo_vty[] = { |
d62a17ae | 133 | {"vty_addr", required_argument, NULL, 'A'}, |
134 | {"vty_port", required_argument, NULL, 'P'}, | |
135 | {NULL}}; | |
4f04a76b DL |
136 | static const struct optspec os_vty = { |
137 | "A:P:", | |
138 | " -A, --vty_addr Set vty's bind address\n" | |
139 | " -P, --vty_port Set vty's port number\n", | |
d62a17ae | 140 | lo_vty}; |
4f04a76b DL |
141 | |
142 | ||
d62a17ae | 143 | static const struct option lo_user[] = {{"user", required_argument, NULL, 'u'}, |
144 | {"group", required_argument, NULL, 'g'}, | |
145 | {NULL}}; | |
146 | static const struct optspec os_user = {"u:g:", | |
147 | " -u, --user User to run as\n" | |
148 | " -g, --group Group to run as\n", | |
149 | lo_user}; | |
4f04a76b DL |
150 | |
151 | ||
689f5a8c DL |
152 | bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, |
153 | const char *path) | |
154 | { | |
155 | memset(sa, 0, sizeof(*sa)); | |
156 | ||
157 | if (!path) | |
158 | path = ZEBRA_SERV_PATH; | |
159 | ||
160 | if (!strncmp(path, ZAPI_TCP_PATHNAME, strlen(ZAPI_TCP_PATHNAME))) { | |
5d13cd09 | 161 | /* note: this functionality is disabled at bottom */ |
689f5a8c DL |
162 | int af; |
163 | int port = ZEBRA_PORT; | |
164 | char *err = NULL; | |
165 | struct sockaddr_in *sin = NULL; | |
166 | struct sockaddr_in6 *sin6 = NULL; | |
167 | ||
168 | path += strlen(ZAPI_TCP_PATHNAME); | |
169 | ||
170 | switch (path[0]) { | |
171 | case '4': | |
172 | path++; | |
173 | af = AF_INET; | |
174 | break; | |
175 | case '6': | |
176 | path++; | |
996c9314 | 177 | /* fallthrough */ |
689f5a8c DL |
178 | default: |
179 | af = AF_INET6; | |
180 | break; | |
181 | } | |
182 | ||
183 | switch (path[0]) { | |
184 | case '\0': | |
185 | break; | |
186 | case ':': | |
187 | path++; | |
188 | port = strtoul(path, &err, 10); | |
189 | if (*err || !*path) | |
190 | return false; | |
191 | break; | |
192 | default: | |
193 | return false; | |
194 | } | |
195 | ||
196 | sa->ss_family = af; | |
197 | switch (af) { | |
198 | case AF_INET: | |
199 | sin = (struct sockaddr_in *)sa; | |
200 | sin->sin_port = htons(port); | |
201 | sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
202 | *sa_len = sizeof(struct sockaddr_in); | |
203 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN | |
204 | sin->sin_len = *sa_len; | |
205 | #endif | |
206 | break; | |
207 | case AF_INET6: | |
208 | sin6 = (struct sockaddr_in6 *)sa; | |
209 | sin6->sin6_port = htons(port); | |
210 | inet_pton(AF_INET6, "::1", &sin6->sin6_addr); | |
211 | *sa_len = sizeof(struct sockaddr_in6); | |
212 | #ifdef SIN6_LEN | |
213 | sin6->sin6_len = *sa_len; | |
214 | #endif | |
215 | break; | |
216 | } | |
5d13cd09 DL |
217 | |
218 | #if 1 | |
219 | /* force-disable this path, because tcp-zebra is a | |
220 | * SECURITY ISSUE. there are no checks at all against | |
221 | * untrusted users on the local system connecting on TCP | |
222 | * and injecting bogus routing data into the entire routing | |
223 | * domain. | |
224 | * | |
225 | * The functionality is only left here because it may be | |
226 | * useful during development, in order to be able to get | |
227 | * tcpdump or wireshark watching ZAPI as TCP. If you want | |
228 | * to do that, flip the #if 1 above to #if 0. */ | |
229 | memset(sa, 0, sizeof(*sa)); | |
230 | return false; | |
231 | #endif | |
689f5a8c DL |
232 | } else { |
233 | /* "sun" is a #define on solaris */ | |
234 | struct sockaddr_un *suna = (struct sockaddr_un *)sa; | |
235 | ||
236 | suna->sun_family = AF_UNIX; | |
237 | strlcpy(suna->sun_path, path, sizeof(suna->sun_path)); | |
238 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN | |
239 | *sa_len = suna->sun_len = SUN_LEN(suna); | |
240 | #else | |
241 | *sa_len = sizeof(suna->sun_family) + strlen(suna->sun_path); | |
242 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ | |
243 | #if 0 | |
244 | /* this is left here for future reference; Linux abstract | |
245 | * socket namespace support can be enabled by replacing | |
246 | * above #if 0 with #ifdef GNU_LINUX. | |
247 | * | |
248 | * THIS IS A SECURITY ISSUE, the abstract socket namespace | |
249 | * does not have user/group permission control on sockets. | |
250 | * we'd need to implement SCM_CREDENTIALS support first to | |
251 | * check that only proper users can connect to abstract | |
252 | * sockets. (same problem as tcp-zebra, except there is a | |
253 | * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.) | |
254 | */ | |
255 | if (suna->sun_path[0] == '@') | |
256 | suna->sun_path[0] = '\0'; | |
257 | #endif | |
258 | } | |
259 | return true; | |
260 | } | |
261 | ||
4f04a76b DL |
262 | static struct frr_daemon_info *di = NULL; |
263 | ||
264 | void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv) | |
265 | { | |
266 | di = daemon; | |
267 | ||
268 | /* basename(), opencoded. */ | |
269 | char *p = strrchr(argv[0], '/'); | |
270 | di->progname = p ? p + 1 : argv[0]; | |
271 | ||
272 | umask(0027); | |
273 | ||
274 | opt_extend(&os_always); | |
eb05883f DL |
275 | if (!(di->flags & FRR_NO_CFG_PID_DRY)) |
276 | opt_extend(&os_cfg_pid_dry); | |
4f04a76b DL |
277 | if (!(di->flags & FRR_NO_PRIVSEP)) |
278 | opt_extend(&os_user); | |
eb05883f DL |
279 | if (!(di->flags & FRR_NO_ZCLIENT)) |
280 | opt_extend(&os_zclient); | |
4f04a76b DL |
281 | if (!(di->flags & FRR_NO_TCPVTY)) |
282 | opt_extend(&os_vty); | |
eb05883f DL |
283 | |
284 | snprintf(config_default, sizeof(config_default), "%s/%s.conf", | |
d62a17ae | 285 | frr_sysconfdir, di->name); |
eb05883f | 286 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", |
d62a17ae | 287 | frr_vtydir, di->name); |
b85120bc DL |
288 | |
289 | strlcpy(frr_protoname, di->logname, sizeof(frr_protoname)); | |
290 | strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst)); | |
689f5a8c DL |
291 | |
292 | strlcpy(frr_zclientpath, ZEBRA_SERV_PATH, sizeof(frr_zclientpath)); | |
4f04a76b DL |
293 | } |
294 | ||
295 | void frr_opt_add(const char *optstr, const struct option *longopts, | |
d62a17ae | 296 | const char *helpstr) |
4f04a76b | 297 | { |
d62a17ae | 298 | const struct optspec main_opts = {optstr, helpstr, longopts}; |
4f04a76b DL |
299 | opt_extend(&main_opts); |
300 | } | |
301 | ||
302 | void frr_help_exit(int status) | |
303 | { | |
304 | FILE *target = status ? stderr : stdout; | |
305 | ||
306 | if (status != 0) | |
307 | fprintf(stderr, "Invalid options.\n\n"); | |
308 | ||
309 | if (di->printhelp) | |
310 | di->printhelp(target); | |
311 | else | |
312 | fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s", | |
d62a17ae | 313 | di->progname, di->proghelp, di->copyright ? "\n\n" : "", |
314 | di->copyright ? di->copyright : "", comb_helpstr); | |
4f04a76b DL |
315 | fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS); |
316 | exit(status); | |
317 | } | |
318 | ||
30771d65 DL |
319 | struct option_chain { |
320 | struct option_chain *next; | |
321 | const char *arg; | |
322 | }; | |
80b4df3b | 323 | |
30771d65 | 324 | static struct option_chain *modules = NULL, **modnext = &modules; |
4f04a76b DL |
325 | static int errors = 0; |
326 | ||
327 | static int frr_opt(int opt) | |
328 | { | |
329 | static int vty_port_set = 0; | |
330 | static int vty_addr_set = 0; | |
30771d65 | 331 | struct option_chain *oc; |
4f04a76b DL |
332 | char *err; |
333 | ||
334 | switch (opt) { | |
335 | case 'h': | |
336 | frr_help_exit(0); | |
337 | break; | |
338 | case 'v': | |
339 | print_version(di->progname); | |
340 | exit(0); | |
341 | break; | |
eb05883f DL |
342 | case 'd': |
343 | di->daemon_mode = 1; | |
344 | break; | |
30771d65 DL |
345 | case 'M': |
346 | oc = XMALLOC(MTYPE_TMP, sizeof(*oc)); | |
347 | oc->arg = optarg; | |
348 | oc->next = NULL; | |
349 | *modnext = oc; | |
350 | modnext = &oc->next; | |
351 | break; | |
eb05883f DL |
352 | case 'i': |
353 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
354 | return 1; | |
355 | di->pid_file = optarg; | |
356 | break; | |
357 | case 'f': | |
358 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
359 | return 1; | |
360 | di->config_file = optarg; | |
361 | break; | |
d1b4fc1f DL |
362 | case 'N': |
363 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
364 | return 1; | |
365 | if (di->pathspace) { | |
366 | fprintf(stderr, | |
367 | "-N/--pathspace option specified more than once!\n"); | |
368 | errors++; | |
369 | break; | |
370 | } | |
371 | if (strchr(optarg, '/') || strchr(optarg, '.')) { | |
372 | fprintf(stderr, | |
373 | "slashes or dots are not permitted in the --pathspace option.\n"); | |
374 | errors++; | |
375 | break; | |
376 | } | |
377 | di->pathspace = optarg; | |
378 | break; | |
eb05883f DL |
379 | case 'C': |
380 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
381 | return 1; | |
382 | di->dryrun = 1; | |
383 | break; | |
cff2b211 DL |
384 | case 't': |
385 | if (di->flags & FRR_NO_CFG_PID_DRY) | |
386 | return 1; | |
387 | di->terminal = 1; | |
388 | break; | |
eb05883f DL |
389 | case 'z': |
390 | if (di->flags & FRR_NO_ZCLIENT) | |
391 | return 1; | |
689f5a8c | 392 | strlcpy(frr_zclientpath, optarg, sizeof(frr_zclientpath)); |
eb05883f | 393 | break; |
4f04a76b DL |
394 | case 'A': |
395 | if (di->flags & FRR_NO_TCPVTY) | |
396 | return 1; | |
397 | if (vty_addr_set) { | |
d62a17ae | 398 | fprintf(stderr, |
399 | "-A option specified more than once!\n"); | |
4f04a76b DL |
400 | errors++; |
401 | break; | |
402 | } | |
403 | vty_addr_set = 1; | |
404 | di->vty_addr = optarg; | |
405 | break; | |
406 | case 'P': | |
407 | if (di->flags & FRR_NO_TCPVTY) | |
408 | return 1; | |
409 | if (vty_port_set) { | |
d62a17ae | 410 | fprintf(stderr, |
411 | "-P option specified more than once!\n"); | |
4f04a76b DL |
412 | errors++; |
413 | break; | |
414 | } | |
415 | vty_port_set = 1; | |
416 | di->vty_port = strtoul(optarg, &err, 0); | |
417 | if (*err || !*optarg) { | |
d62a17ae | 418 | fprintf(stderr, |
419 | "invalid port number \"%s\" for -P option\n", | |
420 | optarg); | |
4f04a76b DL |
421 | errors++; |
422 | break; | |
423 | } | |
424 | break; | |
425 | case OPTION_VTYSOCK: | |
426 | if (di->vty_sock_path) { | |
d62a17ae | 427 | fprintf(stderr, |
428 | "--vty_socket option specified more than once!\n"); | |
4f04a76b DL |
429 | errors++; |
430 | break; | |
431 | } | |
432 | di->vty_sock_path = optarg; | |
433 | break; | |
80b4df3b MW |
434 | case OPTION_MODULEDIR: |
435 | if (di->module_path) { | |
d62a17ae | 436 | fprintf(stderr, |
437 | "----moduledir option specified more than once!\n"); | |
80b4df3b MW |
438 | errors++; |
439 | break; | |
440 | } | |
441 | di->module_path = optarg; | |
442 | break; | |
4f04a76b DL |
443 | case 'u': |
444 | if (di->flags & FRR_NO_PRIVSEP) | |
445 | return 1; | |
446 | di->privs->user = optarg; | |
447 | break; | |
448 | case 'g': | |
449 | if (di->flags & FRR_NO_PRIVSEP) | |
450 | return 1; | |
451 | di->privs->group = optarg; | |
452 | break; | |
f8507817 DS |
453 | case OPTION_LOG: |
454 | di->early_logging = optarg; | |
455 | break; | |
e9b4e74a DS |
456 | case OPTION_LOGLEVEL: |
457 | di->early_loglevel = optarg; | |
458 | break; | |
4f04a76b DL |
459 | default: |
460 | return 1; | |
461 | } | |
462 | return 0; | |
463 | } | |
464 | ||
d62a17ae | 465 | int frr_getopt(int argc, char *const argv[], int *longindex) |
4f04a76b DL |
466 | { |
467 | int opt; | |
468 | int lidx; | |
469 | ||
470 | comb_next_lo->name = NULL; | |
471 | ||
472 | do { | |
473 | opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx); | |
474 | if (frr_opt(opt)) | |
475 | break; | |
476 | } while (opt != -1); | |
477 | ||
478 | if (opt == -1 && errors) | |
479 | frr_help_exit(1); | |
480 | if (longindex) | |
481 | *longindex = lidx; | |
482 | return opt; | |
483 | } | |
484 | ||
beaa5470 DL |
485 | static void frr_mkdir(const char *path, bool strip) |
486 | { | |
487 | char buf[256]; | |
488 | mode_t prev; | |
489 | int ret; | |
490 | struct zprivs_ids_t ids; | |
491 | ||
492 | if (strip) { | |
493 | char *slash = strrchr(path, '/'); | |
494 | size_t plen; | |
495 | if (!slash) | |
496 | return; | |
497 | plen = slash - path; | |
498 | if (plen > sizeof(buf) - 1) | |
499 | return; | |
500 | memcpy(buf, path, plen); | |
501 | buf[plen] = '\0'; | |
502 | path = buf; | |
503 | } | |
504 | ||
505 | /* o+rx (..5) is needed for the frrvty group to work properly; | |
506 | * without it, users in the frrvty group can't access the vty sockets. | |
507 | */ | |
508 | prev = umask(0022); | |
509 | ret = mkdir(path, 0755); | |
510 | umask(prev); | |
511 | ||
512 | if (ret != 0) { | |
513 | /* if EEXIST, return without touching the permissions, | |
514 | * so user-set custom permissions are left in place | |
515 | */ | |
516 | if (errno == EEXIST) | |
517 | return; | |
518 | ||
519 | zlog_warn("failed to mkdir \"%s\": %s", path, strerror(errno)); | |
520 | return; | |
521 | } | |
522 | ||
523 | zprivs_get_ids(&ids); | |
524 | if (chown(path, ids.uid_normal, ids.gid_normal)) | |
525 | zlog_warn("failed to chown \"%s\": %s", path, strerror(errno)); | |
526 | } | |
527 | ||
a5b38c5b | 528 | static struct thread_master *master; |
4f04a76b DL |
529 | struct thread_master *frr_init(void) |
530 | { | |
30771d65 DL |
531 | struct option_chain *oc; |
532 | struct frrmod_runtime *module; | |
533 | char moderr[256]; | |
d1b4fc1f | 534 | char p_instance[16] = "", p_pathspace[256] = ""; |
80b4df3b MW |
535 | const char *dir; |
536 | dir = di->module_path ? di->module_path : frr_moduledir; | |
4f04a76b DL |
537 | |
538 | srandom(time(NULL)); | |
539 | ||
d1b4fc1f | 540 | if (di->instance) { |
d62a17ae | 541 | snprintf(frr_protonameinst, sizeof(frr_protonameinst), "%s[%u]", |
542 | di->logname, di->instance); | |
d1b4fc1f DL |
543 | snprintf(p_instance, sizeof(p_instance), "-%d", di->instance); |
544 | } | |
545 | if (di->pathspace) | |
b39404c1 | 546 | snprintf(p_pathspace, sizeof(p_pathspace), "%s/", |
d1b4fc1f DL |
547 | di->pathspace); |
548 | ||
36077833 | 549 | snprintf(config_default, sizeof(config_default), "%s%s%s%s.conf", |
d1b4fc1f | 550 | frr_sysconfdir, p_pathspace, di->name, p_instance); |
b39404c1 | 551 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s%s%s.pid", |
d1b4fc1f | 552 | frr_vtydir, p_pathspace, di->name, p_instance); |
b85120bc | 553 | |
37a1f2fb DL |
554 | zprivs_preinit(di->privs); |
555 | ||
d62a17ae | 556 | openzlog(di->progname, di->logname, di->instance, |
557 | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); | |
f8507817 | 558 | |
e9b4e74a | 559 | command_setup_early_logging(di->early_logging, di->early_loglevel); |
4f04a76b | 560 | |
689f5a8c DL |
561 | if (!frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
562 | frr_zclientpath)) { | |
563 | fprintf(stderr, "Invalid zserv socket path: %s\n", | |
564 | frr_zclientpath); | |
565 | exit(1); | |
566 | } | |
567 | ||
b8c1fde3 DL |
568 | /* don't mkdir these as root... */ |
569 | if (!(di->flags & FRR_NO_PRIVSEP)) { | |
570 | if (!di->pid_file || !di->vty_path) | |
571 | frr_mkdir(frr_vtydir, false); | |
572 | if (di->pid_file) | |
573 | frr_mkdir(di->pid_file, true); | |
574 | if (di->vty_path) | |
575 | frr_mkdir(di->vty_path, true); | |
576 | } | |
beaa5470 | 577 | |
30771d65 DL |
578 | frrmod_init(di->module); |
579 | while (modules) { | |
580 | modules = (oc = modules)->next; | |
80b4df3b | 581 | module = frrmod_load(oc->arg, dir, moderr, sizeof(moderr)); |
30771d65 DL |
582 | if (!module) { |
583 | fprintf(stderr, "%s\n", moderr); | |
584 | exit(1); | |
585 | } | |
586 | XFREE(MTYPE_TMP, oc); | |
587 | } | |
588 | ||
4f04a76b DL |
589 | zprivs_init(di->privs); |
590 | ||
972a411c | 591 | master = thread_master_create(NULL); |
4f04a76b DL |
592 | signal_init(master, di->n_signals, di->signals); |
593 | ||
857b5446 DL |
594 | if (di->flags & FRR_LIMITED_CLI) |
595 | cmd_init(-1); | |
596 | else | |
597 | cmd_init(1); | |
598 | vty_init(master); | |
599 | memory_init(); | |
600 | ||
4f04a76b DL |
601 | return master; |
602 | } | |
603 | ||
154b9e8f DL |
604 | static int rcvd_signal = 0; |
605 | ||
606 | static void rcv_signal(int signum) | |
607 | { | |
608 | rcvd_signal = signum; | |
609 | /* poll() is interrupted by the signal; handled below */ | |
610 | } | |
611 | ||
f43fbf83 DL |
612 | static void frr_daemon_wait(int fd) |
613 | { | |
614 | struct pollfd pfd[1]; | |
615 | int ret; | |
616 | pid_t exitpid; | |
617 | int exitstat; | |
154b9e8f DL |
618 | sigset_t sigs, prevsigs; |
619 | ||
620 | sigemptyset(&sigs); | |
621 | sigaddset(&sigs, SIGTSTP); | |
622 | sigaddset(&sigs, SIGQUIT); | |
623 | sigaddset(&sigs, SIGINT); | |
624 | sigprocmask(SIG_BLOCK, &sigs, &prevsigs); | |
625 | ||
626 | struct sigaction sa = { | |
627 | .sa_handler = rcv_signal, .sa_flags = SA_RESETHAND, | |
628 | }; | |
629 | sigemptyset(&sa.sa_mask); | |
630 | sigaction(SIGTSTP, &sa, NULL); | |
631 | sigaction(SIGQUIT, &sa, NULL); | |
632 | sigaction(SIGINT, &sa, NULL); | |
f43fbf83 DL |
633 | |
634 | do { | |
154b9e8f DL |
635 | char buf[1]; |
636 | ssize_t nrecv; | |
637 | ||
f43fbf83 DL |
638 | pfd[0].fd = fd; |
639 | pfd[0].events = POLLIN; | |
640 | ||
154b9e8f DL |
641 | rcvd_signal = 0; |
642 | ||
996c9314 | 643 | #if defined(HAVE_PPOLL) |
154b9e8f DL |
644 | ret = ppoll(pfd, 1, NULL, &prevsigs); |
645 | #elif defined(HAVE_POLLTS) | |
646 | ret = pollts(pfd, 1, NULL, &prevsigs); | |
647 | #else | |
648 | /* racy -- only used on FreeBSD 9 */ | |
649 | sigset_t tmpsigs; | |
650 | sigprocmask(SIG_SETMASK, &prevsigs, &tmpsigs); | |
f43fbf83 | 651 | ret = poll(pfd, 1, -1); |
154b9e8f DL |
652 | sigprocmask(SIG_SETMASK, &tmpsigs, NULL); |
653 | #endif | |
f43fbf83 DL |
654 | if (ret < 0 && errno != EINTR && errno != EAGAIN) { |
655 | perror("poll()"); | |
656 | exit(1); | |
657 | } | |
154b9e8f DL |
658 | switch (rcvd_signal) { |
659 | case SIGTSTP: | |
660 | send(fd, "S", 1, 0); | |
661 | do { | |
662 | nrecv = recv(fd, buf, sizeof(buf), 0); | |
663 | } while (nrecv == -1 | |
664 | && (errno == EINTR || errno == EAGAIN)); | |
665 | ||
666 | raise(SIGTSTP); | |
667 | sigaction(SIGTSTP, &sa, NULL); | |
668 | send(fd, "R", 1, 0); | |
669 | break; | |
670 | case SIGINT: | |
671 | send(fd, "I", 1, 0); | |
672 | break; | |
673 | case SIGQUIT: | |
674 | send(fd, "Q", 1, 0); | |
675 | break; | |
676 | } | |
f43fbf83 DL |
677 | } while (ret <= 0); |
678 | ||
679 | exitpid = waitpid(-1, &exitstat, WNOHANG); | |
680 | if (exitpid == 0) | |
681 | /* child successfully went to main loop & closed socket */ | |
682 | exit(0); | |
683 | ||
684 | /* child failed one way or another ... */ | |
6bd2b360 DL |
685 | if (WIFEXITED(exitstat) && WEXITSTATUS(exitstat) == 0) |
686 | /* can happen in --terminal case if exit is fast enough */ | |
687 | (void)0; | |
688 | else if (WIFEXITED(exitstat)) | |
f43fbf83 DL |
689 | fprintf(stderr, "%s failed to start, exited %d\n", di->name, |
690 | WEXITSTATUS(exitstat)); | |
691 | else if (WIFSIGNALED(exitstat)) | |
692 | fprintf(stderr, "%s crashed in startup, signal %d\n", di->name, | |
693 | WTERMSIG(exitstat)); | |
694 | else | |
695 | fprintf(stderr, "%s failed to start, unknown problem\n", | |
696 | di->name); | |
697 | exit(1); | |
698 | } | |
699 | ||
700 | static int daemon_ctl_sock = -1; | |
701 | ||
702 | static void frr_daemonize(void) | |
703 | { | |
704 | int fds[2]; | |
705 | pid_t pid; | |
706 | ||
707 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) { | |
708 | perror("socketpair() for daemon control"); | |
709 | exit(1); | |
710 | } | |
711 | set_cloexec(fds[0]); | |
712 | set_cloexec(fds[1]); | |
713 | ||
714 | pid = fork(); | |
715 | if (pid < 0) { | |
716 | perror("fork()"); | |
717 | exit(1); | |
718 | } | |
719 | if (pid == 0) { | |
720 | /* child */ | |
721 | close(fds[0]); | |
722 | if (setsid() < 0) { | |
723 | perror("setsid()"); | |
724 | exit(1); | |
725 | } | |
726 | ||
727 | daemon_ctl_sock = fds[1]; | |
728 | return; | |
729 | } | |
730 | ||
731 | close(fds[1]); | |
732 | frr_daemon_wait(fds[0]); | |
733 | } | |
734 | ||
9e224e60 DS |
735 | /* |
736 | * Why is this a thread? | |
737 | * | |
738 | * The read in of config for integrated config happens *after* | |
739 | * thread execution starts( because it is passed in via a vtysh -b -n ) | |
740 | * While if you are not using integrated config we want the ability | |
741 | * to read the config in after thread execution starts, so that | |
742 | * we can match this behavior. | |
743 | */ | |
744 | static int frr_config_read_in(struct thread *t) | |
4f04a76b | 745 | { |
573de11f DS |
746 | if (!vty_read_config(di->config_file, config_default) && |
747 | di->backup_config_file) { | |
fe64533a DS |
748 | char *orig = XSTRDUP(MTYPE_TMP, host_config_get()); |
749 | ||
573de11f DS |
750 | zlog_info("Attempting to read backup config file: %s specified", |
751 | di->backup_config_file); | |
752 | vty_read_config(di->backup_config_file, config_default); | |
fe64533a DS |
753 | |
754 | host_config_set(orig); | |
755 | XFREE(MTYPE_TMP, orig); | |
573de11f | 756 | } |
9e224e60 DS |
757 | return 0; |
758 | } | |
759 | ||
760 | void frr_config_fork(void) | |
761 | { | |
762 | hook_call(frr_late_init, master); | |
eb05883f DL |
763 | |
764 | /* Don't start execution if we are in dry-run mode */ | |
9e224e60 DS |
765 | if (di->dryrun) { |
766 | frr_config_read_in(NULL); | |
eb05883f | 767 | exit(0); |
9e224e60 DS |
768 | } |
769 | ||
770 | thread_add_event(master, frr_config_read_in, NULL, 0, &di->read_in); | |
eb05883f | 771 | |
154b9e8f | 772 | if (di->daemon_mode || di->terminal) |
f43fbf83 | 773 | frr_daemonize(); |
eb05883f DL |
774 | |
775 | if (!di->pid_file) | |
776 | di->pid_file = pidfile_default; | |
d62a17ae | 777 | pid_output(di->pid_file); |
eb05883f DL |
778 | } |
779 | ||
780 | void frr_vty_serv(void) | |
781 | { | |
d62a17ae | 782 | /* allow explicit override of vty_path in the future |
eb05883f DL |
783 | * (not currently set anywhere) */ |
784 | if (!di->vty_path) { | |
785 | const char *dir; | |
d1b4fc1f DL |
786 | char defvtydir[256]; |
787 | ||
788 | snprintf(defvtydir, sizeof(defvtydir), "%s%s%s", frr_vtydir, | |
789 | di->pathspace ? "/" : "", | |
790 | di->pathspace ? di->pathspace : ""); | |
791 | ||
792 | dir = di->vty_sock_path ? di->vty_sock_path : defvtydir; | |
eb05883f DL |
793 | |
794 | if (di->instance) | |
795 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 796 | "%s/%s-%d.vty", dir, di->name, di->instance); |
eb05883f DL |
797 | else |
798 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 799 | "%s/%s.vty", dir, di->name); |
eb05883f DL |
800 | |
801 | di->vty_path = vtypath_default; | |
802 | } | |
803 | ||
804 | vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); | |
4f04a76b DL |
805 | } |
806 | ||
154b9e8f | 807 | static void frr_terminal_close(int isexit) |
cff2b211 | 808 | { |
993bab89 RW |
809 | int nullfd; |
810 | ||
154b9e8f DL |
811 | if (daemon_ctl_sock != -1) { |
812 | close(daemon_ctl_sock); | |
813 | daemon_ctl_sock = -1; | |
814 | } | |
815 | ||
816 | if (!di->daemon_mode || isexit) { | |
cff2b211 | 817 | printf("\n%s exiting\n", di->name); |
154b9e8f DL |
818 | if (!isexit) |
819 | raise(SIGINT); | |
820 | return; | |
cff2b211 DL |
821 | } else { |
822 | printf("\n%s daemonizing\n", di->name); | |
823 | fflush(stdout); | |
824 | } | |
825 | ||
993bab89 RW |
826 | nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
827 | if (nullfd == -1) { | |
828 | zlog_err("%s: failed to open /dev/null: %s", __func__, | |
829 | safe_strerror(errno)); | |
830 | } else { | |
831 | dup2(nullfd, 0); | |
832 | dup2(nullfd, 1); | |
833 | dup2(nullfd, 2); | |
834 | close(nullfd); | |
835 | } | |
154b9e8f | 836 | } |
cff2b211 | 837 | |
154b9e8f DL |
838 | static struct thread *daemon_ctl_thread = NULL; |
839 | ||
840 | static int frr_daemon_ctl(struct thread *t) | |
841 | { | |
842 | char buf[1]; | |
843 | ssize_t nr; | |
844 | ||
845 | nr = recv(daemon_ctl_sock, buf, sizeof(buf), 0); | |
846 | if (nr < 0 && (errno == EINTR || errno == EAGAIN)) | |
847 | goto out; | |
848 | if (nr <= 0) | |
849 | return 0; | |
850 | ||
851 | switch (buf[0]) { | |
996c9314 | 852 | case 'S': /* SIGTSTP */ |
154b9e8f | 853 | vty_stdio_suspend(); |
e339d7c0 | 854 | if (send(daemon_ctl_sock, "s", 1, 0) < 0) |
855 | zlog_err("%s send(\"s\") error (SIGTSTP propagation)", | |
856 | (di && di->name ? di->name : "")); | |
154b9e8f | 857 | break; |
996c9314 | 858 | case 'R': /* SIGTCNT [implicit] */ |
154b9e8f DL |
859 | vty_stdio_resume(); |
860 | break; | |
996c9314 | 861 | case 'I': /* SIGINT */ |
154b9e8f DL |
862 | di->daemon_mode = false; |
863 | raise(SIGINT); | |
864 | break; | |
996c9314 | 865 | case 'Q': /* SIGQUIT */ |
154b9e8f DL |
866 | di->daemon_mode = true; |
867 | vty_stdio_close(); | |
868 | break; | |
cff2b211 | 869 | } |
154b9e8f DL |
870 | |
871 | out: | |
872 | thread_add_read(master, frr_daemon_ctl, NULL, daemon_ctl_sock, | |
873 | &daemon_ctl_thread); | |
874 | return 0; | |
cff2b211 DL |
875 | } |
876 | ||
16077f2f DL |
877 | void frr_run(struct thread_master *master) |
878 | { | |
879 | char instanceinfo[64] = ""; | |
880 | ||
881 | frr_vty_serv(); | |
882 | ||
883 | if (di->instance) | |
884 | snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", | |
d62a17ae | 885 | di->instance); |
886 | ||
887 | zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION, | |
888 | instanceinfo, di->vty_port, di->startinfo); | |
16077f2f | 889 | |
cff2b211 DL |
890 | if (di->terminal) { |
891 | vty_stdio(frr_terminal_close); | |
154b9e8f DL |
892 | if (daemon_ctl_sock != -1) { |
893 | set_nonblocking(daemon_ctl_sock); | |
894 | thread_add_read(master, frr_daemon_ctl, NULL, | |
895 | daemon_ctl_sock, &daemon_ctl_thread); | |
896 | } | |
eef3d030 | 897 | } else if (di->daemon_mode) { |
c9c8d0d1 | 898 | int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
993bab89 RW |
899 | if (nullfd == -1) { |
900 | zlog_err("%s: failed to open /dev/null: %s", __func__, | |
901 | safe_strerror(errno)); | |
902 | } else { | |
903 | dup2(nullfd, 0); | |
904 | dup2(nullfd, 1); | |
905 | dup2(nullfd, 2); | |
906 | close(nullfd); | |
907 | } | |
c9c8d0d1 DL |
908 | |
909 | if (daemon_ctl_sock != -1) | |
910 | close(daemon_ctl_sock); | |
f43fbf83 DL |
911 | daemon_ctl_sock = -1; |
912 | } | |
913 | ||
d34cb7f0 DL |
914 | /* end fixed stderr startup logging */ |
915 | zlog_startup_stderr = false; | |
916 | ||
16077f2f DL |
917 | struct thread thread; |
918 | while (thread_fetch(master, &thread)) | |
919 | thread_call(&thread); | |
920 | } | |
03951374 DL |
921 | |
922 | void frr_early_fini(void) | |
923 | { | |
924 | hook_call(frr_early_fini); | |
925 | } | |
926 | ||
927 | void frr_fini(void) | |
928 | { | |
9eed278b DL |
929 | FILE *fp; |
930 | char filename[128]; | |
931 | int have_leftovers; | |
932 | ||
03951374 DL |
933 | hook_call(frr_fini); |
934 | ||
935 | /* memory_init -> nothing needed */ | |
936 | vty_terminate(); | |
937 | cmd_terminate(); | |
938 | zprivs_terminate(di->privs); | |
939 | /* signal_init -> nothing needed */ | |
940 | thread_master_free(master); | |
e5716b16 | 941 | master = NULL; |
03951374 DL |
942 | closezlog(); |
943 | /* frrmod_init -> nothing needed / hooks */ | |
9eed278b DL |
944 | |
945 | if (!debug_memstats_at_exit) | |
946 | return; | |
947 | ||
948 | have_leftovers = log_memstats(stderr, di->name); | |
949 | ||
950 | /* in case we decide at runtime that we want exit-memstats for | |
951 | * a daemon, but it has no stderr because it's daemonized | |
952 | * (only do this if we actually have something to print though) | |
953 | */ | |
954 | if (!have_leftovers) | |
955 | return; | |
956 | ||
996c9314 LB |
957 | snprintf(filename, sizeof(filename), "/tmp/frr-memstats-%s-%llu-%llu", |
958 | di->name, (unsigned long long)getpid(), | |
9eed278b DL |
959 | (unsigned long long)time(NULL)); |
960 | ||
961 | fp = fopen(filename, "w"); | |
962 | if (fp) { | |
963 | log_memstats(fp, di->name); | |
964 | fclose(fp); | |
965 | } | |
03951374 | 966 | } |