/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-06-18 22:51:12 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110618225112-rnt2m4ek32758f80
using scandir instead of readdir

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <stddef.h>             /* NULL */
37
37
#include <string.h>             /* strchr(), memcmp() */
38
38
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
 
39
                                   fscanf() */
41
40
#include <unistd.h>             /* close(), readlink(), read(),
42
41
                                   fork(), setsid(), chdir(), dup2(),
43
42
                                   STDERR_FILENO, execv(), access() */
51
50
#include <error.h>              /* error() */
52
51
#include <errno.h>              /* TEMP_FAILURE_RETRY */
53
52
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
53
 
56
54
sig_atomic_t interrupted_by_signal = 0;
57
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
72
70
  interrupted_by_signal = 1;
73
71
}
74
72
 
75
 
/* Function to use when printing errors */
76
 
void error_plus(int status, int errnum, const char *formatstring,
77
 
                ...){
78
 
  va_list ap;
79
 
  char *text;
80
 
  int ret;
81
 
  
82
 
  va_start(ap, formatstring);
83
 
  ret = vasprintf(&text, formatstring, ap);
84
 
  if (ret == -1){
85
 
    fprintf(stderr, "Mandos plugin %s: ",
86
 
            program_invocation_short_name);
87
 
    vfprintf(stderr, formatstring, ap);
88
 
    fprintf(stderr, ": ");
89
 
    fprintf(stderr, "%s\n", strerror(errnum));
90
 
    error(status, errno, "vasprintf while printing error");
91
 
    return;
92
 
  }
93
 
  fprintf(stderr, "Mandos plugin ");
94
 
  error(status, errnum, "%s", text);
95
 
  free(text);
96
 
}
97
 
 
98
73
/* Create prompt string */
99
74
char *makeprompt(void){
100
75
  int ret = 0;
134
109
bool become_a_daemon(void){
135
110
  int ret = setuid(geteuid());
136
111
  if(ret == -1){
137
 
    error_plus(0, errno, "setuid");
 
112
    error(0, errno, "setuid");
138
113
  }
139
114
    
140
115
  setsid();
141
116
  ret = chdir("/");
142
117
  if(ret == -1){
143
 
    error_plus(0, errno, "chdir");
 
118
    error(0, errno, "chdir");
144
119
    return false;
145
120
  }
146
121
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
147
122
  if(ret == -1){
148
 
    error_plus(0, errno, "dup2");
 
123
    error(0, errno, "dup2");
149
124
    return false;
150
125
  }
151
126
  return true;
159
134
  pid_t pid;
160
135
  pid = fork();
161
136
  if(pid == -1){
162
 
    error_plus(0, errno, "fork");
 
137
    error(0, errno, "fork");
163
138
    return false;
164
139
  }
165
140
  if(pid == 0){
176
151
    for (; argv[i]!=NULL; i++){
177
152
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
178
153
      if (tmp == NULL){
179
 
        error_plus(0, errno, "realloc");
 
154
        error(0, errno, "realloc");
180
155
        free(new_argv);
181
156
        _exit(EX_OSERR);
182
157
      }
186
161
    new_argv[i] = NULL;
187
162
    
188
163
    execv(path, (char *const *)new_argv);
189
 
    error_plus(0, errno, "execv");
 
164
    error(0, errno, "execv");
190
165
    _exit(EXIT_FAILURE);
191
166
  }
192
167
  if(pid_return != NULL){
201
176
    return false;
202
177
  }
203
178
  if(ret == -1){
204
 
    error_plus(0, errno, "waitpid");
 
179
    error(0, errno, "waitpid");
205
180
    return false;
206
181
  }
207
182
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
227
202
  char *exe_link;
228
203
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
229
204
  if(ret == -1){
230
 
    error_plus(0, errno, "asprintf");
 
205
    error(0, errno, "asprintf");
231
206
    return 0;
232
207
  }
233
208
  
236
211
  if(ret == -1){
237
212
    free(exe_link);
238
213
    if(errno != ENOENT){
239
 
      error_plus(0, errno, "lstat");
 
214
      error(0, errno, "lstat");
240
215
    }
241
216
    return 0;
242
217
  }
270
245
    fclose(pidfile);
271
246
  }
272
247
  if(maxvalue == 0){
273
 
    struct dirent **direntries = NULL;
 
248
    struct dirent **direntries;
274
249
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
275
250
    if (ret == -1){
276
 
      error_plus(0, errno, "scandir");
 
251
      error(0, errno, "scandir");
277
252
    }
278
253
    if (ret > 0){
279
254
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
280
255
      if (ret < 0){
281
 
        error_plus(0, errno, "sscanf");
 
256
        error(0, errno, "sscanf");
282
257
      }
283
258
    }
284
 
    /* scandir might preallocate for this variable (man page unclear).
285
 
       even if ret == 0, therefore we need to free it. */
286
 
    free(direntries);
287
259
  }
288
260
  pid_t pid;
289
261
  pid = (pid_t)maxvalue;
303
275
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
304
276
                 (uintmax_t)pid);
305
277
  if(ret == -1){
306
 
    error_plus(0, errno, "asprintf");
 
278
    error(0, errno, "asprintf");
307
279
    return NULL;
308
280
  }
309
281
  
311
283
  cl_fd = open(cmdline_filename, O_RDONLY);
312
284
  free(cmdline_filename);
313
285
  if(cl_fd == -1){
314
 
    error_plus(0, errno, "open");
 
286
    error(0, errno, "open");
315
287
    return NULL;
316
288
  }
317
289
  
325
297
    if(cmdline_len + blocksize > cmdline_allocated){
326
298
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
327
299
      if(tmp == NULL){
328
 
        error_plus(0, errno, "realloc");
 
300
        error(0, errno, "realloc");
329
301
        free(cmdline);
330
302
        close(cl_fd);
331
303
        return NULL;
338
310
    sret = read(cl_fd, cmdline + cmdline_len,
339
311
                cmdline_allocated - cmdline_len);
340
312
    if(sret == -1){
341
 
      error_plus(0, errno, "read");
 
313
      error(0, errno, "read");
342
314
      free(cmdline);
343
315
      close(cl_fd);
344
316
      return NULL;
347
319
  } while(sret != 0);
348
320
  ret = close(cl_fd);
349
321
  if(ret == -1){
350
 
    error_plus(0, errno, "close");
 
322
    error(0, errno, "close");
351
323
    free(cmdline);
352
324
    return NULL;
353
325
  }
356
328
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
357
329
                       * sizeof(char *)); /* Get number of args */
358
330
  if(argv == NULL){
359
 
    error_plus(0, errno, "argv = malloc()");
 
331
    error(0, errno, "argv = malloc()");
360
332
    free(cmdline);
361
333
    return NULL;
362
334
  }
389
361
        *sig != 0; sig++){
390
362
      ret = sigaddset(&new_action.sa_mask, *sig);
391
363
      if(ret == -1){
392
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
364
        error(EX_OSERR, errno, "sigaddset");
393
365
      }
394
366
      ret = sigaction(*sig, NULL, &old_action);
395
367
      if(ret == -1){
396
 
        error_plus(EX_OSERR, errno, "sigaction");
 
368
        error(EX_OSERR, errno, "sigaction");
397
369
      }
398
370
      if(old_action.sa_handler != SIG_IGN){
399
371
        ret = sigaction(*sig, &new_action, NULL);
400
372
        if(ret == -1){
401
 
          error_plus(EX_OSERR, errno, "sigaction");
 
373
          error(EX_OSERR, errno, "sigaction");
402
374
        }
403
375
      }
404
376
    }
423
395
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
424
396
  free(prompt);
425
397
  if(ret == -1){
426
 
    error_plus(EX_OSERR, errno, "asprintf");
 
398
    error(EX_OSERR, errno, "asprintf");
427
399
  }
428
400
  
429
401
  /* plymouth ask-for-password --prompt="$prompt" */
445
417
  const char **plymouthd_argv;
446
418
  pid_t pid = get_pid();
447
419
  if(pid == 0){
448
 
    error_plus(0, 0, "plymouthd pid not found");
 
420
    error(0, 0, "plymouthd pid not found");
449
421
    plymouthd_argv = plymouthd_default_argv;
450
422
  } else {
451
423
    plymouthd_argv = getargv(pid);