1
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
sigemptyset(), sigaddset(), SIGINT,
4
SIGHUP, SIGTERM, sigaction(),
6
#include <stdbool.h> /* bool, false, true */
7
#include <fcntl.h> /* open(), O_RDONLY */
8
#include <iso646.h> /* and, or, not*/
9
#include <sys/types.h> /* size_t, ssize_t, pid_t, struct dirent,
11
#include <sys/wait.h> /* waitpid() */
12
#include <stddef.h> /* NULL */
13
#include <string.h> /* strchr(), memcmp() */
14
#include <stdio.h> /* asprintf(), perror(), fopen(), fscanf() */
15
#include <unistd.h> /* close(), readlink(), read(), fork()
16
setsid(), chdir(), dup2()
17
STDERR_FILENO, execv(), access() */
18
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
19
EXIT_SUCCESS, malloc(), _exit(),
21
#include <dirent.h> /* scandir(), alphasort() */
22
#include <inttypes.h> /* intmax_t, strtoumax(), SCNuMAX */
23
#include <sys/stat.h> /* struct stat, lstat() */
24
#include <sysexits.h> /* EX_OSERR */
25
#include <error.h> /* error() */
26
#include <errno.h> /* TEMP_FAILURE_RETRY */
29
sig_atomic_t interrupted_by_signal = 0;
30
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
31
const char plymouth_path[] = "/bin/plymouth";
32
const char plymouthd_path[] = "/sbin/plymouthd";
33
const char *plymouthd_default_argv[] = {"/sbin/plymouthd", "--mode=boot",
34
"--attach-to-session",
35
"--pid-file=/dev/.initramfs/plymouth.pid",
38
static void termination_handler(__attribute__((unused))int signum){
39
if(interrupted_by_signal){
42
interrupted_by_signal = 1;
45
/* Create prompt string */
46
char *makeprompt(void){
49
const char *const cryptsource = getenv("cryptsource");
50
const char *const crypttarget = getenv("crypttarget");
51
const char prompt_start[] = "Enter passphrase to unlock the disk";
53
if(cryptsource == NULL){
54
if(crypttarget == NULL){
55
ret = asprintf(&prompt, "%s: ", prompt_start);
57
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
61
if(crypttarget == NULL){
62
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
64
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
65
cryptsource, crypttarget);
74
void kill_and_wait(pid_t pid){
75
TEMP_FAILURE_RETRY(kill(pid, SIGTERM));
76
TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
79
bool become_a_daemon(void){
80
int ret = setuid(geteuid());
82
error(0, errno, "setuid");
88
error(0, errno, "chdir");
91
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
93
error(0, errno, "dup2");
99
bool exec_and_wait(pid_t *pid_return, const char *path,
100
const char **argv, bool interruptable,
107
error(0, errno, "fork");
113
if(not become_a_daemon()){
118
char **new_argv = NULL;
121
for (; argv[i]!=(char *)NULL; i++){
122
tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
124
error(0, errno, "realloc");
128
new_argv = (char **)tmp;
129
new_argv[i] = strdup(argv[i]);
131
new_argv[i] = (char *) NULL;
133
execv(path, (char *const *)new_argv);
134
error(0, errno, "execv");
137
if(pid_return != NULL){
141
ret = waitpid(pid, &status, 0);
142
} while(ret == -1 and errno == EINTR
143
and ((not interrupted_by_signal)
144
or (not interruptable)));
145
if(interrupted_by_signal and interruptable){
149
error(0, errno, "waitpid");
152
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
158
int is_plymouth(const struct dirent *proc_entry){
164
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
166
if(errno != 0 or *tmp != '\0' or maxvalue != (uintmax_t)((pid_t)maxvalue)){
170
char exe_target[sizeof(plymouth_path)];
172
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
174
error(0, errno, "asprintf");
178
struct stat exe_stat;
179
ret = lstat(exe_link, &exe_stat);
183
error(0, errno, "lstat");
188
if(not S_ISLNK(exe_stat.st_mode)
189
or exe_stat.st_uid != 0
190
or exe_stat.st_gid != 0){
195
ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
197
if((sret != (ssize_t)sizeof(plymouth_path)-1) or
198
(memcmp(plymouth_path, exe_target,
199
sizeof(plymouth_path)-1) != 0)){
207
FILE *pidfile = fopen(plymouth_pid, "r");
208
uintmax_t maxvalue = 0;
210
ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
217
struct dirent **direntries;
218
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
219
sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
222
pid = (pid_t)maxvalue;
223
if((uintmax_t)pid == maxvalue){
230
const char **getargv(pid_t pid){
232
char *cmdline_filename;
236
ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
239
error(0, errno, "asprintf");
243
/* Open /proc/<pid>/cmdline */
244
cl_fd = open(cmdline_filename, O_RDONLY);
245
free(cmdline_filename);
247
error(0, errno, "open");
251
size_t cmdline_allocated = 0;
252
size_t cmdline_len = 0;
253
char *cmdline = NULL;
255
const size_t blocksize = 1024;
257
/* Allocate more space? */
258
if(cmdline_len + blocksize > cmdline_allocated){
259
tmp = realloc(cmdline, cmdline_allocated + blocksize);
261
error(0, errno, "realloc");
267
cmdline_allocated += blocksize;
271
sret = read(cl_fd, cmdline + cmdline_len,
272
cmdline_allocated - cmdline_len);
274
error(0, errno, "read");
279
cmdline_len += (size_t)sret;
283
error(0, errno, "close");
288
/* we got cmdline and cmdline_len, ignore rest... */
289
const char **argv = NULL;
290
size_t argv_size = 0;
291
for(char *arg = cmdline; arg-cmdline < (ssize_t)cmdline_len;
292
arg = strchr(arg, '\0')+1){
293
tmp = realloc(argv, ((++argv_size)+1)*sizeof(char *));
295
error(0, errno, "realloc");
299
argv = (const char **)tmp;
300
argv[argv_size-1] = arg;
302
argv[argv_size] = NULL;
306
int main(__attribute__((unused))int argc,
307
__attribute__((unused))char **argv){
310
pid_t plymouth_command_pid;
314
/* test -x /bin/plymouth */
315
ret = access(plymouth_path, X_OK);
320
{ /* Add signal handlers */
321
struct sigaction old_action,
322
new_action = { .sa_handler = termination_handler,
324
sigemptyset(&new_action.sa_mask);
325
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
326
ret = sigaddset(&new_action.sa_mask, *sig);
328
error(0, errno, "sigaddset");
331
ret = sigaction(*sig, NULL, &old_action);
333
error(0, errno, "sigaction");
336
if(old_action.sa_handler != SIG_IGN){
337
ret = sigaction(*sig, &new_action, NULL);
339
error(0, errno, "sigaction");
346
/* plymouth --ping */
347
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
348
(const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
351
if(interrupted_by_signal){
352
kill_and_wait(plymouth_command_pid);
357
prompt = makeprompt();
358
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
361
error(0, errno, "asprintf");
365
/* plymouth ask-for-password --prompt="$prompt" */
366
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
367
(const char *[]){plymouth_path, "ask-for-password", prompt_arg, NULL},
371
if(interrupted_by_signal){
372
kill_and_wait(plymouth_command_pid);
382
const char **plymouthd_argv = NULL;
383
pid_t pid = get_pid();
385
error(0, 0, "plymouthd pid not found");
387
plymouthd_argv = getargv(pid);
389
if(plymouthd_argv == NULL){
390
plymouthd_argv = plymouthd_default_argv;
393
bret = exec_and_wait(NULL, plymouth_path,
394
(const char *[]){plymouth_path, "quit", NULL}, false, false);
398
bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv, false, true);
402
exec_and_wait(NULL, plymouth_path,
403
(const char *[]){ plymouth_path, "show-splash", NULL }, false, false);