2017-05-05 23:28:01 +08:00
|
|
|
/*
|
|
|
|
* Generic implementation of background process infrastructure.
|
|
|
|
*/
|
|
|
|
#include "sub-process.h"
|
|
|
|
#include "sigchain.h"
|
|
|
|
#include "pkt-line.h"
|
|
|
|
|
2017-07-01 03:14:05 +08:00
|
|
|
int cmd2process_cmp(const void *unused_cmp_data,
|
2019-10-07 07:30:37 +08:00
|
|
|
const struct hashmap_entry *eptr,
|
|
|
|
const struct hashmap_entry *entry_or_key,
|
2017-07-01 03:14:05 +08:00
|
|
|
const void *unused_keydata)
|
2017-05-05 23:28:01 +08:00
|
|
|
{
|
2019-10-07 07:30:37 +08:00
|
|
|
const struct subprocess_entry *e1, *e2;
|
|
|
|
|
|
|
|
e1 = container_of(eptr, const struct subprocess_entry, ent);
|
|
|
|
e2 = container_of(entry_or_key, const struct subprocess_entry, ent);
|
2017-07-01 08:28:33 +08:00
|
|
|
|
2017-05-05 23:28:01 +08:00
|
|
|
return strcmp(e1->cmd, e2->cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
|
|
|
|
{
|
|
|
|
struct subprocess_entry key;
|
|
|
|
|
2019-10-07 07:30:27 +08:00
|
|
|
hashmap_entry_init(&key.ent, strhash(cmd));
|
2017-05-05 23:28:01 +08:00
|
|
|
key.cmd = cmd;
|
2019-10-07 07:30:42 +08:00
|
|
|
return hashmap_get_entry(hashmap, &key, ent, NULL);
|
2017-05-05 23:28:01 +08:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:28:02 +08:00
|
|
|
int subprocess_read_status(int fd, struct strbuf *status)
|
2017-05-05 23:28:01 +08:00
|
|
|
{
|
|
|
|
struct strbuf **pair;
|
|
|
|
char *line;
|
2017-05-05 23:28:02 +08:00
|
|
|
int len;
|
|
|
|
|
2017-05-05 23:28:01 +08:00
|
|
|
for (;;) {
|
2017-05-05 23:28:02 +08:00
|
|
|
len = packet_read_line_gently(fd, NULL, &line);
|
|
|
|
if ((len < 0) || !line)
|
2017-05-05 23:28:01 +08:00
|
|
|
break;
|
|
|
|
pair = strbuf_split_str(line, '=', 2);
|
|
|
|
if (pair[0] && pair[0]->len && pair[1]) {
|
|
|
|
/* the last "status=<foo>" line wins */
|
|
|
|
if (!strcmp(pair[0]->buf, "status=")) {
|
|
|
|
strbuf_reset(status);
|
|
|
|
strbuf_addbuf(status, pair[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strbuf_list_free(pair);
|
|
|
|
}
|
2017-05-05 23:28:02 +08:00
|
|
|
|
|
|
|
return (len < 0) ? len : 0;
|
2017-05-05 23:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
|
|
|
|
{
|
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry->process.clean_on_exit = 0;
|
|
|
|
kill(entry->process.pid, SIGTERM);
|
|
|
|
finish_command(&entry->process);
|
|
|
|
|
2019-10-07 07:30:31 +08:00
|
|
|
hashmap_remove(hashmap, &entry->ent, NULL);
|
2017-05-05 23:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void subprocess_exit_handler(struct child_process *process)
|
|
|
|
{
|
|
|
|
sigchain_push(SIGPIPE, SIG_IGN);
|
|
|
|
/* Closing the pipe signals the subprocess to initiate a shutdown. */
|
|
|
|
close(process->in);
|
|
|
|
close(process->out);
|
|
|
|
sigchain_pop(SIGPIPE);
|
|
|
|
/* Finish command will wait until the shutdown is complete. */
|
|
|
|
finish_command(process);
|
|
|
|
}
|
|
|
|
|
|
|
|
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
|
|
|
|
subprocess_start_fn startfn)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct child_process *process;
|
|
|
|
|
|
|
|
entry->cmd = cmd;
|
|
|
|
process = &entry->process;
|
|
|
|
|
|
|
|
child_process_init(process);
|
2020-07-29 04:25:12 +08:00
|
|
|
strvec_push(&process->args, cmd);
|
2017-05-05 23:28:01 +08:00
|
|
|
process->use_shell = 1;
|
|
|
|
process->in = -1;
|
|
|
|
process->out = -1;
|
|
|
|
process->clean_on_exit = 1;
|
|
|
|
process->clean_on_exit_handler = subprocess_exit_handler;
|
2019-02-23 06:25:05 +08:00
|
|
|
process->trace2_child_class = "subprocess";
|
2017-05-05 23:28:01 +08:00
|
|
|
|
|
|
|
err = start_command(process);
|
|
|
|
if (err) {
|
|
|
|
error("cannot fork to run subprocess '%s'", cmd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-10-07 07:30:27 +08:00
|
|
|
hashmap_entry_init(&entry->ent, strhash(cmd));
|
2017-05-05 23:28:01 +08:00
|
|
|
|
|
|
|
err = startfn(entry);
|
|
|
|
if (err) {
|
|
|
|
error("initialization for subprocess '%s' failed", cmd);
|
|
|
|
subprocess_stop(hashmap, entry);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-10-07 07:30:29 +08:00
|
|
|
hashmap_add(hashmap, &entry->ent);
|
2017-05-05 23:28:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-27 02:17:29 +08:00
|
|
|
|
|
|
|
static int handshake_version(struct child_process *process,
|
|
|
|
const char *welcome_prefix, int *versions,
|
|
|
|
int *chosen_version)
|
|
|
|
{
|
|
|
|
int version_scratch;
|
|
|
|
int i;
|
|
|
|
char *line;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (!chosen_version)
|
|
|
|
chosen_version = &version_scratch;
|
|
|
|
|
|
|
|
if (packet_write_fmt_gently(process->in, "%s-client\n",
|
|
|
|
welcome_prefix))
|
|
|
|
return error("Could not write client identification");
|
|
|
|
for (i = 0; versions[i]; i++) {
|
|
|
|
if (packet_write_fmt_gently(process->in, "version=%d\n",
|
|
|
|
versions[i]))
|
|
|
|
return error("Could not write requested version");
|
|
|
|
}
|
|
|
|
if (packet_flush_gently(process->in))
|
|
|
|
return error("Could not write flush packet");
|
|
|
|
|
|
|
|
if (!(line = packet_read_line(process->out, NULL)) ||
|
|
|
|
!skip_prefix(line, welcome_prefix, &p) ||
|
|
|
|
strcmp(p, "-server"))
|
|
|
|
return error("Unexpected line '%s', expected %s-server",
|
|
|
|
line ? line : "<flush packet>", welcome_prefix);
|
|
|
|
if (!(line = packet_read_line(process->out, NULL)) ||
|
|
|
|
!skip_prefix(line, "version=", &p) ||
|
|
|
|
strtol_i(p, 10, chosen_version))
|
|
|
|
return error("Unexpected line '%s', expected version",
|
|
|
|
line ? line : "<flush packet>");
|
|
|
|
if ((line = packet_read_line(process->out, NULL)))
|
|
|
|
return error("Unexpected line '%s', expected flush", line);
|
|
|
|
|
|
|
|
/* Check to make sure that the version received is supported */
|
|
|
|
for (i = 0; versions[i]; i++) {
|
|
|
|
if (versions[i] == *chosen_version)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!versions[i])
|
|
|
|
return error("Version %d not supported", *chosen_version);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handshake_capabilities(struct child_process *process,
|
|
|
|
struct subprocess_capability *capabilities,
|
|
|
|
unsigned int *supported_capabilities)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
for (i = 0; capabilities[i].name; i++) {
|
|
|
|
if (packet_write_fmt_gently(process->in, "capability=%s\n",
|
|
|
|
capabilities[i].name))
|
|
|
|
return error("Could not write requested capability");
|
|
|
|
}
|
|
|
|
if (packet_flush_gently(process->in))
|
|
|
|
return error("Could not write flush packet");
|
|
|
|
|
|
|
|
while ((line = packet_read_line(process->out, NULL))) {
|
|
|
|
const char *p;
|
|
|
|
if (!skip_prefix(line, "capability=", &p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0;
|
|
|
|
capabilities[i].name && strcmp(p, capabilities[i].name);
|
|
|
|
i++)
|
|
|
|
;
|
|
|
|
if (capabilities[i].name) {
|
|
|
|
if (supported_capabilities)
|
|
|
|
*supported_capabilities |= capabilities[i].flag;
|
|
|
|
} else {
|
2017-09-11 11:21:29 +08:00
|
|
|
die("subprocess '%s' requested unsupported capability '%s'",
|
run-command API: remove "argv" member, always use "args"
Remove the "argv" member from the run-command API, ever since "args"
was added in c460c0ecdca (run-command: store an optional argv_array,
2014-05-15) being able to provide either "argv" or "args" has led to
some confusion and bugs.
If we hadn't gone in that direction and only had an "argv" our
problems wouldn't have been solved either, as noted in [1] (and in the
documentation amended here) it comes with inherent memory management
issues: The caller would have to hang on to the "argv" until the
run-command API was finished. If the "argv" was an argument to main()
this wasn't an issue, but if it it was manually constructed using the
API might be painful.
We also have a recent report[2] of a user of the API segfaulting,
which is a direct result of it being complex to use. This commit
addresses the root cause of that bug.
This change is larger than I'd like, but there's no easy way to avoid
it that wouldn't involve even more verbose intermediate steps. We use
the "argv" as the source of truth over the "args", so we need to
change all parts of run-command.[ch] itself, as well as the trace2
logging at the same time.
The resulting Windows-specific code in start_command() is a bit nasty,
as we're now assigning to a strvec's "v" member, instead of to our own
"argv". There was a suggestion of some alternate approaches in reply
to an earlier version of this commit[3], but let's leave larger a
larger and needless refactoring of this code for now.
1. http://lore.kernel.org/git/YT6BnnXeAWn8BycF@coredump.intra.peff.net
2. https://lore.kernel.org/git/20211120194048.12125-1-ematsumiya@suse.de/
3. https://lore.kernel.org/git/patch-5.5-ea1011f7473-20211122T153605Z-avarab@gmail.com/
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-11-26 06:52:22 +08:00
|
|
|
process->args.v[0], p);
|
2017-07-27 02:17:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int subprocess_handshake(struct subprocess_entry *entry,
|
|
|
|
const char *welcome_prefix,
|
|
|
|
int *versions,
|
|
|
|
int *chosen_version,
|
|
|
|
struct subprocess_capability *capabilities,
|
|
|
|
unsigned int *supported_capabilities)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
struct child_process *process = &entry->process;
|
|
|
|
|
|
|
|
sigchain_push(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
retval = handshake_version(process, welcome_prefix, versions,
|
|
|
|
chosen_version) ||
|
|
|
|
handshake_capabilities(process, capabilities,
|
|
|
|
supported_capabilities);
|
|
|
|
|
|
|
|
sigchain_pop(SIGPIPE);
|
|
|
|
return retval;
|
|
|
|
}
|