-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.c
402 lines (330 loc) · 8.13 KB
/
signals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2003,2006,2010,2013,2015 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#define _XOPEN_SOURCE 500 /* for setitimer(2) and siginterrupt(3) */
#define NEED_OS_TIMER
#define NEED_OS_FORK
#include "os.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
#ifdef __DJGPP__
#include <dos.h>
#endif
#ifdef __CYGWIN__
#include <windows.h>
#endif
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "tty.h"
#include "options.h"
#include "config.h"
#include "bench.h"
#include "john.h"
volatile int event_pending = 0;
volatile int event_abort = 0, event_save = 0, event_status = 0;
volatile int event_ticksafety = 0;
static int timer_save_interval, timer_save_value;
static clock_t timer_ticksafety_interval, timer_ticksafety_value;
#if !OS_TIMER
#include <time.h>
#include <sys/times.h>
static clock_t timer_emu_interval = 0;
static unsigned int timer_emu_count = 0, timer_emu_max = 0;
void sig_timer_emu_init(clock_t interval)
{
timer_emu_interval = interval;
timer_emu_count = 0; timer_emu_max = 0;
}
void sig_timer_emu_tick(void)
{
static clock_t last = 0;
clock_t current;
struct tms buf;
if (++timer_emu_count < timer_emu_max) return;
current = times(&buf);
if (!last) {
last = current;
return;
}
if (current - last < timer_emu_interval && current >= last) {
timer_emu_max += timer_emu_max + 1;
return;
}
last = current;
timer_emu_count = 0;
timer_emu_max >>= 1;
raise(SIGALRM);
}
#endif
#if OS_FORK
static void signal_children(int signum)
{
int i;
for (i = 0; i < john_child_count; i++)
if (john_child_pids[i])
kill(john_child_pids[i], signum);
}
#endif
static void sig_install_update(void);
static void sig_handle_update(int signum)
{
event_save = event_pending = 1;
#ifndef SA_RESTART
sig_install_update();
#endif
}
static void sig_install_update(void)
{
#ifdef SA_RESTART
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_handle_update;
sa.sa_flags = SA_RESTART;
sigaction(SIGHUP, &sa, NULL);
#else
signal(SIGHUP, sig_handle_update);
#endif
}
static void sig_remove_update(void)
{
signal(SIGHUP, SIG_IGN);
}
void check_abort(int be_async_signal_safe)
{
if (!event_abort) return;
tty_done();
if (be_async_signal_safe) {
if (john_main_process)
write_loop(2, "Session aborted\n", 16);
_exit(1);
}
if (john_main_process)
fprintf(stderr, "Session aborted\n");
error();
}
static void sig_install_abort(void);
static void sig_handle_abort(int signum)
{
int saved_errno = errno;
#if OS_FORK
if (john_main_process) {
/*
* We assume that our children are running on the same tty with us, so if we
* receive a SIGINT they probably do as well without us needing to forward the
* signal to them. If we forwarded the signal anyway, this could result in
* them receiving the signal twice for a single Ctrl-C keypress and proceeding
* with immediate abort without updating the files, which is behavior that we
* reserve for (presumably intentional) repeated Ctrl-C keypress.
*
* We forward the signal as SIGINT even though ours was different (typically a
* SIGTERM) in order not to trigger a repeated same signal for children if the
* user does e.g. "killall john", which would send SIGTERM directly to children
* and also have us forward a signal.
*/
if (signum != SIGINT)
signal_children(SIGINT);
} else {
static int prev_signum;
/*
* If it's not the same signal twice in a row, don't proceed with immediate
* abort since these two signals could have been triggered by the same killall
* (perhaps a SIGTERM from killall directly and a SIGINT as forwarded by our
* parent). event_abort would be set back to 1 just below the check_abort()
* call. We only reset it to 0 temporarily to skip the immediate abort here.
*/
if (prev_signum && signum != prev_signum)
event_abort = 0;
prev_signum = signum;
}
#endif
check_abort(1);
event_abort = event_pending = 1;
write_loop(2, "Wait...\r", 8);
sig_install_abort();
errno = saved_errno;
}
#ifdef __CYGWIN__
static CALLBACK BOOL sig_handle_abort_ctrl(DWORD ctrltype)
{
sig_handle_abort(SIGINT);
return TRUE;
}
#endif
static void sig_install_abort(void)
{
#ifdef __DJGPP__
setcbrk(1);
#endif
#ifdef __CYGWIN__
/*
* "If the HandlerRoutine parameter is NULL, [...] a FALSE value restores
* normal processing of CTRL+C input. This attribute of ignoring or processing
* CTRL+C is inherited by child processes." So restore normal processing here
* in case our parent (such as Johnny the GUI) had disabled it.
*/
SetConsoleCtrlHandler(NULL, FALSE);
SetConsoleCtrlHandler(sig_handle_abort_ctrl, TRUE);
#endif
signal(SIGINT, sig_handle_abort);
signal(SIGTERM, sig_handle_abort);
#ifdef SIGXCPU
signal(SIGXCPU, sig_handle_abort);
#endif
#ifdef SIGXFSZ
signal(SIGXFSZ, sig_handle_abort);
#endif
}
static void sig_remove_abort(void)
{
#ifdef __CYGWIN__
SetConsoleCtrlHandler(sig_handle_abort_ctrl, FALSE);
#endif
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#ifdef SIGXCPU
signal(SIGXCPU, SIG_DFL);
#endif
#ifdef SIGXFSZ
signal(SIGXFSZ, SIG_DFL);
#endif
}
static void sig_install_timer(void);
static void sig_handle_timer(int signum)
{
int saved_errno = errno;
if (!--timer_save_value) {
timer_save_value = timer_save_interval;
event_save = event_pending = 1;
}
if (!--timer_ticksafety_value) {
timer_ticksafety_value = timer_ticksafety_interval;
event_ticksafety = event_pending = 1;
}
if (john_main_process) {
int c;
#if OS_FORK
int new_abort = 0, new_status = 0;
#endif
while ((c = tty_getchar()) >= 0) {
if (c == 3 || c == 'q') {
#if OS_FORK
new_abort = 1;
#endif
sig_handle_abort(0);
} else {
#if OS_FORK
new_status = 1;
#endif
event_status = event_pending = 1;
}
}
#if OS_FORK
if (new_abort || new_status)
signal_children(new_abort ? SIGTERM : SIGUSR2);
#endif
}
#if !OS_TIMER
signal(SIGALRM, sig_handle_timer);
#elif !defined(SA_RESTART) && !defined(__DJGPP__)
sig_install_timer();
#endif
errno = saved_errno;
}
#if OS_TIMER
static void sig_init_timer(void)
{
struct itimerval it;
it.it_value.tv_sec = TIMER_INTERVAL;
it.it_value.tv_usec = 0;
#if defined(SA_RESTART) || defined(__DJGPP__)
it.it_interval = it.it_value;
#else
memset(&it.it_interval, 0, sizeof(it.it_interval));
#endif
if (setitimer(ITIMER_REAL, &it, NULL))
pexit("setitimer");
}
#endif
static void sig_install_timer(void)
{
#if !OS_TIMER
signal(SIGALRM, sig_handle_timer);
sig_timer_emu_init(TIMER_INTERVAL * clk_tck);
#else
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_handle_timer;
#ifdef SA_RESTART
sa.sa_flags = SA_RESTART;
#endif
sigaction(SIGALRM, &sa, NULL);
#if !defined(SA_RESTART) && !defined(__DJGPP__)
siginterrupt(SIGALRM, 0);
#endif
sig_init_timer();
#endif
}
static void sig_remove_timer(void)
{
#if OS_TIMER
struct itimerval it;
memset(&it, 0, sizeof(it));
if (setitimer(ITIMER_REAL, &it, NULL)) perror("setitimer");
#endif
signal(SIGALRM, SIG_DFL);
}
#if OS_FORK
static void sig_handle_status(int signum)
{
event_status = event_pending = 1;
signal(SIGUSR2, sig_handle_status);
}
#endif
static void sig_done(void);
void sig_init(void)
{
clk_tck_init();
timer_save_interval = cfg_get_int(SECTION_OPTIONS, NULL, "Save");
if (timer_save_interval < 0)
timer_save_interval = TIMER_SAVE_DELAY;
else
if ((timer_save_interval /= TIMER_INTERVAL) <= 0)
timer_save_interval = 1;
timer_save_value = timer_save_interval;
timer_ticksafety_interval = (clock_t)1 << (sizeof(clock_t) * 8 - 4);
timer_ticksafety_interval /= clk_tck;
if ((timer_ticksafety_interval /= TIMER_INTERVAL) <= 0)
timer_ticksafety_interval = 1;
timer_ticksafety_value = timer_ticksafety_interval;
atexit(sig_done);
sig_install_update();
sig_install_abort();
sig_install_timer();
#if OS_FORK
signal(SIGUSR2, sig_handle_status);
#endif
}
void sig_init_child(void)
{
#if OS_TIMER
sig_init_timer();
#endif
}
static void sig_done(void)
{
sig_remove_update();
sig_remove_abort();
sig_remove_timer();
}