1706f2543Smrg/***********************************************************
2706f2543Smrg
3706f2543SmrgCopyright 1987, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included in
12706f2543Smrgall copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15706f2543SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16706f2543SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17706f2543SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18706f2543SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19706f2543SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20706f2543Smrg
21706f2543SmrgExcept as contained in this notice, the name of The Open Group shall not be
22706f2543Smrgused in advertising or otherwise to promote the sale, use or other dealings
23706f2543Smrgin this Software without prior written authorization from The Open Group.
24706f2543Smrg
25706f2543Smrg
26706f2543SmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27706f2543Smrg
28706f2543Smrg                        All Rights Reserved
29706f2543Smrg
30706f2543SmrgPermission to use, copy, modify, and distribute this software and its
31706f2543Smrgdocumentation for any purpose and without fee is hereby granted,
32706f2543Smrgprovided that the above copyright notice appear in all copies and that
33706f2543Smrgboth that copyright notice and this permission notice appear in
34706f2543Smrgsupporting documentation, and that the name of Digital not be
35706f2543Smrgused in advertising or publicity pertaining to distribution of the
36706f2543Smrgsoftware without specific, written prior permission.
37706f2543Smrg
38706f2543SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39706f2543SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40706f2543SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41706f2543SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42706f2543SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43706f2543SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44706f2543SmrgSOFTWARE.
45706f2543Smrg
46706f2543Smrg******************************************************************/
47706f2543Smrg
48706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
49706f2543Smrg#include <dix-config.h>
50706f2543Smrg#endif
51706f2543Smrg
52706f2543Smrg#include <stdio.h>
53706f2543Smrg#include <X11/X.h>
54706f2543Smrg#include "os.h"
55706f2543Smrg#include "osdep.h"
56706f2543Smrg#include <X11/Xos.h>
57706f2543Smrg#include <signal.h>
58706f2543Smrg#include <errno.h>
59706f2543Smrg#ifdef HAVE_DLFCN_H
60706f2543Smrg# include <dlfcn.h>
61706f2543Smrg#endif
62706f2543Smrg#ifdef HAVE_BACKTRACE
63706f2543Smrg#include <execinfo.h>
64706f2543Smrg#endif
65706f2543Smrg
66706f2543Smrg
67706f2543Smrg#include "dixstruct.h"
68706f2543Smrg
69706f2543Smrg#ifndef PATH_MAX
70706f2543Smrg#ifdef MAXPATHLEN
71706f2543Smrg#define PATH_MAX MAXPATHLEN
72706f2543Smrg#else
73706f2543Smrg#define PATH_MAX 1024
74706f2543Smrg#endif
75706f2543Smrg#endif
76706f2543Smrg
77706f2543Smrg
78706f2543Smrg#if !defined(SYSV) && !defined(WIN32)
79706f2543Smrg#include <sys/resource.h>
80706f2543Smrg#endif
81706f2543Smrg
82706f2543Smrg#ifndef ADMPATH
83706f2543Smrg#define ADMPATH "/usr/adm/X%smsgs"
84706f2543Smrg#endif
85706f2543Smrg
86706f2543Smrgextern char *display;
87706f2543Smrg#ifdef RLIMIT_DATA
88706f2543Smrgint limitDataSpace = -1;
89706f2543Smrg#endif
90706f2543Smrg#ifdef RLIMIT_STACK
91706f2543Smrgint limitStackSpace = -1;
92706f2543Smrg#endif
93706f2543Smrg#ifdef RLIMIT_NOFILE
94706f2543Smrgint limitNoFile = -1;
95706f2543Smrg#endif
96706f2543Smrg
97706f2543Smrgstatic OsSigWrapperPtr OsSigWrapper = NULL;
98706f2543Smrg
99706f2543SmrgOsSigWrapperPtr
100706f2543SmrgOsRegisterSigWrapper(OsSigWrapperPtr newSigWrapper)
101706f2543Smrg{
102706f2543Smrg    OsSigWrapperPtr oldSigWrapper = OsSigWrapper;
103706f2543Smrg
104706f2543Smrg    OsSigWrapper = newSigWrapper;
105706f2543Smrg
106706f2543Smrg    return oldSigWrapper;
107706f2543Smrg}
108706f2543Smrg
109706f2543Smrg/*
110706f2543Smrg * OsSigHandler --
111706f2543Smrg *    Catch unexpected signals and exit or continue cleanly.
112706f2543Smrg */
113706f2543Smrgstatic void
114706f2543Smrg#ifdef SA_SIGINFO
115706f2543SmrgOsSigHandler(int signo, siginfo_t *sip, void *unused)
116706f2543Smrg#else
117706f2543SmrgOsSigHandler(int signo)
118706f2543Smrg#endif
119706f2543Smrg{
120706f2543Smrg#ifdef RTLD_DI_SETSIGNAL
121706f2543Smrg  const char *dlerr = dlerror();
122706f2543Smrg
123706f2543Smrg  if (dlerr) {
124706f2543Smrg      LogMessage(X_ERROR, "Dynamic loader error: %s\n", dlerr);
125706f2543Smrg  }
126706f2543Smrg#endif /* RTLD_DI_SETSIGNAL */
127706f2543Smrg
128706f2543Smrg  if (OsSigWrapper != NULL) {
129706f2543Smrg      if (OsSigWrapper(signo) == 0) {
130706f2543Smrg	  /* ddx handled signal and wants us to continue */
131706f2543Smrg	  return;
132706f2543Smrg      }
133706f2543Smrg  }
134706f2543Smrg
135706f2543Smrg  /* log, cleanup, and abort */
136706f2543Smrg  xorg_backtrace();
137706f2543Smrg
138706f2543Smrg#ifdef SA_SIGINFO
139706f2543Smrg  if (sip->si_code == SI_USER) {
140706f2543Smrg      ErrorF("Recieved signal %d sent by process %ld, uid %ld\n",
141706f2543Smrg	     signo, (long) sip->si_pid, (long) sip->si_uid);
142706f2543Smrg  } else {
143706f2543Smrg      switch (signo) {
144706f2543Smrg          case SIGSEGV:
145706f2543Smrg          case SIGBUS:
146706f2543Smrg          case SIGILL:
147706f2543Smrg          case SIGFPE:
148706f2543Smrg	      ErrorF("%s at address %p\n", strsignal(signo), sip->si_addr);
149706f2543Smrg      }
150706f2543Smrg  }
151706f2543Smrg#endif
152706f2543Smrg
153706f2543Smrg  FatalError("Caught signal %d (%s). Server aborting\n",
154706f2543Smrg	     signo, strsignal(signo));
155706f2543Smrg}
156706f2543Smrg
157706f2543Smrgvoid
158706f2543SmrgOsInit(void)
159706f2543Smrg{
160706f2543Smrg    static Bool been_here = FALSE;
161706f2543Smrg    static char* devnull = "/dev/null";
162706f2543Smrg    char fname[PATH_MAX];
163706f2543Smrg
164706f2543Smrg    if (!been_here) {
165706f2543Smrg	struct sigaction act, oact;
166706f2543Smrg	int i;
167706f2543Smrg	int siglist[] = { SIGSEGV, SIGQUIT, SIGILL, SIGFPE, SIGBUS,
168706f2543Smrg			  SIGSYS,
169706f2543Smrg			  SIGXCPU,
170706f2543Smrg			  SIGXFSZ,
171706f2543Smrg#ifdef SIGEMT
172706f2543Smrg			  SIGEMT,
173706f2543Smrg#endif
174706f2543Smrg			  0 /* must be last */ };
175706f2543Smrg	sigemptyset(&act.sa_mask);
176706f2543Smrg#ifdef SA_SIGINFO
177706f2543Smrg	act.sa_sigaction = OsSigHandler;
178706f2543Smrg	act.sa_flags = SA_SIGINFO;
179706f2543Smrg#else
180706f2543Smrg        act.sa_handler = OsSigHandler;
181706f2543Smrg        act.sa_flags = 0;
182706f2543Smrg#endif
183706f2543Smrg	for (i = 0; siglist[i] != 0; i++) {
184706f2543Smrg	    if (sigaction(siglist[i], &act, &oact)) {
185706f2543Smrg		ErrorF("failed to install signal handler for signal %d: %s\n",
186706f2543Smrg		       siglist[i], strerror(errno));
187706f2543Smrg	    }
188706f2543Smrg	}
189706f2543Smrg#ifdef HAVE_BACKTRACE
190706f2543Smrg	/*
191706f2543Smrg	 * initialize the backtracer, since the ctor calls dlopen(), which
192706f2543Smrg	 * calls malloc(), which isn't signal-safe.
193706f2543Smrg	 */
194706f2543Smrg	do {
195706f2543Smrg	    void *array;
196706f2543Smrg	    backtrace(&array, 1);
197706f2543Smrg	} while (0);
198706f2543Smrg#endif
199706f2543Smrg
200706f2543Smrg#ifdef RTLD_DI_SETSIGNAL
201706f2543Smrg	/* Tell runtime linker to send a signal we can catch instead of SIGKILL
202706f2543Smrg	 * for failures to load libraries/modules at runtime so we can clean up
203706f2543Smrg	 * after ourselves.
204706f2543Smrg	 */
205706f2543Smrg	int failure_signal = SIGQUIT;
206706f2543Smrg	dlinfo(RTLD_SELF, RTLD_DI_SETSIGNAL, &failure_signal);
207706f2543Smrg#endif
208706f2543Smrg
209706f2543Smrg#if !defined(__CYGWIN__)
210706f2543Smrg	fclose(stdin);
211706f2543Smrg	fclose(stdout);
212706f2543Smrg#endif
213706f2543Smrg	/*
214706f2543Smrg	 * If a write of zero bytes to stderr returns non-zero, i.e. -1,
215706f2543Smrg	 * then writing to stderr failed, and we'll write somewhere else
216706f2543Smrg	 * instead. (Apparently this never happens in the Real World.)
217706f2543Smrg	 */
218706f2543Smrg	if (write (2, fname, 0) == -1)
219706f2543Smrg	{
220706f2543Smrg	    FILE *err;
221706f2543Smrg
222706f2543Smrg	    if (strlen (display) + strlen (ADMPATH) + 1 < sizeof fname)
223706f2543Smrg		sprintf (fname, ADMPATH, display);
224706f2543Smrg	    else
225706f2543Smrg		strcpy (fname, devnull);
226706f2543Smrg	    /*
227706f2543Smrg	     * uses stdio to avoid os dependencies here,
228706f2543Smrg	     * a real os would use
229706f2543Smrg 	     *  open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
230706f2543Smrg	     */
231706f2543Smrg	    if (!(err = fopen (fname, "a+")))
232706f2543Smrg		err = fopen (devnull, "w");
233706f2543Smrg	    if (err && (fileno(err) != 2)) {
234706f2543Smrg		dup2 (fileno (err), 2);
235706f2543Smrg		fclose (err);
236706f2543Smrg	    }
237706f2543Smrg#if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
238706f2543Smrg	    {
239706f2543Smrg	    static char buf[BUFSIZ];
240706f2543Smrg	    setvbuf (stderr, buf, _IOLBF, BUFSIZ);
241706f2543Smrg	    }
242706f2543Smrg#else
243706f2543Smrg	    setlinebuf(stderr);
244706f2543Smrg#endif
245706f2543Smrg	}
246706f2543Smrg
247706f2543Smrg	if (getpgrp () == 0)
248706f2543Smrg	    setpgid (0, 0);
249706f2543Smrg
250706f2543Smrg#ifdef RLIMIT_DATA
251706f2543Smrg	if (limitDataSpace >= 0)
252706f2543Smrg	{
253706f2543Smrg	    struct rlimit	rlim;
254706f2543Smrg
255706f2543Smrg	    if (!getrlimit(RLIMIT_DATA, &rlim))
256706f2543Smrg	    {
257706f2543Smrg		if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
258706f2543Smrg		    rlim.rlim_cur = limitDataSpace;
259706f2543Smrg		else
260706f2543Smrg		    rlim.rlim_cur = rlim.rlim_max;
261706f2543Smrg		(void)setrlimit(RLIMIT_DATA, &rlim);
262706f2543Smrg	    }
263706f2543Smrg	}
264706f2543Smrg#endif
265706f2543Smrg#ifdef RLIMIT_STACK
266706f2543Smrg	if (limitStackSpace >= 0)
267706f2543Smrg	{
268706f2543Smrg	    struct rlimit	rlim;
269706f2543Smrg
270706f2543Smrg	    if (!getrlimit(RLIMIT_STACK, &rlim))
271706f2543Smrg	    {
272706f2543Smrg		if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
273706f2543Smrg		    rlim.rlim_cur = limitStackSpace;
274706f2543Smrg		else
275706f2543Smrg		    rlim.rlim_cur = rlim.rlim_max;
276706f2543Smrg		(void)setrlimit(RLIMIT_STACK, &rlim);
277706f2543Smrg	    }
278706f2543Smrg	}
279706f2543Smrg#endif
280706f2543Smrg#ifdef RLIMIT_NOFILE
281706f2543Smrg	if (limitNoFile >= 0)
282706f2543Smrg	{
283706f2543Smrg	    struct rlimit	rlim;
284706f2543Smrg
285706f2543Smrg	    if (!getrlimit(RLIMIT_NOFILE, &rlim))
286706f2543Smrg	    {
287706f2543Smrg		if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
288706f2543Smrg		    rlim.rlim_cur = limitNoFile;
289706f2543Smrg		else
290706f2543Smrg		    rlim.rlim_cur = rlim.rlim_max;
291706f2543Smrg		(void)setrlimit(RLIMIT_NOFILE, &rlim);
292706f2543Smrg	    }
293706f2543Smrg	}
294706f2543Smrg#endif
295706f2543Smrg	LockServer();
296706f2543Smrg	been_here = TRUE;
297706f2543Smrg    }
298706f2543Smrg    TimerInit();
299706f2543Smrg    OsVendorInit();
300706f2543Smrg    /*
301706f2543Smrg     * No log file by default.  OsVendorInit() should call LogInit() with the
302706f2543Smrg     * log file name if logging to a file is desired.
303706f2543Smrg     */
304706f2543Smrg    LogInit(NULL, NULL);
305706f2543Smrg    SmartScheduleInit ();
306706f2543Smrg}
307706f2543Smrg
308706f2543Smrgvoid
309706f2543SmrgOsCleanup(Bool terminating)
310706f2543Smrg{
311706f2543Smrg    if (terminating)
312706f2543Smrg    {
313706f2543Smrg	UnlockServer();
314706f2543Smrg    }
315706f2543Smrg}
316