File: run-debootstrap.c

package info (click to toggle)
base-installer 1.154
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,172 kB
  • ctags: 144
  • sloc: sh: 1,574; ansic: 699; makefile: 59; perl: 50
file content (448 lines) | stat: -rw-r--r-- 13,432 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

#include <cdebconf/debconfclient.h>
#include <debian-installer.h>

#include "waypoints.h"

#define DEBCONF_BASE          "base-installer/debootstrap/"

struct debconfclient *debconf = NULL;
int progress_start_position = 0;

/* args = read_arg_lines("EA: ", ifp, &arg_count, &line); */
char **
read_arg_lines(const char *prefix, FILE *ifp, int *arg_count, char **final_line,
	       int *llen)
{
    static char **args = NULL;
    static int arg_max = 0;
    size_t dummy = 0;

    if (args == NULL)
    {
        arg_max = 4;
        args = malloc(sizeof(char *) * arg_max);
    }
    *arg_count = 0;
    while (1)
    {
        *final_line = NULL;
        if ((*llen = getline(final_line, &dummy, ifp)) <= 0)
        {
            return NULL;
        }
        (*final_line)[*llen-1] = 0;
        if (strstr(*final_line, prefix) == *final_line)
        {
            if (*arg_count >= arg_max) {
                arg_max += 4;
                args = realloc(args, sizeof(char *) * arg_max);
            }
            args[(*arg_count)++] = strdup(*final_line+strlen(prefix));
            /* we got arguments. */
        }
        else
            break;
    }
    return args;
}

char *
n_sprintf(char *fmt, int arg_count, char **args)
{
    char *ret;

    switch (arg_count)
    {
        case 0: ret = strdup(fmt); break;
        case 1: asprintf(&ret, fmt, args[0]); break;
        case 2: asprintf(&ret, fmt, args[0], args[1]); break;
        case 3: asprintf(&ret, fmt, args[0], args[1], args[2]); break;
        default: ret = NULL; break;
    }
    return ret;
}

void
n_subst(char *template, int arg_count, char **args)
{
    int i;

    for (i = 0; i < arg_count; i++)
    {
        debconf->commandf(debconf, "SUBST %s SUBST%d %s", template, i, args[i]);
    }
}

/* changes in 'code' */
char *
find_template(const char *prefix, char *code)
{
    char *p;

    for (p = code; *p; p++)
        *p = tolower(*p);
    asprintf(&p, DEBCONF_BASE "%s/%s", prefix, code);
    if (debconf_metaget(debconf, p, "description") == 0)
        return p;
    else
    {
        free(p);
        return NULL;
    }
}

int get_progress_start_position (void) {
    const char *progress_env = getenv("PB_PROGRESS");
    if (progress_env)
        return atoi(progress_env);
    else
        return 0;
}

/* Calculate progress bar location, starting at
 * previous waypoint, and advancing the percent of
 * the current section that corresponds to the percent
 * of the debootstrap progress indicator. */
void set_progress (int current_section, int phigh, int plow) {
    float section_fraction;
    int section_span, prev_waypoint, percent;

    prev_waypoint = waypoints[current_section].startpercent;
    if (current_section > 0)
        section_span = waypoints[current_section].endpercent - prev_waypoint;
    else
        section_span = 0;

    if (phigh > 0)
        section_fraction = (float) plow / (float) phigh;
    else
        section_fraction = 0;
    if (section_fraction > 1)
        section_fraction = 1;
			
    percent = prev_waypoint + (section_span * section_fraction);

#if 0
    fprintf(stderr, "waypoint: %s (%i); prev endpercent %i; span: %i; fraction: %.9f (%i / %i); percent: %i\n",
            waypoints[current_section].progress_id,
            current_section, prev_waypoint, section_span, 
            section_fraction, plow, phigh, percent);
#endif

    debconf_progress_set(debconf, progress_start_position + percent);
}

/*
 * Copied from boot-floppies/utilities/dbootstrap/extract_base.c
 * and modified to use cdebconf progress bars
 */
static int
exec_debootstrap(char **argv){
    char **args = NULL;
    int arg_count;
    int from_db[2]; /* 0=read, 1=write */
    FILE *ifp;
    pid_t pid;
    int status, rv;
    char *ptr, *line, *template, *section_text = NULL;
    int llen;
    size_t dummy = 0;
    int current_section = 0;
    int child_exit = 0;

    pipe(from_db);

    if ((pid = fork()) == 0)
    {
        close(from_db[0]);

        if (dup2(from_db[1], 3) == -1)
            perror("dup2");
        close(from_db[1]);

        if (freopen("/dev/null", "r", stdin) == NULL)
            perror("freopen");

        setenv("PERL_BADLANG", "0", 1);
        /* These are needed to hack around a hack (!) in update-inetd
         * and to not confuse debconf's postinst */
        unsetenv("DEBIAN_HAS_FRONTEND");
        unsetenv("DEBIAN_FRONTEND");
        unsetenv("DEBCONF_FRONTEND");
        unsetenv("DEBCONF_REDIR");
        if (execv(argv[0], argv) != 0)
            perror("execv");
        return -1;
    }
    else if (pid == -1)
    {
        perror("fork");
        return -1;
    }

    progress_start_position = get_progress_start_position();

    close(from_db[1]);

    if ((ifp = fdopen(from_db[0], "r")) == NULL) {
        perror("fdopen");
        return -1;
    }

    line = NULL;
    llen = getline(&line, &dummy, ifp);
    while (!child_exit && llen > 0)
    {
        line[llen-1] = 0;

	/* fprintf(stderr, "got line: %s\n", line); */
	
        ptr = line;
        switch (ptr[0])
        {
            case 'E':
                {
                    ptr += 3;
                    /* ptr now contains the identifier of the error. */
                    template = find_template("error", ptr);
                    args = read_arg_lines("EA: ", ifp, &arg_count, &line,
					  &llen);
                    if (args == NULL)
                    {
                        child_exit = 1;
                        break;
                    }
                    if (template != NULL)
                    {
                        n_subst(template, arg_count, args);
                        debconf_input(debconf, "critical", template);
                        debconf_go(debconf);
                    }
                    else if (strstr(line, "EF:") == line)
                    {
                        ptr = n_sprintf(line+4, arg_count, args);
                        if (ptr == NULL)
                            return -1;
                        /* fallback error message */
                        debconf_subst(debconf, DEBCONF_BASE "fallback-error",
				      "ERROR", ptr);
                        debconf_input(debconf, "critical",
				      DEBCONF_BASE "fallback-error");
                        debconf_go(debconf);
                        free(ptr);
                    }
                    else
                    {
                        /* err, don't really know what to do here... there
                         * should always be a fallback... */
                    }
                    return -1;
                }
            case 'W':
                {
                    ptr += 3;
                    /* ptr now contains the identifier of the warning */
                    template = find_template("warning", ptr);

                    /* fprintf(stderr, "warning template: %s\n", template); */
		    
                    args = read_arg_lines("WA: ", ifp, &arg_count, &line,
					  &llen);
                    if (args == NULL)
                    {
                        child_exit = 1;
                        break;
                    }
                    if (template != NULL)
                    {
			/* It's hard to choose whether to display a warning
			 * as an error/informational template or as a
			 * progress item. Currently a progress item seems
			 * to fit best with how debootstrap uses warnings
			 * that we care about. */
                        n_subst(template, arg_count, args);
                        debconf_progress_info(debconf, template);
                    }
                    else if (strstr(line, "WF:") == line)
                    {
                        ptr = n_sprintf(line+4, arg_count, args);
                        if (ptr == NULL)
                            return -1;
                        /* Fallback warning message. Unlike the above,
			 * display this as an error, since it could be
			 * arbitrarily bad. */
                        debconf_subst(debconf, DEBCONF_BASE "fallback-warning",
				      "INFO", ptr);
			debconf_subst(debconf, DEBCONF_BASE "fallback-warning",
			              "SECTION", section_text);
                        debconf_input(debconf, "critical",
				      DEBCONF_BASE "fallback-warning");
                        debconf_go(debconf);
                        free(ptr);
                    }
                    else
                    {
                        /* err, don't really know what to do here... there
                         * should always be a fallback... */
                    }

		    break;
                }
            case 'P':
                {
                    int plow = 0, phigh = 0;
                    char what[1024] = "";
		    char *section_template;

                    sscanf(line+3, "%d %d %s", &plow, &phigh, what);
                    if (what[0])
		    {
			int i;
			for (i = 0; waypoints[i].progress_id != NULL; i++)
			{
			    if (strcmp(waypoints[i].progress_id, what) == 0)
			        {
                                    set_progress(i, phigh, plow);

				    /* Get the description of the section
				     * template for this waypoint. */
				    if (current_section == i)
					    break; /* optimisation */
			            current_section = i;
			            section_template = find_template("section", what);
			            if (section_template)
				    {
                                            if (! debconf_metaget(debconf, section_template, "description"))
					    {
                                                free(section_text);
                                                section_text = strdup(debconf->value);
				            }
                                            free(section_template);
			            }

			            break;
				}
			}
		    }
		    
                    args = read_arg_lines("PA: ", ifp, &arg_count, &line,
					  &llen);
                    if (args == NULL)
                    {
                        child_exit = 1;
                        break;
                    }
                    if (strstr(line, "PF:") == line)
		    {
                        /* Does not currently need to do anything;
			 * the implementation of debootstrap could change
			 * though.. */
                    }
		    else
			continue;
		    
                    break;
                }
            case 'I':
                {
                    ptr += 3;
                    /* ptr now contains the identifier of the info */
                    template = find_template("info", ptr);

                    /* fprintf(stderr, "info template: %s\n", template); */
		    
                    if (strcmp(ptr, "basesuccess") == 0 && template != NULL)
                    {
                        /* all done */
                        child_exit = 1;
                        break;
                    }
                    args = read_arg_lines("IA: ", ifp, &arg_count, &line,
					  &llen);
                    if (args == NULL)
                    {
                        child_exit = 1;
                        break;
                    }
                    if (template != NULL)
                    {
                        n_subst(template, arg_count, args);
			debconf_subst(debconf, template,
			              "SECTION", section_text);
                        debconf_progress_info(debconf, template);
                    }
                    else if (strstr(line, "IF:") == line)
                    {
                        ptr = n_sprintf(line+4, arg_count, args);
                        if (ptr == NULL)
                            return -1;
                        /* fallback info message */
                        debconf_subst(debconf, DEBCONF_BASE "fallback-info",
				      "INFO", ptr);
			debconf_subst(debconf, DEBCONF_BASE "fallback-info",
			              "SECTION", section_text);
                        debconf_progress_info(debconf,
					      DEBCONF_BASE "fallback-info");
                        free(ptr);
                    }
                    else
                    {
                        /* err, don't really know what to do here... there
                         * should always be a fallback... */
                    }

		    break;
                }
        }

	if (child_exit)
	    break;

        line = NULL;
	llen = getline(&line, &dummy, ifp);
    }

    if (waitpid(pid, &status, 0) != -1 && (WIFEXITED(status) != 0))
    {
        rv = WEXITSTATUS(status);
        if (rv != 0)
        {
            debconf->commandf(debconf, "SUBST %serror-exitcode EXITCODE %d",
			      DEBCONF_BASE, rv);
            debconf_input(debconf, "critical", DEBCONF_BASE "error-exitcode");
            debconf_go(debconf);
        }
        return rv;
    }
    else
    {
        kill(SIGKILL, pid);
        debconf_input(debconf, "critical", DEBCONF_BASE "error-abnormal");
        debconf_go(debconf);
        return 1;
    }
}

int
main(int argc, char *argv[])
{
    char **args;
    int i;

    di_system_init("run-debootstrap");
    debconf = debconfclient_new();
    args = (char **)malloc(sizeof(char *) * (argc + 1));
    args[0] = "/usr/sbin/debootstrap";
    for (i = 1; i < argc; i++)
        args[i] = argv[i];
    args[argc] = NULL;
    return exec_debootstrap(args);
}