]>
Commit | Line | Data |
---|---|---|
99605d62 BP |
1 | /* |
2 | * Generic implementation of background process infrastructure. | |
3 | */ | |
4 | #include "sub-process.h" | |
5 | #include "sigchain.h" | |
6 | #include "pkt-line.h" | |
7 | ||
7663cdc8 | 8 | int cmd2process_cmp(const void *unused_cmp_data, |
9ab42958 SB |
9 | const void *entry, |
10 | const void *entry_or_key, | |
7663cdc8 | 11 | const void *unused_keydata) |
99605d62 | 12 | { |
9ab42958 SB |
13 | const struct subprocess_entry *e1 = entry; |
14 | const struct subprocess_entry *e2 = entry_or_key; | |
15 | ||
99605d62 BP |
16 | return strcmp(e1->cmd, e2->cmd); |
17 | } | |
18 | ||
19 | struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd) | |
20 | { | |
21 | struct subprocess_entry key; | |
22 | ||
d22245a2 | 23 | hashmap_entry_init(&key.ent, strhash(cmd)); |
99605d62 | 24 | key.cmd = cmd; |
f23a4651 EW |
25 | return hashmap_get_entry(hashmap, &key, NULL, |
26 | struct subprocess_entry, ent); | |
99605d62 BP |
27 | } |
28 | ||
4f2a2e9f | 29 | int subprocess_read_status(int fd, struct strbuf *status) |
99605d62 BP |
30 | { |
31 | struct strbuf **pair; | |
32 | char *line; | |
4f2a2e9f BP |
33 | int len; |
34 | ||
99605d62 | 35 | for (;;) { |
4f2a2e9f BP |
36 | len = packet_read_line_gently(fd, NULL, &line); |
37 | if ((len < 0) || !line) | |
99605d62 BP |
38 | break; |
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=")) { | |
43 | strbuf_reset(status); | |
44 | strbuf_addbuf(status, pair[1]); | |
45 | } | |
46 | } | |
47 | strbuf_list_free(pair); | |
48 | } | |
4f2a2e9f BP |
49 | |
50 | return (len < 0) ? len : 0; | |
99605d62 BP |
51 | } |
52 | ||
53 | void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry) | |
54 | { | |
55 | if (!entry) | |
56 | return; | |
57 | ||
58 | entry->process.clean_on_exit = 0; | |
59 | kill(entry->process.pid, SIGTERM); | |
60 | finish_command(&entry->process); | |
61 | ||
28ee7941 | 62 | hashmap_remove(hashmap, &entry->ent, NULL); |
99605d62 BP |
63 | } |
64 | ||
65 | static void subprocess_exit_handler(struct child_process *process) | |
66 | { | |
67 | sigchain_push(SIGPIPE, SIG_IGN); | |
68 | /* Closing the pipe signals the subprocess to initiate a shutdown. */ | |
69 | close(process->in); | |
70 | close(process->out); | |
71 | sigchain_pop(SIGPIPE); | |
72 | /* Finish command will wait until the shutdown is complete. */ | |
73 | finish_command(process); | |
74 | } | |
75 | ||
76 | int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd, | |
77 | subprocess_start_fn startfn) | |
78 | { | |
79 | int err; | |
80 | struct child_process *process; | |
99605d62 BP |
81 | |
82 | entry->cmd = cmd; | |
83 | process = &entry->process; | |
84 | ||
85 | child_process_init(process); | |
2944a94c | 86 | argv_array_push(&process->args, cmd); |
99605d62 BP |
87 | process->use_shell = 1; |
88 | process->in = -1; | |
89 | process->out = -1; | |
90 | process->clean_on_exit = 1; | |
91 | process->clean_on_exit_handler = subprocess_exit_handler; | |
0671b4d1 | 92 | process->trace2_child_class = "subprocess"; |
99605d62 BP |
93 | |
94 | err = start_command(process); | |
95 | if (err) { | |
96 | error("cannot fork to run subprocess '%s'", cmd); | |
97 | return err; | |
98 | } | |
99 | ||
d22245a2 | 100 | hashmap_entry_init(&entry->ent, strhash(cmd)); |
99605d62 BP |
101 | |
102 | err = startfn(entry); | |
103 | if (err) { | |
104 | error("initialization for subprocess '%s' failed", cmd); | |
105 | subprocess_stop(hashmap, entry); | |
106 | return err; | |
107 | } | |
108 | ||
b94e5c1d | 109 | hashmap_add(hashmap, &entry->ent); |
99605d62 BP |
110 | return 0; |
111 | } | |
fa64a2fd JT |
112 | |
113 | static int handshake_version(struct child_process *process, | |
114 | const char *welcome_prefix, int *versions, | |
115 | int *chosen_version) | |
116 | { | |
117 | int version_scratch; | |
118 | int i; | |
119 | char *line; | |
120 | const char *p; | |
121 | ||
122 | if (!chosen_version) | |
123 | chosen_version = &version_scratch; | |
124 | ||
125 | if (packet_write_fmt_gently(process->in, "%s-client\n", | |
126 | welcome_prefix)) | |
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", | |
130 | versions[i])) | |
131 | return error("Could not write requested version"); | |
132 | } | |
133 | if (packet_flush_gently(process->in)) | |
134 | return error("Could not write flush packet"); | |
135 | ||
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); | |
148 | ||
149 | /* Check to make sure that the version received is supported */ | |
150 | for (i = 0; versions[i]; i++) { | |
151 | if (versions[i] == *chosen_version) | |
152 | break; | |
153 | } | |
154 | if (!versions[i]) | |
155 | return error("Version %d not supported", *chosen_version); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | static int handshake_capabilities(struct child_process *process, | |
161 | struct subprocess_capability *capabilities, | |
162 | unsigned int *supported_capabilities) | |
163 | { | |
164 | int i; | |
165 | char *line; | |
166 | ||
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"); | |
171 | } | |
172 | if (packet_flush_gently(process->in)) | |
173 | return error("Could not write flush packet"); | |
174 | ||
175 | while ((line = packet_read_line(process->out, NULL))) { | |
176 | const char *p; | |
177 | if (!skip_prefix(line, "capability=", &p)) | |
178 | continue; | |
179 | ||
180 | for (i = 0; | |
181 | capabilities[i].name && strcmp(p, capabilities[i].name); | |
182 | i++) | |
183 | ; | |
184 | if (capabilities[i].name) { | |
185 | if (supported_capabilities) | |
186 | *supported_capabilities |= capabilities[i].flag; | |
187 | } else { | |
ab46e6fc JH |
188 | die("subprocess '%s' requested unsupported capability '%s'", |
189 | process->argv[0], p); | |
fa64a2fd JT |
190 | } |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | int subprocess_handshake(struct subprocess_entry *entry, | |
197 | const char *welcome_prefix, | |
198 | int *versions, | |
199 | int *chosen_version, | |
200 | struct subprocess_capability *capabilities, | |
201 | unsigned int *supported_capabilities) | |
202 | { | |
203 | int retval; | |
204 | struct child_process *process = &entry->process; | |
205 | ||
206 | sigchain_push(SIGPIPE, SIG_IGN); | |
207 | ||
208 | retval = handshake_version(process, welcome_prefix, versions, | |
209 | chosen_version) || | |
210 | handshake_capabilities(process, capabilities, | |
211 | supported_capabilities); | |
212 | ||
213 | sigchain_pop(SIGPIPE); | |
214 | return retval; | |
215 | } |