From f7a1a9fb20b793a32dacdc8ca319952be31ace60 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 16 Mar 2010 02:44:47 +0100 Subject: [PATCH] ipc: correctly shutdown IPC sockets when exiting/restarting --- i3-msg/main.c | 2 ++ include/ipc.h | 6 ++++++ src/commands.c | 2 ++ src/ipc.c | 13 +++++++++++++ src/util.c | 3 +++ 5 files changed, 26 insertions(+) diff --git a/i3-msg/main.c b/i3-msg/main.c index 193d04e9..a1bdd72e 100644 --- a/i3-msg/main.c +++ b/i3-msg/main.c @@ -73,6 +73,8 @@ static void ipc_recv_message(int sockfd, uint32_t message_type, int n = read(sockfd, msg + read_bytes, to_read); if (n == -1) err(EXIT_FAILURE, "read() failed"); + if (n == 0) + errx(EXIT_FAILURE, "received EOF instead of reply"); read_bytes += n; to_read -= n; diff --git a/include/ipc.h b/include/ipc.h index b798b5ff..63d59141 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -67,5 +67,11 @@ int ipc_create_socket(const char *filename); */ void ipc_send_event(const char *event, uint32_t message_type, const char *payload); +/** + * Calls shutdown() on each socket and closes it. This function to be called + * when exiting or restarting only! + * + */ +void ipc_shutdown(); #endif diff --git a/src/commands.c b/src/commands.c index bef0989f..273f9d39 100644 --- a/src/commands.c +++ b/src/commands.c @@ -33,6 +33,7 @@ #include "log.h" #include "sighandler.h" #include "manage.h" +#include "ipc.h" bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) { /* If this container is empty, we’re done */ @@ -1015,6 +1016,7 @@ void parse_command(xcb_connection_t *conn, const char *command) { if (STARTS_WITH(command, "exit")) { LOG("User issued exit-command, exiting without error.\n"); restore_geometry(global_conn); + ipc_shutdown(); exit(EXIT_SUCCESS); } diff --git a/src/ipc.c b/src/ipc.c index f74f437e..f2dfd1f6 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -105,6 +105,19 @@ void ipc_send_event(const char *event, uint32_t message_type, const char *payloa } } +/* + * Calls shutdown() on each socket and closes it. This function to be called + * when exiting or restarting only! + * + */ +void ipc_shutdown() { + ipc_client *current; + TAILQ_FOREACH(current, &all_clients, clients) { + shutdown(current->fd, SHUT_RDWR); + close(current->fd); + } +} + /* * Executes the command and returns whether it could be successfully parsed * or not (at the moment, always returns true). diff --git a/src/util.c b/src/util.c index dab3199b..cb37d30a 100644 --- a/src/util.c +++ b/src/util.c @@ -35,6 +35,7 @@ #include "ewmh.h" #include "manage.h" #include "workspace.h" +#include "ipc.h" static iconv_t conversion_descriptor = 0; struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent); @@ -513,6 +514,8 @@ static char **append_argument(char **original, char *argument) { void i3_restart() { restore_geometry(global_conn); + ipc_shutdown(); + LOG("restarting \"%s\"...\n", start_argv[0]); /* make sure -a is in the argument list or append it */ start_argv = append_argument(start_argv, "-a"); -- 2.39.5