1
1
#define _GNU_SOURCE /* asprintf() */
2
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
sigemptyset(), sigaddset(),
4
sigaction, SIGINT, SIG_IGN, SIGHUP,
5
SIGTERM, kill(), SIGKILL */
3
sigemptyset(), sigaddset(), SIGINT,
4
SIGHUP, SIGTERM, sigaction(),
5
SIG_IGN, kill(), SIGKILL */
6
#include <stdbool.h> /* bool, false, true */
7
#include <fcntl.h> /* open(), O_WRONLY, O_RDONLY */
8
#include <iso646.h> /* and, or, not*/
9
#include <errno.h> /* errno, EINTR */
10
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
6
12
#include <stddef.h> /* NULL */
13
#include <string.h> /* strlen(), memcmp() */
14
#include <stdio.h> /* asprintf(), perror() */
15
#include <unistd.h> /* close(), write(), readlink(),
16
read(), STDOUT_FILENO, sleep(),
17
fork(), setuid(), geteuid(),
18
setsid(), chdir(), dup2(),
19
STDERR_FILENO, execv() */
20
#include <stdlib.h> /* free(), EXIT_FAILURE, strtoul(),
21
realloc(), EXIT_SUCCESS, malloc(),
7
23
#include <stdlib.h> /* getenv() */
8
#include <stdio.h> /* asprintf(), perror() */
9
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS,
11
#include <sys/types.h> /* pid_t, DIR, struct dirent,
13
24
#include <dirent.h> /* opendir(), readdir(), closedir() */
14
#include <unistd.h> /* readlink(), fork(), execl(),
16
#include <string.h> /* memcmp() */
17
#include <iso646.h> /* and */
18
#include <stdbool.h> /* bool, false, true */
19
#include <errno.h> /* errno */
20
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
22
#include <fcntl.h> /* open(), O_RDONLY */
25
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
24
27
sig_atomic_t interrupted_by_signal = 0;
27
30
interrupted_by_signal = 1;
33
static bool usplash_write(const char *cmd, const char *arg){
35
* usplash_write("TIMEOUT", "15") will write "TIMEOUT 15\0"
36
* usplash_write("PULSATE", NULL) will write "PULSATE\0"
43
fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
44
if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
47
}while(fifo_fd == -1);
51
char *cmd_line_alloc = NULL;
54
cmd_line_len = strlen(cmd);
57
ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
58
if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
65
cmd_line = cmd_line_alloc;
66
cmd_line_len = (size_t)ret + 1;
70
while(not interrupted_by_signal and written < cmd_line_len){
71
ret = write(fifo_fd, cmd_line + written,
72
cmd_line_len - written);
74
if(errno != EINTR or interrupted_by_signal){
84
written += (size_t)ret;
89
if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
93
if(interrupted_by_signal){
30
99
int main(__attribute__((unused))int argc,
31
100
__attribute__((unused))char **argv){
218
309
/* Write command to FIFO */
219
310
if(not interrupted_by_signal){
220
int fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
227
ret = asprintf(&command, "INPUTQUIET %s", prompt);
235
size_t command_len = (size_t)ret + 1;
237
while(not interrupted_by_signal and written < command_len){
238
ret = write(fifo_fd, command + written, command_len - written);
240
if(interrupted_by_signal){
248
an_error_occured = true;
251
written += (size_t)ret;
253
ret = close(fifo_fd);
254
if(ret == -1 and not interrupted_by_signal){
255
an_error_occured = true;
311
if(not usplash_write("TIMEOUT", "0")
312
and (errno != EINTR)){
313
perror("usplash_write");
314
an_error_occured = true;
317
if(not interrupted_by_signal and not an_error_occured){
318
if(not usplash_write("INPUTQUIET", prompt)
319
and (errno != EINTR)){
320
perror("usplash_write");
321
an_error_occured = true;
326
/* This is not really a loop; while() is used to be able to "break"
327
out of it; those breaks are marked "Big" */
328
while(not interrupted_by_signal and not an_error_occured){
263
329
char *buf = NULL;
264
330
size_t buf_len = 0;
335
fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
339
an_error_occured = true;
342
if(interrupted_by_signal){
346
}while(fifo_fd == -1);
347
if(interrupted_by_signal or an_error_occured){
266
351
/* Read from FIFO */
267
if(not interrupted_by_signal and not an_error_occured){
268
int fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
269
if(fifo_fd == -1 and not interrupted_by_signal){
352
size_t buf_allocated = 0;
353
const size_t blocksize = 1024;
355
if(buf_len + blocksize > buf_allocated){
356
char *tmp = realloc(buf, buf_allocated + blocksize);
359
an_error_occured = true;
363
buf_allocated += blocksize;
273
size_t buf_allocated = 0;
274
const int blocksize = 1024;
276
if(buf_len + blocksize > buf_allocated){
277
char *tmp = realloc(buf, buf_allocated + blocksize);
280
an_error_occured = true;
284
buf_allocated += blocksize;
286
366
sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
289
an_error_occured = true;
370
an_error_occured = true;
373
if(interrupted_by_signal){
292
buf_len += (size_t)sret;
293
}while(not interrupted_by_signal and sret != 0);
378
if(interrupted_by_signal or an_error_occured){
382
buf_len += (size_t)sret;
385
if(interrupted_by_signal or an_error_occured){
389
if(not usplash_write("TIMEOUT", "15")
390
and (errno != EINTR)){
391
perror("usplash_write");
392
an_error_occured = true;
394
if(interrupted_by_signal or an_error_occured){
297
398
/* Print password to stdout */
298
if(not interrupted_by_signal and not an_error_occured){
400
while(written < buf_len){
301
402
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
302
if(sret == -1 and not interrupted_by_signal){
304
an_error_occured = true;
406
an_error_occured = true;
409
if(interrupted_by_signal){
307
written += (size_t)sret;
308
}while(written < buf_len);
309
if(not interrupted_by_signal and not an_error_occured){
414
if(interrupted_by_signal or an_error_occured){
315
kill(usplash_pid, SIGTERM);
417
written += (size_t)sret;
420
if(not interrupted_by_signal and not an_error_occured){
425
} /* end of non-loop while() */
427
/* If we got here, an error or interrupt must have happened */
429
/* Create argc and argv for new usplash*/
317
430
int cmdline_argc = 0;
318
431
char **cmdline_argv = malloc(sizeof(char *));
319
/* Create argv and argc for new usplash*/
321
ptrdiff_t position = 0;
322
while((size_t)position < cmdline_len){
434
while(position < cmdline_len){
323
435
char **tmp = realloc(cmdline_argv,
324
(sizeof(char *) * (size_t)(cmdline_argc + 2)));
437
* (size_t)(cmdline_argc + 2)));
326
439
perror("realloc");
327
440
free(cmdline_argv);
330
443
cmdline_argv = tmp;
331
444
cmdline_argv[cmdline_argc] = cmdline + position;
333
position = (char *)rawmemchr(cmdline + position, '\0')
446
position += strlen(cmdline + position) + 1;
336
448
cmdline_argv[cmdline_argc] = NULL;
450
/* Kill old usplash */
451
kill(usplash_pid, SIGTERM);
453
while(kill(usplash_pid, 0) == 0){
454
kill(usplash_pid, SIGKILL);
338
457
pid_t new_usplash_pid = fork();
339
458
if(new_usplash_pid == 0){
340
459
/* Child; will become new usplash process */
341
while(kill(usplash_pid, 0)){
343
kill(usplash_pid, SIGKILL);
461
/* Make the effective user ID (root) the only user ID instead of
462
the real user ID (mandos) */
463
ret = setuid(geteuid());
470
/* if(fork() != 0){ */
471
/* _exit(EXIT_SUCCESS); */
346
473
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
349
476
_exit(EXIT_FAILURE);
351
execv("/sbin/usplash", cmdline_argv);
479
execv(usplash_name, cmdline_argv);
480
if(not interrupted_by_signal){
490
if(not usplash_write("PULSATE", NULL)
491
and (errno != EINTR)){
492
perror("usplash_write");
354
495
return EXIT_FAILURE;