advice: revamp advise API
[git] / sub-process.c
1 /*
2  * Generic implementation of background process infrastructure.
3  */
4 #include "sub-process.h"
5 #include "sigchain.h"
6 #include "pkt-line.h"
7
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)
12 {
13         const struct subprocess_entry *e1, *e2;
14
15         e1 = container_of(eptr, const struct subprocess_entry, ent);
16         e2 = container_of(entry_or_key, const struct subprocess_entry, ent);
17
18         return strcmp(e1->cmd, e2->cmd);
19 }
20
21 struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd)
22 {
23         struct subprocess_entry key;
24
25         hashmap_entry_init(&key.ent, strhash(cmd));
26         key.cmd = cmd;
27         return hashmap_get_entry(hashmap, &key, ent, NULL);
28 }
29
30 int subprocess_read_status(int fd, struct strbuf *status)
31 {
32         struct strbuf **pair;
33         char *line;
34         int len;
35
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]);
46                         }
47                 }
48                 strbuf_list_free(pair);
49         }
50
51         return (len < 0) ? len : 0;
52 }
53
54 void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
55 {
56         if (!entry)
57                 return;
58
59         entry->process.clean_on_exit = 0;
60         kill(entry->process.pid, SIGTERM);
61         finish_command(&entry->process);
62
63         hashmap_remove(hashmap, &entry->ent, NULL);
64 }
65
66 static void subprocess_exit_handler(struct child_process *process)
67 {
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);
75 }
76
77 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
78         subprocess_start_fn startfn)
79 {
80         int err;
81         struct child_process *process;
82
83         entry->cmd = cmd;
84         process = &entry->process;
85
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";
94
95         err = start_command(process);
96         if (err) {
97                 error("cannot fork to run subprocess '%s'", cmd);
98                 return err;
99         }
100
101         hashmap_entry_init(&entry->ent, strhash(cmd));
102
103         err = startfn(entry);
104         if (err) {
105                 error("initialization for subprocess '%s' failed", cmd);
106                 subprocess_stop(hashmap, entry);
107                 return err;
108         }
109
110         hashmap_add(hashmap, &entry->ent);
111         return 0;
112 }
113
114 static int handshake_version(struct child_process *process,
115                              const char *welcome_prefix, int *versions,
116                              int *chosen_version)
117 {
118         int version_scratch;
119         int i;
120         char *line;
121         const char *p;
122
123         if (!chosen_version)
124                 chosen_version = &version_scratch;
125
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");
133         }
134         if (packet_flush_gently(process->in))
135                 return error("Could not write flush packet");
136
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);
149
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;
154         }
155         if (!versions[i])
156                 return error("Version %d not supported", *chosen_version);
157
158         return 0;
159 }
160
161 static int handshake_capabilities(struct child_process *process,
162                                   struct subprocess_capability *capabilities,
163                                   unsigned int *supported_capabilities)
164 {
165         int i;
166         char *line;
167
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");
172         }
173         if (packet_flush_gently(process->in))
174                 return error("Could not write flush packet");
175
176         while ((line = packet_read_line(process->out, NULL))) {
177                 const char *p;
178                 if (!skip_prefix(line, "capability=", &p))
179                         continue;
180
181                 for (i = 0;
182                      capabilities[i].name && strcmp(p, capabilities[i].name);
183                      i++)
184                         ;
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);
191                 }
192         }
193
194         return 0;
195 }
196
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)
203 {
204         int retval;
205         struct child_process *process = &entry->process;
206
207         sigchain_push(SIGPIPE, SIG_IGN);
208
209         retval = handshake_version(process, welcome_prefix, versions,
210                                    chosen_version) ||
211                  handshake_capabilities(process, capabilities,
212                                         supported_capabilities);
213
214         sigchain_pop(SIGPIPE);
215         return retval;
216 }