/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 Hogeborn
  • Date: 2017-02-23 20:20:34 UTC
  • mfrom: (899 trunk)
  • mto: This revision was merged to the branch mainline in revision 900.
  • Revision ID: teddy@recompile.se-20170223202034-0820bcq0k7d1j9kf
Merge from trunk

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