ipc: correctly shutdown IPC sockets when exiting/restarting

This commit is contained in:
Michael Stapelberg 2010-03-16 02:44:47 +01:00
parent fca826a6f9
commit f7a1a9fb20
5 changed files with 26 additions and 0 deletions

View File

@ -73,6 +73,8 @@ static void ipc_recv_message(int sockfd, uint32_t message_type,
int n = read(sockfd, msg + read_bytes, to_read); int n = read(sockfd, msg + read_bytes, to_read);
if (n == -1) if (n == -1)
err(EXIT_FAILURE, "read() failed"); err(EXIT_FAILURE, "read() failed");
if (n == 0)
errx(EXIT_FAILURE, "received EOF instead of reply");
read_bytes += n; read_bytes += n;
to_read -= n; to_read -= n;

View File

@ -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); 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 #endif

View File

@ -33,6 +33,7 @@
#include "log.h" #include "log.h"
#include "sighandler.h" #include "sighandler.h"
#include "manage.h" #include "manage.h"
#include "ipc.h"
bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) { bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
/* If this container is empty, were done */ /* If this container is empty, were done */
@ -1015,6 +1016,7 @@ void parse_command(xcb_connection_t *conn, const char *command) {
if (STARTS_WITH(command, "exit")) { if (STARTS_WITH(command, "exit")) {
LOG("User issued exit-command, exiting without error.\n"); LOG("User issued exit-command, exiting without error.\n");
restore_geometry(global_conn); restore_geometry(global_conn);
ipc_shutdown();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -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 * Executes the command and returns whether it could be successfully parsed
* or not (at the moment, always returns true). * or not (at the moment, always returns true).

View File

@ -35,6 +35,7 @@
#include "ewmh.h" #include "ewmh.h"
#include "manage.h" #include "manage.h"
#include "workspace.h" #include "workspace.h"
#include "ipc.h"
static iconv_t conversion_descriptor = 0; static iconv_t conversion_descriptor = 0;
struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent); 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() { void i3_restart() {
restore_geometry(global_conn); restore_geometry(global_conn);
ipc_shutdown();
LOG("restarting \"%s\"...\n", start_argv[0]); LOG("restarting \"%s\"...\n", start_argv[0]);
/* make sure -a is in the argument list or append it */ /* make sure -a is in the argument list or append it */
start_argv = append_argument(start_argv, "-a"); start_argv = append_argument(start_argv, "-a");