]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
build: use libbsd if available, also use `setproctitle()`
authorVincent Bernat <bernat@luffy.cx>
Fri, 21 Jun 2013 00:55:50 +0000 (02:55 +0200)
committerVincent Bernat <bernat@luffy.cx>
Fri, 21 Jun 2013 00:55:50 +0000 (02:55 +0200)
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
src/compat/compat.h
src/compat/setproctitle.c [new file with mode: 0644]
src/daemon/lldpd.c
src/daemon/priv.c

index 5689c9e4cca9d9a3018908d9ec1b011335a0c5f4..ce765756564b88f3e4d63ea80c7d1b2a203d0c12 100644 (file)
@@ -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]))))
index fde896074abc00bd397c16e479e6e45895ea359a..8d44eac1ab2b4a7c17f4fcb0efba2adadc471808 100644 (file)
 
 #include <stdio.h>
 #include <stddef.h>
+#ifdef HAVE_LIBBSD
+# include <bsd/stdio.h>
+# include <bsd/string.h>
+# include <bsd/unistd.h>
+#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 (file)
index 0000000..074de0d
--- /dev/null
@@ -0,0 +1,22 @@
+/* -*- mode: c; c-file-style: "openbsd" -*- */
+/*
+ * Copyright (c) 2013 Vincent Bernat <bernat@luffy.cx>
+ *
+ * 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. */
+}
index 7924e838adf838cadd68fa998cff17e6f949a0d1..89ffd91987cfde4296bc0314637558bb2ca02073 100644 (file)
@@ -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
index 553d9b600a8367e4145305c6f41b9bf143379fb0..2bbde48985ec06cca2feef260365dce8e18e8eb7 100644 (file)
@@ -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) {