2 * Generic implementation of background process infrastructure.
4 #include "sub-process.h"
8 int cmd2process_cmp(const void *unused_cmp_data,
9 const struct hashmap_entry *eptr,
10 const struct hashmap_entry *entry_or_key,
11 const void *unused_keydata)
13 const struct subprocess_entry *e1, *e2;
15 e1 = container_of(eptr, const struct subprocess_entry, ent);
16 e2 = container_of(entry_or_key, const struct subprocess_entry, ent);
18 return strcmp(e1->cmd, e2->cmd);
21 struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
23 struct subprocess_entry key;
25 hashmap_entry_init(&key.ent, strhash(cmd));
27 return hashmap_get_entry(hashmap, &key, ent, NULL);
30 int subprocess_read_status(int fd, struct strbuf *status)
37 len = packet_read_line_gently(fd, NULL, &line);
38 if ((len < 0) || !line)
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=")) {
45 strbuf_addbuf(status, pair[1]);
48 strbuf_list_free(pair);
51 return (len < 0) ? len : 0;
54 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
59 entry->process.clean_on_exit = 0;
60 kill(entry->process.pid, SIGTERM);
61 finish_command(&entry->process);
63 hashmap_remove(hashmap, &entry->ent, NULL);
66 static void subprocess_exit_handler(struct child_process *process)
68 sigchain_push(SIGPIPE, SIG_IGN);
69 /* Closing the pipe signals the subprocess to initiate a shutdown. */
72 sigchain_pop(SIGPIPE);
73 /* Finish command will wait until the shutdown is complete. */
74 finish_command(process);
77 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
78 subprocess_start_fn startfn)
81 struct child_process *process;
84 process = &entry->process;
86 child_process_init(process);
87 argv_array_push(&process->args, cmd);
88 process->use_shell = 1;
91 process->clean_on_exit = 1;
92 process->clean_on_exit_handler = subprocess_exit_handler;
93 process->trace2_child_class = "subprocess";
95 err = start_command(process);
97 error("cannot fork to run subprocess '%s'", cmd);
101 hashmap_entry_init(&entry->ent, strhash(cmd));
103 err = startfn(entry);
105 error("initialization for subprocess '%s' failed", cmd);
106 subprocess_stop(hashmap, entry);
110 hashmap_add(hashmap, &entry->ent);
114 static int handshake_version(struct child_process *process,
115 const char *welcome_prefix, int *versions,
124 chosen_version = &version_scratch;
126 if (packet_write_fmt_gently(process->in, "%s-client\n",
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",
132 return error("Could not write requested version");
134 if (packet_flush_gently(process->in))
135 return error("Could not write flush packet");
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);
150 /* Check to make sure that the version received is supported */
151 for (i = 0; versions[i]; i++) {
152 if (versions[i] == *chosen_version)
156 return error("Version %d not supported", *chosen_version);
161 static int handshake_capabilities(struct child_process *process,
162 struct subprocess_capability *capabilities,
163 unsigned int *supported_capabilities)
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");
173 if (packet_flush_gently(process->in))
174 return error("Could not write flush packet");
176 while ((line = packet_read_line(process->out, NULL))) {
178 if (!skip_prefix(line, "capability=", &p))
182 capabilities[i].name && strcmp(p, capabilities[i].name);
185 if (capabilities[i].name) {
186 if (supported_capabilities)
187 *supported_capabilities |= capabilities[i].flag;
189 die("subprocess '%s' requested unsupported capability '%s'",
190 process->argv[0], p);
197 int subprocess_handshake(struct subprocess_entry *entry,
198 const char *welcome_prefix,
201 struct subprocess_capability *capabilities,
202 unsigned int *supported_capabilities)
205 struct child_process *process = &entry->process;
207 sigchain_push(SIGPIPE, SIG_IGN);
209 retval = handshake_version(process, welcome_prefix, versions,
211 handshake_capabilities(process, capabilities,
212 supported_capabilities);
214 sigchain_pop(SIGPIPE);