@@ -21,6 +21,7 @@ obj-y += swupdate.o \
state.o \
syslog.o \
installer.o \
+ network_utils.o \
network_thread.o \
stream_interface.o \
progress_thread.o \
@@ -26,6 +26,7 @@
#include "util.h"
#include "network_ipc.h"
#include "network_interface.h"
+#include "network_utils.h"
#include "installer.h"
#include "installer_priv.h"
#include "swupdate.h"
@@ -252,7 +253,10 @@ int listener_create(const char *path, int type)
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;
@@ -311,20 +315,6 @@ static void empty_pipe(int fd)
} while (1);
}
-static void unlink_socket(void)
-{
-#ifdef CONFIG_SYSTEMD
- if (sd_booted()) {
- /*
- * There were socket fds handed-over by systemd,
- * so don't delete the socket file.
- */
- return;
- }
-#endif
- unlink(get_ctrl_socket());
-}
-
static void send_subprocess_reply(
const struct subprocess_msg_elem *const subprocess_msg)
{
@@ -473,11 +463,6 @@ void *network_thread (void *data)
exit(2);
}
- if (atexit(unlink_socket) != 0) {
- TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
- get_ctrl_socket());
- }
-
thread_ready();
do {
clilen = sizeof(cliaddr);
new file mode 100644
@@ -0,0 +1,60 @@
+/*
+ * (C) Copyright 2023
+ * Felix Moessbauer <felix.moessbauer@siemens.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "bsdqueue.h"
+#include "network_utils.h"
+#include "util.h"
+
+struct socket_meta {
+ char *path;
+ SIMPLEQ_ENTRY(socket_meta) next;
+};
+
+static pthread_mutex_t sockets_toclose_lock = PTHREAD_MUTEX_INITIALIZER;
+SIMPLEQ_HEAD(self_sockets, socket_meta);
+static struct self_sockets sockets_toclose;
+
+int register_socket_unlink(const char* path){
+ struct socket_meta *socketm = malloc(sizeof(*socketm));
+ if(!socketm){
+ return -1;
+ }
+ socketm->path = strdup(path);
+ if(!socketm->path){
+ free(socketm);
+ return -1;
+ }
+ pthread_mutex_lock(&sockets_toclose_lock);
+ SIMPLEQ_INSERT_TAIL(&sockets_toclose, socketm, next);
+ pthread_mutex_unlock(&sockets_toclose_lock);
+ return 0;
+}
+
+static void unlink_sockets(void)
+{
+ pthread_mutex_lock(&sockets_toclose_lock);
+ while(!SIMPLEQ_EMPTY(&sockets_toclose)) {
+ struct socket_meta *socketm;
+ socketm = SIMPLEQ_FIRST(&sockets_toclose);
+ TRACE("unlink socket %s", socketm->path);
+ unlink(socketm->path);
+ free(socketm->path);
+ SIMPLEQ_REMOVE_HEAD(&sockets_toclose, next);
+ free(socketm);
+ }
+ pthread_mutex_unlock(&sockets_toclose_lock);
+}
+
+int init_socket_unlink_handler(void){
+ SIMPLEQ_INIT(&sockets_toclose);
+ return atexit(unlink_sockets);
+}
@@ -252,20 +252,6 @@ void swupdate_progress_done(const char *info)
pthread_mutex_unlock(&pprog->lock);
}
-static void unlink_socket(void)
-{
-#ifdef CONFIG_SYSTEMD
- if (sd_booted()) {
- /*
- * There were socket fds handed-over by systemd,
- * so don't delete the socket file.
- */
- return;
- }
-#endif
- unlink(get_prog_socket());
-}
-
void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
{
int listen, connfd;
@@ -284,11 +270,6 @@ void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
exit(2);
}
- if (atexit(unlink_socket) != 0) {
- TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
- get_prog_socket());
- }
-
thread_ready();
do {
clilen = sizeof(cliaddr);
@@ -39,6 +39,7 @@
#include "mongoose_interface.h"
#include "download_interface.h"
#include "network_ipc.h"
+#include "network_utils.h"
#include "sslapi.h"
#include "suricatta/suricatta.h"
#include "delta_process.h"
@@ -885,6 +886,9 @@ int main(int argc, char **argv)
* Start daemon if just a check is required
* SWUpdate will exit after the check
*/
+ if(init_socket_unlink_handler() != 0){
+ TRACE("Cannot setup socket cleanup on exit, sockets won't be unlinked.");
+ }
network_daemon = start_thread(network_initializer, &swcfg);
start_thread(progress_bar_thread, NULL);
new file mode 100644
@@ -0,0 +1,23 @@
+/*
+ * (C) Copyright 2023
+ * Felix Moessbauer <felix.moessbauer@siemens.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+#pragma once
+
+/**
+ * \brief initialize unlink functionality for sockets
+ *
+ * Call this function before \c register_socket_unlink
+ * \return 0 on success
+ */
+int init_socket_unlink_handler(void);
+
+/**
+ * \brief unlink socket path on exit
+ *
+ * \note threadsafe
+ * \return 0 on success
+ */
+int register_socket_unlink(const char* path);