/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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
75
75
}
76
76
 
77
77
/* Function to use when printing errors */
78
 
__attribute__((format (gnu_printf, 3, 4)))
79
78
void error_plus(int status, int errnum, const char *formatstring,
80
79
                ...){
81
80
  va_list ap;
84
83
  
85
84
  va_start(ap, formatstring);
86
85
  ret = vasprintf(&text, formatstring, ap);
87
 
  if(ret == -1){
 
86
  if (ret == -1){
88
87
    fprintf(stderr, "Mandos plugin %s: ",
89
88
            program_invocation_short_name);
90
89
    vfprintf(stderr, formatstring, ap);
154
153
  return true;
155
154
}
156
155
 
157
 
__attribute__((nonnull (2, 3)))
158
156
bool exec_and_wait(pid_t *pid_return, const char *path,
159
 
                   const char * const *argv, bool interruptable,
 
157
                   const char **argv, bool interruptable,
160
158
                   bool daemonize){
161
159
  int status;
162
160
  int ret;
179
177
    int i = 0;
180
178
    for (; argv[i]!=NULL; i++){
181
179
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
182
 
      if(tmp == NULL){
 
180
      if (tmp == NULL){
183
181
        error_plus(0, errno, "realloc");
184
182
        free(new_argv);
185
183
        _exit(EX_OSERR);
214
212
  return false;
215
213
}
216
214
 
217
 
__attribute__((nonnull))
218
215
int is_plymouth(const struct dirent *proc_entry){
219
216
  int ret;
220
217
  {
221
 
    uintmax_t proc_id;
 
218
    uintmax_t maxvalue;
222
219
    char *tmp;
223
220
    errno = 0;
224
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
221
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
225
222
 
226
223
    if(errno != 0 or *tmp != '\0'
227
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
224
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
228
225
      return 0;
229
226
    }
230
227
  }
265
262
 
266
263
pid_t get_pid(void){
267
264
  int ret;
268
 
  uintmax_t proc_id = 0;
 
265
  uintmax_t maxvalue = 0;
269
266
  FILE *pidfile = fopen(plymouth_pid, "r");
270
267
  /* Try the new pid file location */
271
268
  if(pidfile != NULL){
272
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
269
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
273
270
    if(ret != 1){
274
 
      proc_id = 0;
 
271
      maxvalue = 0;
275
272
    }
276
273
    fclose(pidfile);
277
274
  }
278
275
  /* Try the old pid file location */
279
 
  if(proc_id == 0){
 
276
  if(maxvalue == 0){
280
277
    pidfile = fopen(plymouth_pid, "r");
281
278
    if(pidfile != NULL){
282
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
279
      ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
283
280
      if(ret != 1){
284
 
        proc_id = 0;
 
281
        maxvalue = 0;
285
282
      }
286
283
      fclose(pidfile);
287
284
    }
288
285
  }
289
286
  /* Look for a plymouth process */
290
 
  if(proc_id == 0){
 
287
  if(maxvalue == 0){
291
288
    struct dirent **direntries = NULL;
292
289
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
293
 
    if(ret == -1){
 
290
    if (ret == -1){
294
291
      error_plus(0, errno, "scandir");
295
292
    }
296
 
    if(ret > 0){
297
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
298
 
      if(ret < 0){
 
293
    if (ret > 0){
 
294
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
295
      if (ret < 0){
299
296
        error_plus(0, errno, "sscanf");
300
297
      }
301
298
    }
304
301
    free(direntries);
305
302
  }
306
303
  pid_t pid;
307
 
  pid = (pid_t)proc_id;
308
 
  if((uintmax_t)pid == proc_id){
 
304
  pid = (pid_t)maxvalue;
 
305
  if((uintmax_t)pid == maxvalue){
309
306
    return pid;
310
307
  }
311
308
  
312
309
  return 0;
313
310
}
314
311
 
315
 
const char * const * getargv(pid_t pid){
 
312
const char **getargv(pid_t pid){
316
313
  int cl_fd;
317
314
  char *cmdline_filename;
318
315
  ssize_t sret;
379
376
    return NULL;
380
377
  }
381
378
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
382
 
  return (const char * const *)argv;
 
379
  return (const char **)argv;
383
380
}
384
381
 
385
382
int main(__attribute__((unused))int argc,
460
457
  }
461
458
  kill_and_wait(plymouth_command_pid);
462
459
  
463
 
  const char * const *plymouthd_argv;
 
460
  const char **plymouthd_argv;
464
461
  pid_t pid = get_pid();
465
462
  if(pid == 0){
466
463
    error_plus(0, 0, "plymouthd pid not found");