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