t5318: use 'test_cmp_bin' to compare commit-graph files
[alt-git.git] / sub-process.c
blob8d2a1707cfe1a7d0fb6b589f0857a6389a8a3d70
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 void *entry,
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, strhash(cmd));
24 key.cmd = cmd;
25 return hashmap_get(hashmap, &key, NULL);
28 int subprocess_read_status(int fd, struct strbuf *status)
30 struct strbuf **pair;
31 char *line;
32 int len;
34 for (;;) {
35 len = packet_read_line_gently(fd, NULL, &line);
36 if ((len < 0) || !line)
37 break;
38 pair = strbuf_split_str(line, '=', 2);
39 if (pair[0] && pair[0]->len && pair[1]) {
40 /* the last "status=<foo>" line wins */
41 if (!strcmp(pair[0]->buf, "status=")) {
42 strbuf_reset(status);
43 strbuf_addbuf(status, pair[1]);
46 strbuf_list_free(pair);
49 return (len < 0) ? len : 0;
52 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
54 if (!entry)
55 return;
57 entry->process.clean_on_exit = 0;
58 kill(entry->process.pid, SIGTERM);
59 finish_command(&entry->process);
61 hashmap_remove(hashmap, entry, NULL);
64 static void subprocess_exit_handler(struct child_process *process)
66 sigchain_push(SIGPIPE, SIG_IGN);
67 /* Closing the pipe signals the subprocess to initiate a shutdown. */
68 close(process->in);
69 close(process->out);
70 sigchain_pop(SIGPIPE);
71 /* Finish command will wait until the shutdown is complete. */
72 finish_command(process);
75 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
76 subprocess_start_fn startfn)
78 int err;
79 struct child_process *process;
81 entry->cmd = cmd;
82 process = &entry->process;
84 child_process_init(process);
85 argv_array_push(&process->args, cmd);
86 process->use_shell = 1;
87 process->in = -1;
88 process->out = -1;
89 process->clean_on_exit = 1;
90 process->clean_on_exit_handler = subprocess_exit_handler;
92 err = start_command(process);
93 if (err) {
94 error("cannot fork to run subprocess '%s'", cmd);
95 return err;
98 hashmap_entry_init(entry, strhash(cmd));
100 err = startfn(entry);
101 if (err) {
102 error("initialization for subprocess '%s' failed", cmd);
103 subprocess_stop(hashmap, entry);
104 return err;
107 hashmap_add(hashmap, entry);
108 return 0;
111 static int handshake_version(struct child_process *process,
112 const char *welcome_prefix, int *versions,
113 int *chosen_version)
115 int version_scratch;
116 int i;
117 char *line;
118 const char *p;
120 if (!chosen_version)
121 chosen_version = &version_scratch;
123 if (packet_write_fmt_gently(process->in, "%s-client\n",
124 welcome_prefix))
125 return error("Could not write client identification");
126 for (i = 0; versions[i]; i++) {
127 if (packet_write_fmt_gently(process->in, "version=%d\n",
128 versions[i]))
129 return error("Could not write requested version");
131 if (packet_flush_gently(process->in))
132 return error("Could not write flush packet");
134 if (!(line = packet_read_line(process->out, NULL)) ||
135 !skip_prefix(line, welcome_prefix, &p) ||
136 strcmp(p, "-server"))
137 return error("Unexpected line '%s', expected %s-server",
138 line ? line : "<flush packet>", welcome_prefix);
139 if (!(line = packet_read_line(process->out, NULL)) ||
140 !skip_prefix(line, "version=", &p) ||
141 strtol_i(p, 10, chosen_version))
142 return error("Unexpected line '%s', expected version",
143 line ? line : "<flush packet>");
144 if ((line = packet_read_line(process->out, NULL)))
145 return error("Unexpected line '%s', expected flush", line);
147 /* Check to make sure that the version received is supported */
148 for (i = 0; versions[i]; i++) {
149 if (versions[i] == *chosen_version)
150 break;
152 if (!versions[i])
153 return error("Version %d not supported", *chosen_version);
155 return 0;
158 static int handshake_capabilities(struct child_process *process,
159 struct subprocess_capability *capabilities,
160 unsigned int *supported_capabilities)
162 int i;
163 char *line;
165 for (i = 0; capabilities[i].name; i++) {
166 if (packet_write_fmt_gently(process->in, "capability=%s\n",
167 capabilities[i].name))
168 return error("Could not write requested capability");
170 if (packet_flush_gently(process->in))
171 return error("Could not write flush packet");
173 while ((line = packet_read_line(process->out, NULL))) {
174 const char *p;
175 if (!skip_prefix(line, "capability=", &p))
176 continue;
178 for (i = 0;
179 capabilities[i].name && strcmp(p, capabilities[i].name);
180 i++)
182 if (capabilities[i].name) {
183 if (supported_capabilities)
184 *supported_capabilities |= capabilities[i].flag;
185 } else {
186 die("subprocess '%s' requested unsupported capability '%s'",
187 process->argv[0], p);
191 return 0;
194 int subprocess_handshake(struct subprocess_entry *entry,
195 const char *welcome_prefix,
196 int *versions,
197 int *chosen_version,
198 struct subprocess_capability *capabilities,
199 unsigned int *supported_capabilities)
201 int retval;
202 struct child_process *process = &entry->process;
204 sigchain_push(SIGPIPE, SIG_IGN);
206 retval = handshake_version(process, welcome_prefix, versions,
207 chosen_version) ||
208 handshake_capabilities(process, capabilities,
209 supported_capabilities);
211 sigchain_pop(SIGPIPE);
212 return retval;