mirror of
https://github.com/git/git.git
synced 2026-08-08 17:11:48 +00:00
subprocess_read_status() reads "status=<key>" packets up to a flush with
packet_read_line_gently(), which is gentle only about EOF. A malformed
length header still dies inside pkt-line, and an empty packet is
indistinguishable from the flush that ends the section. A protocol
violation in a status section therefore either kills the whole command
or silently truncates the section. That posture fits the filter
protocol's callers, which treat their process as required
infrastructure; the diff process consult added later in this series
treats its process as optional, and any protocol error must degrade to
the builtin diff rather than abort the command.
Add subprocess_read_status_gently(): the same status loop, reading
through packet_read_with_status() with the gentle options, returning
-1 on a truncated or malformed packet and on an empty packet where a
status line or the terminating flush belongs. subprocess_read_status()
and its callers are unchanged.
The handshake has its gentle counterpart in 061a68e443 (sub-process:
use gentle handshake to avoid die() on startup failure, 2026-06-01),
which turned truncated handshake reads into error returns for every
caller. This series' base includes that commit, so a process that
dies during the handshake feeds the same non-fatal fallback as a
status failure here, and an optional diff process degrades to the
builtin diff on either kind of protocol error.
Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
271 lines
6.9 KiB
C
271 lines
6.9 KiB
C
/*
|
|
* Generic implementation of background process infrastructure.
|
|
*/
|
|
#include "git-compat-util.h"
|
|
#include "sub-process.h"
|
|
#include "sigchain.h"
|
|
#include "pkt-line.h"
|
|
|
|
int cmd2process_cmp(const void *cmp_data UNUSED,
|
|
const struct hashmap_entry *eptr,
|
|
const struct hashmap_entry *entry_or_key,
|
|
const void *keydata UNUSED)
|
|
{
|
|
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);
|
|
|
|
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.ent, strhash(cmd));
|
|
key.cmd = cmd;
|
|
return hashmap_get_entry(hashmap, &key, ent, NULL);
|
|
}
|
|
|
|
int subprocess_read_status(int fd, struct strbuf *status)
|
|
{
|
|
int len;
|
|
|
|
for (;;) {
|
|
char *line;
|
|
const char *value;
|
|
|
|
len = packet_read_line_gently(fd, NULL, &line);
|
|
if ((len < 0) || !line)
|
|
break;
|
|
if (skip_prefix(line, "status=", &value)) {
|
|
/* the last "status=<foo>" line wins */
|
|
strbuf_reset(status);
|
|
strbuf_addstr(status, value);
|
|
}
|
|
}
|
|
|
|
return (len < 0) ? len : 0;
|
|
}
|
|
|
|
int subprocess_read_status_gently(int fd, struct strbuf *status)
|
|
{
|
|
for (;;) {
|
|
int pktlen = -1;
|
|
enum packet_read_status rs;
|
|
const char *value;
|
|
|
|
rs = packet_read_with_status(fd, NULL, NULL, packet_buffer,
|
|
sizeof(packet_buffer), &pktlen,
|
|
PACKET_READ_CHOMP_NEWLINE |
|
|
PACKET_READ_GENTLE_ON_EOF |
|
|
PACKET_READ_GENTLE_ON_READ_ERROR);
|
|
if (rs == PACKET_READ_FLUSH)
|
|
return 0;
|
|
if (rs != PACKET_READ_NORMAL || !pktlen)
|
|
return -1;
|
|
if (skip_prefix(packet_buffer, "status=", &value)) {
|
|
/* the last "status=<foo>" line wins */
|
|
strbuf_reset(status);
|
|
strbuf_addstr(status, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
void subprocess_stop_command(struct subprocess_entry *entry)
|
|
{
|
|
if (!entry)
|
|
return;
|
|
|
|
entry->process.clean_on_exit = 0;
|
|
kill(entry->process.pid, SIGTERM);
|
|
finish_command(&entry->process);
|
|
}
|
|
|
|
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
|
|
{
|
|
if (!entry)
|
|
return;
|
|
|
|
subprocess_stop_command(entry);
|
|
hashmap_remove(hashmap, &entry->ent, 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_command(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);
|
|
strvec_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;
|
|
process->trace2_child_class = "subprocess";
|
|
|
|
err = start_command(process);
|
|
if (err) {
|
|
error("cannot fork to run subprocess '%s'", cmd);
|
|
return err;
|
|
}
|
|
|
|
err = startfn(entry);
|
|
if (err) {
|
|
error("initialization for subprocess '%s' failed", cmd);
|
|
subprocess_stop_command(entry);
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
|
|
subprocess_start_fn startfn)
|
|
{
|
|
int err;
|
|
|
|
err = subprocess_start_command(entry, cmd, startfn);
|
|
if (err)
|
|
return err;
|
|
|
|
hashmap_entry_init(&entry->ent, strhash(cmd));
|
|
hashmap_add(hashmap, &entry->ent);
|
|
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 (packet_read_line_gently(process->out, NULL, &line) < 0)
|
|
return error("could not read greeting from subprocess '%s'",
|
|
process->args.v[0]);
|
|
if (!line || !skip_prefix(line, welcome_prefix, &p) ||
|
|
strcmp(p, "-server"))
|
|
return error("Unexpected line '%s', expected %s-server",
|
|
line ? line : "<flush packet>", welcome_prefix);
|
|
if (packet_read_line_gently(process->out, NULL, &line) < 0)
|
|
return error("could not read version from subprocess '%s'",
|
|
process->args.v[0]);
|
|
if (!line || !skip_prefix(line, "version=", &p) ||
|
|
strtol_i(p, 10, chosen_version))
|
|
return error("Unexpected line '%s', expected version",
|
|
line ? line : "<flush packet>");
|
|
if (packet_read_line_gently(process->out, NULL, &line) < 0)
|
|
return error("could not read version flush from subprocess '%s'",
|
|
process->args.v[0]);
|
|
if (line)
|
|
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");
|
|
|
|
for (;;) {
|
|
const char *p;
|
|
int len = packet_read_line_gently(process->out, NULL, &line);
|
|
|
|
if (len < 0)
|
|
return error("could not read capabilities from subprocess '%s'",
|
|
process->args.v[0]);
|
|
if (!line)
|
|
break;
|
|
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 {
|
|
die("subprocess '%s' requested unsupported capability '%s'",
|
|
process->args.v[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;
|
|
}
|