Debian Codegraph @

newt-plugin-terminal.c

raw symbols SCIP
1 /*
2 * cdebconf newt plugin to provide a clean terminal
3 *
4 * Copyright (C) 2009 Canonical Ltd.
5 * Written by Colin Watson.
6 */
7
8 #define _GNU_SOURCE
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <syslog.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <signal.h>
22
23 #include <newt.h>
24
25 #include <debian-installer.h>
26
27 #include <cdebconf/frontend.h>
28 #include <cdebconf/question.h>
29 #include <cdebconf/cdebconf_newt.h>
30
31 #define MODULE "terminal"
32
33 #define error(fmt, args...) syslog(LOG_ERR, MODULE ": " fmt, ##args)
34
35 #define DEFAULT_COMMAND_LINE "/bin/sh"
36
37 void cleanup_bterm_terminfo(char *tempdir);
38 char *setup_bterm_terminfo(void);
39 int cdebconf_newt_handler_terminal(struct frontend *obj, struct question *q);
40
41 /* Best effort to clean up after setup_bterm_terminfo. Does not error; if it
42 * fails then some junk temporary files will be left behind. Frees tempdir.
43 */
44 void
45 cleanup_bterm_terminfo(char *tempdir)
46 {
47 DIR *dir = opendir(tempdir);
48 struct dirent *dirent;
49
50 if (!dir) {
51 free(tempdir);
52 return;
53 }
54 while ((dirent = readdir(dir)) != NULL) {
55 char *path;
56 struct stat st;
57
58 if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))
59 continue;
60
61 if (asprintf(&path, "%s/%s", tempdir, dirent->d_name) < 0)
62 continue;
63 if (lstat(path, &st) < 0)
64 continue;
65 if (S_ISDIR(st.st_mode))
66 cleanup_bterm_terminfo(path); /* also frees path */
67 else {
68 unlink(path);
69 free(path);
70 }
71 }
72
73 closedir(dir);
74 rmdir(tempdir);
75 free(tempdir);
76 }
77
78 /* Astonishingly inconvenient pain to arrange for TERM=bterm to work in
79 * /target. This would be a lot easier if bogl-bterm's terminfo definition
80 * were moved into ncurses. Returns the name of a temporary directory that
81 * must be cleaned up.
82 */
83 char *
84 setup_bterm_terminfo(void)
85 {
86 struct stat st;
87 char *tempdir = NULL;
88 char *targetfile;
89 FILE *source = NULL, *target = NULL;
90 char buf[4096];
91 size_t r;
92
93 #define FAIL(fmt, args...) do { error(fmt, ##args); goto fail; } while (0)
94
95 /* Debian- and d-i-specific paths follow. Sorry. */
96
97 if (stat("/target/bin/sh", &st) < 0)
98 /* We don't seem to be working with a target system. */
99 return NULL;
100
101 source = fopen("/usr/share/terminfo/b/bterm", "r");
102 if (!source)
103 /* We don't know about bterm either, so can't do anything. */
104
105 if (stat("/target/usr/share/terminfo/b/bterm", &st) == 0)
106 /* The target system already knows about bterm. */
107 goto fail;
108
109 tempdir = strdup("/target/tmp/cdebconf-terminal.XXXXXX");
110 if (!tempdir)
111 FAIL("strdup failed: %s", strerror(errno));
112 tempdir = mkdtemp(tempdir);
113 if (!tempdir)
114 FAIL("mkdtemp failed: %s", strerror(errno));
115 if (asprintf(&targetfile, "%s/b", tempdir) < 0)
116 FAIL("asprintf failed: %s", strerror(errno));
117 if (mkdir(targetfile, 0700) < 0)
118 FAIL("mkdir(%s) failed: %s", targetfile, strerror(errno));
119 if (asprintf(&targetfile, "%s/b/bterm", tempdir) < 0)
120 FAIL("asprintf failed: %s", strerror(errno));
121 target = fopen(targetfile, "w");
122 if (!target)
123 FAIL("fopen(%s, \"w\") failed: %s", targetfile, strerror(errno));
124
125 while ((r = fread(buf, 1, 4096, source)) > 0) {
126 size_t w = fwrite(buf, 1, r, target);
127 if (w < r)
128 FAIL("short write to %s", targetfile);
129 }
130 if (ferror(source))
131 FAIL("error reading from /usr/share/terminfo/b/bterm");
132 fclose(target);
133 fclose(source);
134
135 #undef FAIL
136
137 return tempdir;
138
139 fail:
140 if (target)
141 fclose(target);
142 if (tempdir)
143 cleanup_bterm_terminfo(tempdir);
144 if (source)
145 fclose(source);
146 return NULL;
147 }
148
149 /* But we already have a terminal!
150 *
151 * Well, not quite. Firstly, we have to disconnect from debconf (which isn't
152 * too hard; the debconf-disconnect utility in di-utils does it, although in
153 * a cdebconf plugin there's no need for this since our standard file
154 * descriptors are already as desired). Secondly, the terminal type is
155 * "bterm", which the installed system doesn't know about; this causes some
156 * inconveniences. These problems can more or less be handled outside
157 * cdebconf if all we need is to start a shell.
158 *
159 * Much more significantly, though, newt is probably already displaying a
160 * window. In order to do a good enough job to be able to run a full-screen
161 * curses application such as aptitude, we need to tell it to tear
162 * everything down, and then we need to put it all back so that it doesn't
163 * look dreadful when we exit (particularly if e.g. a progress bar is being
164 * displayed).
165 */
166 int
167 cdebconf_newt_handler_terminal(struct frontend *obj, struct question *q)
168 {
169 struct question *saved_progress_title = NULL;
170 int saved_progress_min, saved_progress_max, saved_progress_cur;
171 struct question *saved_progress_info = NULL;
172 const char *command_line;
173 const char *term;
174 char *bterm_tempdir = NULL;
175 struct sigaction sa, osa_sigchld;
176 pid_t pid;
177 int status;
178
179 /* Tear down newt. */
180 newtPopHelpLine();
181 if (obj->progress_title) {
182 saved_progress_title = obj->progress_title;
183 question_ref(saved_progress_title);
184 }
185 saved_progress_min = obj->progress_min;
186 saved_progress_max = obj->progress_max;
187 saved_progress_cur = obj->progress_cur;
188 saved_progress_info = cdebconf_newt_get_progress_info(obj);
189 if (saved_progress_info)
190 question_ref(saved_progress_info);
191 newtFinished();
192
193 command_line = question_get_variable(q, "COMMAND_LINE");
194 if (NULL == command_line)
195 command_line = DEFAULT_COMMAND_LINE;
196
197 term = getenv("TERM");
198 if (term && !strcmp(term, "bterm"))
199 /* ncurses in /target may not have a terminfo definition for bterm.
200 * Try to give it one.
201 */
202 bterm_tempdir = setup_bterm_terminfo();
203
204 /* cdebconf handles SIGCHLD so that it can be notified of confmodule
205 * death. We don't want that here. (Maybe cdebconf should be more
206 * specific about its waitpid instead?)
207 */
208 memset (&sa, 0, sizeof sa);
209 sa.sa_handler = SIG_DFL;
210 sigemptyset(&sa.sa_mask);
211 sa.sa_flags = 0;
212 if (sigaction (SIGCHLD, &sa, &osa_sigchld) < 0) {
213 error("can't set SIGCHLD disposition to default: %s", strerror(errno));
214 return DC_NOTOK;
215 }
216
217 pid = fork();
218 if (-1 == pid) {
219 error("can't fork: %s", strerror(errno));
220 return DC_NOTOK;
221 }
222 if (0 == pid) {
223 /* Child process. Remove all debconf-ish environment variables,
224 * redirect file descriptors, and exec.
225 */
226 size_t cur_env = 0, max_env = 16;
227 char **newenviron = di_new(char *, max_env), **envp;
228 char *argv[4];
229
230 #define ENSURE_ENV do { \
231 if (cur_env >= max_env) { \
232 max_env *= 2; \
233 newenviron = di_renew(char *, newenviron, max_env); \
234 } \
235 } while (0)
236
237 for (envp = environ; envp && *envp; ++envp) {
238 if (strncmp(*envp, "DEBIAN_", 7) != 0 &&
239 strncmp(*envp, "DEBCONF_", 8) != 0) {
240 ENSURE_ENV;
241 newenviron[cur_env++] = strdup(*envp);
242 }
243 }
244 if (bterm_tempdir) {
245 ENSURE_ENV;
246 /* bterm_tempdir always starts with "/target"; remove that,
247 * since the point of the exercise is to make things work for
248 * programs chrooted into /target within d-i. Programs outside
249 * the chroot will already have a working terminfo definition
250 * anyway.
251 */
252 if (asprintf(&newenviron[cur_env++],
253 "TERMINFO=%s", bterm_tempdir + 7) < 0) {
254 /* Not a lot we can do; let's just go on. */
255 }
256 /* UTF-8 line-drawing characters appear not to work in bterm.
257 * ncurses disables these for some known-broken cases such as
258 * TERM=linux; tell it to do so here as well.
259 */
260 ENSURE_ENV;
261 newenviron[cur_env++] = strdup("NCURSES_NO_UTF8_ACS=1");
262 }
263 ENSURE_ENV;
264 newenviron[cur_env] = NULL;
265
266 #undef ENSURE_ENV
267
268 argv[0] = strdup("sh");
269 argv[1] = strdup("-c");
270 argv[2] = strdup(command_line);
271 argv[3] = NULL;
272 execve("/bin/sh", argv, newenviron);
273 exit(127);
274 }
275
276 /* Parent process. */
277 if (waitpid(pid, &status, 0) < 0)
278 error("waitpid failed: %s", strerror(errno));
279
280 /* Restore SIGCHLD handling. */
281 sigaction(SIGCHLD, &osa_sigchld, NULL);
282
283 if (bterm_tempdir)
284 cleanup_bterm_terminfo(bterm_tempdir);
285
286 /* Set newt back up again. */
287 cdebconf_newt_setup();
288 if (saved_progress_title) {
289 obj->methods.progress_start(obj,
290 saved_progress_min, saved_progress_max,
291 saved_progress_title);
292 question_deref(saved_progress_title);
293 obj->methods.progress_set(obj, saved_progress_cur);
294 if (saved_progress_info) {
295 obj->methods.progress_info(obj, saved_progress_info);
296 question_deref(saved_progress_info);
297 }
298 }
299
300 if (status != 0)
301 return DC_NOTOK;
302 else
303 return DC_OK;
304 }