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