diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-03-29 12:15:33 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-03-29 12:26:43 -0400 |
commit | 116ab1ed2b4230aea1ab634618af3162cabbb37f (patch) | |
tree | 9fbc436568e804faaa8885c181e59a303feb06ab | |
parent | ba2d4ac206ff1321ea953d3305d8bda048922983 (diff) | |
download | bfs-116ab1ed2b4230aea1ab634618af3162cabbb37f.tar.xz |
xspawn: Use list.h for the action list
-rw-r--r-- | src/xspawn.c | 36 | ||||
-rw-r--r-- | src/xspawn.h | 4 |
2 files changed, 19 insertions, 21 deletions
diff --git a/src/xspawn.c b/src/xspawn.c index a30c264..e6ce0de 100644 --- a/src/xspawn.c +++ b/src/xspawn.c @@ -4,6 +4,7 @@ #include "xspawn.h" #include "bfstd.h" #include "config.h" +#include "list.h" #include <errno.h> #include <fcntl.h> #include <stdbool.h> @@ -32,7 +33,7 @@ enum bfs_spawn_op { * A spawn action. */ struct bfs_spawn_action { - struct bfs_spawn_action *next; + struct slink link; enum bfs_spawn_op op; int in_fd; @@ -43,18 +44,15 @@ struct bfs_spawn_action { int bfs_spawn_init(struct bfs_spawn *ctx) { ctx->flags = 0; - ctx->actions = NULL; - ctx->tail = &ctx->actions; + slist_init(&ctx->actions); return 0; } int bfs_spawn_destroy(struct bfs_spawn *ctx) { - struct bfs_spawn_action *action = ctx->actions; - while (action) { - struct bfs_spawn_action *next = action->next; + LIST_DRAIN(&ctx->actions, struct bfs_spawn_action, action) { free(action); - action = next; } + return 0; } @@ -66,15 +64,16 @@ int bfs_spawn_setflags(struct bfs_spawn *ctx, enum bfs_spawn_flags flags) { /** Add a spawn action to the chain. */ static struct bfs_spawn_action *bfs_spawn_add(struct bfs_spawn *ctx, enum bfs_spawn_op op) { struct bfs_spawn_action *action = malloc(sizeof(*action)); - if (action) { - action->next = NULL; - action->op = op; - action->in_fd = -1; - action->out_fd = -1; - - *ctx->tail = action; - ctx->tail = &action->next; + if (!action) { + return NULL; } + + slink_init(&action->link); + action->op = op; + action->in_fd = -1; + action->out_fd = -1; + + slist_append(&ctx->actions, &action->link); return action; } @@ -137,12 +136,10 @@ int bfs_spawn_addsetrlimit(struct bfs_spawn *ctx, int resource, const struct rli /** Actually exec() the new process. */ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char **argv, char **envp, int pipefd[2]) { - int error; - const struct bfs_spawn_action *actions = ctx ? ctx->actions : NULL; - xclose(pipefd[0]); - for (const struct bfs_spawn_action *action = actions; action; action = action->next) { + const struct slink *head = ctx ? ctx->actions.head : NULL; + LIST_FOR_EACH_FROM(head, struct bfs_spawn_action, action) { // Move the error-reporting pipe out of the way if necessary... if (action->out_fd == pipefd[1]) { int fd = dup_cloexec(pipefd[1]); @@ -185,6 +182,7 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char ** execve(exe, argv, envp); + int error; fail: error = errno; diff --git a/src/xspawn.h b/src/xspawn.h index 3dbf5d2..7e673f1 100644 --- a/src/xspawn.h +++ b/src/xspawn.h @@ -8,6 +8,7 @@ #ifndef BFS_XSPAWN_H #define BFS_XSPAWN_H +#include "list.h" #include <sys/resource.h> #include <sys/types.h> @@ -24,8 +25,7 @@ enum bfs_spawn_flags { */ struct bfs_spawn { enum bfs_spawn_flags flags; - struct bfs_spawn_action *actions; - struct bfs_spawn_action **tail; + struct slist actions; }; /** |