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