/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-2016 Teddy Hogeborn
6
 
 * Copyright © 2010-2016 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;
174
172
      }
175
173
    }
176
174
    
177
 
    char **new_argv = malloc(sizeof(const char *));
178
 
    if(new_argv == NULL){
179
 
      error_plus(0, errno, "malloc");
180
 
      _exit(EX_OSERR);
181
 
    }
 
175
    char **new_argv = NULL;
182
176
    char **tmp;
183
177
    int i = 0;
184
178
    for (; argv[i]!=NULL; i++){
185
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 2));
186
 
      if(tmp == NULL){
 
179
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
180
      if (tmp == NULL){
187
181
        error_plus(0, errno, "realloc");
188
182
        free(new_argv);
189
183
        _exit(EX_OSERR);
218
212
  return false;
219
213
}
220
214
 
221
 
__attribute__((nonnull))
222
215
int is_plymouth(const struct dirent *proc_entry){
223
216
  int ret;
224
217
  {
225
 
    uintmax_t proc_id;
 
218
    uintmax_t maxvalue;
226
219
    char *tmp;
227
220
    errno = 0;
228
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
221
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
229
222
 
230
223
    if(errno != 0 or *tmp != '\0'
231
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
224
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
232
225
      return 0;
233
226
    }
234
227
  }
269
262
 
270
263
pid_t get_pid(void){
271
264
  int ret;
272
 
  uintmax_t proc_id = 0;
 
265
  uintmax_t maxvalue = 0;
273
266
  FILE *pidfile = fopen(plymouth_pid, "r");
274
267
  /* Try the new pid file location */
275
268
  if(pidfile != NULL){
276
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
269
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
277
270
    if(ret != 1){
278
 
      proc_id = 0;
 
271
      maxvalue = 0;
279
272
    }
280
273
    fclose(pidfile);
281
274
  }
282
275
  /* Try the old pid file location */
283
 
  if(proc_id == 0){
 
276
  if(maxvalue == 0){
284
277
    pidfile = fopen(plymouth_pid, "r");
285
278
    if(pidfile != NULL){
286
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
279
      ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
287
280
      if(ret != 1){
288
 
        proc_id = 0;
 
281
        maxvalue = 0;
289
282
      }
290
283
      fclose(pidfile);
291
284
    }
292
285
  }
293
286
  /* Look for a plymouth process */
294
 
  if(proc_id == 0){
 
287
  if(maxvalue == 0){
295
288
    struct dirent **direntries = NULL;
296
289
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
297
 
    if(ret == -1){
 
290
    if (ret == -1){
298
291
      error_plus(0, errno, "scandir");
299
292
    }
300
 
    if(ret > 0){
301
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
302
 
      if(ret < 0){
 
293
    if (ret > 0){
 
294
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
295
      if (ret < 0){
303
296
        error_plus(0, errno, "sscanf");
304
297
      }
305
298
    }
308
301
    free(direntries);
309
302
  }
310
303
  pid_t pid;
311
 
  pid = (pid_t)proc_id;
312
 
  if((uintmax_t)pid == proc_id){
 
304
  pid = (pid_t)maxvalue;
 
305
  if((uintmax_t)pid == maxvalue){
313
306
    return pid;
314
307
  }
315
308
  
316
309
  return 0;
317
310
}
318
311
 
319
 
const char * const * getargv(pid_t pid){
 
312
const char **getargv(pid_t pid){
320
313
  int cl_fd;
321
314
  char *cmdline_filename;
322
315
  ssize_t sret;
383
376
    return NULL;
384
377
  }
385
378
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
386
 
  return (const char * const *)argv;
 
379
  return (const char **)argv;
387
380
}
388
381
 
389
382
int main(__attribute__((unused))int argc,
464
457
  }
465
458
  kill_and_wait(plymouth_command_pid);
466
459
  
467
 
  const char * const *plymouthd_argv;
 
460
  const char **plymouthd_argv;
468
461
  pid_t pid = get_pid();
469
462
  if(pid == 0){
470
463
    error_plus(0, 0, "plymouthd pid not found");