1
1
#define _GNU_SOURCE /* asprintf() */
2
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
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
3
sigemptyset(), sigaddset(),
4
sigaction, SIGINT, SIG_IGN, SIGHUP,
5
SIGTERM, kill(), SIGKILL */
12
6
#include <stddef.h> /* NULL */
13
#include <string.h> /* strlen(), memcmp() */
7
#include <stdlib.h> /* getenv() */
14
8
#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(),
23
#include <stdlib.h> /* getenv() */
9
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS,
11
#include <sys/types.h> /* pid_t, DIR, struct dirent,
24
13
#include <dirent.h> /* opendir(), readdir(), closedir() */
25
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
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 */
27
24
sig_atomic_t interrupted_by_signal = 0;
30
27
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){
99
30
int main(__attribute__((unused))int argc,
100
31
__attribute__((unused))char **argv){
309
218
/* Write command to FIFO */
310
219
if(not interrupted_by_signal){
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;
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;
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){
329
263
char *buf = NULL;
330
264
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){
351
266
/* Read from FIFO */
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;
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){
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;
366
286
sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
370
an_error_occured = true;
373
if(interrupted_by_signal){
289
an_error_occured = true;
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){
292
buf_len += (size_t)sret;
293
}while(not interrupted_by_signal and sret != 0);
398
297
/* Print password to stdout */
400
while(written < buf_len){
298
if(not interrupted_by_signal and not an_error_occured){
402
301
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
406
an_error_occured = true;
409
if(interrupted_by_signal){
302
if(sret == -1 and not interrupted_by_signal){
304
an_error_occured = true;
414
if(interrupted_by_signal or an_error_occured){
307
written += (size_t)sret;
308
}while(written < buf_len);
309
if(not interrupted_by_signal and not an_error_occured){
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*/
315
kill(usplash_pid, SIGTERM);
430
317
int cmdline_argc = 0;
431
318
char **cmdline_argv = malloc(sizeof(char *));
319
/* Create argv and argc for new usplash*/
434
while(position < cmdline_len){
321
ptrdiff_t position = 0;
322
while((size_t)position < cmdline_len){
435
323
char **tmp = realloc(cmdline_argv,
437
* (size_t)(cmdline_argc + 2)));
324
(sizeof(char *) * (size_t)(cmdline_argc + 2)));
439
326
perror("realloc");
440
327
free(cmdline_argv);
443
330
cmdline_argv = tmp;
444
331
cmdline_argv[cmdline_argc] = cmdline + position;
446
position += strlen(cmdline + position) + 1;
333
position = (char *)rawmemchr(cmdline + position, '\0')
448
336
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);
457
338
pid_t new_usplash_pid = fork();
458
339
if(new_usplash_pid == 0){
459
340
/* Child; will become new usplash process */
461
/* Make the effective user ID (root) the only user ID instead of
462
the real user ID (mandos) */
463
ret = setuid(geteuid());
341
while(kill(usplash_pid, 0)){
343
kill(usplash_pid, SIGKILL);
470
/* if(fork() != 0){ */
471
/* _exit(EXIT_SUCCESS); */
473
346
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
476
349
_exit(EXIT_FAILURE);
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");
351
execv("/sbin/usplash", cmdline_argv);
495
354
return EXIT_FAILURE;