/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: teddy at bsnet
  • Date: 2010-09-15 17:17:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100915171746-bg5cppurdd0pcvcy
* debian/watch: Also watch for "*.tar.xz" files.

* plugins.d/plymouth.c (getargv): Use argz_count() and argz_extract().
* plugins.d/usplash.c (main): - '' -

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
27
#include <argz.h>               /* argz_count(), argz_extract() */
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)
 
289
  char **argv = malloc((argz_count(cmdline, cmdline_len)+1)
295
290
                       * sizeof(char *)); /* Get number of args */
296
291
  if(argv == NULL){
297
292
    error(0, errno, "argv = malloc()");
313
308
  /* test -x /bin/plymouth */
314
309
  ret = access(plymouth_path, X_OK);
315
310
  if(ret == -1){
316
 
    /* Plymouth is probably not installed.  Don't print an error
317
 
       message, just exit. */
318
 
    exit(EX_UNAVAILABLE);
 
311
    exit(EXIT_FAILURE);
319
312
  }
320
 
  
 
313
 
321
314
  { /* Add signal handlers */
322
315
    struct sigaction old_action,
323
316
      new_action = { .sa_handler = termination_handler,
324
317
                     .sa_flags = 0 };
325
318
    sigemptyset(&new_action.sa_mask);
326
 
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
327
 
        *sig != 0; sig++){
 
319
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
328
320
      ret = sigaddset(&new_action.sa_mask, *sig);
329
321
      if(ret == -1){
330
 
        error(EX_OSERR, errno, "sigaddset");
 
322
        error(0, errno, "sigaddset");
 
323
        exit(EX_OSERR);
331
324
      }
332
325
      ret = sigaction(*sig, NULL, &old_action);
333
326
      if(ret == -1){
334
 
        error(EX_OSERR, errno, "sigaction");
 
327
        error(0, errno, "sigaction");
 
328
        exit(EX_OSERR);
335
329
      }
336
330
      if(old_action.sa_handler != SIG_IGN){
337
331
        ret = sigaction(*sig, &new_action, NULL);
338
332
        if(ret == -1){
339
 
          error(EX_OSERR, errno, "sigaction");
 
333
          error(0, errno, "sigaction");
 
334
          exit(EX_OSERR);
340
335
        }
341
336
      }
342
337
    }
343
338
  }
344
 
  
 
339
    
345
340
  /* plymouth --ping */
346
341
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
347
 
                       (const char *[])
348
 
                       { plymouth_path, "--ping", NULL },
 
342
                       (const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
349
343
                       true, false);
350
344
  if(not bret){
351
345
    if(interrupted_by_signal){
352
346
      kill_and_wait(plymouth_command_pid);
353
 
      exit(EXIT_FAILURE);
354
347
    }
355
 
    /* Plymouth is probably not running.  Don't print an error
356
 
       message, just exit. */
357
 
    exit(EX_UNAVAILABLE);
 
348
    exit(EXIT_FAILURE);
358
349
  }
359
350
  
360
351
  prompt = makeprompt();
361
352
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
362
353
  free(prompt);
363
354
  if(ret == -1){
364
 
    error(EX_OSERR, errno, "asprintf");
 
355
    error(0, errno, "asprintf");
 
356
    exit(EXIT_FAILURE);
365
357
  }
366
358
  
367
359
  /* 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 },
 
360
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
 
361
                       (const char *[]){plymouth_path, "ask-for-password", prompt_arg, NULL},
372
362
                       true, false);
373
363
  free(prompt_arg);
 
364
  if(not bret){
 
365
    if(interrupted_by_signal){
 
366
      kill_and_wait(plymouth_command_pid);
 
367
    } else {
 
368
      exit(EXIT_FAILURE);
 
369
    }
 
370
  }
 
371
  
374
372
  if(bret){
375
373
    exit(EXIT_SUCCESS);
376
374
  }
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
375
  
383
 
  const char **plymouthd_argv;
 
376
  const char **plymouthd_argv = NULL;
384
377
  pid_t pid = get_pid();
385
378
  if(pid == 0){
386
379
    error(0, 0, "plymouthd pid not found");
 
380
  } else {
 
381
    plymouthd_argv = getargv(pid);
 
382
  }
 
383
  if(plymouthd_argv == NULL){
387
384
    plymouthd_argv = plymouthd_default_argv;
388
 
  } else {
389
 
    plymouthd_argv = getargv(pid);
390
385
  }
391
386
  
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);
 
387
  bret = exec_and_wait(NULL, plymouth_path,
 
388
                       (const char *[]){plymouth_path, "quit", NULL}, false, false);
 
389
  if(not bret){
 
390
    exit(EXIT_FAILURE);
 
391
  }
 
392
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv, false, true);
 
393
  if(not bret){
 
394
    exit(EXIT_FAILURE);
 
395
  }
 
396
  exec_and_wait(NULL, plymouth_path,
 
397
                (const char *[]){ plymouth_path, "show-splash", NULL }, false, false);
406
398
  exit(EXIT_FAILURE);
407
399
}