summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-05-20 13:01:43 -0400
committerTavian Barnes <tavianator@tavianator.com>2021-05-20 13:08:01 -0400
commitb08424dd960c2241ce14a61c0241c90f612cd6b4 (patch)
tree98604aff94318e476dfa15e34bd0f6ec76b7e729
parentdb1888d8454db4ae39a87809956e5e44bd0a7407 (diff)
downloadbfs-b08424dd960c2241ce14a61c0241c90f612cd6b4.tar.xz
spawn: New bfs_spawn_addsetrlimit() action
-rw-r--r--spawn.c19
-rw-r--r--spawn.h8
2 files changed, 27 insertions, 0 deletions
diff --git a/spawn.c b/spawn.c
index 094214b..4879e12 100644
--- a/spawn.c
+++ b/spawn.c
@@ -29,6 +29,7 @@ enum bfs_spawn_op {
BFS_SPAWN_CLOSE,
BFS_SPAWN_DUP2,
BFS_SPAWN_FCHDIR,
+ BFS_SPAWN_SETRLIMIT,
};
/**
@@ -40,6 +41,8 @@ struct bfs_spawn_action {
enum bfs_spawn_op op;
int in_fd;
int out_fd;
+ int resource;
+ struct rlimit rlimit;
};
int bfs_spawn_init(struct bfs_spawn *ctx) {
@@ -125,6 +128,17 @@ int bfs_spawn_addfchdir(struct bfs_spawn *ctx, int fd) {
}
}
+int bfs_spawn_addsetrlimit(struct bfs_spawn *ctx, int resource, const struct rlimit *rl) {
+ struct bfs_spawn_action *action = bfs_spawn_add(ctx, BFS_SPAWN_SETRLIMIT);
+ if (action) {
+ action->resource = resource;
+ action->rlimit = *rl;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
/** Facade for execvpe() which is non-standard. */
static int bfs_execvpe(const char *exe, char **argv, char **envp) {
#if __GLIBC__ || __linux__ || __NetBSD__ || __OpenBSD__
@@ -177,6 +191,11 @@ static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char **
goto fail;
}
break;
+ case BFS_SPAWN_SETRLIMIT:
+ if (setrlimit(action->resource, &action->rlimit) != 0) {
+ goto fail;
+ }
+ break;
}
}
diff --git a/spawn.h b/spawn.h
index 0225ed8..a2c23fd 100644
--- a/spawn.h
+++ b/spawn.h
@@ -21,6 +21,7 @@
#ifndef BFS_SPAWN_H
#define BFS_SPAWN_H
+#include <sys/resource.h>
#include <sys/types.h>
/**
@@ -83,6 +84,13 @@ int bfs_spawn_adddup2(struct bfs_spawn *ctx, int oldfd, int newfd);
int bfs_spawn_addfchdir(struct bfs_spawn *ctx, int fd);
/**
+ * Add a setrlimit() action to a bfs_spawn() context.
+ *
+ * @return 0 on success, -1 on failure.
+ */
+int bfs_spawn_addsetrlimit(struct bfs_spawn *ctx, int resource, const struct rlimit *rl);
+
+/**
* Spawn a new process.
*
* @param exe