/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: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Plymouth - Read a password from Plymouth and output it
4
4
 * 
5
 
 * Copyright © 2010-2014 Teddy Hogeborn
6
 
 * Copyright © 2010-2014 Björn Påhlsson
 
5
 * Copyright © 2010-2011 Teddy Hogeborn
 
6
 * Copyright © 2010-2011 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
54
54
#include <stdarg.h>             /* va_list, va_start(), ... */
55
55
 
56
56
sig_atomic_t interrupted_by_signal = 0;
57
 
 
58
 
/* Used by Ubuntu 11.04 (Natty Narwahl) */
59
 
const char plymouth_old_pid[] = "/dev/.initramfs/plymouth.pid";
60
 
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
61
 
const char plymouth_pid[] = "/run/initramfs/plymouth.pid";
62
 
 
 
57
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
63
58
const char plymouth_path[] = "/bin/plymouth";
64
59
const char plymouthd_path[] = "/sbin/plymouthd";
65
60
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
66
61
                                        "--mode=boot",
67
62
                                        "--attach-to-session",
 
63
                                        "--pid-file="
 
64
                                        "/dev/.initramfs/"
 
65
                                        "plymouth.pid",
68
66
                                        NULL };
69
67
 
70
68
static void termination_handler(__attribute__((unused))int signum){
75
73
}
76
74
 
77
75
/* Function to use when printing errors */
78
 
__attribute__((format (gnu_printf, 3, 4)))
79
76
void error_plus(int status, int errnum, const char *formatstring,
80
77
                ...){
81
78
  va_list ap;
84
81
  
85
82
  va_start(ap, formatstring);
86
83
  ret = vasprintf(&text, formatstring, ap);
87
 
  if(ret == -1){
 
84
  if (ret == -1){
88
85
    fprintf(stderr, "Mandos plugin %s: ",
89
86
            program_invocation_short_name);
90
87
    vfprintf(stderr, formatstring, ap);
154
151
  return true;
155
152
}
156
153
 
157
 
__attribute__((nonnull (2, 3)))
158
154
bool exec_and_wait(pid_t *pid_return, const char *path,
159
 
                   const char * const *argv, bool interruptable,
 
155
                   const char **argv, bool interruptable,
160
156
                   bool daemonize){
161
157
  int status;
162
158
  int ret;
179
175
    int i = 0;
180
176
    for (; argv[i]!=NULL; i++){
181
177
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
182
 
      if(tmp == NULL){
 
178
      if (tmp == NULL){
183
179
        error_plus(0, errno, "realloc");
184
180
        free(new_argv);
185
181
        _exit(EX_OSERR);
214
210
  return false;
215
211
}
216
212
 
217
 
__attribute__((nonnull))
218
213
int is_plymouth(const struct dirent *proc_entry){
219
214
  int ret;
220
215
  {
221
 
    uintmax_t proc_id;
 
216
    uintmax_t maxvalue;
222
217
    char *tmp;
223
218
    errno = 0;
224
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
219
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
225
220
 
226
221
    if(errno != 0 or *tmp != '\0'
227
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
222
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
228
223
      return 0;
229
224
    }
230
225
  }
265
260
 
266
261
pid_t get_pid(void){
267
262
  int ret;
268
 
  uintmax_t proc_id = 0;
269
263
  FILE *pidfile = fopen(plymouth_pid, "r");
270
 
  /* Try the new pid file location */
 
264
  uintmax_t maxvalue = 0;
271
265
  if(pidfile != NULL){
272
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
266
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
273
267
    if(ret != 1){
274
 
      proc_id = 0;
 
268
      maxvalue = 0;
275
269
    }
276
270
    fclose(pidfile);
277
271
  }
278
 
  /* Try the old pid file location */
279
 
  if(proc_id == 0){
280
 
    pidfile = fopen(plymouth_pid, "r");
281
 
    if(pidfile != NULL){
282
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
283
 
      if(ret != 1){
284
 
        proc_id = 0;
285
 
      }
286
 
      fclose(pidfile);
287
 
    }
288
 
  }
289
 
  /* Look for a plymouth process */
290
 
  if(proc_id == 0){
 
272
  if(maxvalue == 0){
291
273
    struct dirent **direntries = NULL;
292
274
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
293
 
    if(ret == -1){
 
275
    if (ret == -1){
294
276
      error_plus(0, errno, "scandir");
295
277
    }
296
 
    if(ret > 0){
297
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
298
 
      if(ret < 0){
 
278
    if (ret > 0){
 
279
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
280
      if (ret < 0){
299
281
        error_plus(0, errno, "sscanf");
300
282
      }
301
283
    }
304
286
    free(direntries);
305
287
  }
306
288
  pid_t pid;
307
 
  pid = (pid_t)proc_id;
308
 
  if((uintmax_t)pid == proc_id){
 
289
  pid = (pid_t)maxvalue;
 
290
  if((uintmax_t)pid == maxvalue){
309
291
    return pid;
310
292
  }
311
293
  
312
294
  return 0;
313
295
}
314
296
 
315
 
const char * const * getargv(pid_t pid){
 
297
const char **getargv(pid_t pid){
316
298
  int cl_fd;
317
299
  char *cmdline_filename;
318
300
  ssize_t sret;
379
361
    return NULL;
380
362
  }
381
363
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
382
 
  return (const char * const *)argv;
 
364
  return (const char **)argv;
383
365
}
384
366
 
385
367
int main(__attribute__((unused))int argc,
460
442
  }
461
443
  kill_and_wait(plymouth_command_pid);
462
444
  
463
 
  const char * const *plymouthd_argv;
 
445
  const char **plymouthd_argv;
464
446
  pid_t pid = get_pid();
465
447
  if(pid == 0){
466
448
    error_plus(0, 0, "plymouthd pid not found");