27
27
interrupted_by_signal = 1;
30
static bool usplash_write(const char *cmd, const char *arg){
32
* usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
33
* usplash_write("PULSATE", NULL); -> "PULSATE\0"
40
fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
41
if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
44
}while(fifo_fd == -1);
47
char *cmd_line_alloc = NULL;
50
ret = (int)strlen(cmd);
53
ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
54
if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
61
cmd_line = cmd_line_alloc;
64
size_t cmd_line_len = (size_t)ret + 1;
66
while(not interrupted_by_signal and written < cmd_line_len){
67
ret = write(fifo_fd, cmd_line + written,
68
cmd_line_len - written);
70
if(errno != EINTR or interrupted_by_signal){
80
written += (size_t)ret;
85
if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
89
if(interrupted_by_signal){
95
30
int main(__attribute__((unused))int argc,
96
31
__attribute__((unused))char **argv){
283
218
/* Write command to FIFO */
284
219
if(not interrupted_by_signal){
285
if(not usplash_write("TIMEOUT", "0")
286
and (errno != EINTR)){
287
perror("usplash_write");
288
an_error_occured = true;
291
if(not interrupted_by_signal and not an_error_occured){
292
if(not usplash_write("INPUTQUIET", prompt)
293
and (errno != EINTR)){
294
perror("usplash_write");
295
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;
300
/* This is not really a loop; while() is used to be able to "break"
301
out of it; those breaks are marked "Big" */
302
while(not interrupted_by_signal and not an_error_occured){
303
263
char *buf = NULL;
304
264
size_t buf_len = 0;
309
fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
313
an_error_occured = true;
316
if(interrupted_by_signal){
320
}while(fifo_fd == -1);
321
if(interrupted_by_signal or an_error_occured){
325
266
/* Read from FIFO */
326
size_t buf_allocated = 0;
327
const size_t blocksize = 1024;
329
if(buf_len + blocksize > buf_allocated){
330
char *tmp = realloc(buf, buf_allocated + blocksize);
333
an_error_occured = true;
337
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;
340
286
sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
344
an_error_occured = true;
347
if(interrupted_by_signal){
289
an_error_occured = true;
352
if(interrupted_by_signal or an_error_occured){
356
buf_len += (size_t)sret;
359
if(interrupted_by_signal or an_error_occured){
363
if(not usplash_write("TIMEOUT", "15")
364
and (errno != EINTR)){
365
perror("usplash_write");
366
an_error_occured = true;
368
if(interrupted_by_signal or an_error_occured){
292
buf_len += (size_t)sret;
293
}while(not interrupted_by_signal and sret != 0);
372
297
/* Print password to stdout */
298
if(not interrupted_by_signal and not an_error_occured){
376
301
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
380
an_error_occured = true;
383
if(interrupted_by_signal){
302
if(sret == -1 and not interrupted_by_signal){
304
an_error_occured = true;
388
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){
392
written += (size_t)sret;
393
}while(written < buf_len);
394
if(not interrupted_by_signal and not an_error_occured){
400
/* If we got here, an error or interrupt must have happened */
315
kill(usplash_pid, SIGTERM);
402
317
int cmdline_argc = 0;
403
318
char **cmdline_argv = malloc(sizeof(char *));
404
319
/* Create argv and argc for new usplash*/
407
while(position < cmdline_len){
321
ptrdiff_t position = 0;
322
while((size_t)position < cmdline_len){
408
323
char **tmp = realloc(cmdline_argv,
409
324
(sizeof(char *) * (size_t)(cmdline_argc + 2)));
415
330
cmdline_argv = tmp;
416
331
cmdline_argv[cmdline_argc] = cmdline + position;
418
position += strlen(cmdline + position) + 1;
333
position = (char *)rawmemchr(cmdline + position, '\0')
420
336
cmdline_argv[cmdline_argc] = NULL;
422
/* Kill old usplash */
423
kill(usplash_pid, SIGTERM);
425
while(kill(usplash_pid, 0) == 0){
426
kill(usplash_pid, SIGKILL);
429
338
pid_t new_usplash_pid = fork();
430
339
if(new_usplash_pid == 0){
431
340
/* Child; will become new usplash process */
433
/* Make the effective user ID (root) the only user ID instead of
434
the real user ID (mandos) */
435
ret = setuid(geteuid());
341
while(kill(usplash_pid, 0)){
343
kill(usplash_pid, SIGKILL);
442
/* if(fork() != 0){ */
443
/* _exit(EXIT_SUCCESS); */
445
346
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
448
349
_exit(EXIT_FAILURE);
451
execv(usplash_name, cmdline_argv);
454
if(not usplash_write("PULSATE", NULL)
455
and (errno != EINTR)){
456
perror("usplash_write");
351
execv("/sbin/usplash", cmdline_argv);
459
354
return EXIT_FAILURE;