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