From 66879d46ac2b03fc7d44952bb29ad7d87335b52e Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 21 Jun 2013 02:55:50 +0200 Subject: [PATCH] build: use libbsd if available, also use `setproctitle()` The monitor process will be titled "monitor", while the unprivileged one will have the number of neighbors displayed. We provide an empty fallback since this function is not essential. On Linux, we expect `setproctitle()` to be available in `libbsd`. This makes functions like `strlcpy()` and `fgetln()` also available. However, the headers are `bsd/string.h`, so we either need to declare the prototype or include those new headers (or use the overlay system). A simple thing to do is to detect the usage of libbsd and include the appropriate headers in this case. --- configure.ac | 9 ++++++++- src/compat/compat.h | 9 +++++++++ src/compat/setproctitle.c | 22 ++++++++++++++++++++++ src/daemon/lldpd.c | 19 +++++++++++++++++++ src/daemon/priv.c | 1 + 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/compat/setproctitle.c diff --git a/configure.ac b/configure.ac index 5689c9e4..ce765756 100644 --- a/configure.ac +++ b/configure.ac @@ -95,9 +95,16 @@ lldp_CHECK___PROGNAME AC_CONFIG_LIBOBJ_DIR([src/compat]) AC_FUNC_MALLOC AC_FUNC_REALLOC -AC_REPLACE_FUNCS([strlcpy strnlen fgetln]) +AC_SEARCH_LIBS([setproctitle], [util bsd]) +AC_REPLACE_FUNCS([strlcpy strnlen fgetln setproctitle]) AC_CHECK_FUNCS([setresuid setresgid]) +case " $LIBS " in + *\ -lbsd\ *) + AC_DEFINE(HAVE_LIBBSD, 1, [Define if libbsd is used]) + ;; +esac + AC_SEARCH_LIBS([__res_init], resolv bind, AC_DEFINE([HAVE_RES_INIT], 1, [Define to indicate that res_init() exists]), AC_SEARCH_LIBS([res_9_init], resolv bind, AC_DEFINE([HAVE_RES_INIT], 1, [Define to indicate that res_init() exists]), AC_SEARCH_LIBS([res_init], resolv bind, AC_DEFINE([HAVE_RES_INIT], 1, [Define to indicate that res_init() exists])))) diff --git a/src/compat/compat.h b/src/compat/compat.h index fde89607..8d44eac1 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -35,6 +35,11 @@ #include #include +#ifdef HAVE_LIBBSD +# include +# include +# include +#endif #if !HAVE_STRLCPY size_t strlcpy(char *, const char *, size_t); @@ -48,6 +53,10 @@ size_t strnlen(const char *, size_t); char *fgetln(FILE *, size_t *); #endif +#if !HAVE_SETPROCTITLE +void setproctitle(const char *fmt, ...); +#endif + #if !HAVE_MALLOC void *malloc(size_t size); #endif diff --git a/src/compat/setproctitle.c b/src/compat/setproctitle.c new file mode 100644 index 00000000..074de0db --- /dev/null +++ b/src/compat/setproctitle.c @@ -0,0 +1,22 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ +/* + * Copyright (c) 2013 Vincent Bernat + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +void +setproctitle(const char *fmt, ...) +{ + /* Do nothing. */ +} diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 7924e838..89ffd919 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -213,6 +213,21 @@ lldpd_hardware_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware) free(hardware); } +static void +lldpd_count_neighbors(struct lldpd *cfg) +{ + struct lldpd_hardware *hardware; + struct lldpd_port *port; + unsigned neighbors = 0; + TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) { + TAILQ_FOREACH(port, &hardware->h_rports, p_entries) { + if (!port->p_hidden_in) + neighbors++; + } + } + setproctitle("%d neighbor%s", neighbors, (neighbors > 1)?"s":""); +} + static void notify_clients_deletion(struct lldpd_hardware *hardware, struct lldpd_port *rport) @@ -222,6 +237,7 @@ notify_clients_deletion(struct lldpd_hardware *hardware, #ifdef USE_SNMP agent_notify(hardware, NEIGHBOR_CHANGE_DELETED, rport); #endif + lldpd_count_neighbors(hardware->h_cfg); } static void @@ -294,6 +310,7 @@ lldpd_cleanup(struct lldpd *cfg) } } + lldpd_count_neighbors(cfg); levent_schedule_cleanup(cfg); } @@ -794,6 +811,7 @@ lldpd_recv(struct lldpd *cfg, struct lldpd_hardware *hardware, int fd) hardware->h_ifname); lldpd_decode(cfg, buffer, n, hardware); lldpd_hide_all(cfg); /* Immediatly hide */ + lldpd_count_neighbors(cfg); free(buffer); } @@ -986,6 +1004,7 @@ lldpd_loop(struct lldpd *cfg) lldpd_update_localports(cfg); log_debug("loop", "update information for local chassis"); lldpd_update_localchassis(cfg); + lldpd_count_neighbors(cfg); } static void diff --git a/src/daemon/priv.c b/src/daemon/priv.c index 553d9b60..2bbde489 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -632,6 +632,7 @@ priv_loop() int cmd; struct dispatch_actions *a; + setproctitle("monitor"); while (!may_read(remote, &cmd, sizeof(int))) { for (a = actions; a->function != NULL; a++) { if (cmd == a->msg) { -- 2.39.5