blob: e85f21fa1a7c2bc59f032b5b2a2cffd6cab771a5 [file] [log] [blame]
Ben Peart99605d62017-05-05 11:28:01 -04001#ifndef SUBPROCESS_H
2#define SUBPROCESS_H
3
4#include "git-compat-util.h"
5#include "hashmap.h"
6#include "run-command.h"
7
8/*
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -07009 * The sub-process API makes it possible to run background sub-processes
10 * for the entire lifetime of a Git invocation. If Git needs to communicate
11 * with an external process multiple times, then this can reduces the process
12 * invocation overhead. Git and the sub-process communicate through stdin and
13 * stdout.
14 *
15 * The sub-processes are kept in a hashmap by command name and looked up
16 * via the subprocess_find_entry function. If an existing instance can not
17 * be found then a new process should be created and started. When the
18 * parent git command terminates, all sub-processes are also terminated.
19 *
20 * This API is based on the run-command API.
Ben Peart99605d62017-05-05 11:28:01 -040021 */
22
23 /* data structures */
24
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070025/* Members should not be accessed directly. */
Ben Peart99605d62017-05-05 11:28:01 -040026struct subprocess_entry {
Eric Wonge2b50382019-10-06 23:30:43 +000027 struct hashmap_entry ent;
Ben Peart99605d62017-05-05 11:28:01 -040028 const char *cmd;
29 struct child_process process;
30};
31
Jonathan Tanfa64a2f2017-07-26 11:17:29 -070032struct subprocess_capability {
33 const char *name;
34
35 /*
36 * subprocess_handshake will "|=" this value to supported_capabilities
37 * if the server reports that it supports this capability.
38 */
39 unsigned int flag;
40};
41
Ben Peart99605d62017-05-05 11:28:01 -040042/* subprocess functions */
43
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070044/* Function to test two subprocess hashmap entries for equality. */
Denton Liu55454422019-04-29 04:28:14 -040045int cmd2process_cmp(const void *unused_cmp_data,
Eric Wong939af162019-10-06 23:30:37 +000046 const struct hashmap_entry *e,
47 const struct hashmap_entry *entry_or_key,
Denton Liuad6dad02019-04-29 04:28:23 -040048 const void *unused_keydata);
Ben Peart99605d62017-05-05 11:28:01 -040049
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070050/*
51 * User-supplied function to initialize the sub-process. This is
52 * typically used to negotiate the interface version and capabilities.
53 */
Ben Peart99605d62017-05-05 11:28:01 -040054typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070055
56/* Start a subprocess and add it to the subprocess hashmap. */
Ben Peart99605d62017-05-05 11:28:01 -040057int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
58 subprocess_start_fn startfn);
59
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070060/* Kill a subprocess and remove it from the subprocess hashmap. */
Ben Peart99605d62017-05-05 11:28:01 -040061void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);
62
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070063/* Find a subprocess in the subprocess hashmap. */
Ben Peart99605d62017-05-05 11:28:01 -040064struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd);
65
66/* subprocess helper functions */
67
Jonathan Tan7e2e1bb2017-07-26 11:17:28 -070068/* Get the underlying `struct child_process` from a subprocess. */
Ben Peart99605d62017-05-05 11:28:01 -040069static inline struct child_process *subprocess_get_child_process(
70 struct subprocess_entry *entry)
71{
72 return &entry->process;
73}
74
75/*
Jonathan Tanaddad102018-01-25 10:53:24 -080076 * Perform the version and capability negotiation as described in the
77 * "Handshake" section of long-running-process-protocol.txt using the
Jonathan Tanfa64a2f2017-07-26 11:17:29 -070078 * given requested versions and capabilities. The "versions" and "capabilities"
79 * parameters are arrays terminated by a 0 or blank struct.
80 *
81 * This function is typically called when a subprocess is started (as part of
82 * the "startfn" passed to subprocess_start).
83 */
84int subprocess_handshake(struct subprocess_entry *entry,
85 const char *welcome_prefix,
86 int *versions,
87 int *chosen_version,
88 struct subprocess_capability *capabilities,
89 unsigned int *supported_capabilities);
90
91/*
Ben Peart99605d62017-05-05 11:28:01 -040092 * Helper function that will read packets looking for "status=<foo>"
93 * key/value pairs and return the value from the last "status" packet
94 */
95
Ben Peart4f2a2e92017-05-05 11:28:02 -040096int subprocess_read_status(int fd, struct strbuf *status);
Ben Peart99605d62017-05-05 11:28:01 -040097
98#endif