osinit.c (9584B)
1 /*********************************************************** 2 3 Copyright 1987, 1998 The Open Group 4 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 9 documentation. 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 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. 20 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. 24 25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. 26 27 All Rights Reserved 28 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. 36 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 43 SOFTWARE. 44 45 ******************************************************************/ 46 47 #ifdef HAVE_DIX_CONFIG_H 48 #include <dix-config.h> 49 #endif 50 51 #include <stdio.h> 52 #include <X11/X.h> 53 #include "os.h" 54 #include "osdep.h" 55 #include "opaque.h" 56 #include <X11/Xos.h> 57 #include <signal.h> 58 #include <errno.h> 59 #ifdef HAVE_DLFCN_H 60 #include <dlfcn.h> 61 #endif 62 #ifdef HAVE_BACKTRACE 63 #include <execinfo.h> 64 #endif 65 66 #include "misc.h" 67 68 #include "dixstruct.h" 69 70 #if !defined(SYSV) && !defined(WIN32) 71 #include <sys/resource.h> 72 #endif 73 74 #ifndef ADMPATH 75 #define ADMPATH "/usr/adm/X%smsgs" 76 #endif 77 78 #ifdef RLIMIT_DATA 79 int limitDataSpace = -1; 80 #endif 81 #ifdef RLIMIT_STACK 82 int limitStackSpace = -1; 83 #endif 84 #ifdef RLIMIT_NOFILE 85 int limitNoFile = -1; 86 #endif 87 88 /* The actual user defined max number of clients */ 89 int LimitClients = LIMITCLIENTS; 90 91 static OsSigWrapperPtr OsSigWrapper = NULL; 92 93 OsSigWrapperPtr 94 OsRegisterSigWrapper(OsSigWrapperPtr newSigWrapper) 95 { 96 OsSigWrapperPtr oldSigWrapper = OsSigWrapper; 97 98 OsSigWrapper = newSigWrapper; 99 100 return oldSigWrapper; 101 } 102 103 /* 104 * OsSigHandler -- 105 * Catch unexpected signals and exit or continue cleanly. 106 */ 107 #if !defined(WIN32) || defined(__CYGWIN__) 108 static void 109 #ifdef SA_SIGINFO 110 OsSigHandler(int signo, siginfo_t * sip, void *unused) 111 #else 112 OsSigHandler(int signo) 113 #endif 114 { 115 #ifdef RTLD_DI_SETSIGNAL 116 # define SIGNAL_FOR_RTLD_ERROR SIGQUIT 117 if (signo == SIGNAL_FOR_RTLD_ERROR) { 118 const char *dlerr = dlerror(); 119 120 if (dlerr) { 121 LogMessageVerbSigSafe(X_ERROR, 1, 122 "Dynamic loader error: %s\n", dlerr); 123 } 124 } 125 #endif /* RTLD_DI_SETSIGNAL */ 126 127 if (OsSigWrapper != NULL) { 128 if (OsSigWrapper(signo) == 0) { 129 /* ddx handled signal and wants us to continue */ 130 return; 131 } 132 } 133 134 /* log, cleanup, and abort */ 135 xorg_backtrace(); 136 137 #ifdef SA_SIGINFO 138 if (sip->si_code == SI_USER) { 139 ErrorFSigSafe("Received signal %u sent by process %u, uid %u\n", signo, 140 sip->si_pid, sip->si_uid); 141 } 142 else { 143 switch (signo) { 144 case SIGSEGV: 145 case SIGBUS: 146 case SIGILL: 147 case SIGFPE: 148 ErrorFSigSafe("%s at address %p\n", strsignal(signo), sip->si_addr); 149 } 150 } 151 #endif 152 153 if (signo != SIGQUIT) 154 CoreDump = TRUE; 155 156 FatalError("Caught signal %d (%s). Server aborting\n", 157 signo, strsignal(signo)); 158 } 159 #endif /* !WIN32 || __CYGWIN__ */ 160 161 #include "busfault.h" 162 163 void 164 OsInit(void) 165 { 166 static Bool been_here = FALSE; 167 #ifndef XQUARTZ 168 static const char *devnull = "/dev/null"; 169 char fname[PATH_MAX]; 170 #endif 171 172 if (!been_here) { 173 #if !defined(WIN32) || defined(__CYGWIN__) 174 struct sigaction act, oact; 175 int i; 176 177 int siglist[] = { SIGSEGV, SIGQUIT, SIGILL, SIGFPE, SIGBUS, 178 SIGABRT, 179 SIGSYS, 180 SIGXCPU, 181 SIGXFSZ, 182 #ifdef SIGEMT 183 SIGEMT, 184 #endif 185 0 /* must be last */ 186 }; 187 sigemptyset(&act.sa_mask); 188 #ifdef SA_SIGINFO 189 act.sa_sigaction = OsSigHandler; 190 act.sa_flags = SA_SIGINFO; 191 #else 192 act.sa_handler = OsSigHandler; 193 act.sa_flags = 0; 194 #endif 195 for (i = 0; siglist[i] != 0; i++) { 196 if (sigaction(siglist[i], &act, &oact)) { 197 ErrorF("failed to install signal handler for signal %d: %s\n", 198 siglist[i], strerror(errno)); 199 } 200 } 201 #endif /* !WIN32 || __CYGWIN__ */ 202 #ifdef BUSFAULT 203 busfault_init(); 204 #endif 205 server_poll = ospoll_create(); 206 if (!server_poll) 207 FatalError("failed to allocate poll structure"); 208 209 #ifdef HAVE_BACKTRACE 210 /* 211 * initialize the backtracer, since the ctor calls dlopen(), which 212 * calls malloc(), which isn't signal-safe. 213 */ 214 do { 215 void *array; 216 217 backtrace(&array, 1); 218 } while (0); 219 #endif 220 221 #ifdef RTLD_DI_SETSIGNAL 222 /* Tell runtime linker to send a signal we can catch instead of SIGKILL 223 * for failures to load libraries/modules at runtime so we can clean up 224 * after ourselves. 225 */ 226 { 227 int failure_signal = SIGNAL_FOR_RTLD_ERROR; 228 229 dlinfo(RTLD_SELF, RTLD_DI_SETSIGNAL, &failure_signal); 230 } 231 #endif 232 233 #if !defined(XQUARTZ) /* STDIN is already /dev/null and STDOUT/STDERR is managed by console_redirect.c */ 234 /* 235 * If a write of zero bytes to stderr returns non-zero, i.e. -1, 236 * then writing to stderr failed, and we'll write somewhere else 237 * instead. (Apparently this never happens in the Real World.) 238 */ 239 if (write(2, fname, 0) == -1) { 240 FILE *err; 241 242 if (strlen(display) + strlen(ADMPATH) + 1 < sizeof fname) 243 snprintf(fname, sizeof(fname), ADMPATH, display); 244 else 245 strcpy(fname, devnull); 246 /* 247 * uses stdio to avoid os dependencies here, 248 * a real os would use 249 * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666) 250 */ 251 if (!(err = fopen(fname, "a+"))) 252 err = fopen(devnull, "w"); 253 if (err && (fileno(err) != 2)) { 254 dup2(fileno(err), 2); 255 fclose(err); 256 } 257 #if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__) 258 { 259 static char buf[BUFSIZ]; 260 261 setvbuf(stderr, buf, _IOLBF, BUFSIZ); 262 } 263 #else 264 setlinebuf(stderr); 265 #endif 266 } 267 #endif /* !XQUARTZ */ 268 269 #if !defined(WIN32) || defined(__CYGWIN__) 270 if (getpgrp() == 0) 271 setpgid(0, 0); 272 #endif 273 274 #ifdef RLIMIT_DATA 275 if (limitDataSpace >= 0) { 276 struct rlimit rlim; 277 278 if (!getrlimit(RLIMIT_DATA, &rlim)) { 279 if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max)) 280 rlim.rlim_cur = limitDataSpace; 281 else 282 rlim.rlim_cur = rlim.rlim_max; 283 (void) setrlimit(RLIMIT_DATA, &rlim); 284 } 285 } 286 #endif 287 #ifdef RLIMIT_STACK 288 if (limitStackSpace >= 0) { 289 struct rlimit rlim; 290 291 if (!getrlimit(RLIMIT_STACK, &rlim)) { 292 if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max)) 293 rlim.rlim_cur = limitStackSpace; 294 else 295 rlim.rlim_cur = rlim.rlim_max; 296 (void) setrlimit(RLIMIT_STACK, &rlim); 297 } 298 } 299 #endif 300 #ifdef RLIMIT_NOFILE 301 if (limitNoFile >= 0) { 302 struct rlimit rlim; 303 304 if (!getrlimit(RLIMIT_NOFILE, &rlim)) { 305 if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max)) 306 rlim.rlim_cur = limitNoFile; 307 else 308 rlim.rlim_cur = rlim.rlim_max; 309 (void) setrlimit(RLIMIT_NOFILE, &rlim); 310 } 311 } 312 #endif 313 LockServer(); 314 been_here = TRUE; 315 } 316 TimerInit(); 317 OsVendorInit(); 318 OsResetSignals(); 319 /* 320 * No log file by default. OsVendorInit() should call LogInit() with the 321 * log file name if logging to a file is desired. 322 */ 323 LogInit(NULL, NULL); 324 SmartScheduleInit(); 325 } 326 327 void 328 OsCleanup(Bool terminating) 329 { 330 if (terminating) { 331 UnlockServer(); 332 } 333 }