aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-09-26 09:56:25 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-09-26 09:56:25 -0400
commit3e610e935858995f411df7bb0a77e2efeeae3d66 (patch)
tree03cd872c7d756b5c2d3165d83afc4fd15676c86c /src
parent71b45aa6ba60a651185cf47792fbf61b7e2b07a8 (diff)
downloadstasis-3e610e935858995f411df7bb0a77e2efeeae3d66.tar.gz
Implement cmd and working_dir
Diffstat (limited to 'src')
-rw-r--r--src/multiprocessing.c29
1 files changed, 13 insertions, 16 deletions
diff --git a/src/multiprocessing.c b/src/multiprocessing.c
index 433d57a..5c66672 100644
--- a/src/multiprocessing.c
+++ b/src/multiprocessing.c
@@ -7,8 +7,7 @@ static struct MultiProcessingTask *mp_pool_next_available(struct MultiProcessing
return &pool->task[pool->num_used];
}
-int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, const char *cmd) {
- char *cwd = NULL;
+int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) {
FILE *fp_log = NULL;
// The task starts inside the requested working directory
@@ -42,23 +41,14 @@ int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, co
timebuf[strlen(timebuf) ? strlen(timebuf) - 1 : 0] = 0;
}
- // Get the path to the current working directory
- // Used by the log header. Informative only.
- cwd = getcwd(NULL, PATH_MAX);
- if (!cwd) {
- perror(cwd);
- exit(1);
- }
-
// Generate log header
fprintf(fp_log, "# STARTED: %s\n", timebuf ? timebuf : "unknown");
fprintf(fp_log, "# PID: %d\n", task->parent_pid);
- fprintf(fp_log, "# WORKDIR: %s\n", cwd);
- fprintf(fp_log, "# COMMAND:\n%s\n", cmd);
+ fprintf(fp_log, "# WORKDIR: %s\n", task->working_dir);
+ fprintf(fp_log, "# COMMAND:\n%s\n", task->cmd);
fprintf(fp_log, "# OUTPUT:\n");
// Commit header to log file / clean up
fflush(fp_log);
- guard_free(cwd);
// Execute task
fflush(stdout);
@@ -87,18 +77,18 @@ int parent(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, p
return 0;
}
-static int mp_task_fork(struct MultiProcessingPool *pool, struct MultiProcessingTask *task, const char *cmd) {
+static int mp_task_fork(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) {
pid_t pid = fork();
int child_status = 0;
if (pid == -1) {
return -1;
} else if (pid == 0) {
- child(pool, task, cmd);
+ child(pool, task);
}
return parent(pool, task, pid, &child_status);
}
-struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const char *ident, char *cmd) {
+struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const char *ident, char *working_dir, char *cmd) {
struct MultiProcessingTask *slot = mp_pool_next_available(pool);
if (pool->num_used != pool->num_alloc) {
pool->num_used++;
@@ -118,6 +108,13 @@ struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const
strcat(slot->log_file, pool->log_root);
strcat(slot->log_file, "/");
+ // Set working directory
+ if (isempty(working_dir)) {
+ strcpy(slot->working_dir, ".");
+ } else {
+ strcpy(slot->working_dir, working_dir);
+ }
+
// Create a temporary file to act as our intermediate command script
FILE *tp = NULL;
char *t_name;