| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 42 |
|
| 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); |
| 67 |
else { |
| 68 |
unlink(path); |
| 69 |
free(path); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
closedir(dir); |
| 74 |
rmdir(tempdir); |
| 75 |
free(tempdir); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 96 |
|
| 97 |
if (stat("/target/bin/sh", &st) < 0) |
| 98 |
|
| 99 |
return NULL; |
| 100 |
|
| 101 |
source = fopen("/usr/share/terminfo/b/bterm", "r"); |
| 102 |
if (!source) |
| 103 |
|
| 104 |
|
| 105 |
if (stat("/target/usr/share/terminfo/b/bterm", &st) == 0) |
| 106 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 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 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
bterm_tempdir = setup_bterm_terminfo(); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 224 |
|
| 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 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (asprintf(&newenviron[cur_env++], |
| 253 |
"TERMINFO=%s", bterm_tempdir + 7) < 0) { |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 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 |
|
| 277 |
if (waitpid(pid, &status, 0) < 0) |
| 278 |
error("waitpid failed: %s", strerror(errno)); |
| 279 |
|
| 280 |
|
| 281 |
sigaction(SIGCHLD, &osa_sigchld, NULL); |
| 282 |
|
| 283 |
if (bterm_tempdir) |
| 284 |
cleanup_bterm_terminfo(bterm_tempdir); |
| 285 |
|
| 286 |
|
| 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 |
} |