File: dpkg-preconfigure.c

package info (click to toggle)
cdebconf 0.227
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,552 kB
  • sloc: ansic: 16,568; sh: 549; makefile: 456; sql: 52; perl: 13
file content (361 lines) | stat: -rw-r--r-- 8,842 bytes parent folder | download | duplicates (3)
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

#include "common.h"
#include "configuration.h"
#include "confmodule.h"
#include "database.h"
#include "frontend.h"
#include "question.h"
#include "template.h"

#include <getopt.h>
#include <locale.h>
#include <string.h>
#include <sys/stat.h>

#define APT_EXTRACTTEMPLATES "apt-extracttemplates"

struct packagename {
    char *p;
    struct packagename *next;
};

static int apt = 0;
static struct option options[] = {
    { "help", 0, NULL, 'h' },
    { "apt", 0, &apt, 'a' },
    { "frontend", 1, NULL, 'f' },
    { "priority", 1, NULL, 'p' },
    { 0, 0, 0, 0 }
};

struct package {
    char *buffer;
    const char *name;
    char *version;
    const char *templatefile;
    char *configfile;
    struct package *next;
};

static void usage(const char *exename)
{
    printf("Usage:%s [options] [debs]\n", exename);
    printf("\t--apt - Apt mode\n");
    exit(0);
}

static struct package *extract(const char *file)
{
    int pipefd[2];
    int pid;

    if (pipe(pipefd) < 0)
    {
        perror("pipe");
        return NULL;
    }

    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        close(pipefd[0]);
        close(pipefd[1]);
        return NULL;
    }
    else if (pid == 0) /* child */
    {
        close(pipefd[0]);
        dup2(pipefd[1], 1);
        close(pipefd[1]);
        execlp(APT_EXTRACTTEMPLATES, APT_EXTRACTTEMPLATES, file, NULL);
        /* if we reach here, then execlp failed */
        perror("execlp");
        _exit (127);
    }
    else /* parent */
    {
        FILE *query;
        char buffer[1024];
        struct package *retval = NULL;

        close(pipefd[1]);
        
        query = fdopen(pipefd[0], "r");
        while (!feof(query))
        {
            if (fgets(buffer, 1024, query))
            {
                CHOMP(buffer);
                struct package *p = NEW(struct package);
                memset(p, 0, sizeof(*p));
                p->buffer = strdup(buffer);

                p->name = strtok(p->buffer, " \t\n");
                p->version = strtok(NULL, " \t\n");
                p->templatefile = strtok(NULL, " \t\n");
                p->configfile = strtok(NULL, " \t\n");

                if (p->configfile == NULL)
                {
                    free(p->buffer);
                    free(p);
                    retval = NULL;
                }
                else
                {
                    p->next = retval;
                    retval = p;
                }
            }
        }
        fclose(query);

        return retval;
    }
}

static int extracttemplates_available()
{
    int retval = 1;
    char *fp = NULL, *p;
    int flen = 0;
    char *path = getenv("PATH");

    if (!path)
        return 1;

    path = strdup(path);
    p = path;
    while (p && retval)
    {
        char *n = strchr(p, ':');
        if (n)
            *n++ = '\0';
        if (*p)
        {
            struct stat s;
            int len = strlen(p) + 1 + strlen(APT_EXTRACTTEMPLATES) + 1;
            if (len > flen)
            {
                char *np = realloc(fp, len);
                if (!np)
                    break;
                fp = np;
            }
            if (p[strlen(p)-1] == '/')
                sprintf(fp, "%s%s", p, APT_EXTRACTTEMPLATES);
            else
                sprintf(fp, "%s/%s", p, APT_EXTRACTTEMPLATES);
            if (!access(fp, X_OK) && !stat(fp, &s) && S_ISREG(s.st_mode))
                retval = 0;
        }

        p = n;
    }
    free(path);
    free(fp);

    return retval;
}

int main(int argc, char **argv)
{
    struct configuration *config;
    struct frontend *frontend;
    struct question_db *qdb;
    struct template_db *tdb;
    struct confmodule *confmodule;
    struct package *pkg; 
    struct packagename *pkgs = NULL;
    int c;
    
    setlocale(LC_ALL, "");
    
	if (getuid() != 0)
		DIE("%s must be run as root", argv[0]);

    config = config_new();

    while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
    {
        switch (c)
        {
            case 'f':
                config->set(config, "_cmdline::frontend", optarg);
                break;
            case 'p':
                config->set(config, "_cmdline::priority", optarg);
                break;
            case 'h':
                usage(argv[0]);
                break;
            default:
                break;
        }
    }

    if (!apt && optind == argc)
    {
        fprintf(stderr, "dpkg-preconfigure: must specify some debs to preconfigure\n");
        exit(1);
    }

    if (extracttemplates_available())
    {
        INFO(INFO_WARN, "delaying package configuration, since apt-utils is not installed\n");
        exit(0);
    }

    /* parse the configuration info */
    if (config->read(config, DEBCONFCONFIG) == 0)
        DIE("Error reading configuration information");

    /* initialize database and frontend modules */
    if ((tdb = template_db_new(config, NULL)) == 0)
        DIE("Cannot initialize DebConf templates database");
    if (tdb->methods.load(tdb) != DC_OK)
        DIE("Cannot initialize debconf templates database");
    
    if ((qdb = question_db_new(config, tdb, NULL)) == 0)
        DIE("Cannot initialize DebConf configuration database");
    if (qdb->methods.load(qdb) != DC_OK)
        DIE("Cannot initialize debconf configuration database");

    if ((frontend = frontend_new(config, tdb, qdb)) == 0)
        DIE("Cannot initialize DebConf frontend");

    c = 0;
    
    if (apt) //read list of packages from stdin
    {
        int buflen = 0;
        int offset = 0;
        int bytesread = 0;
        char *buffer = NULL;
        char *file;

        do {
            buflen += 2048;
            buffer = realloc(buffer, buflen);
            bytesread = fread(buffer + offset, 1, buflen - offset, stdin);
            offset += bytesread;
        } while (bytesread == 2048);

        buffer[offset] = '\0';

        file = strtok(buffer, " \n\t");

        while (file)
        {
            struct packagename *q = NEW(struct packagename);
            if (q)
            {
                q->p = strdup(file);
                q->next = pkgs;
                pkgs = q;
                c++;
            }
            
            file = strtok(NULL, " \n\t");
        }

        free(buffer);
    }
    else
    {
        int i;
        if (optind == argc)
        {
            fputs("dpkg-preconfigure: must specify some debs to preconfigure\n", stderr);
            exit(0);
        }
        
        for (i = optind; i < argc; i++)
        {
            struct packagename *q = NEW(struct packagename);
            if (q)
            {
                q->p = strdup(argv[i]);
                q->next = pkgs;
                pkgs = q;
                c++;
            }
        }
    }

    struct packagename *pack = pkgs;
    struct package *packages = NULL;
    int packagescount = 0;

    while (pack)
    {
        struct package *p = extract(pack->p);

        if (p)
        {
            p->next = packages;
            packages = p;
            packagescount++;
            if (c > 30)
            {
                fprintf(stderr, "\rExtracting templates from packages: %d%%", 100 * packagescount / c);
            }
        }
        struct packagename *n = pack;
        pack = pack->next;
        free(n->p);
        free(n);
    }

    if (c > 30)
        fputc('\n', stderr);

    if (apt && packages)
    {
        printf("Preconfiguring packages ...\n");
    }

    for (pkg = packages; pkg != NULL; pkg = pkg->next)
    {
        template_db_loadfile(tdb, qdb, pkg->templatefile, pkg->name, DC_LOADTEMPLATE_MERGE);
        unlink(pkg->templatefile);
    }

    confmodule = confmodule_new(config, tdb, qdb, frontend);
    
    for (pkg = packages; pkg != NULL; pkg = pkg->next)
    {
        struct stat filestat;
        if (0 == stat(pkg->configfile, &filestat) && filestat.st_size > 0)
        {
            char *argvv[] = {
                NULL,
                pkg->configfile,
                "configure",
                pkg->version,
                NULL
            };
            fprintf(stderr, "preconfiguring %s (%s)\n", pkg->name, pkg->version);
            if (chmod(pkg->configfile, 0755))
            {
                DIE("debconf: can't chmod: \n");
            }
            confmodule->owner = pkg->name;
            frontend->methods.set_title(frontend, pkg->name);
            confmodule->run(confmodule, 4, argvv);
            if (DC_OK != confmodule->communicate(confmodule))
            {
                fprintf(stderr, "%s failed to preconfigure, with exit status %s\n", pkg->name, "");
            }
        }
        unlink(pkg->configfile);
    }

    confmodule->shutdown(confmodule);

    confmodule->save(confmodule);
    confmodule_delete(confmodule);

    return 0;
}