From 116ab1ed2b4230aea1ab634618af3162cabbb37f Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 29 Mar 2023 12:15:33 -0400 Subject: xspawn: Use list.h for the action list --- src/xspawn.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'src/xspawn.c') 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 #include #include @@ -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; -- cgit v1.2.3