progress: call trace2_region_leave() only after calling _enter()
[git/debian.git] / sub-process.c
blob1b1af9dcbd95990dfd9d0ef11471f766a6015a80
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 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));
26 key.cmd = cmd;
27 return hashmap_get_entry(hashmap, &key, ent, NULL);
30 int subprocess_read_status(int fd, struct strbuf *status)
32 struct strbuf **pair;
33 char *line;
34 int len;
36 for (;;) {
37 len = packet_read_line_gently(fd, NULL, &line);
38 if ((len < 0) || !line)
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]);
48 strbuf_list_free(pair);
51 return (len < 0) ? len : 0;
54 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
56 if (!entry)
57 return;
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. */
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);
77 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
78 subprocess_start_fn startfn)
80 int err;
81 struct child_process *process;
83 entry->cmd = cmd;
84 process = &entry->process;
86 child_process_init(process);
87 argv_array_push(&process->args, cmd);
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;
93 process->trace2_child_class = "subprocess";
95 err = start_command(process);
96 if (err) {
97 error("cannot fork to run subprocess '%s'", cmd);
98 return err;
101 hashmap_entry_init(&entry->ent, strhash(cmd));
103 err = startfn(entry);
104 if (err) {
105 error("initialization for subprocess '%s' failed", cmd);
106 subprocess_stop(hashmap, entry);
107 return err;
110 hashmap_add(hashmap, &entry->ent);
111 return 0;
114 static int handshake_version(struct child_process *process,
115 const char *welcome_prefix, int *versions,
116 int *chosen_version)
118 int version_scratch;
119 int i;
120 char *line;
121 const char *p;
123 if (!chosen_version)
124 chosen_version = &version_scratch;
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");
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)
153 break;
155 if (!versions[i])
156 return error("Version %d not supported", *chosen_version);
158 return 0;
161 static int handshake_capabilities(struct child_process *process,
162 struct subprocess_capability *capabilities,
163 unsigned int *supported_capabilities)
165 int i;
166 char *line;
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))) {
177 const char *p;
178 if (!skip_prefix(line, "capability=", &p))
179 continue;
181 for (i = 0;
182 capabilities[i].name && strcmp(p, capabilities[i].name);
183 i++)
185 if (capabilities[i].name) {
186 if (supported_capabilities)
187 *supported_capabilities |= capabilities[i].flag;
188 } else {
189 die("subprocess '%s' requested unsupported capability '%s'",
190 process->argv[0], p);
194 return 0;
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)
204 int retval;
205 struct child_process *process = &entry->process;
207 sigchain_push(SIGPIPE, SIG_IGN);
209 retval = handshake_version(process, welcome_prefix, versions,
210 chosen_version) ||
211 handshake_capabilities(process, capabilities,
212 supported_capabilities);
214 sigchain_pop(SIGPIPE);
215 return retval;