Merge branch 'rj/restore-plug-leaks'
[alt-git.git] / sub-process.c
blob1daf5a975254b9b9ea286c5486f4d07f3874bb26
1 /*
2 * Generic implementation of background process infrastructure.
3 */
4 #include "git-compat-util.h"
5 #include "sub-process.h"
6 #include "sigchain.h"
7 #include "pkt-line.h"
9 int cmd2process_cmp(const void *cmp_data UNUSED,
10 const struct hashmap_entry *eptr,
11 const struct hashmap_entry *entry_or_key,
12 const void *keydata UNUSED)
14 const struct subprocess_entry *e1, *e2;
16 e1 = container_of(eptr, const struct subprocess_entry, ent);
17 e2 = container_of(entry_or_key, const struct subprocess_entry, ent);
19 return strcmp(e1->cmd, e2->cmd);
22 struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
24 struct subprocess_entry key;
26 hashmap_entry_init(&key.ent, strhash(cmd));
27 key.cmd = cmd;
28 return hashmap_get_entry(hashmap, &key, ent, NULL);
31 int subprocess_read_status(int fd, struct strbuf *status)
33 struct strbuf **pair;
34 char *line;
35 int len;
37 for (;;) {
38 len = packet_read_line_gently(fd, NULL, &line);
39 if ((len < 0) || !line)
40 break;
41 pair = strbuf_split_str(line, '=', 2);
42 if (pair[0] && pair[0]->len && pair[1]) {
43 /* the last "status=<foo>" line wins */
44 if (!strcmp(pair[0]->buf, "status=")) {
45 strbuf_reset(status);
46 strbuf_addbuf(status, pair[1]);
49 strbuf_list_free(pair);
52 return (len < 0) ? len : 0;
55 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
57 if (!entry)
58 return;
60 entry->process.clean_on_exit = 0;
61 kill(entry->process.pid, SIGTERM);
62 finish_command(&entry->process);
64 hashmap_remove(hashmap, &entry->ent, NULL);
67 static void subprocess_exit_handler(struct child_process *process)
69 sigchain_push(SIGPIPE, SIG_IGN);
70 /* Closing the pipe signals the subprocess to initiate a shutdown. */
71 close(process->in);
72 close(process->out);
73 sigchain_pop(SIGPIPE);
74 /* Finish command will wait until the shutdown is complete. */
75 finish_command(process);
78 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
79 subprocess_start_fn startfn)
81 int err;
82 struct child_process *process;
84 entry->cmd = cmd;
85 process = &entry->process;
87 child_process_init(process);
88 strvec_push(&process->args, cmd);
89 process->use_shell = 1;
90 process->in = -1;
91 process->out = -1;
92 process->clean_on_exit = 1;
93 process->clean_on_exit_handler = subprocess_exit_handler;
94 process->trace2_child_class = "subprocess";
96 err = start_command(process);
97 if (err) {
98 error("cannot fork to run subprocess '%s'", cmd);
99 return err;
102 hashmap_entry_init(&entry->ent, strhash(cmd));
104 err = startfn(entry);
105 if (err) {
106 error("initialization for subprocess '%s' failed", cmd);
107 subprocess_stop(hashmap, entry);
108 return err;
111 hashmap_add(hashmap, &entry->ent);
112 return 0;
115 static int handshake_version(struct child_process *process,
116 const char *welcome_prefix, int *versions,
117 int *chosen_version)
119 int version_scratch;
120 int i;
121 char *line;
122 const char *p;
124 if (!chosen_version)
125 chosen_version = &version_scratch;
127 if (packet_write_fmt_gently(process->in, "%s-client\n",
128 welcome_prefix))
129 return error("Could not write client identification");
130 for (i = 0; versions[i]; i++) {
131 if (packet_write_fmt_gently(process->in, "version=%d\n",
132 versions[i]))
133 return error("Could not write requested version");
135 if (packet_flush_gently(process->in))
136 return error("Could not write flush packet");
138 if (!(line = packet_read_line(process->out, NULL)) ||
139 !skip_prefix(line, welcome_prefix, &p) ||
140 strcmp(p, "-server"))
141 return error("Unexpected line '%s', expected %s-server",
142 line ? line : "<flush packet>", welcome_prefix);
143 if (!(line = packet_read_line(process->out, NULL)) ||
144 !skip_prefix(line, "version=", &p) ||
145 strtol_i(p, 10, chosen_version))
146 return error("Unexpected line '%s', expected version",
147 line ? line : "<flush packet>");
148 if ((line = packet_read_line(process->out, NULL)))
149 return error("Unexpected line '%s', expected flush", line);
151 /* Check to make sure that the version received is supported */
152 for (i = 0; versions[i]; i++) {
153 if (versions[i] == *chosen_version)
154 break;
156 if (!versions[i])
157 return error("Version %d not supported", *chosen_version);
159 return 0;
162 static int handshake_capabilities(struct child_process *process,
163 struct subprocess_capability *capabilities,
164 unsigned int *supported_capabilities)
166 int i;
167 char *line;
169 for (i = 0; capabilities[i].name; i++) {
170 if (packet_write_fmt_gently(process->in, "capability=%s\n",
171 capabilities[i].name))
172 return error("Could not write requested capability");
174 if (packet_flush_gently(process->in))
175 return error("Could not write flush packet");
177 while ((line = packet_read_line(process->out, NULL))) {
178 const char *p;
179 if (!skip_prefix(line, "capability=", &p))
180 continue;
182 for (i = 0;
183 capabilities[i].name && strcmp(p, capabilities[i].name);
184 i++)
186 if (capabilities[i].name) {
187 if (supported_capabilities)
188 *supported_capabilities |= capabilities[i].flag;
189 } else {
190 die("subprocess '%s' requested unsupported capability '%s'",
191 process->args.v[0], p);
195 return 0;
198 int subprocess_handshake(struct subprocess_entry *entry,
199 const char *welcome_prefix,
200 int *versions,
201 int *chosen_version,
202 struct subprocess_capability *capabilities,
203 unsigned int *supported_capabilities)
205 int retval;
206 struct child_process *process = &entry->process;
208 sigchain_push(SIGPIPE, SIG_IGN);
210 retval = handshake_version(process, welcome_prefix, versions,
211 chosen_version) ||
212 handshake_capabilities(process, capabilities,
213 supported_capabilities);
215 sigchain_pop(SIGPIPE);
216 return retval;