From 03563b1407e436b2863509ebf09d412e79cbd1dd Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 18 Jan 2022 11:27:54 -0500 Subject: util: New close() wrappers to check for EBADF and preserve errno --- spawn.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'spawn.c') diff --git a/spawn.c b/spawn.c index d4ff4fd..31f9897 100644 --- a/spawn.c +++ b/spawn.c @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2018-2019 Tavian Barnes * + * Copyright (C) 2018-2022 Tavian Barnes * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -146,7 +146,7 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char ** int error; const struct bfs_spawn_action *actions = ctx ? ctx->actions : NULL; - close(pipefd[0]); + xclose(pipefd[0]); for (const struct bfs_spawn_action *action = actions; action; action = action->next) { // Move the error-reporting pipe out of the way if necessary... @@ -155,7 +155,7 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char ** if (fd < 0) { goto fail; } - close(pipefd[1]); + xclose(pipefd[1]); pipefd[1] = fd; } @@ -198,7 +198,7 @@ fail: // unsuccessfully, but won't know why (void) xwrite(pipefd[1], &error, sizeof(error)); - close(pipefd[1]); + xclose(pipefd[1]); _Exit(127); } @@ -224,15 +224,11 @@ pid_t bfs_spawn(const char *exe, const struct bfs_spawn *ctx, char **argv, char return -1; } - int error; pid_t pid = fork(); - if (pid < 0) { - error = errno; - close(pipefd[1]); - close(pipefd[0]); + close_quietly(pipefd[1]); + close_quietly(pipefd[0]); free(resolved); - errno = error; return -1; } else if (pid == 0) { // Child @@ -240,11 +236,12 @@ pid_t bfs_spawn(const char *exe, const struct bfs_spawn *ctx, char **argv, char } // Parent - close(pipefd[1]); + xclose(pipefd[1]); free(resolved); + int error; ssize_t nbytes = xread(pipefd[0], &error, sizeof(error)); - close(pipefd[0]); + xclose(pipefd[0]); if (nbytes == sizeof(error)) { int wstatus; waitpid(pid, &wstatus, 0); -- cgit v1.2.3