Skip to content

Commit

Permalink
#164 pass sh environment variables as table
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 18, 2024
1 parent af7adbf commit b79f4ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 3 additions & 1 deletion inc/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#include <sys/types.h>

#include "stable.h"

char *pid_path(void);

pid_t pid_active_server(void);

void pid_file_create(void);

void spawn_sh_cmd(const char * const command, char * const message);
void spawn_sh_cmd(const char * const command, const struct STable * const env);

// exit; caller should return afterwards
void wd_exit(int __status);
Expand Down
5 changes: 4 additions & 1 deletion src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ void handle_success(void) {
log_info("\nExecuting CHANGE_SUCCESS_CMD:");
log_info(" %s", cfg->change_success_cmd);

spawn_sh_cmd(cfg->change_success_cmd, deltas_brief);
const struct STable *env = stable_init(5, 5);
stable_put(env, "WD_MESSAGE", deltas_brief);
spawn_sh_cmd(cfg->change_success_cmd, env);
stable_free(env);
}

log_info("\nChanges successful");
Expand Down
14 changes: 8 additions & 6 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unistd.h>

#include "process.h"
#include "stable.h"

#include "log.h"

Expand Down Expand Up @@ -105,14 +106,12 @@ void pid_file_create(void) {
free(path);
}

void spawn_sh_cmd(const char * const command, char * const message) {
if (!command || !message)
void spawn_sh_cmd(const char * const command, const struct STable * const env) {
if (!command)
return;

// experiments show that environment variable length tops out at 128k: variable itself plus contents
if (strlen(message) > 1024 * 120) {
message[1024 * 120] = '\0';
}
char value[1024 * 120];

pid_t pid = fork();
if (pid < 0) {
Expand All @@ -130,7 +129,10 @@ void spawn_sh_cmd(const char * const command, char * const message) {
sa.sa_handler = SIG_DFL;
sigaction(SIGCHLD, &sa, NULL);

setenv("WD_MESSAGE", message, 1);
for (const struct STableIter *i = stable_iter(env); i; i = stable_next(i)) {
snprintf(value, sizeof(value), "%s", (char*)i->val);
setenv(i->key, value, 1);
}

// execute command in the child process
execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
Expand Down

0 comments on commit b79f4ba

Please sign in to comment.