summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2021-02-04 22:25:49 +0100
committerDavid Lamparter <equinox@diac24.net>2021-02-12 19:28:22 +0100
commit6e3253b9790bdb22eff3b74b940c8c20d8cbea2c (patch)
tree7d13c7c0c4e251f39e93858d061f5e50d6a82269
parent35da69f1545446ecd9981b5d5fd5f3b4d912e50a (diff)
lib: memorize what fds were open at startup
... in case the user does something like `zebra 3>logfile`. Also useful for some module purposes, maybe even feeding config at some point in the future. Signed-off-by: David Lamparter <equinox@diac24.net>
-rw-r--r--lib/libfrr.c23
-rw-r--r--lib/libfrr.h3
2 files changed, 25 insertions, 1 deletions
diff --git a/lib/libfrr.c b/lib/libfrr.c
index b83883779c..51b97369c9 100644
--- a/lib/libfrr.c
+++ b/lib/libfrr.c
@@ -71,6 +71,7 @@ static char vtypath_default[512];
bool debug_memstats_at_exit = false;
static bool nodetach_term, nodetach_daemon;
+static uint64_t startup_fds;
static char comb_optstr[256];
static struct option comb_lo[64];
@@ -341,6 +342,28 @@ void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv)
strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst));
di->cli_mode = FRR_CLI_CLASSIC;
+
+ /* we may be starting with extra FDs open for whatever purpose,
+ * e.g. logging, some module, etc. Recording them here allows later
+ * checking whether an fd is valid for such extension purposes,
+ * without this we could end up e.g. logging to a BGP session fd.
+ */
+ startup_fds = 0;
+ for (int i = 0; i < 64; i++) {
+ struct stat st;
+
+ if (fstat(i, &st))
+ continue;
+ if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode))
+ continue;
+
+ startup_fds |= UINT64_C(0x1) << (uint64_t)i;
+ }
+}
+
+bool frr_is_startup_fd(int fd)
+{
+ return !!(startup_fds & (UINT64_C(0x1) << (uint64_t)fd));
}
void frr_opt_add(const char *optstr, const struct option *longopts,
diff --git a/lib/libfrr.h b/lib/libfrr.h
index c446931468..825f502bdf 100644
--- a/lib/libfrr.h
+++ b/lib/libfrr.h
@@ -138,7 +138,8 @@ extern __attribute__((__noreturn__)) void frr_help_exit(int status);
extern struct thread_master *frr_init(void);
extern const char *frr_get_progname(void);
extern enum frr_cli_mode frr_get_cli_mode(void);
-uint32_t frr_get_fd_limit(void);
+extern uint32_t frr_get_fd_limit(void);
+extern bool frr_is_startup_fd(int fd);
DECLARE_HOOK(frr_late_init, (struct thread_master * tm), (tm))
DECLARE_HOOK(frr_very_late_init, (struct thread_master * tm), (tm))