File: debconf.c

package info (click to toggle)
cdebconf 0.182
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,668 kB
  • sloc: ansic: 16,289; sh: 547; makefile: 438; sql: 52; perl: 13
file content (275 lines) | stat: -rw-r--r-- 7,312 bytes parent folder | download | duplicates (5)
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
/**
 * @file debconf.c
 * @brief Configuration module interface
 */
#include "confmodule.h"
#include "configuration.h"
#include "question.h"
#include "frontend.h"
#include "database.h"

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <debian-installer.h>

static struct configuration *config = NULL;
static struct frontend *frontend = NULL;
static struct confmodule *confmodule = NULL;
static struct question_db *questions = NULL;
static struct template_db *templates = NULL;
static const char *owner;
static const char *priority;

static struct option options[] = {
    { "owner", 1, 0, 'o' },
    { "frontend", 1, 0, 'f' },
    { "priority", 1, 0, 'p' },
    { 0, 0, 0, 0 },
};

static int save(void)
{
	return confmodule->save(confmodule);
}

static void cleanup()
{
	if (confmodule->frontend != NULL)
		frontend_delete(confmodule->frontend);
	if (confmodule->questions != NULL)
		question_db_delete(confmodule->questions);
	if (confmodule->templates != NULL)
		template_db_delete(confmodule->templates);
	if (confmodule->config != NULL)
		config_delete(confmodule->config);
}

static void sighandler(int sig)
{
	int status = 1;
	if (sig == SIGCHLD)
	{
		while (waitpid(0, &status, WNOHANG) > 0)
			sigchld_status = status;
		signal_received = sig;
		return;
	}
	save();
	/*
	 * SIGUSR1 used to reconfigure the language. Now it
	 * only saves the database.
	 */
	if (sig == SIGUSR1)
		return;
	cleanup();
	exit(status);
}

static void help(const char *exename)
{
    fprintf(stderr, "%s [-ffrontend] [-ppriority] [-oowner] <config script>\n", exename);
    fprintf(stderr, "%s [--frontend=frontend] [--priority=priority] [--owner=owner] <config script>\n", exename);
    exit(-1);
}

static void parsecmdline(struct configuration *config, int argc, char **argv)
{
    int c;

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

    if (optind >= argc)
    {
        help(argv[0]);
    }
}

static void guess_owner(char **argv)
{
    const char *runningscript = argv[optind];
    const char *filename;
    const char *extension;

    /* Already set with --owner */
    if (owner)
        return;

    /* Override */
    if ((owner = getenv("DEBCONF_PACKAGE")) != NULL)
        return;

    /* Set by dpkg since 1.15.4, so it should stop here in normal conditions */
    if ((owner = getenv("DPKG_MAINTSCRIPT_PACKAGE")) != NULL)
        return;

    filename = strrchr(runningscript, '/');
    if (filename == NULL)
        filename = runningscript;
    else
        filename++;
    extension = strrchr(filename ,'.');

    if (extension &&
        (!strcmp(extension+1, "preinst") || !strcmp(extension+1, "postinst") ||
         !strcmp(extension+1, "prerm") || !strcmp(extension+1, "postrm") ||
         !strcmp(extension+1, "config")))
        owner = strndup(filename, extension - filename);
}

static void runconfigscript(int argc, char **argv)
{
    char *base, *dot;
    const char *ext = strrchr(argv[optind], '.');
    if (!ext)
        return;

    if (strcmp(ext+1, "postinst") && strcmp(ext+1, "preinst"))
        return;

    base = malloc(strlen(argv[optind]) + strlen(".templates") + 1);
    strcpy(base, argv[optind]);
    dot = strrchr(base, '.');
    if (dot)
    {
        strcpy(dot, ".templates");
        template_db_loadfile(templates, questions, base, owner, DC_LOADTEMPLATE_MERGE);

        strcpy(dot, ".config");
		if (!strcmp(ext+1, "postinst") && (argc > optind + 1) &&
		    !strcmp(argv[optind + 1], "configure") && (0 == access(base, F_OK)))
        {
            char configurestring[] = "configure";
            int exitcode;
            char *version = getenv("DPKG_MAINTSCRIPT_VERSION");
            if (!version && (argc > optind + 2))
            {
                version = argv[optind + 2];
            }

            /* startup the confmodule; run the config script and talk to it */
            confmodule = confmodule_new(config, templates, questions, frontend);
            confmodule->owner = owner;
            char * args[] = 
            {
                NULL,
                base,
                configurestring,
                version
            };
            confmodule->run(confmodule, version ? 4 : 3, args );
            confmodule->communicate(confmodule);
            confmodule->shutdown(confmodule);

        	exitcode = confmodule->exitcode;

            confmodule->save(confmodule);

            confmodule_delete(confmodule);

            if (exitcode)
                exit(exitcode);

            /* The frontend might have been changed in the config script */
            frontend = confmodule->frontend;
        }
    }

    free(base);
}

int main(int argc, char **argv)
{
	struct sigaction sa;

	sa.sa_handler = &sighandler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
	sigaction(SIGCHLD, &sa, NULL);

	signal(SIGINT, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGUSR1, sighandler);
	setlocale (LC_ALL, "");

	config = config_new();
	if (!config) {
	  DIE("Cannot read new config");
	}
	parsecmdline(config, argc, argv);

    if (!priority)
        priority = getenv("DEBIAN_PRIORITY");
    if (priority)
        config->set(config, "_cmdline::priority", priority);

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

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

    guess_owner(argv);
    
    /* set title */
    if (owner)
    {
        char *title;
        if (asprintf(&title, "Configuring %s", owner) >= 0)
        {
            frontend->methods.set_title(frontend, title);
            free(title);
        }
    }
    else
        owner = "unknown";

    runconfigscript(argc, argv);

    /* reset signal_received, otherwise ->communicate() will exit immediately */
    signal_received = 0;
    
	/* startup the confmodule; run the config script and talk to it */
	confmodule = confmodule_new(config, templates, questions, frontend);
        confmodule->owner = owner;
	confmodule->run(confmodule, argc - optind + 1, argv + optind - 1);
	confmodule->communicate(confmodule);
        confmodule->shutdown(confmodule);

	/* shutting down .... sync the database and shutdown the modules */
	save();
	cleanup();

	return confmodule->exitcode;
}