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