summaryrefslogtreecommitdiffstats
path: root/spawn.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2022-01-18 11:27:54 -0500
committerTavian Barnes <tavianator@tavianator.com>2022-01-18 12:27:29 -0500
commit03563b1407e436b2863509ebf09d412e79cbd1dd (patch)
treed2e5b735c8be3078b7b76c31c9168330a7f2557b /spawn.c
parentabbb00766a8d10f63bbafb60bb13eb4672d7f44a (diff)
downloadbfs-03563b1407e436b2863509ebf09d412e79cbd1dd.tar.xz
util: New close() wrappers to check for EBADF and preserve errno
Diffstat (limited to 'spawn.c')
-rw-r--r--spawn.c21
1 files changed, 9 insertions, 12 deletions
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 <tavianator@tavianator.com> *
+ * Copyright (C) 2018-2022 Tavian Barnes <tavianator@tavianator.com> *
* *
* 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);