/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/plymouth.c

  • Committer: Björn Påhlsson
  • Date: 2010-09-07 18:48:56 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184856-waz6cvxbm7ranha2
added the actually plugin file for plymouth

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#include <stdbool.h>            /* bool, false, true */
7
7
#include <fcntl.h>              /* open(), O_RDONLY */
8
8
#include <iso646.h>             /* and, or, not*/
9
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct
10
 
                                   dirent, waitpid() */
 
9
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct dirent,
 
10
                                   waitpid() */
11
11
#include <sys/wait.h>           /* waitpid() */
12
12
#include <stddef.h>             /* NULL */
13
13
#include <string.h>             /* strchr(), memcmp() */
14
 
#include <stdio.h>              /* asprintf(), perror(), fopen(),
15
 
                                   fscanf() */
16
 
#include <unistd.h>             /* close(), readlink(), read(),
17
 
                                   fork(), setsid(), chdir(), dup2(),
 
14
#include <stdio.h>              /* asprintf(), perror(), fopen(), fscanf() */
 
15
#include <unistd.h>             /* close(), readlink(), read(), fork()
 
16
                                   setsid(), chdir(), dup2()
18
17
                                   STDERR_FILENO, execv(), access() */
19
18
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
20
19
                                   EXIT_SUCCESS, malloc(), _exit(),
22
21
#include <dirent.h>             /* scandir(), alphasort() */
23
22
#include <inttypes.h>           /* intmax_t, strtoumax(), SCNuMAX */
24
23
#include <sys/stat.h>           /* struct stat, lstat() */
25
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
24
#include <sysexits.h>           /* EX_OSERR */
26
25
#include <error.h>              /* error() */
27
26
#include <errno.h>              /* TEMP_FAILURE_RETRY */
28
 
#include <argz.h>               /* argz_count(), argz_extract() */
 
27
#include <stdarg.h>
29
28
 
30
29
sig_atomic_t interrupted_by_signal = 0;
31
30
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
32
31
const char plymouth_path[] = "/bin/plymouth";
33
32
const char plymouthd_path[] = "/sbin/plymouthd";
34
 
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
35
 
                                        "--mode=boot",
 
33
const char *plymouthd_default_argv[] = {"/sbin/plymouthd", "--mode=boot",
36
34
                                        "--attach-to-session",
37
 
                                        "--pid-file="
38
 
                                        "/dev/.initramfs/"
39
 
                                        "plymouth.pid",
 
35
                                        "--pid-file=/dev/.initramfs/plymouth.pid",
40
36
                                        NULL };
41
37
 
42
38
static void termination_handler(__attribute__((unused))int signum){
127
123
      if (tmp == NULL){
128
124
        error(0, errno, "realloc");
129
125
        free(new_argv);
130
 
        _exit(EX_OSERR);
 
126
        _exit(EXIT_FAILURE);
131
127
      }
132
128
      new_argv = (char **)tmp;
133
129
      new_argv[i] = strdup(argv[i]);
153
149
    error(0, errno, "waitpid");
154
150
    return false;
155
151
  }
156
 
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
 
152
  if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
157
153
    return true;
158
154
  }
159
155
  return false;
167
163
    errno = 0;
168
164
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
169
165
 
170
 
    if(errno != 0 or *tmp != '\0'
171
 
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
166
    if(errno != 0 or *tmp != '\0' or maxvalue != (uintmax_t)((pid_t)maxvalue)){
172
167
      return 0;
173
168
    }
174
169
  }
179
174
    error(0, errno, "asprintf");
180
175
    return 0;
181
176
  }
182
 
  
 
177
 
183
178
  struct stat exe_stat;
184
179
  ret = lstat(exe_link, &exe_stat);
185
180
  if(ret == -1){
196
191
    free(exe_link);
197
192
    return 0;
198
193
  }
199
 
  
 
194
 
200
195
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
201
196
  free(exe_link);
202
197
  if((sret != (ssize_t)sizeof(plymouth_path)-1) or
291
286
  }
292
287
  
293
288
  /* we got cmdline and cmdline_len, ignore rest... */
294
 
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
295
 
                       * sizeof(char *)); /* Get number of args */
296
 
  if(argv == NULL){
297
 
    error(0, errno, "argv = malloc()");
298
 
    free(cmdline);
299
 
    return NULL;
 
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 *));
 
294
    if(tmp == NULL){
 
295
      error(0, errno, "realloc");
 
296
      free(argv);
 
297
      return NULL;
 
298
    }
 
299
    argv = (const char **)tmp;
 
300
    argv[argv_size-1] = arg;
300
301
  }
301
 
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
302
 
  return (const char **)argv;
 
302
  argv[argv_size] = NULL;
 
303
  return argv;
303
304
}
304
305
 
305
306
int main(__attribute__((unused))int argc,
313
314
  /* test -x /bin/plymouth */
314
315
  ret = access(plymouth_path, X_OK);
315
316
  if(ret == -1){
316
 
    /* Plymouth is probably not installed.  Don't print an error
317
 
       message, just exit. */
318
 
    exit(EX_UNAVAILABLE);
 
317
    exit(EXIT_FAILURE);
319
318
  }
320
 
  
 
319
 
321
320
  { /* Add signal handlers */
322
321
    struct sigaction old_action,
323
322
      new_action = { .sa_handler = termination_handler,
324
323
                     .sa_flags = 0 };
325
324
    sigemptyset(&new_action.sa_mask);
326
 
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
327
 
        *sig != 0; sig++){
 
325
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
328
326
      ret = sigaddset(&new_action.sa_mask, *sig);
329
327
      if(ret == -1){
330
 
        error(EX_OSERR, errno, "sigaddset");
 
328
        error(0, errno, "sigaddset");
 
329
        exit(EX_OSERR);
331
330
      }
332
331
      ret = sigaction(*sig, NULL, &old_action);
333
332
      if(ret == -1){
334
 
        error(EX_OSERR, errno, "sigaction");
 
333
        error(0, errno, "sigaction");
 
334
        exit(EX_OSERR);
335
335
      }
336
336
      if(old_action.sa_handler != SIG_IGN){
337
337
        ret = sigaction(*sig, &new_action, NULL);
338
338
        if(ret == -1){
339
 
          error(EX_OSERR, errno, "sigaction");
 
339
          error(0, errno, "sigaction");
 
340
          exit(EX_OSERR);
340
341
        }
341
342
      }
342
343
    }
343
344
  }
344
 
  
 
345
    
345
346
  /* plymouth --ping */
346
347
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
347
 
                       (const char *[])
348
 
                       { plymouth_path, "--ping", NULL },
 
348
                       (const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
349
349
                       true, false);
350
350
  if(not bret){
351
351
    if(interrupted_by_signal){
352
352
      kill_and_wait(plymouth_command_pid);
353
 
      exit(EXIT_FAILURE);
354
353
    }
355
 
    /* Plymouth is probably not running.  Don't print an error
356
 
       message, just exit. */
357
 
    exit(EX_UNAVAILABLE);
 
354
    exit(EXIT_FAILURE);
358
355
  }
359
356
  
360
357
  prompt = makeprompt();
361
358
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
362
359
  free(prompt);
363
360
  if(ret == -1){
364
 
    error(EX_OSERR, errno, "asprintf");
 
361
    error(0, errno, "asprintf");
 
362
    exit(EXIT_FAILURE);
365
363
  }
366
364
  
367
365
  /* plymouth ask-for-password --prompt="$prompt" */
368
 
  bret = exec_and_wait(&plymouth_command_pid,
369
 
                       plymouth_path, (const char *[])
370
 
                       { plymouth_path, "ask-for-password",
371
 
                           prompt_arg, NULL },
 
366
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
 
367
                       (const char *[]){plymouth_path, "ask-for-password", prompt_arg, NULL},
372
368
                       true, false);
373
369
  free(prompt_arg);
 
370
  if(not bret){
 
371
    if(interrupted_by_signal){
 
372
      kill_and_wait(plymouth_command_pid);
 
373
    } else {
 
374
      exit(EXIT_FAILURE);
 
375
    }
 
376
  }
 
377
  
374
378
  if(bret){
375
379
    exit(EXIT_SUCCESS);
376
380
  }
377
 
  if(not interrupted_by_signal){
378
 
    /* exec_and_wait failed for some other reason */
379
 
    exit(EXIT_FAILURE);
380
 
  }
381
 
  kill_and_wait(plymouth_command_pid);
382
381
  
383
 
  const char **plymouthd_argv;
 
382
  const char **plymouthd_argv = NULL;
384
383
  pid_t pid = get_pid();
385
384
  if(pid == 0){
386
385
    error(0, 0, "plymouthd pid not found");
 
386
  } else {
 
387
    plymouthd_argv = getargv(pid);
 
388
  }
 
389
  if(plymouthd_argv == NULL){
387
390
    plymouthd_argv = plymouthd_default_argv;
388
 
  } else {
389
 
    plymouthd_argv = getargv(pid);
390
391
  }
391
392
  
392
 
  bret = exec_and_wait(NULL, plymouth_path, (const char *[])
393
 
                       { plymouth_path, "quit", NULL },
394
 
                       false, false);
395
 
  if(not bret){
396
 
    exit(EXIT_FAILURE);
397
 
  }
398
 
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
399
 
                       false, true);
400
 
  if(not bret){
401
 
    exit(EXIT_FAILURE);
402
 
  }
403
 
  exec_and_wait(NULL, plymouth_path, (const char *[])
404
 
                { plymouth_path, "show-splash", NULL },
405
 
                false, false);
 
393
  bret = exec_and_wait(NULL, plymouth_path,
 
394
                       (const char *[]){plymouth_path, "quit", NULL}, false, false);
 
395
  if(not bret){
 
396
    exit(EXIT_FAILURE);
 
397
  }
 
398
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv, false, true);
 
399
  if(not bret){
 
400
    exit(EXIT_FAILURE);
 
401
  }
 
402
  exec_and_wait(NULL, plymouth_path,
 
403
                (const char *[]){ plymouth_path, "show-splash", NULL }, false, false);
406
404
  exit(EXIT_FAILURE);
407
405
}