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