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