File: debconf-communicate.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 (178 lines) | stat: -rw-r--r-- 4,089 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
/**
 * @file debconf-communicate.c
 * @brief Configuration module interface
 */
#include "confmodule.h"
#include "configuration.h"
#include "frontend.h"
#include "database.h"

#include <signal.h>
#include <string.h>
#include <getopt.h>
#include <locale.h>
#include <errno.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 struct option options[] = {
    { "frontend", 1, 0, 'f' },
    { 0, 0, 0, 0 }
};

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

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

static void sighandler(int sig)
{
	save();
	if (sig != SIGUSR1) {
		cleanup();
		exit(1);
	}
}

static void help(const char *exename)
{
    fprintf(stderr, "%s [-ffrontend] [package]\n", exename);
    exit(-1);
}

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

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

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

int main(int argc, char **argv)
{
    int code = 127;
    char buf[1024], *ret;
    char *in;
    size_t insize = 1024;

	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);

	/* 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 ((questions = question_db_new(config, templates, NULL)) == 0)
		DIE("Cannot initialize DebConf configuration database");
	if ((frontend = frontend_new(config, templates, questions)) == 0)
		DIE("Cannot initialize DebConf frontend");

	/* load templates and config */
	templates->methods.load(templates);
    questions->methods.load(questions);

	/* startup the confmodule; run the config script and talk to it */
	confmodule = confmodule_new(config, templates, questions, frontend);

    if (optind < argc)
        confmodule->owner = argv[optind];
    else
        confmodule->owner = "unknown";

    in = malloc(insize);
    if (!in)
        DIE("Out of memory");
    memset(in, 0, insize);

    /* start feeding user commands to debconf ... */
    while (1) {
        int done = 0;
        buf[0] = 0;
        in[0] = 0;
        while (strchr(buf, '\n') == NULL) {
            int r = read(0, buf, sizeof(buf) - 1);
            if (r < 0) {
                if (errno == EINTR)
                    continue;
                code = 127;
                done = 1;
                break;
            }
            if (r == 0) {
                done = 1;
                break;
            }
            buf[r] = 0;
            if (strlen(in) + r + 1 > insize) {
                insize += sizeof(buf);
                in = realloc(in, insize);
                if (!in)
                    DIE("Out of memory");
            }
            strcat(in, buf);
        }
        if (done)
            break;

        ret = confmodule->process_command(confmodule, in);

        if (ret[0] == 0)
            break;

        /* extract the first number as the return code */
        code = atoi(ret);

        write(1, ret, strlen(ret));
        write(1, "\n", 1);
        free(ret);
    }
    free(in);

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

	return code;
}