Message ID | 20231023022529.15081-3-felix.moessbauer@siemens.com |
---|---|
State | Changes Requested |
Headers | show |
Series | Rework socket creation to better integrate with systemd | expand |
On 23.10.23 04:25, 'Felix Moessbauer' via swupdate wrote: > The listener_create which previously was implemented in network_thread.c > was shared across multiple components. We now move that function to the > new network_utils.c file. By that, creation and cleanup happen in the > same unit. > > Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com> > --- > core/network_thread.c | 61 ----------------------------------- > core/network_utils.c | 64 +++++++++++++++++++++++++++++++++++++ > core/progress_thread.c | 2 +- > include/network_interface.h | 1 - > include/network_utils.h | 9 ++++++ > 5 files changed, 74 insertions(+), 63 deletions(-) > > diff --git a/core/network_thread.c b/core/network_thread.c > index 936b8cf..ca23908 100644 > --- a/core/network_thread.c > +++ b/core/network_thread.c > @@ -36,12 +36,6 @@ > #include "state.h" > #include "swupdate_vars.h" > > -#ifdef CONFIG_SYSTEMD > -#include <systemd/sd-daemon.h> > -#endif > - > -#define LISTENQ 1024 > - > #define NUM_CACHED_MESSAGES 100 > #define DEFAULT_INTERNAL_TIMEOUT 60 > > @@ -222,61 +216,6 @@ static void network_notifier(RECOVERY_STATUS status, int error, int level, const > pthread_mutex_unlock(&msglock); > } > > -int listener_create(const char *path, int type) > -{ > - struct sockaddr_un servaddr; > - int listenfd = -1; > - > -#ifdef CONFIG_SYSTEMD > - if (sd_booted()) { > - for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_listen_fds(0); fd++) { > - if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path, 0)) { > - listenfd = fd; > - break; > - } > - } > - if (listenfd == -1) { > - TRACE("got no socket at %s from systemd", path); > - } else { > - TRACE("got socket fd=%d at %s from systemd", listenfd, path); > - } > - } > -#endif > - > - if (listenfd == -1) { > - TRACE("creating socket at %s", path); > - listenfd = socket(AF_LOCAL, type, 0); > - if (listenfd < 0) { > - return -1; > - } > - unlink(path); > - bzero(&servaddr, sizeof(servaddr)); > - servaddr.sun_family = AF_LOCAL; > - strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path) - 1); > - if(register_socket_unlink(path) != 0){ > - ERROR("Out of memory, skipping..."); > - return -1; > - } > - if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { > - close(listenfd); > - return -1; > - } > - > - if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) > - WARN("chmod cannot be set on socket, error %s", strerror(errno)); > - } > - > - if (fcntl(listenfd, F_SETFD, FD_CLOEXEC) < 0) > - WARN("Could not set %d as cloexec: %s", listenfd, strerror(errno)); > - > - if (type == SOCK_STREAM) > - if (listen(listenfd, LISTENQ) < 0) { > - close(listenfd); > - return -1; > - } > - return listenfd; > -} > - > static void cleanum_msg_list(void) > { > struct msg_elem *notification; > diff --git a/core/network_utils.c b/core/network_utils.c > index fd539c1..350c7f6 100644 > --- a/core/network_utils.c > +++ b/core/network_utils.c > @@ -9,11 +9,21 @@ > #include <stdlib.h> > #include <string.h> > #include <unistd.h> > +#include <errno.h> > +#include <sys/un.h> > +#include <sys/stat.h> > +#include <sys/socket.h> > + > +#ifdef CONFIG_SYSTEMD > +#include <systemd/sd-daemon.h> > +#endif > > #include "bsdqueue.h" > #include "network_utils.h" > #include "util.h" > > +#define LISTENQ 1024 > + > struct socket_meta { > char *path; > SIMPLEQ_ENTRY(socket_meta) next; > @@ -23,6 +33,60 @@ static pthread_mutex_t sockets_toclose_lock = PTHREAD_MUTEX_INITIALIZER; > SIMPLEQ_HEAD(self_sockets, socket_meta); > static struct self_sockets sockets_toclose; > > + > +int listener_create(const char *path, int type) > +{ > + struct sockaddr_un servaddr; > + int listenfd = -1; > + > +#ifdef CONFIG_SYSTEMD > + for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_listen_fds(0); fd++) { > + if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path, 0)) { > + listenfd = fd; > + break; > + } > + } > + if (listenfd == -1) { > + TRACE("got no socket at %s from systemd", path); > + } else { > + TRACE("got socket fd=%d at %s from systemd", listenfd, path); > + } > +#endif > + > + if (listenfd == -1) { > + TRACE("creating socket at %s", path); > + listenfd = socket(AF_LOCAL, type, 0); > + if (listenfd < 0) { > + return -1; > + } > + unlink(path); > + bzero(&servaddr, sizeof(servaddr)); > + servaddr.sun_family = AF_LOCAL; > + strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path) - 1); > + if(register_socket_unlink(path) != 0){ > + ERROR("Out of memory, skipping..."); > + return -1; > + } > + if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { > + close(listenfd); > + return -1; > + } > + > + if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) > + WARN("chmod cannot be set on socket, error %s", strerror(errno)); > + } > + > + if (fcntl(listenfd, F_SETFD, FD_CLOEXEC) < 0) > + WARN("Could not set %d as cloexec: %s", listenfd, strerror(errno)); > + > + if (type == SOCK_STREAM) > + if (listen(listenfd, LISTENQ) < 0) { > + close(listenfd); > + return -1; > + } > + return listenfd; > +} > + > int register_socket_unlink(const char* path){ > struct socket_meta *socketm = malloc(sizeof(*socketm)); > if(!socketm){ > diff --git a/core/progress_thread.c b/core/progress_thread.c > index baa3899..1a27ca5 100644 > --- a/core/progress_thread.c > +++ b/core/progress_thread.c > @@ -28,7 +28,7 @@ > #include "util.h" > #include "pctl.h" > #include "network_ipc.h" > -#include "network_interface.h" > +#include "network_utils.h" > #include <progress.h> > #include "generated/autoconf.h" > > diff --git a/include/network_interface.h b/include/network_interface.h > index a636d99..0a60cad 100644 > --- a/include/network_interface.h > +++ b/include/network_interface.h > @@ -8,7 +8,6 @@ > #pragma once > void *network_initializer(void *data); > void *network_thread(void *data); > -int listener_create(const char *path, int type); > > extern bool stream_wkup; > extern pthread_mutex_t stream_mutex; > diff --git a/include/network_utils.h b/include/network_utils.h > index 3bd198b..bbb1d17 100644 > --- a/include/network_utils.h > +++ b/include/network_utils.h > @@ -6,6 +6,15 @@ > */ > #pragma once > > +/** > + * \brief create or attach to socket at path > + * > + * \param path absolute path to socket file > + * \param type socket type of socket() > + * \return fd on success, -1 on error > +*/ > +int listener_create(const char *path, int type); > + > /** > * \brief initialize unlink functionality for sockets > * It makes sense. Acked-by: Stefano Babic <stefano.babic@swupdate.org> Best regards, Stefano Babic
diff --git a/core/network_thread.c b/core/network_thread.c index 936b8cf..ca23908 100644 --- a/core/network_thread.c +++ b/core/network_thread.c @@ -36,12 +36,6 @@ #include "state.h" #include "swupdate_vars.h" -#ifdef CONFIG_SYSTEMD -#include <systemd/sd-daemon.h> -#endif - -#define LISTENQ 1024 - #define NUM_CACHED_MESSAGES 100 #define DEFAULT_INTERNAL_TIMEOUT 60 @@ -222,61 +216,6 @@ static void network_notifier(RECOVERY_STATUS status, int error, int level, const pthread_mutex_unlock(&msglock); } -int listener_create(const char *path, int type) -{ - struct sockaddr_un servaddr; - int listenfd = -1; - -#ifdef CONFIG_SYSTEMD - if (sd_booted()) { - for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_listen_fds(0); fd++) { - if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path, 0)) { - listenfd = fd; - break; - } - } - if (listenfd == -1) { - TRACE("got no socket at %s from systemd", path); - } else { - TRACE("got socket fd=%d at %s from systemd", listenfd, path); - } - } -#endif - - if (listenfd == -1) { - TRACE("creating socket at %s", path); - listenfd = socket(AF_LOCAL, type, 0); - if (listenfd < 0) { - return -1; - } - unlink(path); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sun_family = AF_LOCAL; - strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path) - 1); - if(register_socket_unlink(path) != 0){ - ERROR("Out of memory, skipping..."); - return -1; - } - if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { - close(listenfd); - return -1; - } - - if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) - WARN("chmod cannot be set on socket, error %s", strerror(errno)); - } - - if (fcntl(listenfd, F_SETFD, FD_CLOEXEC) < 0) - WARN("Could not set %d as cloexec: %s", listenfd, strerror(errno)); - - if (type == SOCK_STREAM) - if (listen(listenfd, LISTENQ) < 0) { - close(listenfd); - return -1; - } - return listenfd; -} - static void cleanum_msg_list(void) { struct msg_elem *notification; diff --git a/core/network_utils.c b/core/network_utils.c index fd539c1..350c7f6 100644 --- a/core/network_utils.c +++ b/core/network_utils.c @@ -9,11 +9,21 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <errno.h> +#include <sys/un.h> +#include <sys/stat.h> +#include <sys/socket.h> + +#ifdef CONFIG_SYSTEMD +#include <systemd/sd-daemon.h> +#endif #include "bsdqueue.h" #include "network_utils.h" #include "util.h" +#define LISTENQ 1024 + struct socket_meta { char *path; SIMPLEQ_ENTRY(socket_meta) next; @@ -23,6 +33,60 @@ static pthread_mutex_t sockets_toclose_lock = PTHREAD_MUTEX_INITIALIZER; SIMPLEQ_HEAD(self_sockets, socket_meta); static struct self_sockets sockets_toclose; + +int listener_create(const char *path, int type) +{ + struct sockaddr_un servaddr; + int listenfd = -1; + +#ifdef CONFIG_SYSTEMD + for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_listen_fds(0); fd++) { + if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path, 0)) { + listenfd = fd; + break; + } + } + if (listenfd == -1) { + TRACE("got no socket at %s from systemd", path); + } else { + TRACE("got socket fd=%d at %s from systemd", listenfd, path); + } +#endif + + if (listenfd == -1) { + TRACE("creating socket at %s", path); + listenfd = socket(AF_LOCAL, type, 0); + if (listenfd < 0) { + return -1; + } + unlink(path); + bzero(&servaddr, sizeof(servaddr)); + servaddr.sun_family = AF_LOCAL; + strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path) - 1); + if(register_socket_unlink(path) != 0){ + ERROR("Out of memory, skipping..."); + return -1; + } + if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { + close(listenfd); + return -1; + } + + if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) + WARN("chmod cannot be set on socket, error %s", strerror(errno)); + } + + if (fcntl(listenfd, F_SETFD, FD_CLOEXEC) < 0) + WARN("Could not set %d as cloexec: %s", listenfd, strerror(errno)); + + if (type == SOCK_STREAM) + if (listen(listenfd, LISTENQ) < 0) { + close(listenfd); + return -1; + } + return listenfd; +} + int register_socket_unlink(const char* path){ struct socket_meta *socketm = malloc(sizeof(*socketm)); if(!socketm){ diff --git a/core/progress_thread.c b/core/progress_thread.c index baa3899..1a27ca5 100644 --- a/core/progress_thread.c +++ b/core/progress_thread.c @@ -28,7 +28,7 @@ #include "util.h" #include "pctl.h" #include "network_ipc.h" -#include "network_interface.h" +#include "network_utils.h" #include <progress.h> #include "generated/autoconf.h" diff --git a/include/network_interface.h b/include/network_interface.h index a636d99..0a60cad 100644 --- a/include/network_interface.h +++ b/include/network_interface.h @@ -8,7 +8,6 @@ #pragma once void *network_initializer(void *data); void *network_thread(void *data); -int listener_create(const char *path, int type); extern bool stream_wkup; extern pthread_mutex_t stream_mutex; diff --git a/include/network_utils.h b/include/network_utils.h index 3bd198b..bbb1d17 100644 --- a/include/network_utils.h +++ b/include/network_utils.h @@ -6,6 +6,15 @@ */ #pragma once +/** + * \brief create or attach to socket at path + * + * \param path absolute path to socket file + * \param type socket type of socket() + * \return fd on success, -1 on error +*/ +int listener_create(const char *path, int type); + /** * \brief initialize unlink functionality for sockets *
The listener_create which previously was implemented in network_thread.c was shared across multiple components. We now move that function to the new network_utils.c file. By that, creation and cleanup happen in the same unit. Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com> --- core/network_thread.c | 61 ----------------------------------- core/network_utils.c | 64 +++++++++++++++++++++++++++++++++++++ core/progress_thread.c | 2 +- include/network_interface.h | 1 - include/network_utils.h | 9 ++++++ 5 files changed, 74 insertions(+), 63 deletions(-)