/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/password-prompt.c

  • Committer: Teddy Hogeborn
  • Date: 2009-09-05 01:05:25 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905010525-7qeq05ztwc2ddhuv
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
                                    Bug fix: remove signal handler
                                    before re-raising signal.

* plugins.d/password-prompt.c (main): Check return values from
                                      "sigaddset()".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
33
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
34
34
                                   sigaction, sigemptyset(),
35
35
                                   sigaction(), sigaddset(), SIGINT,
36
 
                                   SIGQUIT, SIGHUP, SIGTERM */
 
36
                                   SIGQUIT, SIGHUP, SIGTERM,
 
37
                                   raise() */
37
38
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
39
#include <sys/types.h>          /* ssize_t */
39
40
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
51
52
                                   ARGP_KEY_ARG, ARGP_KEY_END,
52
53
                                   ARGP_ERR_UNKNOWN */
53
54
 
54
 
volatile bool quit_now = false;
 
55
volatile sig_atomic_t quit_now = 0;
 
56
int signal_received;
55
57
bool debug = false;
56
58
const char *argp_program_version = "password-prompt " VERSION;
57
59
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
58
60
 
59
 
static void termination_handler(__attribute__((unused))int signum){
60
 
  quit_now = true;
 
61
static void termination_handler(int signum){
 
62
  if(quit_now){
 
63
    return;
 
64
  }
 
65
  quit_now = 1;
 
66
  signal_received = signum;
61
67
}
62
68
 
63
69
int main(int argc, char **argv){
80
86
      { .name = NULL }
81
87
    };
82
88
    
83
 
    error_t parse_opt (int key, char *arg, struct argp_state *state) {
84
 
      switch (key) {
 
89
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
90
      switch (key){
85
91
      case 'p':
86
92
        prefix = arg;
87
93
        break;
123
129
  }
124
130
  
125
131
  sigemptyset(&new_action.sa_mask);
126
 
  sigaddset(&new_action.sa_mask, SIGINT);
127
 
  sigaddset(&new_action.sa_mask, SIGHUP);
128
 
  sigaddset(&new_action.sa_mask, SIGTERM);
 
132
  ret = sigaddset(&new_action.sa_mask, SIGINT);
 
133
  if(ret == -1){
 
134
    perror("sigaddset");
 
135
    return EXIT_FAILURE;
 
136
  }
 
137
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
138
  if(ret == -1){
 
139
    perror("sigaddset");
 
140
    return EXIT_FAILURE;
 
141
  }
 
142
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
143
  if(ret == -1){
 
144
    perror("sigaddset");
 
145
    return EXIT_FAILURE;
 
146
  }
 
147
  /* Need to check if the handler is SIG_IGN before handling:
 
148
     | [[info:libc:Initial Signal Actions]] |
 
149
     | [[info:libc:Basic Signal Handling]]  |
 
150
  */
129
151
  ret = sigaction(SIGINT, NULL, &old_action);
130
152
  if(ret == -1){
131
153
    perror("sigaction");
243
265
       read from stdin */
244
266
    fputc('\n', stderr);
245
267
    if(debug and not quit_now){
246
 
      /* If quit_now is true, we were interrupted by a signal, and
 
268
      /* If quit_now is nonzero, we were interrupted by a signal, and
247
269
         will print that later, so no need to show this too. */
248
270
      fprintf(stderr, "getline() returned 0, retrying.\n");
249
271
    }
258
280
    perror("tcsetattr+echo");
259
281
  }
260
282
  
 
283
  if(quit_now){
 
284
    sigemptyset(&old_action.sa_mask);
 
285
    old_action.sa_handler = SIG_DFL;
 
286
    ret = sigaction(signal_received, &old_action, NULL);
 
287
    if(ret == -1){
 
288
      perror("sigaction");
 
289
    }
 
290
    raise(signal_received);
 
291
  }
 
292
  
261
293
  if(debug){
262
294
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
263
295
            status);