1
#define _GNU_SOURCE /* asprintf() */
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
sigemptyset(), sigaddset(),
4
sigaction, SIGINT, SIG_IGN, SIGHUP,
5
SIGTERM, kill(), SIGKILL */
6
#include <stddef.h> /* NULL */
7
#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
#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 <errno.h> /* errno */
19
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
22
sig_atomic_t interrupted_by_signal = 0;
24
static void termination_handler(__attribute__((unused))int signum){
25
interrupted_by_signal = 1;
28
int main(__attribute__((unused))int argc, char **argv){
31
/* Create prompt string */
34
const char *const cryptsource = getenv("cryptsource");
35
const char *const crypttarget = getenv("crypttarget");
36
const char *const prompt_start = "getpass "
37
"Enter passphrase to unlock the disk";
39
if(cryptsource == NULL){
40
if(crypttarget == NULL){
41
ret = asprintf(&prompt, "%s: ", prompt_start);
43
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
47
if(crypttarget == NULL){
48
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
50
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
51
cryptsource, crypttarget);
59
/* Find splashy process */
60
pid_t splashy_pid = 0;
62
const char splashy_name[] = "/sbin/splashy";
63
DIR *proc_dir = opendir("/proc");
69
for(struct dirent *proc_ent = readdir(proc_dir);
71
proc_ent = readdir(proc_dir)){
72
pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
77
/* Find the executable name by doing readlink() on the
78
/proc/<pid>/exe link */
80
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
87
char exe_target[sizeof(splashy_name)];
88
ssize_t sret = readlink(exe_link, exe_target,
90
if((sret == ((ssize_t)sizeof(exe_target)-1))
91
and (memcmp(splashy_name, exe_target,
92
sizeof(exe_target)-1) == 0)){
104
/* Set up the signal handler */
106
struct sigaction old_action,
107
new_action = { .sa_handler = termination_handler,
109
sigemptyset(&new_action.sa_mask);
110
sigaddset(&new_action.sa_mask, SIGINT);
111
sigaddset(&new_action.sa_mask, SIGHUP);
112
sigaddset(&new_action.sa_mask, SIGTERM);
113
ret = sigaction(SIGINT, NULL, &old_action);
119
if (old_action.sa_handler != SIG_IGN){
120
ret = sigaction(SIGINT, &new_action, NULL);
127
ret = sigaction(SIGHUP, NULL, &old_action);
133
if (old_action.sa_handler != SIG_IGN){
134
ret = sigaction(SIGHUP, &new_action, NULL);
141
ret = sigaction(SIGTERM, NULL, &old_action);
147
if (old_action.sa_handler != SIG_IGN){
148
ret = sigaction(SIGTERM, &new_action, NULL);
157
/* Fork off the splashy command to prompt for password */
158
pid_t splashy_command_pid = 0;
159
if(not interrupted_by_signal){
160
splashy_command_pid = fork();
161
if(splashy_command_pid == -1){
162
if(not interrupted_by_signal){
168
if(splashy_command_pid == 0){
169
const char splashy_command[] = "/sbin/splashy_update";
170
ret = execl(splashy_command, splashy_command, prompt,
172
if(not interrupted_by_signal and errno != ENOENT){
173
/* Don't report "File not found", since splashy might not be
185
/* Wait for command to complete */
187
while(not interrupted_by_signal){
188
waitpid(splashy_command_pid, &status, 0);
189
if(not interrupted_by_signal
190
and WIFEXITED(status) and WEXITSTATUS(status)==0){
194
kill(splashy_pid, SIGTERM);
195
if(interrupted_by_signal){
196
kill(splashy_command_pid, SIGTERM);
199
pid_t new_splashy_pid = fork();
200
if(new_splashy_pid == 0){
201
while(kill(splashy_pid, 0)){
203
kill(splashy_pid, SIGKILL);
206
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
211
execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);