2 * Generic implementation of background process infrastructure.
4 #include "sub-process.h"
8 int cmd2process_cmp(const void *unused_cmp_data,
10 const void *entry_or_key,
11 const void *unused_keydata)
13 const struct subprocess_entry *e1 = entry;
14 const struct subprocess_entry *e2 = entry_or_key;
16 return strcmp(e1->cmd, e2->cmd);
19 struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
21 struct subprocess_entry key;
23 hashmap_entry_init(&key.ent, strhash(cmd));
25 return hashmap_get_entry(hashmap, &key, NULL,
26 struct subprocess_entry, ent);
29 int subprocess_read_status(int fd, struct strbuf *status)
36 len = packet_read_line_gently(fd, NULL, &line);
37 if ((len < 0) || !line)
39 pair = strbuf_split_str(line, '=', 2);
40 if (pair[0] && pair[0]->len && pair[1]) {
41 /* the last "status=<foo>" line wins */
42 if (!strcmp(pair[0]->buf, "status=")) {
44 strbuf_addbuf(status, pair[1]);
47 strbuf_list_free(pair);
50 return (len < 0) ? len : 0;
53 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
58 entry->process.clean_on_exit = 0;
59 kill(entry->process.pid, SIGTERM);
60 finish_command(&entry->process);
62 hashmap_remove(hashmap, &entry->ent, NULL);
65 static void subprocess_exit_handler(struct child_process *process)
67 sigchain_push(SIGPIPE, SIG_IGN);
68 /* Closing the pipe signals the subprocess to initiate a shutdown. */
71 sigchain_pop(SIGPIPE);
72 /* Finish command will wait until the shutdown is complete. */
73 finish_command(process);
76 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
77 subprocess_start_fn startfn)
80 struct child_process *process;
83 process = &entry->process;
85 child_process_init(process);
86 argv_array_push(&process->args, cmd);
87 process->use_shell = 1;
90 process->clean_on_exit = 1;
91 process->clean_on_exit_handler = subprocess_exit_handler;
92 process->trace2_child_class = "subprocess";
94 err = start_command(process);
96 error("cannot fork to run subprocess '%s'", cmd);
100 hashmap_entry_init(&entry->ent, strhash(cmd));
102 err = startfn(entry);
104 error("initialization for subprocess '%s' failed", cmd);
105 subprocess_stop(hashmap, entry);
109 hashmap_add(hashmap, &entry->ent);
113 static int handshake_version(struct child_process *process,
114 const char *welcome_prefix, int *versions,
123 chosen_version = &version_scratch;
125 if (packet_write_fmt_gently(process->in, "%s-client\n",
127 return error("Could not write client identification");
128 for (i = 0; versions[i]; i++) {
129 if (packet_write_fmt_gently(process->in, "version=%d\n",
131 return error("Could not write requested version");
133 if (packet_flush_gently(process->in))
134 return error("Could not write flush packet");
136 if (!(line = packet_read_line(process->out, NULL)) ||
137 !skip_prefix(line, welcome_prefix, &p) ||
138 strcmp(p, "-server"))
139 return error("Unexpected line '%s', expected %s-server",
140 line ? line : "<flush packet>", welcome_prefix);
141 if (!(line = packet_read_line(process->out, NULL)) ||
142 !skip_prefix(line, "version=", &p) ||
143 strtol_i(p, 10, chosen_version))
144 return error("Unexpected line '%s', expected version",
145 line ? line : "<flush packet>");
146 if ((line = packet_read_line(process->out, NULL)))
147 return error("Unexpected line '%s', expected flush", line);
149 /* Check to make sure that the version received is supported */
150 for (i = 0; versions[i]; i++) {
151 if (versions[i] == *chosen_version)
155 return error("Version %d not supported", *chosen_version);
160 static int handshake_capabilities(struct child_process *process,
161 struct subprocess_capability *capabilities,
162 unsigned int *supported_capabilities)
167 for (i = 0; capabilities[i].name; i++) {
168 if (packet_write_fmt_gently(process->in, "capability=%s\n",
169 capabilities[i].name))
170 return error("Could not write requested capability");
172 if (packet_flush_gently(process->in))
173 return error("Could not write flush packet");
175 while ((line = packet_read_line(process->out, NULL))) {
177 if (!skip_prefix(line, "capability=", &p))
181 capabilities[i].name && strcmp(p, capabilities[i].name);
184 if (capabilities[i].name) {
185 if (supported_capabilities)
186 *supported_capabilities |= capabilities[i].flag;
188 die("subprocess '%s' requested unsupported capability '%s'",
189 process->argv[0], p);
196 int subprocess_handshake(struct subprocess_entry *entry,
197 const char *welcome_prefix,
200 struct subprocess_capability *capabilities,
201 unsigned int *supported_capabilities)
204 struct child_process *process = &entry->process;
206 sigchain_push(SIGPIPE, SIG_IGN);
208 retval = handshake_version(process, welcome_prefix, versions,
210 handshake_capabilities(process, capabilities,
211 supported_capabilities);
213 sigchain_pop(SIGPIPE);