Skip to content

Commit

Permalink
sub-process: add subprocess_start_argv()
Browse files Browse the repository at this point in the history
Add function to start a subprocess with an argv.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
  • Loading branch information
jeffhostetler authored and dscho committed Aug 16, 2023
1 parent 817cf58 commit d7ba07f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
47 changes: 47 additions & 0 deletions sub-process.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "sub-process.h"
#include "sigchain.h"
#include "pkt-line.h"
#include "quote.h"

int cmd2process_cmp(const void *cmp_data UNUSED,
const struct hashmap_entry *eptr,
Expand Down Expand Up @@ -119,6 +120,52 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
return 0;
}

int subprocess_start_strvec(struct hashmap *hashmap,
struct subprocess_entry *entry,
int is_git_cmd,
const struct strvec *argv,
subprocess_start_fn startfn)
{
int err;
int k;
struct child_process *process;
struct strbuf quoted = STRBUF_INIT;

process = &entry->process;

child_process_init(process);
for (k = 0; k < argv->nr; k++)
strvec_push(&process->args, argv->v[k]);
process->use_shell = 1;
process->in = -1;
process->out = -1;
process->git_cmd = is_git_cmd;
process->clean_on_exit = 1;
process->clean_on_exit_handler = subprocess_exit_handler;
process->trace2_child_class = "subprocess";

sq_quote_argv_pretty(&quoted, argv->v);
entry->cmd = strbuf_detach(&quoted, NULL);

err = start_command(process);
if (err) {
error("cannot fork to run subprocess '%s'", entry->cmd);
return err;
}

hashmap_entry_init(&entry->ent, strhash(entry->cmd));

err = startfn(entry);
if (err) {
error("initialization for subprocess '%s' failed", entry->cmd);
subprocess_stop(hashmap, entry);
return err;
}

hashmap_add(hashmap, &entry->ent);
return 0;
}

static int handshake_version(struct child_process *process,
const char *welcome_prefix, int *versions,
int *chosen_version)
Expand Down
6 changes: 6 additions & 0 deletions sub-process.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
subprocess_start_fn startfn);

int subprocess_start_strvec(struct hashmap *hashmap,
struct subprocess_entry *entry,
int is_git_cmd,
const struct strvec *argv,
subprocess_start_fn startfn);

/* Kill a subprocess and remove it from the subprocess hashmap. */
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);

Expand Down

0 comments on commit d7ba07f

Please sign in to comment.