]> git.proxmox.com Git - mirror_frr.git/blame - lib/libfrr.c
*: require semicolon after DEFINE_MTYPE & co
[mirror_frr.git] / lib / libfrr.c
CommitLineData
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"
1c0d8808 33#include "lib_vty.h"
f73126c3 34#include "log_vty.h"
eb05883f 35#include "zclient.h"
30771d65 36#include "module.h"
f43fbf83 37#include "network.h"
b66d022e 38#include "lib_errors.h"
1c2facd1
RW
39#include "db.h"
40#include "northbound_cli.h"
1ae9686c 41#include "northbound_db.h"
ae0994f6 42#include "debug.h"
3e41733f 43#include "frrcu.h"
b9b4c061 44#include "frr_pthread.h"
ac4adef4 45#include "defaults.h"
e613a6f7 46#include "frrscript.h"
eb05883f 47
d62a17ae 48DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm))
88e635ee 49DEFINE_HOOK(frr_very_late_init, (struct thread_master * tm), (tm))
03951374
DL
50DEFINE_KOOH(frr_early_fini, (), ())
51DEFINE_KOOH(frr_fini, (), ())
a5b38c5b 52
eb05883f 53const char frr_sysconfdir[] = SYSCONFDIR;
43e587c1 54char frr_vtydir[256];
1c2facd1
RW
55#ifdef HAVE_SQLITE3
56const char frr_dbdir[] = DAEMON_DB_DIR;
57#endif
80b4df3b 58const char frr_moduledir[] = MODULE_PATH;
e4e0229a 59const char frr_scriptdir[] = SCRIPT_PATH;
eb05883f 60
4f138a3e
DL
61char frr_protoname[256] = "NONE";
62char frr_protonameinst[256] = "NONE";
b85120bc 63
ff44f570 64char config_default[512];
689f5a8c 65char frr_zclientpath[256];
43e587c1 66static char pidfile_default[1024];
1c2facd1
RW
67#ifdef HAVE_SQLITE3
68static char dbfile_default[512];
69#endif
918537e2 70static char vtypath_default[512];
4f04a76b 71
d8729f8c 72bool debug_memstats_at_exit = false;
0a7c7856 73static bool nodetach_term, nodetach_daemon;
6e3253b9 74static uint64_t startup_fds;
9eed278b 75
4f04a76b
DL
76static char comb_optstr[256];
77static struct option comb_lo[64];
78static struct option *comb_next_lo = &comb_lo[0];
79static char comb_helpstr[4096];
80
81struct optspec {
82 const char *optstr;
83 const char *helpstr;
84 const struct option *longopts;
85};
86
87static void opt_extend(const struct optspec *os)
88{
89 const struct option *lo;
90
9f73d2c9 91 strlcat(comb_optstr, os->optstr, sizeof(comb_optstr));
67c726a1 92 strlcat(comb_helpstr, os->helpstr, sizeof(comb_helpstr));
4f04a76b
DL
93 for (lo = os->longopts; lo->name; lo++)
94 memcpy(comb_next_lo++, lo, sizeof(*lo));
95}
96
97
80b4df3b
MW
98#define OPTION_VTYSOCK 1000
99#define OPTION_MODULEDIR 1002
e9b4e74a
DS
100#define OPTION_LOG 1003
101#define OPTION_LOGLEVEL 1004
1c2facd1
RW
102#define OPTION_TCLI 1005
103#define OPTION_DB_FILE 1006
2950f5da 104#define OPTION_LOGGING 1007
1a9f340b 105#define OPTION_LIMIT_FDS 1008
e4e0229a 106#define OPTION_SCRIPTDIR 1009
4f04a76b
DL
107
108static const struct option lo_always[] = {
d62a17ae 109 {"help", no_argument, NULL, 'h'},
110 {"version", no_argument, NULL, 'v'},
111 {"daemon", no_argument, NULL, 'd'},
112 {"module", no_argument, NULL, 'M'},
ac4adef4 113 {"profile", required_argument, NULL, 'F'},
33606a15 114 {"pathspace", required_argument, NULL, 'N'},
d62a17ae 115 {"vty_socket", required_argument, NULL, OPTION_VTYSOCK},
116 {"moduledir", required_argument, NULL, OPTION_MODULEDIR},
e4e0229a 117 {"scriptdir", required_argument, NULL, OPTION_SCRIPTDIR},
f8507817 118 {"log", required_argument, NULL, OPTION_LOG},
e9b4e74a 119 {"log-level", required_argument, NULL, OPTION_LOGLEVEL},
1c2facd1 120 {"tcli", no_argument, NULL, OPTION_TCLI},
2950f5da 121 {"command-log-always", no_argument, NULL, OPTION_LOGGING},
1a9f340b 122 {"limit-fds", required_argument, NULL, OPTION_LIMIT_FDS},
d62a17ae 123 {NULL}};
4f04a76b 124static const struct optspec os_always = {
33606a15 125 "hvdM:F:N:",
4f04a76b
DL
126 " -h, --help Display this help and exit\n"
127 " -v, --version Print program version\n"
eb05883f 128 " -d, --daemon Runs in daemon mode\n"
30771d65 129 " -M, --module Load specified module\n"
ac4adef4 130 " -F, --profile Use specified configuration profile\n"
33606a15 131 " -N, --pathspace Insert prefix into config & socket paths\n"
80b4df3b 132 " --vty_socket Override vty socket path\n"
f8507817 133 " --moduledir Override modules directory\n"
e4e0229a 134 " --scriptdir Override scripts directory\n"
e9b4e74a 135 " --log Set Logging to stdout, syslog, or file:<name>\n"
1c2facd1 136 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
1a9f340b
MS
137 " --tcli Use transaction-based CLI\n"
138 " --limit-fds Limit number of fds supported\n",
d62a17ae 139 lo_always};
4f04a76b
DL
140
141
eb05883f 142static const struct option lo_cfg_pid_dry[] = {
d62a17ae 143 {"pid_file", required_argument, NULL, 'i'},
144 {"config_file", required_argument, NULL, 'f'},
1c2facd1
RW
145#ifdef HAVE_SQLITE3
146 {"db_file", required_argument, NULL, OPTION_DB_FILE},
147#endif
d62a17ae 148 {"dryrun", no_argument, NULL, 'C'},
cff2b211 149 {"terminal", no_argument, NULL, 't'},
d62a17ae 150 {NULL}};
eb05883f 151static const struct optspec os_cfg_pid_dry = {
33606a15 152 "f:i:Ct",
eb05883f
DL
153 " -f, --config_file Set configuration file name\n"
154 " -i, --pid_file Set process identifier file name\n"
1c2facd1
RW
155#ifdef HAVE_SQLITE3
156 " --db_file Set database file name\n"
157#endif
cff2b211
DL
158 " -C, --dryrun Check configuration for validity and exit\n"
159 " -t, --terminal Open terminal session on stdio\n"
160 " -d -t Daemonize after terminal session ends\n",
d62a17ae 161 lo_cfg_pid_dry};
eb05883f
DL
162
163
164static const struct option lo_zclient[] = {
d62a17ae 165 {"socket", required_argument, NULL, 'z'},
166 {NULL}};
eb05883f 167static const struct optspec os_zclient = {
d62a17ae 168 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient};
eb05883f
DL
169
170
4f04a76b 171static const struct option lo_vty[] = {
d62a17ae 172 {"vty_addr", required_argument, NULL, 'A'},
173 {"vty_port", required_argument, NULL, 'P'},
174 {NULL}};
4f04a76b
DL
175static const struct optspec os_vty = {
176 "A:P:",
177 " -A, --vty_addr Set vty's bind address\n"
178 " -P, --vty_port Set vty's port number\n",
d62a17ae 179 lo_vty};
4f04a76b
DL
180
181
d62a17ae 182static const struct option lo_user[] = {{"user", required_argument, NULL, 'u'},
183 {"group", required_argument, NULL, 'g'},
184 {NULL}};
185static const struct optspec os_user = {"u:g:",
186 " -u, --user User to run as\n"
187 " -g, --group Group to run as\n",
188 lo_user};
4f04a76b 189
689f5a8c
DL
190bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len,
191 const char *path)
192{
193 memset(sa, 0, sizeof(*sa));
194
195 if (!path)
4e99f309 196 path = frr_zclientpath;
689f5a8c
DL
197
198 if (!strncmp(path, ZAPI_TCP_PATHNAME, strlen(ZAPI_TCP_PATHNAME))) {
5d13cd09 199 /* note: this functionality is disabled at bottom */
689f5a8c
DL
200 int af;
201 int port = ZEBRA_PORT;
202 char *err = NULL;
203 struct sockaddr_in *sin = NULL;
204 struct sockaddr_in6 *sin6 = NULL;
205
206 path += strlen(ZAPI_TCP_PATHNAME);
207
208 switch (path[0]) {
209 case '4':
210 path++;
211 af = AF_INET;
212 break;
213 case '6':
214 path++;
996c9314 215 /* fallthrough */
689f5a8c
DL
216 default:
217 af = AF_INET6;
218 break;
219 }
220
221 switch (path[0]) {
222 case '\0':
223 break;
224 case ':':
225 path++;
226 port = strtoul(path, &err, 10);
227 if (*err || !*path)
228 return false;
229 break;
230 default:
231 return false;
232 }
233
234 sa->ss_family = af;
235 switch (af) {
236 case AF_INET:
237 sin = (struct sockaddr_in *)sa;
238 sin->sin_port = htons(port);
239 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
240 *sa_len = sizeof(struct sockaddr_in);
241#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
242 sin->sin_len = *sa_len;
243#endif
244 break;
245 case AF_INET6:
246 sin6 = (struct sockaddr_in6 *)sa;
247 sin6->sin6_port = htons(port);
248 inet_pton(AF_INET6, "::1", &sin6->sin6_addr);
249 *sa_len = sizeof(struct sockaddr_in6);
250#ifdef SIN6_LEN
251 sin6->sin6_len = *sa_len;
252#endif
253 break;
254 }
5d13cd09
DL
255
256#if 1
257 /* force-disable this path, because tcp-zebra is a
258 * SECURITY ISSUE. there are no checks at all against
259 * untrusted users on the local system connecting on TCP
260 * and injecting bogus routing data into the entire routing
261 * domain.
262 *
263 * The functionality is only left here because it may be
264 * useful during development, in order to be able to get
265 * tcpdump or wireshark watching ZAPI as TCP. If you want
266 * to do that, flip the #if 1 above to #if 0. */
267 memset(sa, 0, sizeof(*sa));
268 return false;
269#endif
689f5a8c
DL
270 } else {
271 /* "sun" is a #define on solaris */
272 struct sockaddr_un *suna = (struct sockaddr_un *)sa;
273
274 suna->sun_family = AF_UNIX;
275 strlcpy(suna->sun_path, path, sizeof(suna->sun_path));
276#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
277 *sa_len = suna->sun_len = SUN_LEN(suna);
278#else
279 *sa_len = sizeof(suna->sun_family) + strlen(suna->sun_path);
280#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
281#if 0
282 /* this is left here for future reference; Linux abstract
283 * socket namespace support can be enabled by replacing
284 * above #if 0 with #ifdef GNU_LINUX.
285 *
286 * THIS IS A SECURITY ISSUE, the abstract socket namespace
287 * does not have user/group permission control on sockets.
288 * we'd need to implement SCM_CREDENTIALS support first to
289 * check that only proper users can connect to abstract
290 * sockets. (same problem as tcp-zebra, except there is a
291 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
292 */
293 if (suna->sun_path[0] == '@')
294 suna->sun_path[0] = '\0';
295#endif
296 }
297 return true;
298}
299
4f04a76b
DL
300static struct frr_daemon_info *di = NULL;
301
43e587c1
DS
302void frr_init_vtydir(void)
303{
304 snprintf(frr_vtydir, sizeof(frr_vtydir), DAEMON_VTY_DIR, "", "");
305}
306
4f04a76b
DL
307void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv)
308{
309 di = daemon;
310
311 /* basename(), opencoded. */
312 char *p = strrchr(argv[0], '/');
313 di->progname = p ? p + 1 : argv[0];
314
315 umask(0027);
316
317 opt_extend(&os_always);
eb05883f
DL
318 if (!(di->flags & FRR_NO_CFG_PID_DRY))
319 opt_extend(&os_cfg_pid_dry);
4f04a76b
DL
320 if (!(di->flags & FRR_NO_PRIVSEP))
321 opt_extend(&os_user);
eb05883f
DL
322 if (!(di->flags & FRR_NO_ZCLIENT))
323 opt_extend(&os_zclient);
4f04a76b
DL
324 if (!(di->flags & FRR_NO_TCPVTY))
325 opt_extend(&os_vty);
0a7c7856
DL
326 if (di->flags & FRR_DETACH_LATER)
327 nodetach_daemon = true;
eb05883f 328
43e587c1 329 frr_init_vtydir();
eb05883f 330 snprintf(config_default, sizeof(config_default), "%s/%s.conf",
d62a17ae 331 frr_sysconfdir, di->name);
eb05883f 332 snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid",
d62a17ae 333 frr_vtydir, di->name);
43e587c1
DS
334 snprintf(frr_zclientpath, sizeof(frr_zclientpath),
335 ZEBRA_SERV_PATH, "", "");
1c2facd1
RW
336#ifdef HAVE_SQLITE3
337 snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s.db",
338 frr_dbdir, di->name);
339#endif
b85120bc
DL
340
341 strlcpy(frr_protoname, di->logname, sizeof(frr_protoname));
342 strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst));
689f5a8c 343
1c2facd1 344 di->cli_mode = FRR_CLI_CLASSIC;
6e3253b9
DL
345
346 /* we may be starting with extra FDs open for whatever purpose,
347 * e.g. logging, some module, etc. Recording them here allows later
348 * checking whether an fd is valid for such extension purposes,
349 * without this we could end up e.g. logging to a BGP session fd.
350 */
351 startup_fds = 0;
352 for (int i = 0; i < 64; i++) {
353 struct stat st;
354
355 if (fstat(i, &st))
356 continue;
357 if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode))
358 continue;
359
360 startup_fds |= UINT64_C(0x1) << (uint64_t)i;
361 }
362}
363
364bool frr_is_startup_fd(int fd)
365{
366 return !!(startup_fds & (UINT64_C(0x1) << (uint64_t)fd));
4f04a76b
DL
367}
368
369void frr_opt_add(const char *optstr, const struct option *longopts,
d62a17ae 370 const char *helpstr)
4f04a76b 371{
d62a17ae 372 const struct optspec main_opts = {optstr, helpstr, longopts};
4f04a76b
DL
373 opt_extend(&main_opts);
374}
375
376void frr_help_exit(int status)
377{
378 FILE *target = status ? stderr : stdout;
379
380 if (status != 0)
381 fprintf(stderr, "Invalid options.\n\n");
382
383 if (di->printhelp)
384 di->printhelp(target);
385 else
386 fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
d62a17ae 387 di->progname, di->proghelp, di->copyright ? "\n\n" : "",
388 di->copyright ? di->copyright : "", comb_helpstr);
4f04a76b
DL
389 fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS);
390 exit(status);
391}
392
30771d65
DL
393struct option_chain {
394 struct option_chain *next;
395 const char *arg;
396};
80b4df3b 397
30771d65 398static struct option_chain *modules = NULL, **modnext = &modules;
4f04a76b
DL
399static int errors = 0;
400
401static int frr_opt(int opt)
402{
403 static int vty_port_set = 0;
404 static int vty_addr_set = 0;
30771d65 405 struct option_chain *oc;
4f04a76b
DL
406 char *err;
407
408 switch (opt) {
409 case 'h':
410 frr_help_exit(0);
411 break;
412 case 'v':
413 print_version(di->progname);
414 exit(0);
415 break;
eb05883f 416 case 'd':
08c2d52a 417 di->daemon_mode = true;
eb05883f 418 break;
30771d65
DL
419 case 'M':
420 oc = XMALLOC(MTYPE_TMP, sizeof(*oc));
421 oc->arg = optarg;
422 oc->next = NULL;
423 *modnext = oc;
424 modnext = &oc->next;
425 break;
ac4adef4
DL
426 case 'F':
427 if (!frr_defaults_profile_valid(optarg)) {
428 const char **p;
429 FILE *ofd = stderr;
430
431 if (!strcmp(optarg, "help"))
432 ofd = stdout;
433 else
434 fprintf(stderr,
435 "The \"%s\" configuration profile is not valid for this FRR version.\n",
436 optarg);
437
438 fprintf(ofd, "Available profiles are:\n");
439 for (p = frr_defaults_profiles; *p; p++)
440 fprintf(ofd, "%s%s\n",
441 strcmp(*p, DFLT_NAME) ? " " : " * ",
442 *p);
443
444 if (ofd == stdout)
445 exit(0);
446 fprintf(ofd, "\n");
447 errors++;
448 break;
449 }
450 frr_defaults_profile_set(optarg);
451 break;
eb05883f
DL
452 case 'i':
453 if (di->flags & FRR_NO_CFG_PID_DRY)
454 return 1;
455 di->pid_file = optarg;
456 break;
457 case 'f':
458 if (di->flags & FRR_NO_CFG_PID_DRY)
459 return 1;
460 di->config_file = optarg;
461 break;
d1b4fc1f 462 case 'N':
d1b4fc1f
DL
463 if (di->pathspace) {
464 fprintf(stderr,
465 "-N/--pathspace option specified more than once!\n");
466 errors++;
467 break;
468 }
43e587c1
DS
469 if (di->zpathspace)
470 fprintf(stderr,
f79f7a7b 471 "-N option overridden by -z for zebra named socket path\n");
43e587c1 472
d1b4fc1f
DL
473 if (strchr(optarg, '/') || strchr(optarg, '.')) {
474 fprintf(stderr,
475 "slashes or dots are not permitted in the --pathspace option.\n");
476 errors++;
477 break;
478 }
479 di->pathspace = optarg;
4e99f309 480
43e587c1
DS
481 if (!di->zpathspace)
482 snprintf(frr_zclientpath, sizeof(frr_zclientpath),
483 ZEBRA_SERV_PATH, "/", di->pathspace);
484 snprintf(frr_vtydir, sizeof(frr_vtydir), DAEMON_VTY_DIR, "/",
485 di->pathspace);
486 snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid",
487 frr_vtydir, di->name);
d1b4fc1f 488 break;
1c2facd1
RW
489#ifdef HAVE_SQLITE3
490 case OPTION_DB_FILE:
491 if (di->flags & FRR_NO_CFG_PID_DRY)
492 return 1;
493 di->db_file = optarg;
494 break;
495#endif
eb05883f
DL
496 case 'C':
497 if (di->flags & FRR_NO_CFG_PID_DRY)
498 return 1;
08c2d52a 499 di->dryrun = true;
eb05883f 500 break;
cff2b211
DL
501 case 't':
502 if (di->flags & FRR_NO_CFG_PID_DRY)
503 return 1;
08c2d52a 504 di->terminal = true;
cff2b211 505 break;
eb05883f 506 case 'z':
43e587c1
DS
507 di->zpathspace = true;
508 if (di->pathspace)
509 fprintf(stderr,
510 "-z option overrides -N option for zebra named socket path\n");
eb05883f
DL
511 if (di->flags & FRR_NO_ZCLIENT)
512 return 1;
689f5a8c 513 strlcpy(frr_zclientpath, optarg, sizeof(frr_zclientpath));
eb05883f 514 break;
4f04a76b
DL
515 case 'A':
516 if (di->flags & FRR_NO_TCPVTY)
517 return 1;
518 if (vty_addr_set) {
d62a17ae 519 fprintf(stderr,
520 "-A option specified more than once!\n");
4f04a76b
DL
521 errors++;
522 break;
523 }
524 vty_addr_set = 1;
525 di->vty_addr = optarg;
526 break;
527 case 'P':
528 if (di->flags & FRR_NO_TCPVTY)
529 return 1;
530 if (vty_port_set) {
d62a17ae 531 fprintf(stderr,
532 "-P option specified more than once!\n");
4f04a76b
DL
533 errors++;
534 break;
535 }
536 vty_port_set = 1;
537 di->vty_port = strtoul(optarg, &err, 0);
538 if (*err || !*optarg) {
d62a17ae 539 fprintf(stderr,
540 "invalid port number \"%s\" for -P option\n",
541 optarg);
4f04a76b
DL
542 errors++;
543 break;
544 }
545 break;
546 case OPTION_VTYSOCK:
547 if (di->vty_sock_path) {
d62a17ae 548 fprintf(stderr,
549 "--vty_socket option specified more than once!\n");
4f04a76b
DL
550 errors++;
551 break;
552 }
553 di->vty_sock_path = optarg;
554 break;
80b4df3b
MW
555 case OPTION_MODULEDIR:
556 if (di->module_path) {
d62a17ae 557 fprintf(stderr,
558 "----moduledir option specified more than once!\n");
80b4df3b
MW
559 errors++;
560 break;
561 }
562 di->module_path = optarg;
563 break;
e4e0229a
QY
564 case OPTION_SCRIPTDIR:
565 if (di->script_path) {
566 fprintf(stderr, "--scriptdir option specified more than once!\n");
567 errors++;
568 break;
569 }
570 di->script_path = optarg;
571 break;
1c2facd1
RW
572 case OPTION_TCLI:
573 di->cli_mode = FRR_CLI_TRANSACTIONAL;
574 break;
4f04a76b
DL
575 case 'u':
576 if (di->flags & FRR_NO_PRIVSEP)
577 return 1;
578 di->privs->user = optarg;
579 break;
580 case 'g':
581 if (di->flags & FRR_NO_PRIVSEP)
582 return 1;
583 di->privs->group = optarg;
584 break;
f8507817
DS
585 case OPTION_LOG:
586 di->early_logging = optarg;
587 break;
e9b4e74a
DS
588 case OPTION_LOGLEVEL:
589 di->early_loglevel = optarg;
590 break;
2950f5da
DS
591 case OPTION_LOGGING:
592 di->log_always = true;
593 break;
1a9f340b
MS
594 case OPTION_LIMIT_FDS:
595 di->limit_fds = strtoul(optarg, &err, 0);
596 break;
4f04a76b
DL
597 default:
598 return 1;
599 }
600 return 0;
601}
602
d62a17ae 603int frr_getopt(int argc, char *const argv[], int *longindex)
4f04a76b
DL
604{
605 int opt;
606 int lidx;
607
608 comb_next_lo->name = NULL;
609
610 do {
611 opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx);
612 if (frr_opt(opt))
613 break;
614 } while (opt != -1);
615
616 if (opt == -1 && errors)
617 frr_help_exit(1);
618 if (longindex)
619 *longindex = lidx;
620 return opt;
621}
622
beaa5470
DL
623static void frr_mkdir(const char *path, bool strip)
624{
625 char buf[256];
626 mode_t prev;
627 int ret;
628 struct zprivs_ids_t ids;
629
630 if (strip) {
631 char *slash = strrchr(path, '/');
632 size_t plen;
633 if (!slash)
634 return;
635 plen = slash - path;
636 if (plen > sizeof(buf) - 1)
637 return;
638 memcpy(buf, path, plen);
639 buf[plen] = '\0';
640 path = buf;
641 }
642
643 /* o+rx (..5) is needed for the frrvty group to work properly;
644 * without it, users in the frrvty group can't access the vty sockets.
645 */
646 prev = umask(0022);
647 ret = mkdir(path, 0755);
648 umask(prev);
649
650 if (ret != 0) {
651 /* if EEXIST, return without touching the permissions,
652 * so user-set custom permissions are left in place
653 */
654 if (errno == EEXIST)
655 return;
656
1c50c1c0
QY
657 flog_err(EC_LIB_SYSTEM_CALL, "failed to mkdir \"%s\": %s", path,
658 strerror(errno));
beaa5470
DL
659 return;
660 }
661
662 zprivs_get_ids(&ids);
663 if (chown(path, ids.uid_normal, ids.gid_normal))
1c50c1c0
QY
664 flog_err(EC_LIB_SYSTEM_CALL, "failed to chown \"%s\": %s", path,
665 strerror(errno));
beaa5470
DL
666}
667
a5b38c5b 668static struct thread_master *master;
4f04a76b
DL
669struct thread_master *frr_init(void)
670{
30771d65
DL
671 struct option_chain *oc;
672 struct frrmod_runtime *module;
0bdeb5e5 673 struct zprivs_ids_t ids;
30771d65 674 char moderr[256];
d1b4fc1f 675 char p_instance[16] = "", p_pathspace[256] = "";
80b4df3b
MW
676 const char *dir;
677 dir = di->module_path ? di->module_path : frr_moduledir;
4f04a76b
DL
678
679 srandom(time(NULL));
ac4adef4 680 frr_defaults_apply();
4f04a76b 681
d1b4fc1f 682 if (di->instance) {
d62a17ae 683 snprintf(frr_protonameinst, sizeof(frr_protonameinst), "%s[%u]",
684 di->logname, di->instance);
d1b4fc1f
DL
685 snprintf(p_instance, sizeof(p_instance), "-%d", di->instance);
686 }
687 if (di->pathspace)
b39404c1 688 snprintf(p_pathspace, sizeof(p_pathspace), "%s/",
d1b4fc1f
DL
689 di->pathspace);
690
36077833 691 snprintf(config_default, sizeof(config_default), "%s%s%s%s.conf",
d1b4fc1f 692 frr_sysconfdir, p_pathspace, di->name, p_instance);
43e587c1
DS
693 snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s%s.pid",
694 frr_vtydir, di->name, p_instance);
1c2facd1
RW
695#ifdef HAVE_SQLITE3
696 snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s%s%s.db",
697 frr_dbdir, p_pathspace, di->name, p_instance);
698#endif
b85120bc 699
37a1f2fb 700 zprivs_preinit(di->privs);
0bdeb5e5 701 zprivs_get_ids(&ids);
37a1f2fb 702
0bdeb5e5
DL
703 zlog_init(di->progname, di->logname, di->instance,
704 ids.uid_normal, ids.gid_normal);
f8507817 705
e9b4e74a 706 command_setup_early_logging(di->early_logging, di->early_loglevel);
4f04a76b 707
689f5a8c
DL
708 if (!frr_zclient_addr(&zclient_addr, &zclient_addr_len,
709 frr_zclientpath)) {
710 fprintf(stderr, "Invalid zserv socket path: %s\n",
711 frr_zclientpath);
712 exit(1);
713 }
714
b8c1fde3
DL
715 /* don't mkdir these as root... */
716 if (!(di->flags & FRR_NO_PRIVSEP)) {
717 if (!di->pid_file || !di->vty_path)
718 frr_mkdir(frr_vtydir, false);
719 if (di->pid_file)
720 frr_mkdir(di->pid_file, true);
721 if (di->vty_path)
722 frr_mkdir(di->vty_path, true);
723 }
beaa5470 724
30771d65
DL
725 frrmod_init(di->module);
726 while (modules) {
727 modules = (oc = modules)->next;
80b4df3b 728 module = frrmod_load(oc->arg, dir, moderr, sizeof(moderr));
30771d65
DL
729 if (!module) {
730 fprintf(stderr, "%s\n", moderr);
731 exit(1);
732 }
733 XFREE(MTYPE_TMP, oc);
734 }
735
4f04a76b
DL
736 zprivs_init(di->privs);
737
972a411c 738 master = thread_master_create(NULL);
4f04a76b
DL
739 signal_init(master, di->n_signals, di->signals);
740
1c2facd1
RW
741#ifdef HAVE_SQLITE3
742 if (!di->db_file)
743 di->db_file = dbfile_default;
744 db_init(di->db_file);
745#endif
746
857b5446
DL
747 if (di->flags & FRR_LIMITED_CLI)
748 cmd_init(-1);
749 else
750 cmd_init(1);
1c2facd1 751
2950f5da 752 vty_init(master, di->log_always);
1c0d8808 753 lib_cmd_init();
857b5446 754
b9b4c061 755 frr_pthread_init();
fa22080d 756#ifdef HAVE_SCRIPTING
e4e0229a 757 frrscript_init(di->script_path ? di->script_path : frr_scriptdir);
fa22080d 758#endif
b9b4c061 759
85cd2f9f 760 log_ref_init();
1f9128d6 761 log_ref_vty_init();
b66d022e
DS
762 lib_error_init();
763
b90204a8 764 yang_init(true);
ae0994f6
DS
765
766 debug_init_cli();
767
390a8862 768 nb_init(master, di->yang_modules, di->n_yang_modules, true);
1ae9686c
RW
769 if (nb_db_init() != NB_OK)
770 flog_warn(EC_LIB_NB_DATABASE,
771 "%s: failed to initialize northbound database",
772 __func__);
1c2facd1 773
4f04a76b
DL
774 return master;
775}
776
1c2facd1
RW
777const char *frr_get_progname(void)
778{
779 return di ? di->progname : NULL;
780}
781
782enum frr_cli_mode frr_get_cli_mode(void)
783{
784 return di ? di->cli_mode : FRR_CLI_CLASSIC;
785}
786
1a9f340b
MS
787uint32_t frr_get_fd_limit(void)
788{
789 return di ? di->limit_fds : 0;
790}
791
154b9e8f
DL
792static int rcvd_signal = 0;
793
794static void rcv_signal(int signum)
795{
796 rcvd_signal = signum;
797 /* poll() is interrupted by the signal; handled below */
798}
799
f43fbf83
DL
800static void frr_daemon_wait(int fd)
801{
802 struct pollfd pfd[1];
803 int ret;
804 pid_t exitpid;
805 int exitstat;
154b9e8f
DL
806 sigset_t sigs, prevsigs;
807
808 sigemptyset(&sigs);
809 sigaddset(&sigs, SIGTSTP);
810 sigaddset(&sigs, SIGQUIT);
811 sigaddset(&sigs, SIGINT);
812 sigprocmask(SIG_BLOCK, &sigs, &prevsigs);
813
814 struct sigaction sa = {
815 .sa_handler = rcv_signal, .sa_flags = SA_RESETHAND,
816 };
817 sigemptyset(&sa.sa_mask);
818 sigaction(SIGTSTP, &sa, NULL);
819 sigaction(SIGQUIT, &sa, NULL);
820 sigaction(SIGINT, &sa, NULL);
f43fbf83
DL
821
822 do {
154b9e8f
DL
823 char buf[1];
824 ssize_t nrecv;
825
f43fbf83
DL
826 pfd[0].fd = fd;
827 pfd[0].events = POLLIN;
828
154b9e8f
DL
829 rcvd_signal = 0;
830
996c9314 831#if defined(HAVE_PPOLL)
154b9e8f
DL
832 ret = ppoll(pfd, 1, NULL, &prevsigs);
833#elif defined(HAVE_POLLTS)
834 ret = pollts(pfd, 1, NULL, &prevsigs);
835#else
836 /* racy -- only used on FreeBSD 9 */
837 sigset_t tmpsigs;
838 sigprocmask(SIG_SETMASK, &prevsigs, &tmpsigs);
f43fbf83 839 ret = poll(pfd, 1, -1);
154b9e8f
DL
840 sigprocmask(SIG_SETMASK, &tmpsigs, NULL);
841#endif
f43fbf83
DL
842 if (ret < 0 && errno != EINTR && errno != EAGAIN) {
843 perror("poll()");
844 exit(1);
845 }
154b9e8f
DL
846 switch (rcvd_signal) {
847 case SIGTSTP:
848 send(fd, "S", 1, 0);
849 do {
850 nrecv = recv(fd, buf, sizeof(buf), 0);
851 } while (nrecv == -1
852 && (errno == EINTR || errno == EAGAIN));
853
854 raise(SIGTSTP);
855 sigaction(SIGTSTP, &sa, NULL);
856 send(fd, "R", 1, 0);
857 break;
858 case SIGINT:
859 send(fd, "I", 1, 0);
860 break;
861 case SIGQUIT:
862 send(fd, "Q", 1, 0);
863 break;
864 }
f43fbf83
DL
865 } while (ret <= 0);
866
867 exitpid = waitpid(-1, &exitstat, WNOHANG);
868 if (exitpid == 0)
869 /* child successfully went to main loop & closed socket */
870 exit(0);
871
872 /* child failed one way or another ... */
6bd2b360
DL
873 if (WIFEXITED(exitstat) && WEXITSTATUS(exitstat) == 0)
874 /* can happen in --terminal case if exit is fast enough */
875 (void)0;
876 else if (WIFEXITED(exitstat))
f43fbf83
DL
877 fprintf(stderr, "%s failed to start, exited %d\n", di->name,
878 WEXITSTATUS(exitstat));
879 else if (WIFSIGNALED(exitstat))
880 fprintf(stderr, "%s crashed in startup, signal %d\n", di->name,
881 WTERMSIG(exitstat));
882 else
883 fprintf(stderr, "%s failed to start, unknown problem\n",
884 di->name);
885 exit(1);
886}
887
888static int daemon_ctl_sock = -1;
889
890static void frr_daemonize(void)
891{
892 int fds[2];
893 pid_t pid;
894
895 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) {
896 perror("socketpair() for daemon control");
897 exit(1);
898 }
899 set_cloexec(fds[0]);
900 set_cloexec(fds[1]);
901
902 pid = fork();
903 if (pid < 0) {
904 perror("fork()");
905 exit(1);
906 }
907 if (pid == 0) {
908 /* child */
909 close(fds[0]);
910 if (setsid() < 0) {
911 perror("setsid()");
912 exit(1);
913 }
914
915 daemon_ctl_sock = fds[1];
916 return;
917 }
918
919 close(fds[1]);
920 frr_daemon_wait(fds[0]);
921}
922
9e224e60
DS
923/*
924 * Why is this a thread?
925 *
926 * The read in of config for integrated config happens *after*
927 * thread execution starts( because it is passed in via a vtysh -b -n )
928 * While if you are not using integrated config we want the ability
929 * to read the config in after thread execution starts, so that
930 * we can match this behavior.
931 */
932static int frr_config_read_in(struct thread *t)
4f04a76b 933{
91f9fd78
RW
934 if (!vty_read_config(vty_shared_candidate_config, di->config_file,
935 config_default)
936 && di->backup_config_file) {
fe64533a
DS
937 char *orig = XSTRDUP(MTYPE_TMP, host_config_get());
938
573de11f
DS
939 zlog_info("Attempting to read backup config file: %s specified",
940 di->backup_config_file);
91f9fd78
RW
941 vty_read_config(vty_shared_candidate_config,
942 di->backup_config_file, config_default);
fe64533a
DS
943
944 host_config_set(orig);
945 XFREE(MTYPE_TMP, orig);
573de11f 946 }
1c2facd1
RW
947
948 /*
91f9fd78
RW
949 * Automatically commit the candidate configuration after
950 * reading the configuration file.
1c2facd1 951 */
91f9fd78 952 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL) {
13d6b9c1 953 struct nb_context context = {};
df5eda3d 954 char errmsg[BUFSIZ] = {0};
91f9fd78
RW
955 int ret;
956
13d6b9c1
RW
957 context.client = NB_CLIENT_CLI;
958 ret = nb_candidate_commit(&context, vty_shared_candidate_config,
df5eda3d
RW
959 true, "Read configuration file", NULL,
960 errmsg, sizeof(errmsg));
91f9fd78 961 if (ret != NB_OK && ret != NB_ERR_NO_CHANGES)
df5eda3d
RW
962 zlog_err(
963 "%s: failed to read configuration file: %s (%s)",
964 __func__, nb_err_name(ret), errmsg);
91f9fd78 965 }
1c2facd1 966
88e635ee
RW
967 hook_call(frr_very_late_init, master);
968
9e224e60
DS
969 return 0;
970}
971
972void frr_config_fork(void)
973{
974 hook_call(frr_late_init, master);
eb05883f 975
0a7c7856
DL
976 if (!(di->flags & FRR_NO_CFG_PID_DRY)) {
977 /* Don't start execution if we are in dry-run mode */
978 if (di->dryrun) {
979 frr_config_read_in(NULL);
980 exit(0);
981 }
9e224e60 982
0a7c7856
DL
983 thread_add_event(master, frr_config_read_in, NULL, 0,
984 &di->read_in);
985 }
eb05883f 986
154b9e8f 987 if (di->daemon_mode || di->terminal)
f43fbf83 988 frr_daemonize();
eb05883f
DL
989
990 if (!di->pid_file)
991 di->pid_file = pidfile_default;
d62a17ae 992 pid_output(di->pid_file);
e2be2643 993 zlog_tls_buffer_init();
eb05883f
DL
994}
995
0a7c7856 996static void frr_vty_serv(void)
eb05883f 997{
d62a17ae 998 /* allow explicit override of vty_path in the future
eb05883f
DL
999 * (not currently set anywhere) */
1000 if (!di->vty_path) {
1001 const char *dir;
d1b4fc1f
DL
1002 char defvtydir[256];
1003
43e587c1 1004 snprintf(defvtydir, sizeof(defvtydir), "%s", frr_vtydir);
d1b4fc1f
DL
1005
1006 dir = di->vty_sock_path ? di->vty_sock_path : defvtydir;
eb05883f
DL
1007
1008 if (di->instance)
1009 snprintf(vtypath_default, sizeof(vtypath_default),
d62a17ae 1010 "%s/%s-%d.vty", dir, di->name, di->instance);
eb05883f
DL
1011 else
1012 snprintf(vtypath_default, sizeof(vtypath_default),
d62a17ae 1013 "%s/%s.vty", dir, di->name);
eb05883f
DL
1014
1015 di->vty_path = vtypath_default;
1016 }
1017
1018 vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path);
4f04a76b
DL
1019}
1020
0a7c7856
DL
1021static void frr_check_detach(void)
1022{
1023 if (nodetach_term || nodetach_daemon)
1024 return;
1025
1026 if (daemon_ctl_sock != -1)
1027 close(daemon_ctl_sock);
1028 daemon_ctl_sock = -1;
1029}
1030
154b9e8f 1031static void frr_terminal_close(int isexit)
cff2b211 1032{
993bab89
RW
1033 int nullfd;
1034
0a7c7856
DL
1035 nodetach_term = false;
1036 frr_check_detach();
154b9e8f
DL
1037
1038 if (!di->daemon_mode || isexit) {
cff2b211 1039 printf("\n%s exiting\n", di->name);
154b9e8f
DL
1040 if (!isexit)
1041 raise(SIGINT);
1042 return;
cff2b211
DL
1043 } else {
1044 printf("\n%s daemonizing\n", di->name);
1045 fflush(stdout);
1046 }
1047
993bab89
RW
1048 nullfd = open("/dev/null", O_RDONLY | O_NOCTTY);
1049 if (nullfd == -1) {
450971aa 1050 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
1051 "%s: failed to open /dev/null: %s", __func__,
1052 safe_strerror(errno));
993bab89
RW
1053 } else {
1054 dup2(nullfd, 0);
1055 dup2(nullfd, 1);
1056 dup2(nullfd, 2);
1057 close(nullfd);
1058 }
154b9e8f 1059}
cff2b211 1060
154b9e8f
DL
1061static struct thread *daemon_ctl_thread = NULL;
1062
1063static int frr_daemon_ctl(struct thread *t)
1064{
1065 char buf[1];
1066 ssize_t nr;
1067
1068 nr = recv(daemon_ctl_sock, buf, sizeof(buf), 0);
1069 if (nr < 0 && (errno == EINTR || errno == EAGAIN))
1070 goto out;
1071 if (nr <= 0)
1072 return 0;
1073
1074 switch (buf[0]) {
996c9314 1075 case 'S': /* SIGTSTP */
154b9e8f 1076 vty_stdio_suspend();
e339d7c0 1077 if (send(daemon_ctl_sock, "s", 1, 0) < 0)
1078 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
1079 (di && di->name ? di->name : ""));
154b9e8f 1080 break;
996c9314 1081 case 'R': /* SIGTCNT [implicit] */
154b9e8f
DL
1082 vty_stdio_resume();
1083 break;
996c9314 1084 case 'I': /* SIGINT */
154b9e8f
DL
1085 di->daemon_mode = false;
1086 raise(SIGINT);
1087 break;
996c9314 1088 case 'Q': /* SIGQUIT */
154b9e8f
DL
1089 di->daemon_mode = true;
1090 vty_stdio_close();
1091 break;
cff2b211 1092 }
154b9e8f
DL
1093
1094out:
1095 thread_add_read(master, frr_daemon_ctl, NULL, daemon_ctl_sock,
1096 &daemon_ctl_thread);
1097 return 0;
cff2b211
DL
1098}
1099
0a7c7856
DL
1100void frr_detach(void)
1101{
1102 nodetach_daemon = false;
1103 frr_check_detach();
1104}
1105
16077f2f
DL
1106void frr_run(struct thread_master *master)
1107{
1108 char instanceinfo[64] = "";
1109
1110 frr_vty_serv();
1111
1112 if (di->instance)
1113 snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ",
d62a17ae 1114 di->instance);
1115
1116 zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION,
1117 instanceinfo, di->vty_port, di->startinfo);
16077f2f 1118
cff2b211 1119 if (di->terminal) {
0a7c7856
DL
1120 nodetach_term = true;
1121
cff2b211 1122 vty_stdio(frr_terminal_close);
154b9e8f
DL
1123 if (daemon_ctl_sock != -1) {
1124 set_nonblocking(daemon_ctl_sock);
1125 thread_add_read(master, frr_daemon_ctl, NULL,
1126 daemon_ctl_sock, &daemon_ctl_thread);
1127 }
eef3d030 1128 } else if (di->daemon_mode) {
c9c8d0d1 1129 int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY);
993bab89 1130 if (nullfd == -1) {
450971aa 1131 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
1132 "%s: failed to open /dev/null: %s",
1133 __func__, safe_strerror(errno));
993bab89
RW
1134 } else {
1135 dup2(nullfd, 0);
1136 dup2(nullfd, 1);
1137 dup2(nullfd, 2);
1138 close(nullfd);
1139 }
c9c8d0d1 1140
0a7c7856 1141 frr_check_detach();
f43fbf83
DL
1142 }
1143
d34cb7f0 1144 /* end fixed stderr startup logging */
0bdeb5e5 1145 zlog_startup_end();
d34cb7f0 1146
16077f2f
DL
1147 struct thread thread;
1148 while (thread_fetch(master, &thread))
1149 thread_call(&thread);
1150}
03951374
DL
1151
1152void frr_early_fini(void)
1153{
1154 hook_call(frr_early_fini);
1155}
1156
1157void frr_fini(void)
1158{
9eed278b
DL
1159 FILE *fp;
1160 char filename[128];
1161 int have_leftovers;
1162
03951374
DL
1163 hook_call(frr_fini);
1164
03951374
DL
1165 vty_terminate();
1166 cmd_terminate();
1c2facd1
RW
1167 nb_terminate();
1168 yang_terminate();
1169#ifdef HAVE_SQLITE3
1170 db_close();
1171#endif
85cd2f9f 1172 log_ref_fini();
b9b4c061 1173 frr_pthread_finish();
03951374
DL
1174 zprivs_terminate(di->privs);
1175 /* signal_init -> nothing needed */
1176 thread_master_free(master);
e5716b16 1177 master = NULL;
0bdeb5e5
DL
1178 zlog_tls_buffer_fini();
1179 zlog_fini();
03951374 1180 /* frrmod_init -> nothing needed / hooks */
3e41733f 1181 rcu_shutdown();
9eed278b
DL
1182
1183 if (!debug_memstats_at_exit)
1184 return;
1185
1186 have_leftovers = log_memstats(stderr, di->name);
1187
1188 /* in case we decide at runtime that we want exit-memstats for
1189 * a daemon, but it has no stderr because it's daemonized
1190 * (only do this if we actually have something to print though)
1191 */
1192 if (!have_leftovers)
1193 return;
1194
996c9314
LB
1195 snprintf(filename, sizeof(filename), "/tmp/frr-memstats-%s-%llu-%llu",
1196 di->name, (unsigned long long)getpid(),
9eed278b
DL
1197 (unsigned long long)time(NULL));
1198
1199 fp = fopen(filename, "w");
1200 if (fp) {
1201 log_memstats(fp, di->name);
1202 fclose(fp);
1203 }
03951374 1204}
42efb0d4
DL
1205
1206#ifdef INTERP
1207static const char interp[]
1208 __attribute__((section(".interp"), used)) = INTERP;
1209#endif
1210/*
1211 * executable entry point for libfrr.so
1212 *
1213 * note that libc initialization is skipped for this so the set of functions
1214 * that can be called is rather limited
1215 */
1216extern void _libfrr_version(void)
1217 __attribute__((visibility("hidden"), noreturn));
1218void _libfrr_version(void)
1219{
1220 const char banner[] =
1221 FRR_FULL_NAME " " FRR_VERSION ".\n"
1222 FRR_COPYRIGHT GIT_INFO "\n"
1223 "configured with:\n " FRR_CONFIG_ARGS "\n";
1224 write(1, banner, sizeof(banner) - 1);
1225 _exit(0);
1226}