Sync with 2.13.6
[alt-git.git] / sub-process.c
blobfcc4832c14f3fdbb41bea12142a8fbf167d2aa55
1 /*
2 * Generic implementation of background process infrastructure.
3 */
4 #include "sub-process.h"
5 #include "sigchain.h"
6 #include "pkt-line.h"
8 int cmd2process_cmp(const void *unused_cmp_data,
9 const struct subprocess_entry *e1,
10 const struct subprocess_entry *e2,
11 const void *unused_keydata)
13 return strcmp(e1->cmd, e2->cmd);
16 struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
18 struct subprocess_entry key;
20 hashmap_entry_init(&key, strhash(cmd));
21 key.cmd = cmd;
22 return hashmap_get(hashmap, &key, NULL);
25 int subprocess_read_status(int fd, struct strbuf *status)
27 struct strbuf **pair;
28 char *line;
29 int len;
31 for (;;) {
32 len = packet_read_line_gently(fd, NULL, &line);
33 if ((len < 0) || !line)
34 break;
35 pair = strbuf_split_str(line, '=', 2);
36 if (pair[0] && pair[0]->len && pair[1]) {
37 /* the last "status=<foo>" line wins */
38 if (!strcmp(pair[0]->buf, "status=")) {
39 strbuf_reset(status);
40 strbuf_addbuf(status, pair[1]);
43 strbuf_list_free(pair);
46 return (len < 0) ? len : 0;
49 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
51 if (!entry)
52 return;
54 entry->process.clean_on_exit = 0;
55 kill(entry->process.pid, SIGTERM);
56 finish_command(&entry->process);
58 hashmap_remove(hashmap, entry, NULL);
61 static void subprocess_exit_handler(struct child_process *process)
63 sigchain_push(SIGPIPE, SIG_IGN);
64 /* Closing the pipe signals the subprocess to initiate a shutdown. */
65 close(process->in);
66 close(process->out);
67 sigchain_pop(SIGPIPE);
68 /* Finish command will wait until the shutdown is complete. */
69 finish_command(process);
72 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
73 subprocess_start_fn startfn)
75 int err;
76 struct child_process *process;
77 const char *argv[] = { cmd, NULL };
79 entry->cmd = cmd;
80 process = &entry->process;
82 child_process_init(process);
83 process->argv = argv;
84 process->use_shell = 1;
85 process->in = -1;
86 process->out = -1;
87 process->clean_on_exit = 1;
88 process->clean_on_exit_handler = subprocess_exit_handler;
90 err = start_command(process);
91 if (err) {
92 error("cannot fork to run subprocess '%s'", cmd);
93 return err;
96 hashmap_entry_init(entry, strhash(cmd));
98 err = startfn(entry);
99 if (err) {
100 error("initialization for subprocess '%s' failed", cmd);
101 subprocess_stop(hashmap, entry);
102 return err;
105 hashmap_add(hashmap, entry);
106 return 0;
109 static int handshake_version(struct child_process *process,
110 const char *welcome_prefix, int *versions,
111 int *chosen_version)
113 int version_scratch;
114 int i;
115 char *line;
116 const char *p;
118 if (!chosen_version)
119 chosen_version = &version_scratch;
121 if (packet_write_fmt_gently(process->in, "%s-client\n",
122 welcome_prefix))
123 return error("Could not write client identification");
124 for (i = 0; versions[i]; i++) {
125 if (packet_write_fmt_gently(process->in, "version=%d\n",
126 versions[i]))
127 return error("Could not write requested version");
129 if (packet_flush_gently(process->in))
130 return error("Could not write flush packet");
132 if (!(line = packet_read_line(process->out, NULL)) ||
133 !skip_prefix(line, welcome_prefix, &p) ||
134 strcmp(p, "-server"))
135 return error("Unexpected line '%s', expected %s-server",
136 line ? line : "<flush packet>", welcome_prefix);
137 if (!(line = packet_read_line(process->out, NULL)) ||
138 !skip_prefix(line, "version=", &p) ||
139 strtol_i(p, 10, chosen_version))
140 return error("Unexpected line '%s', expected version",
141 line ? line : "<flush packet>");
142 if ((line = packet_read_line(process->out, NULL)))
143 return error("Unexpected line '%s', expected flush", line);
145 /* Check to make sure that the version received is supported */
146 for (i = 0; versions[i]; i++) {
147 if (versions[i] == *chosen_version)
148 break;
150 if (!versions[i])
151 return error("Version %d not supported", *chosen_version);
153 return 0;
156 static int handshake_capabilities(struct child_process *process,
157 struct subprocess_capability *capabilities,
158 unsigned int *supported_capabilities)
160 int i;
161 char *line;
163 for (i = 0; capabilities[i].name; i++) {
164 if (packet_write_fmt_gently(process->in, "capability=%s\n",
165 capabilities[i].name))
166 return error("Could not write requested capability");
168 if (packet_flush_gently(process->in))
169 return error("Could not write flush packet");
171 while ((line = packet_read_line(process->out, NULL))) {
172 const char *p;
173 if (!skip_prefix(line, "capability=", &p))
174 continue;
176 for (i = 0;
177 capabilities[i].name && strcmp(p, capabilities[i].name);
178 i++)
180 if (capabilities[i].name) {
181 if (supported_capabilities)
182 *supported_capabilities |= capabilities[i].flag;
183 } else {
184 warning("subprocess '%s' requested unsupported capability '%s'",
185 process->argv[0], p);
189 return 0;
192 int subprocess_handshake(struct subprocess_entry *entry,
193 const char *welcome_prefix,
194 int *versions,
195 int *chosen_version,
196 struct subprocess_capability *capabilities,
197 unsigned int *supported_capabilities)
199 int retval;
200 struct child_process *process = &entry->process;
202 sigchain_push(SIGPIPE, SIG_IGN);
204 retval = handshake_version(process, welcome_prefix, versions,
205 chosen_version) ||
206 handshake_capabilities(process, capabilities,
207 supported_capabilities);
209 sigchain_pop(SIGPIPE);
210 return retval;