diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-09-26 09:56:25 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-09-26 09:56:25 -0400 |
commit | 3e610e935858995f411df7bb0a77e2efeeae3d66 (patch) | |
tree | 03cd872c7d756b5c2d3165d83afc4fd15676c86c | |
parent | 71b45aa6ba60a651185cf47792fbf61b7e2b07a8 (diff) | |
download | stasis-3e610e935858995f411df7bb0a77e2efeeae3d66.tar.gz |
Implement cmd and working_dir
-rw-r--r-- | include/multiprocessing.h | 2 | ||||
-rw-r--r-- | src/multiprocessing.c | 29 | ||||
-rw-r--r-- | tests/test_multiprocessing.c | 4 |
3 files changed, 16 insertions, 19 deletions
diff --git a/include/multiprocessing.h b/include/multiprocessing.h index aac6d18..d4213d2 100644 --- a/include/multiprocessing.h +++ b/include/multiprocessing.h @@ -95,7 +95,7 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root * @return pointer to MultiProcessingTask structure * @return NULL on error */ -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); /** * Execute all tasks in a pool 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; diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c index e1a84e3..5b3d9d1 100644 --- a/tests/test_multiprocessing.c +++ b/tests/test_multiprocessing.c @@ -58,7 +58,7 @@ void test_mp_task() { struct MultiProcessingTask *task; char task_name[100] = {0}; sprintf(task_name, "mytask%zu", i); - STASIS_ASSERT_FATAL((task = mp_pool_task(pool, task_name, commands[i])) != NULL, "Task should not be NULL"); + STASIS_ASSERT_FATAL((task = mp_pool_task(pool, task_name, NULL, commands[i])) != NULL, "Task should not be NULL"); STASIS_ASSERT(task->pid != 0, "PID should be non-zero at this point"); STASIS_ASSERT(task->parent_pid != MP_POOL_PID_UNUSED, "Parent PID should be non-zero"); STASIS_ASSERT(task->status == -1, "Status should be -1 (not started yet)"); @@ -102,7 +102,7 @@ void test_mp_pool_workflow() { struct MultiProcessingPool *p; struct MultiProcessingTask *task; STASIS_ASSERT((p = mp_pool_init("workflow", "mplogs")) != NULL, "Failed to initialize pool"); - STASIS_ASSERT((task = mp_pool_task(p, "task", (char *) test->input_cmd)) != NULL, "Failed to queue task"); + STASIS_ASSERT((task = mp_pool_task(p, "task", NULL, (char *) test->input_cmd)) != NULL, "Failed to queue task"); STASIS_ASSERT(mp_pool_join(p, get_cpu_count(), test->input_join_flags) == test->expected_result, "Unexpected result"); STASIS_ASSERT(task->status == test->expected_status, "Unexpected status"); STASIS_ASSERT(task->signaled_by == test->expected_signal, "Unexpected signal"); |