abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit e4a6b01fdc29cc6598d097e543f7462d448b06cb
parent d1a44a9c1aa56cc13def314f532fe76f82124219
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 16 Feb 2015 23:28:46 +0100

Make error handling more robust

Diffstat:
abduco.c | 6+++---
server.c | 17++++++++++-------
2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/abduco.c b/abduco.c @@ -316,9 +316,9 @@ static bool create_session(const char *name, char * const argv[]) { case 0: /* child = user application process */ close(server.socket); close(server_pipe[0]); - fcntl(client_pipe[1], F_SETFD, FD_CLOEXEC); - fcntl(server_pipe[1], F_SETFD, FD_CLOEXEC); - execvp(argv[0], argv); + if (fcntl(client_pipe[1], F_SETFD, FD_CLOEXEC) == 0 && + fcntl(server_pipe[1], F_SETFD, FD_CLOEXEC) == 0) + execvp(argv[0], argv); snprintf(errormsg, sizeof(errormsg), "server-execvp: %s: %s\n", argv[0], strerror(errno)); write_all(client_pipe[1], errormsg, strlen(errormsg)); diff --git a/server.c b/server.c @@ -18,17 +18,17 @@ static void client_free(Client *c) { free(c); } -static int server_mark_socket_exec(bool exec, bool usr) { +static void server_mark_socket_exec(bool exec, bool usr) { struct stat sb; if (stat(sockaddr.sun_path, &sb) == -1) - return -1; + return; mode_t mode = sb.st_mode; mode_t flag = usr ? S_IXUSR : S_IXGRP; if (exec) mode |= flag; else mode &= ~flag; - return chmod(sockaddr.sun_path, mode); + chmod(sockaddr.sun_path, mode); } static int server_create_socket(const char *name) { @@ -63,20 +63,23 @@ static int server_set_socket_non_blocking(int sock) { static Client *server_accept_client(void) { int newfd = accept(server.socket, NULL, NULL); - if (newfd == -1) - return NULL; + if (newfd == -1 || server_set_socket_non_blocking(newfd) == -1) + goto error; Client *c = client_malloc(newfd); if (!c) - return NULL; + goto error; if (!server.clients) server_mark_socket_exec(true, true); - server_set_socket_non_blocking(newfd); c->socket = newfd; c->state = STATE_CONNECTED; c->next = server.clients; server.clients = c; server.read_pty = true; return c; +error: + if (newfd != -1) + close(newfd); + return NULL; } static bool server_read_pty(Packet *pkt) {