From efa6a7a380e7a153cab2ae4fb04d15fb0a09f508 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 4 Mar 2016 22:03:38 +0100 Subject: [PATCH] version: display more build information when using "-vv" Display supported features as well as compilation flags used. This will be useful to let integration tests autodetect compiled features. --- configure.ac | 7 ++ src/Makefile.am | 4 +- src/client/lldpcli.8.in | 2 +- src/client/lldpcli.c | 10 ++- src/daemon/lldpd.8.in | 2 +- src/daemon/lldpd.c | 10 ++- src/log.h | 5 ++ src/version.c | 144 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 174 insertions(+), 10 deletions(-) create mode 100644 src/version.c diff --git a/configure.ac b/configure.ac index e8d34508..fc89f109 100644 --- a/configure.ac +++ b/configure.ac @@ -336,6 +336,13 @@ if test x"$os" = x"Linux"; then fi fi +# Build date +if test x"$SOURCE_DATE_EPOCH" != x; then + AC_DEFINE_UNQUOTED(BUILD_DATE, "[$SOURCE_DATE_EPOCH]", [Build date and time]) +fi +AC_DEFINE_UNQUOTED(LLDP_CC, "[$CC $LLDP_CFLAGS $LLDP_CPPFLAGS $CFLAGS $CPPFLAGS]", [C compiler command]) +AC_DEFINE_UNQUOTED(LLDP_LD, "[$LD $LLDP_LDFLAGS $LLDP_BIN_LDFLAGS $LDFLAGS $LIBS]", [Linker compiler command]) + ####################### # Output results AC_SUBST([LLDP_CFLAGS]) diff --git a/src/Makefile.am b/src/Makefile.am index 63e3c1f2..ddbd1135 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,11 +6,11 @@ noinst_LTLIBRARIES = libcommon-daemon-lib.la libcommon-daemon-client.la include_HEADERS = lldp-const.h libcommon_daemon_lib_la_SOURCES = \ - log.c log.h \ + log.c log.h version.c \ marshal.c marshal.h \ ctl.c ctl.h \ lldpd-structs.c lldpd-structs.h lldp-const.h libcommon_daemon_lib_la_LIBADD = compat/libcompat.la -libcommon_daemon_client_la_SOURCES = log.c log.h lldp-const.h +libcommon_daemon_client_la_SOURCES = log.c log.h version.c lldp-const.h libcommon_daemon_client_la_LIBADD = compat/libcompat.la diff --git a/src/client/lldpcli.8.in b/src/client/lldpcli.8.in index b318b8b1..17f61672 100644 --- a/src/client/lldpcli.8.in +++ b/src/client/lldpcli.8.in @@ -55,7 +55,7 @@ Specify the Unix-domain socket used for communication with .It Fl v Show .Nm -version. +version. When repeated, show more build information. .It Fl f Ar format Choose the output format. Currently .Em plain , diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index f04bdadc..ff458233 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -434,7 +434,7 @@ main(int argc, char *argv[]) lldpctl_conn_t *conn = NULL; const char *options = is_lldpctl(argv[0])?"hdvf:u:":"hdsvf:c:u:"; - int gotinputs = 0; + int gotinputs = 0, version = 0; struct inputs inputs; TAILQ_INIT(&inputs); @@ -465,8 +465,7 @@ main(int argc, char *argv[]) ctlname = optarg; break; case 'v': - fprintf(stdout, "%s\n", PACKAGE_VERSION); - exit(0); + version++; break; case 'f': fmt = optarg; @@ -484,6 +483,11 @@ main(int argc, char *argv[]) } } + if (version) { + version_display(stdout, "lldpcli", version > 1); + exit(0); + } + if (!gotinputs) { log_init(use_syslog, debug, __progname); lldpctl_log_level(debug + 1); diff --git a/src/daemon/lldpd.8.in b/src/daemon/lldpd.8.in index d2a83ad2..a413ebc0 100644 --- a/src/daemon/lldpd.8.in +++ b/src/daemon/lldpd.8.in @@ -269,7 +269,7 @@ for configuration. .It Fl v Show .Nm -version. +version. When repeated, show more build information. .El .Sh FILTERING NEIGHBORS In a heterogeneous network, you may see several different hosts on the diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index c815705d..3b35534b 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -1455,7 +1455,7 @@ lldpd_main(int argc, char *argv[], char *envp[]) char *lsb_release = NULL; const char *lldpcli = LLDPCLI_PATH; int smart = 15; - int receiveonly = 0; + int receiveonly = 0, version = 0; int ctl; #ifdef ENABLE_PRIVSEP @@ -1488,8 +1488,7 @@ lldpd_main(int argc, char *argv[], char *envp[]) usage(); break; case 'v': - fprintf(stdout, "%s\n", PACKAGE_VERSION); - exit(0); + version++; break; case 'd': if (daemonize) @@ -1607,6 +1606,11 @@ lldpd_main(int argc, char *argv[], char *envp[]) } } + if (version) { + version_display(stdout, "lldpd", version > 1); + exit(0); + } + if (ctlname == NULL) ctlname = LLDPD_CTL_SOCKET; /* Set correct smart mode */ diff --git a/src/log.h b/src/log.h index 09889d4f..45534464 100644 --- a/src/log.h +++ b/src/log.h @@ -18,6 +18,8 @@ #ifndef _LOG_H #define _LOG_H +#include + /* log.c */ void log_init(int, int, const char *); void log_warn(const char *, const char *, ...) __attribute__ ((format (printf, 2, 3))); @@ -31,4 +33,7 @@ void log_register(void (*cb)(int, const char*)); void log_accept(const char *); void log_level(int); +/* version.c */ +void version_display(FILE *, const char *, int); + #endif diff --git a/src/version.c b/src/version.c new file mode 100644 index 00000000..e3066493 --- /dev/null +++ b/src/version.c @@ -0,0 +1,144 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ +/* + * Copyright (c) 2016 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. + */ + +#if HAVE_CONFIG_H +# include +#endif + +#include +#include "compat/compat.h" + +#ifndef BUILD_DATE +# define BUILD_DATE __DATE__ " " __TIME__ +#endif + +static void +version_display_array(FILE *destination, const char *prefix, const char *const *items) +{ + fprintf(destination, "%s", prefix); + size_t count = 0; + for (const char *const *p = items; *p; p++, count++) + fprintf(destination, "%s%s", count?", ":"", *p); + if (count == 0) + fprintf(destination, "(none)\n"); + else + fprintf(destination, "\n"); +} + +void +version_display(FILE *destination, const char *progname, int verbose) +{ + if (!verbose) { + fprintf(destination, "%s\n", PACKAGE_VERSION); + return; + } + + const char *const lldp_features[] = { +#ifdef ENABLE_LLDPMED + "LLDP-MED", +#endif +#ifdef ENABLE_DOT1 + "Dot1", +#endif +#ifdef ENABLE_DOT3 + "Dot3", +#endif +#ifdef ENABLE_CUSTOM + "Custom TLV", +#endif + NULL}; + const char *const protocols[] = { +#ifdef ENABLE_CDP + "CDP", +#endif +#ifdef ENABLE_FDP + "FDP", +#endif +#ifdef ENABLE_EDP + "EDP", +#endif +#ifdef ENABLE_SONMP + "SONMP", +#endif + NULL}; + const char *const output_formats[] = { +#ifdef USE_XML + "XML", +#endif +#ifdef USE_JSON + "JSON", +#endif + NULL}; + + + fprintf(destination, "%s %s\n", progname, PACKAGE_VERSION); + fprintf(destination, " Built on " BUILD_DATE "\n"); + fprintf(destination, "\n"); + + /* Features */ + if (!strcmp(progname, "lldpd")) { + version_display_array(destination, + "Additional LLDP features: ", lldp_features); + version_display_array(destination, + "Additional protocols: ", protocols); + fprintf(destination, + "SNMP support: " +#ifdef USE_SNMP + "yes\n" +#else + "no\n" +#endif + ); +#ifdef HOST_OS_LINUX + fprintf(destination, + "Old kernel support: " +#ifdef ENABLE_OLDIES + "yes" +#else + "no" +#endif + " (Linux " MIN_LINUX_KERNEL_VERSION "+)\n"); +#endif +#ifdef ENABLE_PRIVSEP + fprintf(destination, + "Privilege separation: " "enabled\n"); + fprintf(destination, + "Privilege separation user: " PRIVSEP_USER "\n"); + fprintf(destination, + "Privilege separation group: " PRIVSEP_GROUP "\n"); + fprintf(destination, + "Privilege separation chroot: " PRIVSEP_CHROOT "\n"); +#else + fprintf(destination, + "Privilege separation: " "disabled\n"); +#endif + } + + if (!strcmp(progname, "lldpcli")) { + version_display_array(destination, + "Additional output formats: ", output_formats); + } + + fprintf(destination, "\n"); + + /* Build */ + fprintf(destination, + "C compiler command: %s\n", LLDP_CC); + fprintf(destination, + "Linker command: %s\n", LLDP_LD); + +} -- 2.39.5