Debian Codegraph @

src/modules/frontend/passthrough/passthrough.c

raw
1 /***********************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * File: passthrough.c
7 *
8 * Description: Passthrough UI for cdebconf
9 *
10 * cdebconf is (c) 2000-2009 Randolph Chung and others under the following
11 * license.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 ***********************************************************************/
37 #include "common.h"
38 #include "commands.h"
39 #include "frontend.h"
40 #include "question.h"
41 #include "strutl.h"
42 #include "template.h"
43
44 #include <stdarg.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
51
52 #include "debian-installer.h"
53
54 struct passthrough_data {
55 FILE *readfh, *writefh;
56 };
57
58 static int talk(struct frontend *obj, char **value, ...) ATTRIBUTE_SENTINEL;
59
60 static int
61 talk(struct frontend *obj, char **value, ...)
62 {
63 struct passthrough_data *data = obj->data;
64 va_list ap;
65 char *command;
66 char buf[1023];
67 char *in;
68 size_t inlen = 0, insize = 1024;
69 char *inp, *space;
70 int ret;
71
72 if (value)
73 *value = NULL;
74
75 va_start(ap, value);
76 command = strjoinv(" ", ap);
77 va_end(ap);
78
79 INFO(INFO_DEBUG, "----> %s", command);
80 fprintf(data->writefh, "%s\n", command);
81 fflush(data->writefh);
82 free(command);
83
84 /* This is disgusting; it's more or less cloned from
85 * confmodule_communicate.
86 */
87 in = di_malloc0(insize);
88 buf[0] = 0;
89 in[0] = 0;
90 while (strchr(buf, '\n') == NULL) {
91 if (fgets(buf, sizeof(buf), data->readfh) == NULL) {
92 if (feof(data->readfh))
93 di_warning("unexpected EOF on data->readfh");
94 else
95 di_warning("read from data->readfh failed: %s", strerror(errno));
96 free(in);
97 return CMDSTATUS_INTERNALERROR;
98 }
99 if (inlen + strlen(buf) + 1 > insize) {
100 insize += sizeof(buf);
101 in = di_realloc(in, insize);
102 }
103 strcat(in, buf);
104 }
105 inp = strstrip(in);
106 INFO(INFO_DEBUG, "<---- %s", inp);
107 space = strchr(inp, ' ');
108 if (space)
109 *space = '\0';
110 ret = atoi(inp);
111 if (value) {
112 if (space)
113 *value = STRDUP(space + 1);
114 else
115 *value = STRDUP("");
116 }
117 free(in);
118 return ret;
119 }
120
121 static int
122 passthrough_initialize(struct frontend *obj, struct configuration *cfg)
123 {
124 struct passthrough_data *data;
125 const char *env_pipe = getenv("DEBCONF_PIPE");
126 const char *env_readfd = getenv("DEBCONF_READFD");
127 const char *env_writefd = getenv("DEBCONF_WRITEFD");
128
129 if (!env_pipe && (!env_readfd || !env_writefd)) {
130 di_warning("neither DEBCONF_PIPE nor DEBCONF_READFD and DEBCONF_WRITEFD were set");
131 return DC_NOTOK;
132 }
133
134 obj->interactive = 1;
135 obj->data = data = calloc(1, sizeof(struct passthrough_data));
136
137 if (env_pipe) {
138 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
139 struct sockaddr_un addr;
140 if (sock < 0) {
141 di_warning("failed to create Unix-domain socket: %s", strerror(errno));
142 return DC_NOTOK;
143 }
144 memset(&addr, 0, sizeof(addr));
145 addr.sun_family = AF_UNIX;
146 strncpy(addr.sun_path, env_pipe, sizeof(addr.sun_path) - 1);
147 if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
148 di_warning("failed to connect Unix-domain socket to %s: %s", env_pipe, strerror(errno));
149 return DC_NOTOK;
150 }
151 data->readfh = data->writefh = fdopen(sock, "r+");
152 if (!data->readfh) {
153 di_warning("failed to open stream for Unix-domain socket connected to %s: %s", env_pipe, strerror(errno));
154 return DC_NOTOK;
155 }
156 } else {
157 data->readfh = fdopen(atoi(env_readfd), "r");
158 if (!data->readfh) {
159 di_warning("failed to open fd %s: %s", env_readfd, strerror(errno));
160 return DC_NOTOK;
161 }
162 data->writefh = fdopen(atoi(env_writefd), "w");
163 if (!data->writefh) {
164 di_warning("failed to open fd %s: %s", env_writefd, strerror(errno));
165 return DC_NOTOK;
166 }
167 }
168
169 return DC_OK;
170 }
171
172 static int
173 passthrough_shutdown(struct frontend *obj)
174 {
175 struct passthrough_data *data = obj->data;
176 if (data && data->readfh)
177 fclose(data->readfh);
178 if (data && data->writefh && data->writefh != data->readfh)
179 fclose(data->writefh);
180 free(data);
181 return DC_OK;
182 }
183
184 static unsigned long
185 passthrough_query_capability(struct frontend *obj)
186 {
187 char *capb_all, *capb_walk, *capb;
188 unsigned long ret = 0;
189
190 if (talk(obj, &capb_all, "CAPB", NULL) != 0)
191 return 0;
192 capb_walk = capb_all;
193 for (capb = strsep(&capb_walk, " "); capb;
194 capb = strsep(&capb_walk, " ")) {
195 if (strcmp(capb, "backup") == 0)
196 ret |= DCF_CAPB_BACKUP;
197 else if (strcmp(capb, "progresscancel") == 0)
198 ret |= DCF_CAPB_PROGRESSCANCEL;
199 else if (strcmp(capb, "align") == 0)
200 ret |= DCF_CAPB_ALIGN;
201 }
202 free(capb_all);
203 return ret;
204 }
205
206 static void
207 passthrough_set_title(struct frontend *obj, const char *title)
208 {
209 DELETE(obj->title);
210 obj->title = STRDUP(title);
211 /* TODO: We really ought to use SETTITLE instead, but for apparently
212 * historical reasons the frontend set_title method only gets the
213 * description.
214 */
215 talk(obj, NULL, "TITLE", title, NULL);
216 }
217
218 static int
219 passthrough_go(struct frontend *obj)
220 {
221 struct question *q;
222
223 /* TODO:
224 * - would be nice to cache the previous value to avoid unnecessarily
225 * resetting this
226 * - support more capabilities than just backup
227 * - this should be done in a proper frontend "capb" method
228 */
229 if (obj->capability & DCF_CAPB_BACKUP)
230 talk(obj, NULL, "CAPB", "backup", NULL);
231 else
232 talk(obj, NULL, "CAPB", NULL);
233
234 for (q = obj->questions; q; q = q->next) {
235 char *desc, *extdesc;
236 const char *value;
237 struct questionvariable *qv;
238
239 talk(obj, NULL, "DATA", q->tag, "type", q->template->type, NULL);
240
241 desc = q_get_description(obj, q);
242 if (desc) {
243 char *desc_esc = strreplace(desc, "\\n", "\\\\n");
244 talk(obj, NULL, "DATA", q->tag, "description", desc_esc, NULL);
245 free(desc_esc);
246 free(desc);
247 }
248
249 extdesc = q_get_extended_description(obj, q);
250 if (extdesc) {
251 char *extdesc_esc = strreplace(extdesc, "\\n", "\\\\n");
252 talk(obj, NULL, "DATA", q->tag, "extended_description",
253 extdesc_esc, NULL);
254 free(extdesc_esc);
255 free(extdesc);
256 }
257
258 if (strcmp(q->template->type, "select") == 0 ||
259 strcmp(q->template->type, "multiselect") == 0) {
260 char *choices_c, *choices_c_esc, *choices, *choices_esc;
261
262 choices_c = question_get_field(obj, q, "C", "choices");
263 choices_c_esc = strreplace(choices_c, "\\n", "\\\\n");
264 talk(obj, NULL, "DATA", q->tag, "choices-c", choices_c_esc, NULL);
265 free(choices_c_esc);
266 free(choices_c);
267
268 choices = q_get_choices(obj, q);
269 choices_esc = strreplace(choices, "\\n", "\\\\n");
270 talk(obj, NULL, "DATA", q->tag, "choices", choices_esc, NULL);
271 free(choices_esc);
272 free(choices);
273 }
274
275 value = question_getvalue(q, "");
276 if (value && *value)
277 talk(obj, NULL, "SET", q->tag, value, NULL);
278
279 for (qv = q->variables; qv; qv = qv->next)
280 talk(obj, NULL, "SUBST", q->tag, qv->variable, qv->value, NULL);
281
282 talk(obj, NULL, "INPUT", q->priority, q->tag, NULL);
283 }
284
285 /* Tell the agent to display the question(s), and check for a back
286 * button.
287 */
288 if (obj->questions && talk(obj, NULL, "GO", NULL) == 30 &&
289 (obj->capability & DCF_CAPB_BACKUP))
290 return DC_GOBACK;
291
292 /* Retrieve the answers. */
293 for (q = obj->questions; q; q = q->next) {
294 char *value;
295 if (talk(obj, &value, "GET", q->tag, NULL) == 0) {
296 /* debconf does translation to C here, but we avoid the need for
297 * that by sending Choices-C.
298 */
299 question_setvalue(q, value);
300 INFO(INFO_DEBUG, "Got \"%s\" for %s", value, q->tag);
301 }
302 free(value);
303 }
304
305 return DC_OK;
306 }
307
308 static void
309 progress_data(struct frontend *obj, struct question *q)
310 {
311 char *desc, *extdesc;
312
313 talk(obj, NULL, "DATA", q->tag, "type", q->template->type, NULL);
314
315 desc = q_get_description(obj, q);
316 if (desc) {
317 talk(obj, NULL, "DATA", q->tag, "description", desc, NULL);
318 free(desc);
319 }
320
321 extdesc = q_get_extended_description(obj, q);
322 if (extdesc) {
323 talk(obj, NULL, "DATA", q->tag, "extended_description", extdesc, NULL);
324 free(extdesc);
325 }
326 }
327
328 static void
329 passthrough_progress_start(struct frontend *obj, int min, int max,
330 struct question *title)
331 {
332 char *min_str, *max_str;
333
334 progress_data(obj, title);
335 asprintf(&min_str, "%d", min);
336 asprintf(&max_str, "%d", max);
337 talk(obj, NULL, "PROGRESS", "START", min_str, max_str, title->tag, NULL);
338 free(max_str);
339 free(min_str);
340 }
341
342 static int
343 passthrough_progress_set(struct frontend *obj, int val)
344 {
345 char *val_str;
346 int ret;
347
348 asprintf(&val_str, "%d", val);
349 ret = talk(obj, NULL, "PROGRESS", "SET", val_str, NULL);
350 free(val_str);
351 return ret;
352 }
353
354 static int
355 passthrough_progress_step(struct frontend *obj, int step)
356 {
357 char *step_str;
358 int ret;
359
360 asprintf(&step_str, "%d", step);
361 ret = talk(obj, NULL, "PROGRESS", "STEP", step_str, NULL);
362 free(step_str);
363 return ret;
364 }
365
366 static int
367 passthrough_progress_info(struct frontend *obj, struct question *info)
368 {
369 progress_data(obj, info);
370 return talk(obj, NULL, "PROGRESS", "INFO", info->tag, NULL);
371 }
372
373 static void
374 passthrough_progress_stop(struct frontend *obj)
375 {
376 talk(obj, NULL, "PROGRESS", "STOP", NULL);
377 }
378
379 struct frontend_module debconf_frontend_module =
380 {
381 .initialize = passthrough_initialize,
382 .shutdown = passthrough_shutdown,
383 .query_capability = passthrough_query_capability,
384 .set_title = passthrough_set_title,
385 .go = passthrough_go,
386 .progress_start = passthrough_progress_start,
387 .progress_set = passthrough_progress_set,
388 .progress_step = passthrough_progress_step,
389 .progress_info = passthrough_progress_info,
390 .progress_stop = passthrough_progress_stop,
391 };