1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ******************************************************************/
47 #ifdef HAVE_DIX_CONFIG_H
48 #include <dix-config.h>
67 #include "dixstruct.h"
69 #if !defined(SYSV) && !defined(WIN32)
70 #include <sys/resource.h>
74 #define ADMPATH "/usr/adm/X%smsgs"
80 int limitDataSpace
= -1;
83 int limitStackSpace
= -1;
89 static OsSigWrapperPtr OsSigWrapper
= NULL
;
92 OsRegisterSigWrapper(OsSigWrapperPtr newSigWrapper
)
94 OsSigWrapperPtr oldSigWrapper
= OsSigWrapper
;
96 OsSigWrapper
= newSigWrapper
;
103 * Catch unexpected signals and exit or continue cleanly.
105 #if !defined(WIN32) || defined(__CYGWIN__)
108 OsSigHandler(int signo
, siginfo_t
* sip
, void *unused
)
110 OsSigHandler(int signo
)
113 #ifdef RTLD_DI_SETSIGNAL
114 const char *dlerr
= dlerror();
117 LogMessageVerbSigSafe(X_ERROR
, 1, "Dynamic loader error: %s\n", dlerr
);
119 #endif /* RTLD_DI_SETSIGNAL */
121 if (OsSigWrapper
!= NULL
) {
122 if (OsSigWrapper(signo
) == 0) {
123 /* ddx handled signal and wants us to continue */
128 /* log, cleanup, and abort */
132 if (sip
->si_code
== SI_USER
) {
133 ErrorFSigSafe("Received signal %u sent by process %u, uid %u\n", signo
,
134 sip
->si_pid
, sip
->si_uid
);
142 ErrorFSigSafe("%s at address %p\n", strsignal(signo
), sip
->si_addr
);
147 FatalError("Caught signal %d (%s). Server aborting\n",
148 signo
, strsignal(signo
));
150 #endif /* !WIN32 || __CYGWIN__ */
152 #include "busfault.h"
157 static Bool been_here
= FALSE
;
158 static const char *devnull
= "/dev/null";
159 char fname
[PATH_MAX
];
162 #if !defined(WIN32) || defined(__CYGWIN__)
163 struct sigaction act
, oact
;
166 int siglist
[] = { SIGSEGV
, SIGQUIT
, SIGILL
, SIGFPE
, SIGBUS
,
175 sigemptyset(&act
.sa_mask
);
177 act
.sa_sigaction
= OsSigHandler
;
178 act
.sa_flags
= SA_SIGINFO
;
180 act
.sa_handler
= OsSigHandler
;
183 for (i
= 0; siglist
[i
] != 0; i
++) {
184 if (sigaction(siglist
[i
], &act
, &oact
)) {
185 ErrorF("failed to install signal handler for signal %d: %s\n",
186 siglist
[i
], strerror(errno
));
189 #endif /* !WIN32 || __CYGWIN__ */
194 #ifdef HAVE_BACKTRACE
196 * initialize the backtracer, since the ctor calls dlopen(), which
197 * calls malloc(), which isn't signal-safe.
202 backtrace(&array
, 1);
206 #ifdef RTLD_DI_SETSIGNAL
207 /* Tell runtime linker to send a signal we can catch instead of SIGKILL
208 * for failures to load libraries/modules at runtime so we can clean up
211 int failure_signal
= SIGQUIT
;
213 dlinfo(RTLD_SELF
, RTLD_DI_SETSIGNAL
, &failure_signal
);
216 #if !defined(XQUARTZ) /* STDIN is already /dev/null and STDOUT/STDERR is managed by console_redirect.c */
217 # if defined(__APPLE__)
218 int devnullfd
= open(devnull
, O_RDWR
, 0);
219 assert(devnullfd
> 2);
221 dup2(devnullfd
, STDIN_FILENO
);
222 dup2(devnullfd
, STDOUT_FILENO
);
224 # elif !defined(__CYGWIN__)
229 * If a write of zero bytes to stderr returns non-zero, i.e. -1,
230 * then writing to stderr failed, and we'll write somewhere else
231 * instead. (Apparently this never happens in the Real World.)
233 if (write(2, fname
, 0) == -1) {
236 if (strlen(display
) + strlen(ADMPATH
) + 1 < sizeof fname
)
237 snprintf(fname
, sizeof(fname
), ADMPATH
, display
);
239 strcpy(fname
, devnull
);
241 * uses stdio to avoid os dependencies here,
242 * a real os would use
243 * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
245 if (!(err
= fopen(fname
, "a+")))
246 err
= fopen(devnull
, "w");
247 if (err
&& (fileno(err
) != 2)) {
248 dup2(fileno(err
), 2);
251 #if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
253 static char buf
[BUFSIZ
];
255 setvbuf(stderr
, buf
, _IOLBF
, BUFSIZ
);
261 #endif /* !XQUARTZ */
263 #if !defined(WIN32) || defined(__CYGWIN__)
269 if (limitDataSpace
>= 0) {
272 if (!getrlimit(RLIMIT_DATA
, &rlim
)) {
273 if ((limitDataSpace
> 0) && (limitDataSpace
< rlim
.rlim_max
))
274 rlim
.rlim_cur
= limitDataSpace
;
276 rlim
.rlim_cur
= rlim
.rlim_max
;
277 (void) setrlimit(RLIMIT_DATA
, &rlim
);
282 if (limitStackSpace
>= 0) {
285 if (!getrlimit(RLIMIT_STACK
, &rlim
)) {
286 if ((limitStackSpace
> 0) && (limitStackSpace
< rlim
.rlim_max
))
287 rlim
.rlim_cur
= limitStackSpace
;
289 rlim
.rlim_cur
= rlim
.rlim_max
;
290 (void) setrlimit(RLIMIT_STACK
, &rlim
);
295 if (limitNoFile
>= 0) {
298 if (!getrlimit(RLIMIT_NOFILE
, &rlim
)) {
299 if ((limitNoFile
> 0) && (limitNoFile
< rlim
.rlim_max
))
300 rlim
.rlim_cur
= limitNoFile
;
302 rlim
.rlim_cur
= rlim
.rlim_max
;
303 (void) setrlimit(RLIMIT_NOFILE
, &rlim
);
314 * No log file by default. OsVendorInit() should call LogInit() with the
315 * log file name if logging to a file is desired.
322 OsCleanup(Bool terminating
)