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