summaryrefslogtreecommitdiffstats
path: root/src/xspawn.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-03-29 12:15:33 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-03-29 12:26:43 -0400
commit116ab1ed2b4230aea1ab634618af3162cabbb37f (patch)
tree9fbc436568e804faaa8885c181e59a303feb06ab /src/xspawn.c
parentba2d4ac206ff1321ea953d3305d8bda048922983 (diff)
downloadbfs-116ab1ed2b4230aea1ab634618af3162cabbb37f.tar.xz
xspawn: Use list.h for the action list
Diffstat (limited to 'src/xspawn.c')
-rw-r--r--src/xspawn.c36
1 files changed, 17 insertions, 19 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;