1 | /* |
---|
2 | |
---|
3 | signals.c |
---|
4 | |
---|
5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
---|
6 | |
---|
7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
---|
8 | All rights reserved |
---|
9 | |
---|
10 | Created: Fri Jan 19 18:09:37 1995 ylo |
---|
11 | |
---|
12 | Manipulation of signal state. This file also contains code to set the |
---|
13 | maximum core dump size. |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * $Log: not supported by cvs2svn $ |
---|
19 | * Revision 1.9 1998/05/23 20:24:15 kivinen |
---|
20 | * Changed () -> (void). |
---|
21 | * |
---|
22 | * Revision 1.8 1998/05/04 13:37:05 kivinen |
---|
23 | * Fixed SIGPWR code so that will check if SIGPWR is same than |
---|
24 | * SIGINFO and only include it to switch clause if it is |
---|
25 | * different. |
---|
26 | * |
---|
27 | * Revision 1.7 1998/04/30 01:56:32 kivinen |
---|
28 | * Added SIGPWR handling. |
---|
29 | * |
---|
30 | * Revision 1.6 1997/04/21 01:07:28 kivinen |
---|
31 | * Added HAVE_INCOMPATIBLE_SIGINFO support. |
---|
32 | * |
---|
33 | * Revision 1.5 1997/03/26 07:16:44 kivinen |
---|
34 | * Change sig <= NSIG to sig < NSIG. |
---|
35 | * |
---|
36 | * Revision 1.4 1996/08/30 08:44:22 ylo |
---|
37 | * Added Sunos/Solaris SIGFREEZE and SIGTHAW to signals with |
---|
38 | * default processing. |
---|
39 | * |
---|
40 | * Revision 1.3 1996/07/12 07:27:18 ttsalo |
---|
41 | * ifdef:d SIGIO |
---|
42 | * |
---|
43 | * Revision 1.2 1996/04/26 00:25:48 ylo |
---|
44 | * Test for SIGURG == SIGIO (which appears to be the case on some |
---|
45 | * Linux versions). |
---|
46 | * |
---|
47 | * Revision 1.1.1.1 1996/02/18 21:38:11 ylo |
---|
48 | * Imported ssh-1.2.13. |
---|
49 | * |
---|
50 | * $EndLog$ |
---|
51 | */ |
---|
52 | |
---|
53 | #include "includes.h" |
---|
54 | #ifdef HAVE_SETRLIMIT |
---|
55 | #include <sys/resource.h> |
---|
56 | #endif /* HAVE_SETRLIMIT */ |
---|
57 | |
---|
58 | #ifndef NSIG |
---|
59 | #define NSIG 100 |
---|
60 | #endif |
---|
61 | |
---|
62 | unsigned long original_core_limit; |
---|
63 | |
---|
64 | static RETSIGTYPE signal_handler(int sig) |
---|
65 | { |
---|
66 | fprintf(stderr, "\nReceived signal %d.\n", sig); |
---|
67 | exit(255); |
---|
68 | } |
---|
69 | |
---|
70 | /* Sets signal handlers so that core dumps are prevented. This also |
---|
71 | sets the maximum core dump size to zero as an extra precaution (where |
---|
72 | supported). The old core dump size limit is saved. */ |
---|
73 | |
---|
74 | void signals_prevent_core(void) |
---|
75 | { |
---|
76 | int sig; |
---|
77 | |
---|
78 | for (sig = 1; sig < NSIG; sig++) |
---|
79 | switch (sig) |
---|
80 | { |
---|
81 | case SIGSTOP: |
---|
82 | case SIGTSTP: |
---|
83 | case SIGCONT: |
---|
84 | case SIGCHLD: |
---|
85 | case SIGTTIN: |
---|
86 | case SIGTTOU: |
---|
87 | #ifdef SIGIO |
---|
88 | case SIGIO: |
---|
89 | #endif |
---|
90 | #if defined(SIGURG) && SIGURG != SIGIO |
---|
91 | case SIGURG: |
---|
92 | #endif |
---|
93 | #ifdef SIGWINCH |
---|
94 | case SIGWINCH: |
---|
95 | #endif |
---|
96 | #if defined(SIGINFO) && !defined(HAVE_INCOMPATIBLE_SIGINFO) |
---|
97 | case SIGINFO: |
---|
98 | #endif |
---|
99 | #if defined(SIGFREEZE) |
---|
100 | case SIGFREEZE: |
---|
101 | #endif |
---|
102 | #if defined(SIGTHAW) |
---|
103 | case SIGTHAW: |
---|
104 | #endif |
---|
105 | #if defined(SIGPWR) |
---|
106 | #if !defined(SIGINFO) || (SIGINFO != SIGPWR) |
---|
107 | case SIGPWR: |
---|
108 | #endif |
---|
109 | #endif |
---|
110 | signal(sig, SIG_DFL); |
---|
111 | break; |
---|
112 | default: |
---|
113 | signal(sig, signal_handler); |
---|
114 | break; |
---|
115 | } |
---|
116 | |
---|
117 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE) |
---|
118 | { |
---|
119 | struct rlimit rl; |
---|
120 | getrlimit(RLIMIT_CORE, &rl); |
---|
121 | original_core_limit = rl.rlim_cur; |
---|
122 | rl.rlim_cur = 0; |
---|
123 | setrlimit(RLIMIT_CORE, &rl); |
---|
124 | } |
---|
125 | #endif /* HAVE_SETRLIMIT && RLIMIT_CORE */ |
---|
126 | } |
---|
127 | |
---|
128 | /* Sets all signals to their default state. Restores RLIMIT_CORE previously |
---|
129 | saved by prevent_core(). */ |
---|
130 | |
---|
131 | void signals_reset(void) |
---|
132 | { |
---|
133 | int sig; |
---|
134 | |
---|
135 | for (sig = 1; sig < NSIG; sig++) |
---|
136 | signal(sig, SIG_DFL); |
---|
137 | |
---|
138 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE) |
---|
139 | { |
---|
140 | struct rlimit rl; |
---|
141 | getrlimit(RLIMIT_CORE, &rl); |
---|
142 | rl.rlim_cur = original_core_limit; |
---|
143 | setrlimit(RLIMIT_CORE, &rl); |
---|
144 | } |
---|
145 | #endif /* HAVE_SETRLIMIT && RLIMIT_CORE */ |
---|
146 | } |
---|