/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-2018 Teddy Hogeborn
6
 
 * Copyright © 2010-2018 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2010-2011 Teddy Hogeborn
 
6
 * Copyright © 2010-2011 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
55
54
#include <stdarg.h>             /* va_list, va_start(), ... */
56
55
 
57
56
sig_atomic_t interrupted_by_signal = 0;
58
 
 
59
 
/* Used by Ubuntu 11.04 (Natty Narwahl) */
60
 
const char plymouth_old_old_pid[] = "/dev/.initramfs/plymouth.pid";
61
 
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
62
 
const char plymouth_old_pid[] = "/run/initramfs/plymouth.pid";
63
 
/* Used by Debian 9 (stretch) */
64
 
const char plymouth_pid[] = "/run/plymouth/pid";
65
 
 
 
57
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
66
58
const char plymouth_path[] = "/bin/plymouth";
67
59
const char plymouthd_path[] = "/sbin/plymouthd";
68
60
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
69
61
                                        "--mode=boot",
70
62
                                        "--attach-to-session",
 
63
                                        "--pid-file="
 
64
                                        "/dev/.initramfs/"
 
65
                                        "plymouth.pid",
71
66
                                        NULL };
72
67
 
73
68
static void termination_handler(__attribute__((unused))int signum){
78
73
}
79
74
 
80
75
/* Function to use when printing errors */
81
 
__attribute__((format (gnu_printf, 3, 4)))
82
76
void error_plus(int status, int errnum, const char *formatstring,
83
77
                ...){
84
78
  va_list ap;
87
81
  
88
82
  va_start(ap, formatstring);
89
83
  ret = vasprintf(&text, formatstring, ap);
90
 
  if(ret == -1){
 
84
  if (ret == -1){
91
85
    fprintf(stderr, "Mandos plugin %s: ",
92
86
            program_invocation_short_name);
93
87
    vfprintf(stderr, formatstring, ap);
157
151
  return true;
158
152
}
159
153
 
160
 
__attribute__((nonnull (2, 3)))
161
154
bool exec_and_wait(pid_t *pid_return, const char *path,
162
 
                   const char * const *argv, bool interruptable,
 
155
                   const char **argv, bool interruptable,
163
156
                   bool daemonize){
164
157
  int status;
165
158
  int ret;
177
170
      }
178
171
    }
179
172
    
180
 
    char **new_argv = malloc(sizeof(const char *));
181
 
    if(new_argv == NULL){
182
 
      error_plus(0, errno, "malloc");
183
 
      _exit(EX_OSERR);
184
 
    }
 
173
    char **new_argv = NULL;
185
174
    char **tmp;
186
175
    int i = 0;
187
 
    for (; argv[i] != NULL; i++){
188
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 2));
189
 
      if(tmp == NULL){
 
176
    for (; argv[i]!=NULL; i++){
 
177
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
178
      if (tmp == NULL){
190
179
        error_plus(0, errno, "realloc");
191
180
        free(new_argv);
192
181
        _exit(EX_OSERR);
221
210
  return false;
222
211
}
223
212
 
224
 
__attribute__((nonnull))
225
213
int is_plymouth(const struct dirent *proc_entry){
226
214
  int ret;
227
215
  {
228
 
    uintmax_t proc_id;
 
216
    uintmax_t maxvalue;
229
217
    char *tmp;
230
218
    errno = 0;
231
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
219
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
232
220
 
233
221
    if(errno != 0 or *tmp != '\0'
234
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
222
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
235
223
      return 0;
236
224
    }
237
225
  }
272
260
 
273
261
pid_t get_pid(void){
274
262
  int ret;
275
 
  uintmax_t proc_id = 0;
276
263
  FILE *pidfile = fopen(plymouth_pid, "r");
277
 
  /* Try the new pid file location */
 
264
  uintmax_t maxvalue = 0;
278
265
  if(pidfile != NULL){
279
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
266
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
280
267
    if(ret != 1){
281
 
      proc_id = 0;
 
268
      maxvalue = 0;
282
269
    }
283
270
    fclose(pidfile);
284
271
  }
285
 
  /* Try the old pid file location */
286
 
  if(proc_id == 0){
287
 
    pidfile = fopen(plymouth_old_pid, "r");
288
 
    if(pidfile != NULL){
289
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
290
 
      if(ret != 1){
291
 
        proc_id = 0;
292
 
      }
293
 
      fclose(pidfile);
294
 
    }
295
 
  }
296
 
  /* Try the old old pid file location */
297
 
  if(proc_id == 0){
298
 
    pidfile = fopen(plymouth_old_old_pid, "r");
299
 
    if(pidfile != NULL){
300
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
301
 
      if(ret != 1){
302
 
        proc_id = 0;
303
 
      }
304
 
      fclose(pidfile);
305
 
    }
306
 
  }
307
 
  /* Look for a plymouth process */
308
 
  if(proc_id == 0){
 
272
  if(maxvalue == 0){
309
273
    struct dirent **direntries = NULL;
310
274
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
311
 
    if(ret == -1){
 
275
    if (ret == -1){
312
276
      error_plus(0, errno, "scandir");
313
277
    }
314
 
    if(ret > 0){
315
 
      for(int i = ret-1; i >= 0; i--){
316
 
        if(proc_id == 0){
317
 
          ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
318
 
          if(ret < 0){
319
 
            error_plus(0, errno, "sscanf");
320
 
          }
321
 
        }
322
 
        free(direntries[i]);
 
278
    if (ret > 0){
 
279
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
280
      if (ret < 0){
 
281
        error_plus(0, errno, "sscanf");
323
282
      }
324
283
    }
325
284
    /* scandir might preallocate for this variable (man page unclear).
327
286
    free(direntries);
328
287
  }
329
288
  pid_t pid;
330
 
  pid = (pid_t)proc_id;
331
 
  if((uintmax_t)pid == proc_id){
 
289
  pid = (pid_t)maxvalue;
 
290
  if((uintmax_t)pid == maxvalue){
332
291
    return pid;
333
292
  }
334
293
  
335
294
  return 0;
336
295
}
337
296
 
338
 
char **getargv(pid_t pid){
 
297
const char **getargv(pid_t pid){
339
298
  int cl_fd;
340
299
  char *cmdline_filename;
341
300
  ssize_t sret;
402
361
    return NULL;
403
362
  }
404
363
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
405
 
  return argv;
 
364
  return (const char **)argv;
406
365
}
407
366
 
408
367
int main(__attribute__((unused))int argc,
483
442
  }
484
443
  kill_and_wait(plymouth_command_pid);
485
444
  
486
 
  char **plymouthd_argv = NULL;
 
445
  const char **plymouthd_argv;
487
446
  pid_t pid = get_pid();
488
447
  if(pid == 0){
489
448
    error_plus(0, 0, "plymouthd pid not found");
 
449
    plymouthd_argv = plymouthd_default_argv;
490
450
  } else {
491
451
    plymouthd_argv = getargv(pid);
492
452
  }
495
455
                       { plymouth_path, "quit", NULL },
496
456
                       false, false);
497
457
  if(not bret){
498
 
    if(plymouthd_argv != NULL){
499
 
      free(*plymouthd_argv);
500
 
      free(plymouthd_argv);
501
 
    }
502
458
    exit(EXIT_FAILURE);
503
459
  }
504
 
  bret = exec_and_wait(NULL, plymouthd_path,
505
 
                       (plymouthd_argv != NULL)
506
 
                       ? (const char * const *)plymouthd_argv
507
 
                       : plymouthd_default_argv,
 
460
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
508
461
                       false, true);
509
 
  if(plymouthd_argv != NULL){
510
 
    free(*plymouthd_argv);
511
 
    free(plymouthd_argv);
512
 
  }
513
462
  if(not bret){
514
463
    exit(EXIT_FAILURE);
515
464
  }