2944a94c6b
Currently the argv is only allocated on the stack, and then assigned to process->argv. When the start_subprocess function goes out of scope, the local argv variable is eliminated from the stack, but the pointer is still kept around in process->argv. Much later when we try to access the same process->argv in finish_command, this leads us to access a memory location that no longer contains what we want. As argv0 is only used for printing errors, this is not easily noticed in normal git operations. However when running t0021-conversion.sh through valgrind, valgrind rightfully complains: ==21024== Invalid read of size 8 ==21024== at 0x2ACF64: finish_command (run-command.c:869) ==21024== by 0x2D6B18: subprocess_exit_handler (sub-process.c:72) ==21024== by 0x2AB41E: cleanup_children (run-command.c:45) ==21024== by 0x2AB526: cleanup_children_on_exit (run-command.c:81) ==21024== by 0x54AD487: __run_exit_handlers (in /usr/lib/libc-2.26.so) ==21024== by 0x54AD4D9: exit (in /usr/lib/libc-2.26.so) ==21024== by 0x11A9EF: handle_builtin (git.c:550) ==21024== by 0x11ABCC: run_argv (git.c:602) ==21024== by 0x11AD8E: cmd_main (git.c:679) ==21024== by 0x1BF125: main (common-main.c:43) ==21024== Address 0x1ffeffec00 is on thread 1's stack ==21024== 1504 bytes below stack pointer ==21024== These days, the child_process structure has its own args array, and the standard way to set up its argv[] is to use that one, instead of assigning to process->argv to point at an array that is outside. Use that facility automatically fixes this issue. Reported-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
211 lines
5.3 KiB
C
211 lines
5.3 KiB
C
/*
|
|
* Generic implementation of background process infrastructure.
|
|
*/
|
|
#include "sub-process.h"
|
|
#include "sigchain.h"
|
|
#include "pkt-line.h"
|
|
|
|
int cmd2process_cmp(const void *unused_cmp_data,
|
|
const struct subprocess_entry *e1,
|
|
const struct subprocess_entry *e2,
|
|
const void *unused_keydata)
|
|
{
|
|
return strcmp(e1->cmd, e2->cmd);
|
|
}
|
|
|
|
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
|
|
{
|
|
struct subprocess_entry key;
|
|
|
|
hashmap_entry_init(&key, strhash(cmd));
|
|
key.cmd = cmd;
|
|
return hashmap_get(hashmap, &key, NULL);
|
|
}
|
|
|
|
int subprocess_read_status(int fd, struct strbuf *status)
|
|
{
|
|
struct strbuf **pair;
|
|
char *line;
|
|
int len;
|
|
|
|
for (;;) {
|
|
len = packet_read_line_gently(fd, NULL, &line);
|
|
if ((len < 0) || !line)
|
|
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);
|
|
}
|
|
|
|
return (len < 0) ? len : 0;
|
|
}
|
|
|
|
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);
|
|
|
|
hashmap_remove(hashmap, entry, NULL);
|
|
}
|
|
|
|
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);
|
|
argv_array_push(&process->args, cmd);
|
|
process->use_shell = 1;
|
|
process->in = -1;
|
|
process->out = -1;
|
|
process->clean_on_exit = 1;
|
|
process->clean_on_exit_handler = subprocess_exit_handler;
|
|
|
|
err = start_command(process);
|
|
if (err) {
|
|
error("cannot fork to run subprocess '%s'", cmd);
|
|
return err;
|
|
}
|
|
|
|
hashmap_entry_init(entry, strhash(cmd));
|
|
|
|
err = startfn(entry);
|
|
if (err) {
|
|
error("initialization for subprocess '%s' failed", cmd);
|
|
subprocess_stop(hashmap, entry);
|
|
return err;
|
|
}
|
|
|
|
hashmap_add(hashmap, entry);
|
|
return 0;
|
|
}
|
|
|
|
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 {
|
|
warning("subprocess '%s' requested unsupported capability '%s'",
|
|
process->argv[0], p);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|