]>
| Commit | Line | Data |
|---|---|---|
| 99605d62 BP |
1 | #ifndef SUBPROCESS_H |
| 2 | #define SUBPROCESS_H | |
| 3 | ||
| 99605d62 BP |
4 | #include "hashmap.h" |
| 5 | #include "run-command.h" | |
| 6 | ||
| 7 | /* | |
| 7e2e1bbb JT |
8 | * The sub-process API makes it possible to run background sub-processes |
| 9 | * for the entire lifetime of a Git invocation. If Git needs to communicate | |
| 10 | * with an external process multiple times, then this can reduces the process | |
| 11 | * invocation overhead. Git and the sub-process communicate through stdin and | |
| 12 | * stdout. | |
| 13 | * | |
| 14 | * The sub-processes are kept in a hashmap by command name and looked up | |
| 15 | * via the subprocess_find_entry function. If an existing instance can not | |
| 16 | * be found then a new process should be created and started. When the | |
| 17 | * parent git command terminates, all sub-processes are also terminated. | |
| 18 | * | |
| 19 | * This API is based on the run-command API. | |
| 99605d62 BP |
20 | */ |
| 21 | ||
| 22 | /* data structures */ | |
| 23 | ||
| 7e2e1bbb | 24 | /* Members should not be accessed directly. */ |
| 99605d62 | 25 | struct subprocess_entry { |
| e2b5038d | 26 | struct hashmap_entry ent; |
| 99605d62 BP |
27 | const char *cmd; |
| 28 | struct child_process process; | |
| 29 | }; | |
| 30 | ||
| fa64a2fd JT |
31 | struct subprocess_capability { |
| 32 | const char *name; | |
| 33 | ||
| 34 | /* | |
| 35 | * subprocess_handshake will "|=" this value to supported_capabilities | |
| 36 | * if the server reports that it supports this capability. | |
| 37 | */ | |
| 38 | unsigned int flag; | |
| 39 | }; | |
| 40 | ||
| 99605d62 BP |
41 | /* subprocess functions */ |
| 42 | ||
| 7e2e1bbb | 43 | /* Function to test two subprocess hashmap entries for equality. */ |
| 55454427 | 44 | int cmd2process_cmp(const void *unused_cmp_data, |
| 939af16e EW |
45 | const struct hashmap_entry *e, |
| 46 | const struct hashmap_entry *entry_or_key, | |
| ad6dad09 | 47 | const void *unused_keydata); |
| 99605d62 | 48 | |
| 7e2e1bbb JT |
49 | /* |
| 50 | * User-supplied function to initialize the sub-process. This is | |
| 51 | * typically used to negotiate the interface version and capabilities. | |
| 52 | */ | |
| 99605d62 | 53 | typedef int(*subprocess_start_fn)(struct subprocess_entry *entry); |
| 7e2e1bbb JT |
54 | |
| 55 | /* Start a subprocess and add it to the subprocess hashmap. */ | |
| 99605d62 BP |
56 | int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd, |
| 57 | subprocess_start_fn startfn); | |
| 58 | ||
| 7e2e1bbb | 59 | /* Kill a subprocess and remove it from the subprocess hashmap. */ |
| 99605d62 BP |
60 | void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry); |
| 61 | ||
| 7e2e1bbb | 62 | /* Find a subprocess in the subprocess hashmap. */ |
| 99605d62 BP |
63 | struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd); |
| 64 | ||
| 65 | /* subprocess helper functions */ | |
| 66 | ||
| 7e2e1bbb | 67 | /* Get the underlying `struct child_process` from a subprocess. */ |
| 99605d62 BP |
68 | static inline struct child_process *subprocess_get_child_process( |
| 69 | struct subprocess_entry *entry) | |
| 70 | { | |
| 71 | return &entry->process; | |
| 72 | } | |
| 73 | ||
| fa64a2fd | 74 | /* |
| addad105 | 75 | * Perform the version and capability negotiation as described in the |
| 3717a577 | 76 | * "Handshake" section of long-running-process-protocol.adoc using the |
| fa64a2fd JT |
77 | * given requested versions and capabilities. The "versions" and "capabilities" |
| 78 | * parameters are arrays terminated by a 0 or blank struct. | |
| 79 | * | |
| 80 | * This function is typically called when a subprocess is started (as part of | |
| 81 | * the "startfn" passed to subprocess_start). | |
| 82 | */ | |
| 83 | int subprocess_handshake(struct subprocess_entry *entry, | |
| 84 | const char *welcome_prefix, | |
| 85 | int *versions, | |
| 86 | int *chosen_version, | |
| 87 | struct subprocess_capability *capabilities, | |
| 88 | unsigned int *supported_capabilities); | |
| 89 | ||
| 99605d62 BP |
90 | /* |
| 91 | * Helper function that will read packets looking for "status=<foo>" | |
| 92 | * key/value pairs and return the value from the last "status" packet | |
| 93 | */ | |
| 94 | ||
| 4f2a2e9f | 95 | int subprocess_read_status(int fd, struct strbuf *status); |
| 99605d62 BP |
96 | |
| 97 | #endif |