From 065732ca34138264e65f8784d5e97be388f08c71 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Thu, 11 Jul 2013 23:13:10 +0200 Subject: [PATCH] priv: correctly declare `priv_cmd` type `enum {} priv_cmd` was the declaration of a variable `priv_cmd` as an anonymous enum. This is not what was expected. Instead, we want `enum priv_cmd` to be a declaration of a named enum: `enum priv_cmd {}` (we could also use `typedef enum {} priv_cmd`, but in lldpd, `typedef` is seldomly used). When `priv_cmd` was moved out of `priv.c`, its symbol was defined in several objects. It seems that recent versions of gcc are able to cope with that (because `priv_cmd` variable was not used, so it was optimized out I think, not sure). However, some older versions like 4.2.1 complained about duplicate objects during the link phase. --- src/daemon/lldpd.h | 4 ++-- src/daemon/priv-linux.c | 12 +++++----- src/daemon/priv.c | 50 ++++++++++++++++++++--------------------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/daemon/lldpd.h b/src/daemon/lldpd.h index 6b2d06ca..30e10357 100644 --- a/src/daemon/lldpd.h +++ b/src/daemon/lldpd.h @@ -226,7 +226,7 @@ int priv_iface_description(const char *, const char *); int asroot_iface_description_os(const char *, const char *); int priv_snmp_socket(struct sockaddr_un *); -enum { +enum priv_cmd { PRIV_PING, PRIV_DELETE_CTL_SOCKET, PRIV_GET_HOSTNAME, @@ -236,7 +236,7 @@ enum { PRIV_IFACE_MULTICAST, PRIV_IFACE_DESCRIPTION, PRIV_SNMP_SOCKET, -} priv_cmd; +}; /* privsep_io.c */ diff --git a/src/daemon/priv-linux.c b/src/daemon/priv-linux.c index c78d569b..68eee70e 100644 --- a/src/daemon/priv-linux.c +++ b/src/daemon/priv-linux.c @@ -32,9 +32,9 @@ int priv_open(char *file) { - int cmd, len, rc; - cmd = PRIV_OPEN; - must_write(&cmd, sizeof(int)); + int len, rc; + enum priv_cmd cmd = PRIV_OPEN; + must_write(&cmd, sizeof(enum priv_cmd)); len = strlen(file); must_write(&len, sizeof(int)); must_write(file, len + 1); @@ -48,9 +48,9 @@ priv_open(char *file) int priv_ethtool(char *ifname, void *ethc, size_t length) { - int cmd, rc, len; - cmd = PRIV_ETHTOOL; - must_write(&cmd, sizeof(int)); + int rc, len; + enum priv_cmd cmd = PRIV_ETHTOOL; + must_write(&cmd, sizeof(enum priv_cmd)); len = strlen(ifname); must_write(&len, sizeof(int)); must_write(ifname, len + 1); diff --git a/src/daemon/priv.c b/src/daemon/priv.c index bc2b4057..904b87be 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -65,9 +65,9 @@ static int monitored = -1; /* Child */ static void priv_ping() { - int cmd, rc; - cmd = PRIV_PING; - must_write(&cmd, sizeof(int)); + int rc; + enum priv_cmd cmd = PRIV_PING; + must_write(&cmd, sizeof(enum priv_cmd)); must_read(&rc, sizeof(int)); log_debug("privsep", "monitor ready"); } @@ -76,10 +76,9 @@ priv_ping() void priv_ctl_cleanup(const char *ctlname) { - int cmd, rc; - int len = strlen(ctlname); - cmd = PRIV_DELETE_CTL_SOCKET; - must_write(&cmd, sizeof(int)); + int rc, len = strlen(ctlname); + enum priv_cmd cmd = PRIV_DELETE_CTL_SOCKET; + must_write(&cmd, sizeof(enum priv_cmd)); must_write(&len, sizeof(int)); must_write(ctlname, len); must_read(&rc, sizeof(int)); @@ -89,10 +88,10 @@ priv_ctl_cleanup(const char *ctlname) char * priv_gethostbyname() { - int cmd, rc; static char *buf = NULL; - cmd = PRIV_GET_HOSTNAME; - must_write(&cmd, sizeof(int)); + int rc; + enum priv_cmd cmd = PRIV_GET_HOSTNAME; + must_write(&cmd, sizeof(enum priv_cmd)); must_read(&rc, sizeof(int)); if ((buf = (char*)realloc(buf, rc+1)) == NULL) fatal("privsep", NULL); @@ -104,10 +103,10 @@ priv_gethostbyname() int priv_iface_init(int index, char *iface) { - int cmd, rc; + int rc; char dev[IFNAMSIZ]; - cmd = PRIV_IFACE_INIT; - must_write(&cmd, sizeof(int)); + enum priv_cmd cmd = PRIV_IFACE_INIT; + must_write(&cmd, sizeof(enum priv_cmd)); must_write(&index, sizeof(int)); strlcpy(dev, iface, IFNAMSIZ); must_write(dev, IFNAMSIZ); @@ -119,9 +118,9 @@ priv_iface_init(int index, char *iface) int priv_iface_multicast(const char *name, u_int8_t *mac, int add) { - int cmd, rc; - cmd = PRIV_IFACE_MULTICAST; - must_write(&cmd, sizeof(int)); + int rc; + enum priv_cmd cmd = PRIV_IFACE_MULTICAST; + must_write(&cmd, sizeof(enum priv_cmd)); must_write(name, IFNAMSIZ); must_write(mac, ETHER_ADDR_LEN); must_write(&add, sizeof(int)); @@ -132,10 +131,9 @@ priv_iface_multicast(const char *name, u_int8_t *mac, int add) int priv_iface_description(const char *name, const char *description) { - int cmd, rc; - int len = strlen(description); - cmd = PRIV_IFACE_DESCRIPTION; - must_write(&cmd, sizeof(int)); + int rc, len = strlen(description); + enum priv_cmd cmd = PRIV_IFACE_DESCRIPTION; + must_write(&cmd, sizeof(enum priv_cmd)); must_write(name, IFNAMSIZ); must_write(&len, sizeof(int)); must_write(description, len); @@ -146,9 +144,9 @@ priv_iface_description(const char *name, const char *description) int priv_snmp_socket(struct sockaddr_un *addr) { - int cmd, rc; - cmd = PRIV_SNMP_SOCKET; - must_write(&cmd, sizeof(int)); + int rc; + enum priv_cmd cmd = PRIV_SNMP_SOCKET; + must_write(&cmd, sizeof(enum priv_cmd)); must_write(addr, sizeof(struct sockaddr_un)); must_read(&rc, sizeof(int)); if (rc < 0) @@ -318,7 +316,7 @@ asroot_snmp_socket() } struct dispatch_actions { - int msg; + enum priv_cmd msg; void(*function)(void); }; @@ -341,11 +339,11 @@ static struct dispatch_actions actions[] = { static void priv_loop() { - int cmd; + enum priv_cmd cmd; struct dispatch_actions *a; setproctitle("monitor"); - while (!may_read(&cmd, sizeof(int))) { + while (!may_read(&cmd, sizeof(enum priv_cmd))) { for (a = actions; a->function != NULL; a++) { if (cmd == a->msg) { a->function(); -- 2.39.5