|
Packit |
4511e4 |
/*
|
|
Packit |
4511e4 |
* Generic implementation of background process infrastructure.
|
|
Packit |
4511e4 |
*/
|
|
Packit |
4511e4 |
#include "sub-process.h"
|
|
Packit |
4511e4 |
#include "sigchain.h"
|
|
Packit |
4511e4 |
#include "pkt-line.h"
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
int cmd2process_cmp(const void *unused_cmp_data,
|
|
Packit Service |
b5fd21 |
const struct hashmap_entry *eptr,
|
|
Packit Service |
b5fd21 |
const struct hashmap_entry *entry_or_key,
|
|
Packit |
4511e4 |
const void *unused_keydata)
|
|
Packit |
4511e4 |
{
|
|
Packit Service |
b5fd21 |
const struct subprocess_entry *e1, *e2;
|
|
Packit Service |
b5fd21 |
|
|
Packit Service |
b5fd21 |
e1 = container_of(eptr, const struct subprocess_entry, ent);
|
|
Packit Service |
b5fd21 |
e2 = container_of(entry_or_key, const struct subprocess_entry, ent);
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
return strcmp(e1->cmd, e2->cmd);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
struct subprocess_entry key;
|
|
Packit |
4511e4 |
|
|
Packit Service |
b5fd21 |
hashmap_entry_init(&key.ent, strhash(cmd));
|
|
Packit |
4511e4 |
key.cmd = cmd;
|
|
Packit Service |
b5fd21 |
return hashmap_get_entry(hashmap, &key, ent, NULL);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
int subprocess_read_status(int fd, struct strbuf *status)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
struct strbuf **pair;
|
|
Packit |
4511e4 |
char *line;
|
|
Packit |
4511e4 |
int len;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
for (;;) {
|
|
Packit |
4511e4 |
len = packet_read_line_gently(fd, NULL, &line);
|
|
Packit |
4511e4 |
if ((len < 0) || !line)
|
|
Packit |
4511e4 |
break;
|
|
Packit |
4511e4 |
pair = strbuf_split_str(line, '=', 2);
|
|
Packit |
4511e4 |
if (pair[0] && pair[0]->len && pair[1]) {
|
|
Packit |
4511e4 |
/* the last "status=<foo>" line wins */
|
|
Packit |
4511e4 |
if (!strcmp(pair[0]->buf, "status=")) {
|
|
Packit |
4511e4 |
strbuf_reset(status);
|
|
Packit |
4511e4 |
strbuf_addbuf(status, pair[1]);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
strbuf_list_free(pair);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
return (len < 0) ? len : 0;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
if (!entry)
|
|
Packit |
4511e4 |
return;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
entry->process.clean_on_exit = 0;
|
|
Packit |
4511e4 |
kill(entry->process.pid, SIGTERM);
|
|
Packit |
4511e4 |
finish_command(&entry->process);
|
|
Packit |
4511e4 |
|
|
Packit Service |
b5fd21 |
hashmap_remove(hashmap, &entry->ent, NULL);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
static void subprocess_exit_handler(struct child_process *process)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
sigchain_push(SIGPIPE, SIG_IGN);
|
|
Packit |
4511e4 |
/* Closing the pipe signals the subprocess to initiate a shutdown. */
|
|
Packit |
4511e4 |
close(process->in);
|
|
Packit |
4511e4 |
close(process->out);
|
|
Packit |
4511e4 |
sigchain_pop(SIGPIPE);
|
|
Packit |
4511e4 |
/* Finish command will wait until the shutdown is complete. */
|
|
Packit |
4511e4 |
finish_command(process);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
|
|
Packit |
4511e4 |
subprocess_start_fn startfn)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
int err;
|
|
Packit |
4511e4 |
struct child_process *process;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
entry->cmd = cmd;
|
|
Packit |
4511e4 |
process = &entry->process;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
child_process_init(process);
|
|
Packit |
4511e4 |
argv_array_push(&process->args, cmd);
|
|
Packit |
4511e4 |
process->use_shell = 1;
|
|
Packit |
4511e4 |
process->in = -1;
|
|
Packit |
4511e4 |
process->out = -1;
|
|
Packit |
4511e4 |
process->clean_on_exit = 1;
|
|
Packit |
4511e4 |
process->clean_on_exit_handler = subprocess_exit_handler;
|
|
Packit Service |
b5fd21 |
process->trace2_child_class = "subprocess";
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
err = start_command(process);
|
|
Packit |
4511e4 |
if (err) {
|
|
Packit |
4511e4 |
error("cannot fork to run subprocess '%s'", cmd);
|
|
Packit |
4511e4 |
return err;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit Service |
b5fd21 |
hashmap_entry_init(&entry->ent, strhash(cmd));
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
err = startfn(entry);
|
|
Packit |
4511e4 |
if (err) {
|
|
Packit |
4511e4 |
error("initialization for subprocess '%s' failed", cmd);
|
|
Packit |
4511e4 |
subprocess_stop(hashmap, entry);
|
|
Packit |
4511e4 |
return err;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit Service |
b5fd21 |
hashmap_add(hashmap, &entry->ent);
|
|
Packit |
4511e4 |
return 0;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
static int handshake_version(struct child_process *process,
|
|
Packit |
4511e4 |
const char *welcome_prefix, int *versions,
|
|
Packit |
4511e4 |
int *chosen_version)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
int version_scratch;
|
|
Packit |
4511e4 |
int i;
|
|
Packit |
4511e4 |
char *line;
|
|
Packit |
4511e4 |
const char *p;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
if (!chosen_version)
|
|
Packit |
4511e4 |
chosen_version = &version_scratch;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
if (packet_write_fmt_gently(process->in, "%s-client\n",
|
|
Packit |
4511e4 |
welcome_prefix))
|
|
Packit |
4511e4 |
return error("Could not write client identification");
|
|
Packit |
4511e4 |
for (i = 0; versions[i]; i++) {
|
|
Packit |
4511e4 |
if (packet_write_fmt_gently(process->in, "version=%d\n",
|
|
Packit |
4511e4 |
versions[i]))
|
|
Packit |
4511e4 |
return error("Could not write requested version");
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
if (packet_flush_gently(process->in))
|
|
Packit |
4511e4 |
return error("Could not write flush packet");
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
if (!(line = packet_read_line(process->out, NULL)) ||
|
|
Packit |
4511e4 |
!skip_prefix(line, welcome_prefix, &p) ||
|
|
Packit |
4511e4 |
strcmp(p, "-server"))
|
|
Packit |
4511e4 |
return error("Unexpected line '%s', expected %s-server",
|
|
Packit |
4511e4 |
line ? line : "<flush packet>", welcome_prefix);
|
|
Packit |
4511e4 |
if (!(line = packet_read_line(process->out, NULL)) ||
|
|
Packit |
4511e4 |
!skip_prefix(line, "version=", &p) ||
|
|
Packit |
4511e4 |
strtol_i(p, 10, chosen_version))
|
|
Packit |
4511e4 |
return error("Unexpected line '%s', expected version",
|
|
Packit |
4511e4 |
line ? line : "<flush packet>");
|
|
Packit |
4511e4 |
if ((line = packet_read_line(process->out, NULL)))
|
|
Packit |
4511e4 |
return error("Unexpected line '%s', expected flush", line);
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
/* Check to make sure that the version received is supported */
|
|
Packit |
4511e4 |
for (i = 0; versions[i]; i++) {
|
|
Packit |
4511e4 |
if (versions[i] == *chosen_version)
|
|
Packit |
4511e4 |
break;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
if (!versions[i])
|
|
Packit |
4511e4 |
return error("Version %d not supported", *chosen_version);
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
return 0;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
static int handshake_capabilities(struct child_process *process,
|
|
Packit |
4511e4 |
struct subprocess_capability *capabilities,
|
|
Packit |
4511e4 |
unsigned int *supported_capabilities)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
int i;
|
|
Packit |
4511e4 |
char *line;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
for (i = 0; capabilities[i].name; i++) {
|
|
Packit |
4511e4 |
if (packet_write_fmt_gently(process->in, "capability=%s\n",
|
|
Packit |
4511e4 |
capabilities[i].name))
|
|
Packit |
4511e4 |
return error("Could not write requested capability");
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
if (packet_flush_gently(process->in))
|
|
Packit |
4511e4 |
return error("Could not write flush packet");
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
while ((line = packet_read_line(process->out, NULL))) {
|
|
Packit |
4511e4 |
const char *p;
|
|
Packit |
4511e4 |
if (!skip_prefix(line, "capability=", &p))
|
|
Packit |
4511e4 |
continue;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
for (i = 0;
|
|
Packit |
4511e4 |
capabilities[i].name && strcmp(p, capabilities[i].name);
|
|
Packit |
4511e4 |
i++)
|
|
Packit |
4511e4 |
;
|
|
Packit |
4511e4 |
if (capabilities[i].name) {
|
|
Packit |
4511e4 |
if (supported_capabilities)
|
|
Packit |
4511e4 |
*supported_capabilities |= capabilities[i].flag;
|
|
Packit |
4511e4 |
} else {
|
|
Packit |
4511e4 |
die("subprocess '%s' requested unsupported capability '%s'",
|
|
Packit |
4511e4 |
process->argv[0], p);
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
return 0;
|
|
Packit |
4511e4 |
}
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
int subprocess_handshake(struct subprocess_entry *entry,
|
|
Packit |
4511e4 |
const char *welcome_prefix,
|
|
Packit |
4511e4 |
int *versions,
|
|
Packit |
4511e4 |
int *chosen_version,
|
|
Packit |
4511e4 |
struct subprocess_capability *capabilities,
|
|
Packit |
4511e4 |
unsigned int *supported_capabilities)
|
|
Packit |
4511e4 |
{
|
|
Packit |
4511e4 |
int retval;
|
|
Packit |
4511e4 |
struct child_process *process = &entry->process;
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
sigchain_push(SIGPIPE, SIG_IGN);
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
retval = handshake_version(process, welcome_prefix, versions,
|
|
Packit |
4511e4 |
chosen_version) ||
|
|
Packit |
4511e4 |
handshake_capabilities(process, capabilities,
|
|
Packit |
4511e4 |
supported_capabilities);
|
|
Packit |
4511e4 |
|
|
Packit |
4511e4 |
sigchain_pop(SIGPIPE);
|
|
Packit |
4511e4 |
return retval;
|
|
Packit |
4511e4 |
}
|