Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Generic implementation of background process infrastructure. |
| 3 | */ |
| 4 | #include "sub-process.h" |
| 5 | #include "sigchain.h" |
| 6 | #include "pkt-line.h" |
| 7 | |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 8 | int cmd2process_cmp(const void *unused_cmp_data, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 9 | const struct hashmap_entry *eptr, |
| 10 | const struct hashmap_entry *entry_or_key, |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 11 | const void *unused_keydata) |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 12 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 13 | const struct subprocess_entry *e1, *e2; |
| 14 | |
| 15 | e1 = container_of(eptr, const struct subprocess_entry, ent); |
| 16 | e2 = container_of(entry_or_key, const struct subprocess_entry, ent); |
Stefan Beller | 9ab4295 | 2017-06-30 17:28:33 -0700 | [diff] [blame] | 17 | |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 18 | return strcmp(e1->cmd, e2->cmd); |
| 19 | } |
| 20 | |
| 21 | struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd) |
| 22 | { |
| 23 | struct subprocess_entry key; |
| 24 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 25 | hashmap_entry_init(&key.ent, strhash(cmd)); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 26 | key.cmd = cmd; |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 27 | return hashmap_get_entry(hashmap, &key, ent, NULL); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 30 | int subprocess_read_status(int fd, struct strbuf *status) |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 31 | { |
| 32 | struct strbuf **pair; |
| 33 | char *line; |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 34 | int len; |
| 35 | |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 36 | for (;;) { |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 37 | len = packet_read_line_gently(fd, NULL, &line); |
| 38 | if ((len < 0) || !line) |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 39 | break; |
| 40 | pair = strbuf_split_str(line, '=', 2); |
| 41 | if (pair[0] && pair[0]->len && pair[1]) { |
| 42 | /* the last "status=<foo>" line wins */ |
| 43 | if (!strcmp(pair[0]->buf, "status=")) { |
| 44 | strbuf_reset(status); |
| 45 | strbuf_addbuf(status, pair[1]); |
| 46 | } |
| 47 | } |
| 48 | strbuf_list_free(pair); |
| 49 | } |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 50 | |
| 51 | return (len < 0) ? len : 0; |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry) |
| 55 | { |
| 56 | if (!entry) |
| 57 | return; |
| 58 | |
| 59 | entry->process.clean_on_exit = 0; |
| 60 | kill(entry->process.pid, SIGTERM); |
| 61 | finish_command(&entry->process); |
| 62 | |
Eric Wong | 28ee794 | 2019-10-06 23:30:31 +0000 | [diff] [blame] | 63 | hashmap_remove(hashmap, &entry->ent, NULL); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void subprocess_exit_handler(struct child_process *process) |
| 67 | { |
| 68 | sigchain_push(SIGPIPE, SIG_IGN); |
| 69 | /* Closing the pipe signals the subprocess to initiate a shutdown. */ |
| 70 | close(process->in); |
| 71 | close(process->out); |
| 72 | sigchain_pop(SIGPIPE); |
| 73 | /* Finish command will wait until the shutdown is complete. */ |
| 74 | finish_command(process); |
| 75 | } |
| 76 | |
| 77 | int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd, |
| 78 | subprocess_start_fn startfn) |
| 79 | { |
| 80 | int err; |
| 81 | struct child_process *process; |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 82 | |
| 83 | entry->cmd = cmd; |
| 84 | process = &entry->process; |
| 85 | |
| 86 | child_process_init(process); |
Johannes Sixt | 2944a94 | 2017-10-03 22:24:57 +0200 | [diff] [blame] | 87 | argv_array_push(&process->args, cmd); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 88 | process->use_shell = 1; |
| 89 | process->in = -1; |
| 90 | process->out = -1; |
| 91 | process->clean_on_exit = 1; |
| 92 | process->clean_on_exit_handler = subprocess_exit_handler; |
Jeff Hostetler | 0671b4d | 2019-02-22 14:25:05 -0800 | [diff] [blame] | 93 | process->trace2_child_class = "subprocess"; |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 94 | |
| 95 | err = start_command(process); |
| 96 | if (err) { |
| 97 | error("cannot fork to run subprocess '%s'", cmd); |
| 98 | return err; |
| 99 | } |
| 100 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 101 | hashmap_entry_init(&entry->ent, strhash(cmd)); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 102 | |
| 103 | err = startfn(entry); |
| 104 | if (err) { |
| 105 | error("initialization for subprocess '%s' failed", cmd); |
| 106 | subprocess_stop(hashmap, entry); |
| 107 | return err; |
| 108 | } |
| 109 | |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 110 | hashmap_add(hashmap, &entry->ent); |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Jonathan Tan | fa64a2f | 2017-07-26 11:17:29 -0700 | [diff] [blame] | 113 | |
| 114 | static int handshake_version(struct child_process *process, |
| 115 | const char *welcome_prefix, int *versions, |
| 116 | int *chosen_version) |
| 117 | { |
| 118 | int version_scratch; |
| 119 | int i; |
| 120 | char *line; |
| 121 | const char *p; |
| 122 | |
| 123 | if (!chosen_version) |
| 124 | chosen_version = &version_scratch; |
| 125 | |
| 126 | if (packet_write_fmt_gently(process->in, "%s-client\n", |
| 127 | welcome_prefix)) |
| 128 | return error("Could not write client identification"); |
| 129 | for (i = 0; versions[i]; i++) { |
| 130 | if (packet_write_fmt_gently(process->in, "version=%d\n", |
| 131 | versions[i])) |
| 132 | return error("Could not write requested version"); |
| 133 | } |
| 134 | if (packet_flush_gently(process->in)) |
| 135 | return error("Could not write flush packet"); |
| 136 | |
| 137 | if (!(line = packet_read_line(process->out, NULL)) || |
| 138 | !skip_prefix(line, welcome_prefix, &p) || |
| 139 | strcmp(p, "-server")) |
| 140 | return error("Unexpected line '%s', expected %s-server", |
| 141 | line ? line : "<flush packet>", welcome_prefix); |
| 142 | if (!(line = packet_read_line(process->out, NULL)) || |
| 143 | !skip_prefix(line, "version=", &p) || |
| 144 | strtol_i(p, 10, chosen_version)) |
| 145 | return error("Unexpected line '%s', expected version", |
| 146 | line ? line : "<flush packet>"); |
| 147 | if ((line = packet_read_line(process->out, NULL))) |
| 148 | return error("Unexpected line '%s', expected flush", line); |
| 149 | |
| 150 | /* Check to make sure that the version received is supported */ |
| 151 | for (i = 0; versions[i]; i++) { |
| 152 | if (versions[i] == *chosen_version) |
| 153 | break; |
| 154 | } |
| 155 | if (!versions[i]) |
| 156 | return error("Version %d not supported", *chosen_version); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int handshake_capabilities(struct child_process *process, |
| 162 | struct subprocess_capability *capabilities, |
| 163 | unsigned int *supported_capabilities) |
| 164 | { |
| 165 | int i; |
| 166 | char *line; |
| 167 | |
| 168 | for (i = 0; capabilities[i].name; i++) { |
| 169 | if (packet_write_fmt_gently(process->in, "capability=%s\n", |
| 170 | capabilities[i].name)) |
| 171 | return error("Could not write requested capability"); |
| 172 | } |
| 173 | if (packet_flush_gently(process->in)) |
| 174 | return error("Could not write flush packet"); |
| 175 | |
| 176 | while ((line = packet_read_line(process->out, NULL))) { |
| 177 | const char *p; |
| 178 | if (!skip_prefix(line, "capability=", &p)) |
| 179 | continue; |
| 180 | |
| 181 | for (i = 0; |
| 182 | capabilities[i].name && strcmp(p, capabilities[i].name); |
| 183 | i++) |
| 184 | ; |
| 185 | if (capabilities[i].name) { |
| 186 | if (supported_capabilities) |
| 187 | *supported_capabilities |= capabilities[i].flag; |
| 188 | } else { |
Junio C Hamano | ab46e6f | 2017-09-11 12:21:29 +0900 | [diff] [blame] | 189 | die("subprocess '%s' requested unsupported capability '%s'", |
| 190 | process->argv[0], p); |
Jonathan Tan | fa64a2f | 2017-07-26 11:17:29 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int subprocess_handshake(struct subprocess_entry *entry, |
| 198 | const char *welcome_prefix, |
| 199 | int *versions, |
| 200 | int *chosen_version, |
| 201 | struct subprocess_capability *capabilities, |
| 202 | unsigned int *supported_capabilities) |
| 203 | { |
| 204 | int retval; |
| 205 | struct child_process *process = &entry->process; |
| 206 | |
| 207 | sigchain_push(SIGPIPE, SIG_IGN); |
| 208 | |
| 209 | retval = handshake_version(process, welcome_prefix, versions, |
| 210 | chosen_version) || |
| 211 | handshake_capabilities(process, capabilities, |
| 212 | supported_capabilities); |
| 213 | |
| 214 | sigchain_pop(SIGPIPE); |
| 215 | return retval; |
| 216 | } |