From 071ff9972aa45458999d51ed5b72013ee5c5a98f Mon Sep 17 00:00:00 2001 From: Eric Multanen Date: Wed, 10 Nov 2010 08:01:18 -0800 Subject: [PATCH 1/3] lldpad: use abstract namespace for client interface socket Change the client interface to use an abstract namespace socket. This is useful in the initrd stage as it does not require the addition of directories and files to the filesystem just for the purpose of supporting the client interface. This patch removes all supporting code for the previous socket style. Note: other clients of lldpad also will need to be modified to utiltize this new socket type (e.g. fcoemon in fcoe-utils). Signed-off-by: Eric Multanen Signed-off-by: Hannes Reinecke --- Makefile.am | 2 +- clif.c | 27 ++++++------ ctrl_iface.c | 104 ++++------------------------------------------ dcbtool.c | 92 +--------------------------------------- include/clif.h | 11 +---- include/clif_msgs.h | 1 + include/clif_sock.h | 33 ++++++++++++++ include/ctrl_iface.h | 4 -- include/lldp_mand_clif.h | 1 - lldpad.c | 6 --- lldptool.c | 96 +----------------------------------------- 11 files changed, 64 insertions(+), 313 deletions(-) create mode 100644 include/clif_sock.h diff --git a/Makefile.am b/Makefile.am index 743e16f..99202b0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,7 +37,7 @@ lldpad_include_HEADERS = include/dcb_types.h include/dcbtool.h \ include/dcb_osdep.h include/clif.h include/lldp_dcbx_cmds.h include/common.h \ include/lldpad.h include/os.h include/includes.h include/lldp_mand_cmds.h \ include/clif_msgs.h include/lldp_basman_cmds.h include/lldp_8023_cmds.h \ -include/lldp_med_cmds.h include/lldp_dcbx_cfg.h +include/lldp_med_cmds.h include/lldp_dcbx_cfg.h include/clif_sock.h noinst_HEADERS = include/config.h include/ctrl_iface.h \ include/dcb_driver_if_types.h include/dcb_driver_interface.h \ diff --git a/clif.c b/clif.c index 5f28240..2ff0c22 100644 --- a/clif.c +++ b/clif.c @@ -43,39 +43,41 @@ #endif /* CONFIG_CLIF_IFACE_UNIX || CONFIG_CLIF_IFACE_UDP */ -struct clif *clif_open(const char *clif_path) +struct clif *clif_open() { struct clif *clif; - static int counter = 0; clif = os_malloc(sizeof(*clif)); if (clif == NULL) return NULL; os_memset(clif, 0, sizeof(*clif)); - clif->s = socket(PF_UNIX, SOCK_DGRAM, 0); + clif->s = socket(AF_LOCAL, SOCK_DGRAM, 0); if (clif->s < 0) { + perror("socket"); os_free(clif); return NULL; } - clif->local.sun_family = AF_UNIX; - os_snprintf(clif->local.sun_path, sizeof(clif->local.sun_path), - "/tmp/lldpad_clif_%d-%d", getpid(), counter++); + clif->local.sun_family = AF_LOCAL; + clif->local.sun_path[0] = '\0'; + os_snprintf(&clif->local.sun_path[1], sizeof(clif->local.sun_path) - 1, + "%s/%d", LLDP_CLIF_SOCK, getpid()); if (bind(clif->s, (struct sockaddr *) &clif->local, - sizeof(clif->local)) < 0) { + sizeof(clif->local)) < 0) { close(clif->s); os_free(clif); return NULL; } - clif->dest.sun_family = AF_UNIX; - os_snprintf(clif->dest.sun_path, sizeof(clif->dest.sun_path), "%s", - clif_path); + clif->dest.sun_family = AF_LOCAL; + clif->dest.sun_path[0] = '\0'; + os_snprintf(&clif->dest.sun_path[1], sizeof(clif->dest.sun_path) - 1, + "%s", LLDP_CLIF_SOCK); + if (connect(clif->s, (struct sockaddr *) &clif->dest, - sizeof(clif->dest)) < 0) { + sizeof(clif->dest)) < 0) { close(clif->s); - unlink(clif->local.sun_path); os_free(clif); return NULL; } @@ -85,7 +87,6 @@ struct clif *clif_open(const char *clif_path) void clif_close(struct clif *clif) { - unlink(clif->local.sun_path); close(clif->s); os_free(clif); } diff --git a/ctrl_iface.c b/ctrl_iface.c index 6e7fe77..d88979e 100644 --- a/ctrl_iface.c +++ b/ctrl_iface.c @@ -379,32 +379,11 @@ static void ctrl_iface_receive(int sock, void *eloop_ctx, memset(reply, 0, reply_size); process_clif_cmd(clifd, &from, fromlen, buf, res, reply, &reply_len); + wpa_hexdump_ascii(MSG_DEBUG, "TX ctrl_iface", (u8 *) reply, reply_len); sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); free(reply); } - -static char *ctrl_iface_path(struct clif_data *clifd) -{ - char *buf; - size_t len; - - if (clifd->ctrl_interface == NULL) - return NULL; - - len = strlen(clifd->ctrl_interface) + strlen(clifd->iface) + - 2; - buf = malloc(len); - if (buf == NULL) - return NULL; - - snprintf(buf, len, "%s/%s", - clifd->ctrl_interface, clifd->iface); - buf[len - 1] = '\0'; - return buf; -} - - int ctrl_iface_register(struct clif_data *clifd) { return eloop_register_read_sock(clifd->ctrl_sock, ctrl_iface_receive, @@ -415,76 +394,26 @@ int ctrl_iface_init(struct clif_data *clifd) { struct sockaddr_un addr; int s = -1; - char *fname = NULL; - int retry; clifd->ctrl_sock = -1; clifd->ctrl_dst = NULL; - if (clifd->ctrl_interface == NULL) - return 0; - - if (mkdir(clifd->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { - if (errno == EEXIST) { - wpa_printf(MSG_DEBUG, "Using existing control " - "interface directory."); - } else { - perror("mkdir[ctrl_interface]"); - goto fail; - } - } - - if (clifd->ctrl_interface_gid_set && - chown(clifd->ctrl_interface, 0, - clifd->ctrl_interface_gid) < 0) { - perror("chown[ctrl_interface]"); - return -1; - } - - if (strlen(clifd->ctrl_interface) + 1 + strlen(clifd->iface) - >= sizeof(addr.sun_path)) - goto fail; - - s = socket(PF_UNIX, SOCK_DGRAM, 0); + s = socket(AF_LOCAL, SOCK_DGRAM, 0); if (s < 0) { - perror("socket(PF_UNIX)"); + perror("socket(AF_LOCAL)"); goto fail; } memset(&addr, 0, sizeof(addr)); - addr.sun_family = AF_UNIX; - fname = ctrl_iface_path(clifd); - if (fname == NULL) - goto fail; - - strncpy(addr.sun_path, fname, sizeof(addr.sun_path)); - for (retry = 0; retry < 2; retry++) { - if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - if (errno == EADDRINUSE) { - unlink(fname); - } - } - else { - break; - } - } - if (retry == 2) { - perror("bind(PF_UNIX)"); + addr.sun_family = AF_LOCAL; + snprintf(&addr.sun_path[1], sizeof(addr.sun_path) - 1, + "%s", LLDP_CLIF_SOCK); + if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + perror("bind(AF_LOCAL)"); goto fail; } - if (clifd->ctrl_interface_gid_set && - chown(fname, 0, clifd->ctrl_interface_gid) < 0) { - perror("chown[ctrl_interface/ifname]"); - goto fail; - } - - if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { - perror("chmod[ctrl_interface/ifname]"); - goto fail; - } - free(fname); - + wpa_printf(MSG_DEBUG, "bound ctrl iface to %s", &addr.sun_path[1]); clifd->ctrl_sock = s; return 0; @@ -492,10 +421,6 @@ int ctrl_iface_init(struct clif_data *clifd) fail: if (s >= 0) close(s); - if (fname) { - unlink(fname); - free(fname); - } return -1; } @@ -508,17 +433,6 @@ void ctrl_iface_deinit(struct clif_data *clifd) eloop_unregister_read_sock(clifd->ctrl_sock); close(clifd->ctrl_sock); clifd->ctrl_sock = -1; - - if (clifd->ctrl_interface && - rmdir(clifd->ctrl_interface) < 0) { - if (errno == ENOTEMPTY) { - wpa_printf(MSG_DEBUG, "Control interface " - "directory not empty - leaving it " - "behind"); - } else { - perror("rmdir[ctrl_interface]"); - } - } } dst = clifd->ctrl_dst; diff --git a/dcbtool.c b/dcbtool.c index 2bf240d..8648736 100644 --- a/dcbtool.c +++ b/dcbtool.c @@ -181,8 +181,6 @@ static const char *commands_help = static struct clif *clif_conn; static int cli_quit = 0; static int cli_attached = 0; -static const char *clif_iface_dir = CLIF_IFACE_DIR; -static char *clif_ifname = NULL; static void usage(void) @@ -245,26 +243,6 @@ int parse_print_message(char *msg, int print) return status; } -static struct clif *cli_open_connection(const char *ifname) -{ - char *cfile; - int flen; - - if (ifname == NULL) - return NULL; - - flen = strlen(clif_iface_dir) + strlen(ifname) + 2; - cfile = malloc(flen); - if (cfile == NULL) - return NULL; - snprintf(cfile, flen, "%s/%s", clif_iface_dir, ifname); - - clif_conn = clif_open(cfile); - free(cfile); - return clif_conn; -} - - static void cli_close_connection(void) { if (clif_conn == NULL) @@ -354,56 +332,6 @@ static int cli_cmd_quit(struct clif *clif, int argc, char *argv[], int raw) } -static void cli_list_interfaces(struct clif *clif) -{ - struct dirent *dent; - DIR *dir; - - dir = opendir(clif_iface_dir); - if (dir == NULL) { - printf("Control interface directory '%s' could not be " - "opened.\n", clif_iface_dir); - return; - } - - printf("Available interfaces:\n"); - while ((dent = readdir(dir))) { - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - printf("%s\n", dent->d_name); - } - closedir(dir); -} - - -static int cli_cmd_interface(struct clif *clif, int argc, - char *argv[], int raw) -{ - if (argc < 1) { - cli_list_interfaces(clif); - return 0; - } - - cli_close_connection(); - free(clif_ifname); - clif_ifname = strdup(argv[0]); - - if (cli_open_connection(clif_ifname)) { - printf("Connected to interface '%s.\n", clif_ifname); - if (clif_attach(clif_conn, NULL) == 0) - cli_attached = 1; - else - printf("Warning: Failed to attach to lldpad.\n"); - } else { - printf( - "Could not connect to lldpad interface '%s' - re-trying\n", - clif_ifname); - } - return 0; -} - - struct cli_cmd { const char *cmd; int (*handler)(struct clif *clif, int argc, char *argv[], int raw); @@ -412,7 +340,6 @@ struct cli_cmd { static struct cli_cmd cli_commands[] = { { "ping", cli_cmd_ping }, { "help", cli_cmd_help }, - { "interface", cli_cmd_interface }, { "license", cli_cmd_license }, { "quit", cli_cmd_quit }, { NULL, NULL } @@ -539,7 +466,7 @@ static void cli_alarm(int sig) cli_close_connection(); } if (!clif_conn) { - clif_conn = cli_open_connection(clif_ifname); + clif_conn = clif_open(); if (clif_conn) { printf("Connection to lldpad re-established\n"); if (clif_attach(clif_conn, NULL) == 0) @@ -603,21 +530,7 @@ int main(int argc, char *argv[]) } for (;;) { - if (clif_ifname == NULL) { - struct dirent *dent; - DIR *dir = opendir(clif_iface_dir); - if (dir) { - while ((dent = readdir(dir))) { - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - clif_ifname = strdup(dent->d_name); - break; - } - closedir(dir); - } - } - clif_conn = cli_open_connection(clif_ifname); + clif_conn = clif_open(); if (clif_conn) { if (warning_displayed) printf("Connection established.\n"); @@ -650,7 +563,6 @@ int main(int argc, char *argv[]) } else ret = request(clif_conn, argc - optind, &argv[optind], raw); - free(clif_ifname); cli_close_connection(); return ret; } diff --git a/include/clif.h b/include/clif.h index 8c34989..3d3f308 100644 --- a/include/clif.h +++ b/include/clif.h @@ -34,10 +34,7 @@ #define CLIF_H #include - -#define CLIF_IFACE_DIR "/var/run/lldpad" -#define CLIF_IFACE_IFNAME "clif" -#define LLDP_CLIF_SOCK CLIF_IFACE_DIR "/" CLIF_IFACE_IFNAME +#include "clif_sock.h" #ifdef __cplusplus extern "C" { @@ -62,15 +59,11 @@ struct clif { /** * clif_open - Open a client interface to the lldpad - * @clif_path: Path for UNIX domain sockets; ignored if UDP sockets are used. * Returns: Pointer to abstract client interface data or %NULL on failure * * This function is used to open a client interface to the lldpad. - * clif_path is usually /var/run/lldpad. This path - * is configured in lldpad and other programs using the client - * interface need to use matching path configuration. */ -struct clif *clif_open(const char *clif_path); +struct clif *clif_open(); /** diff --git a/include/clif_msgs.h b/include/clif_msgs.h index 132492c..6448938 100644 --- a/include/clif_msgs.h +++ b/include/clif_msgs.h @@ -30,6 +30,7 @@ #include "dcb_types.h" #include "lldpad.h" +#include "clif_sock.h" /* Client interface versions */ /* Version 2 diff --git a/include/clif_sock.h b/include/clif_sock.h new file mode 100644 index 0000000..a425fe9 --- /dev/null +++ b/include/clif_sock.h @@ -0,0 +1,33 @@ +/******************************************************************************* + + LLDP Agent Daemon (LLDPAD) Software + Copyright(c) 2007-2010 Intel Corporation. + + This program is free software; you can redistribute it and/or modify it + under the terms and conditions of the GNU General Public License, + version 2, as published by the Free Software Foundation. + + This program is distributed in the hope it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + + The full GNU General Public License is included in this distribution in + the file called "COPYING". + + Contact Information: + e1000-eedc Mailing List + Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 + +*******************************************************************************/ + +#ifndef CLIF_SOCK_H +#define CLIF_SOCK_H + +#define LLDP_CLIF_SOCK "/com/intel/lldpad" + +#endif /* CLIF_SOCK_H */ diff --git a/include/ctrl_iface.h b/include/ctrl_iface.h index a735933..fadce50 100644 --- a/include/ctrl_iface.h +++ b/include/ctrl_iface.h @@ -39,10 +39,6 @@ struct ctrl_dst; struct clif_data { - char iface[IFNAMSIZ+1]; - char *ctrl_interface; - gid_t ctrl_interface_gid; - int ctrl_interface_gid_set; int ctrl_sock; struct ctrl_dst *ctrl_dst; }; diff --git a/include/lldp_mand_clif.h b/include/lldp_mand_clif.h index 350c340..3f2e734 100644 --- a/include/lldp_mand_clif.h +++ b/include/lldp_mand_clif.h @@ -45,7 +45,6 @@ typedef enum { cmd_version, cmd_help, cmd_ping, - cmd_if, cmd_nop, } lldp_cmd; diff --git a/lldpad.c b/lldpad.c index 9f65c6a..b8fd3fd 100644 --- a/lldpad.c +++ b/lldpad.c @@ -51,7 +51,6 @@ #include "lldp_8023.h" #include "config.h" #include "lldpad_shm.h" -#include "clif.h" #include "lldp/agent.h" /* @@ -289,11 +288,6 @@ int main(int argc, char *argv[]) exit(1); } - clifd->ctrl_interface = (char *) CLIF_IFACE_DIR; - strcpy(clifd->iface, CLIF_IFACE_IFNAME); - clifd->ctrl_interface_gid_set = 0; - clifd->ctrl_interface_gid = 0; - if (eloop_init(clifd)) { fprintf(stderr, "failed to initialize event loop\n"); log_message(MSG_ERR_SERVICE_START_FAILURE, diff --git a/lldptool.c b/lldptool.c index a5e24d3..74f8f54 100644 --- a/lldptool.c +++ b/lldptool.c @@ -144,8 +144,6 @@ static const char *commands_help = static struct clif *clif_conn; static int cli_quit = 0; static int cli_attached = 0; -static const char *clif_iface_dir = CLIF_IFACE_DIR; -static char *clif_ifname = NULL; /* * insert to head, so first one is last @@ -256,26 +254,6 @@ int parse_print_message(char *msg, int print) return status; } -static struct clif *cli_open_connection(const char *ifname) -{ - char *cfile; - int flen; - - if (ifname == NULL) - return NULL; - - flen = strlen(clif_iface_dir) + strlen(ifname) + 2; - cfile = malloc(flen); - if (cfile == NULL) - return NULL; - snprintf(cfile, flen, "%s/%s", clif_iface_dir, ifname); - - clif_conn = clif_open(cfile); - free(cfile); - return clif_conn; -} - - static void cli_close_connection(void) { if (clif_conn == NULL) @@ -387,60 +365,6 @@ static int cli_cmd_quit(struct clif *clif, int argc, char *argv[], return 0; } - -static void cli_list_interfaces(struct clif *clif) -{ - struct dirent *dent; - DIR *dir; - - dir = opendir(clif_iface_dir); - if (dir == NULL) { - printf("Control interface directory '%s' could not be " - "opened.\n", clif_iface_dir); - return; - } - - printf("Available interfaces:\n"); - while ((dent = readdir(dir))) { - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - printf("%s\n", dent->d_name); - } - closedir(dir); -} - - -static int cli_cmd_interface(struct clif *clif, int argc, char *argv[], - struct cmd *command, int raw) -{ - char attach_str[9] = ""; - u32 mod_id = LLDP_MOD_MAND; - - if (argc < 1) { - cli_list_interfaces(clif); - return 0; - } - - bin2hexstr((u8*)&mod_id, 4, attach_str, 8); - cli_close_connection(); - free(clif_ifname); - clif_ifname = strdup(argv[0]); - - if (cli_open_connection(clif_ifname)) { - printf("Connected to interface '%s.\n", clif_ifname); - if (clif_attach(clif_conn, attach_str) == 0) - cli_attached = 1; - else - printf("Warning: Failed to attach to lldpad.\n"); - } else { - printf("Could not connect to interface '%s' - re-trying\n", - clif_ifname); - } - return 0; -} - - struct cli_cmd { lldp_cmd cmdcode; const char *cmdstr; @@ -451,7 +375,6 @@ struct cli_cmd { static struct cli_cmd cli_commands[] = { { cmd_ping, "ping", cli_cmd_ping }, { cmd_help, "help", cli_cmd_help }, - { cmd_if, "interface", cli_cmd_interface }, { cmd_license, "license", cli_cmd_license }, { cmd_version, "version", cli_cmd_version }, { cmd_quit, "quit", cli_cmd_quit }, @@ -719,7 +642,7 @@ static void cli_alarm(int sig) cli_close_connection(); } if (!clif_conn) { - clif_conn = cli_open_connection(clif_ifname); + clif_conn = clif_open(); if (clif_conn) { char attach_str[9] = ""; u32 mod_id = LLDP_MOD_MAND; @@ -750,21 +673,7 @@ int main(int argc, char *argv[]) printf("%s\n\n%s\n\n", cli_version, cli_license); for (;;) { - if (clif_ifname == NULL) { - struct dirent *dent; - DIR *dir = opendir(clif_iface_dir); - if (dir) { - while ((dent = readdir(dir))) { - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - clif_ifname = strdup(dent->d_name); - break; - } - closedir(dir); - } - } - clif_conn = cli_open_connection(clif_ifname); + clif_conn = clif_open(); if (clif_conn) { if (warning_displayed) printf("Connection established.\n"); @@ -804,7 +713,6 @@ int main(int argc, char *argv[]) ret = !!ret; } - free(clif_ifname); cli_close_connection(); deinit_modules(); -- 1.7.1