From 3a84e45a3043a0025ecfd5f1a496fafac95fd01f Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 6 Aug 2014 11:21:59 +0200 Subject: [PATCH] server: fix error handling when adding socket When some function during adding socket fails, it must clean everything it set or we can get funky errors. This patch fixes: http://lists.freedesktop.org/archives/wayland-devel/2014-August/016331.html Signed-off-by: Marek Chalupa Reviewed-by: Pekka Paalanen --- src/wayland-server.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/wayland-server.c b/src/wayland-server.c index d646e3e4..3c162d45 100644 --- a/src/wayland-server.c +++ b/src/wayland-server.c @@ -1035,24 +1035,20 @@ wl_socket_lock(struct wl_socket *socket) if (socket->fd_lock < 0) { wl_log("unable to open lockfile %s check permissions\n", socket->lock_addr); - return -1; + goto err; } if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) { wl_log("unable to lock lockfile %s, maybe another compositor is running\n", socket->lock_addr); - close(socket->fd_lock); - socket->fd_lock = -1; - return -1; + goto err_fd; } if (stat(socket->addr.sun_path, &socket_stat) < 0 ) { if (errno != ENOENT) { wl_log("did not manage to stat file %s\n", socket->addr.sun_path); - close(socket->fd_lock); - socket->fd_lock = -1; - return -1; + goto err_fd; } } else if (socket_stat.st_mode & S_IWUSR || socket_stat.st_mode & S_IWGRP) { @@ -1060,6 +1056,18 @@ wl_socket_lock(struct wl_socket *socket) } return 0; +err_fd: + close(socket->fd_lock); + socket->fd_lock = -1; +err: + *socket->lock_addr = 0; + /* we did not set this value here, but without lock the + * socket won't be created anyway. This prevents the + * wl_socket_destroy from unlinking already existing socket + * created by other compositor */ + *socket->addr.sun_path = 0; + + return -1; } static int @@ -1088,6 +1096,7 @@ wl_socket_init_for_display_name(struct wl_socket *s, const char *name) if (name_size > (int)sizeof s->addr.sun_path) { wl_log("error: socket path \"%s/%s\" plus null terminator" " exceeds 108 bytes\n", runtime_dir, name); + *s->addr.sun_path = 0; /* to prevent programs reporting * "failed to add socket: Success" */ errno = ENAMETOOLONG;