First import
[xorg_rtime.git] / xorg-server-1.4 / os / osinit.c
blob1f09f068e9ab714d03d945598579bd17a24bb28b
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
9 documentation.
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.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28 All Rights Reserved
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
46 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
52 #include <stdio.h>
53 #include <X11/X.h>
54 #include "os.h"
55 #include "osdep.h"
56 #include <X11/Xos.h>
58 #ifdef SMART_SCHEDULE
59 #include "dixstruct.h"
60 #endif
62 #ifndef PATH_MAX
63 #ifdef MAXPATHLEN
64 #define PATH_MAX MAXPATHLEN
65 #else
66 #define PATH_MAX 1024
67 #endif
68 #endif
70 #if defined(Lynx) || defined(__SCO__)
71 #include <sys/wait.h>
72 #endif
74 #if !defined(SYSV) && !defined(WIN32) && !defined(Lynx) && !defined(QNX4)
75 #include <sys/resource.h>
76 #endif
78 #ifndef ADMPATH
79 #define ADMPATH "/usr/adm/X%smsgs"
80 #endif
82 extern char *display;
83 #ifdef RLIMIT_DATA
84 int limitDataSpace = -1;
85 #endif
86 #ifdef RLIMIT_STACK
87 int limitStackSpace = -1;
88 #endif
89 #ifdef RLIMIT_NOFILE
90 int limitNoFile = -1;
91 #endif
93 Bool OsDelayInitColors = FALSE;
95 void
96 OsInit(void)
98 static Bool been_here = FALSE;
99 static char* admpath = ADMPATH;
100 static char* devnull = "/dev/null";
101 char fname[PATH_MAX];
103 #ifdef macII
104 set42sig();
105 #endif
107 if (!been_here) {
108 #if !defined(__SCO__) && !defined(__CYGWIN__) && !defined(__UNIXWARE__)
109 fclose(stdin);
110 fclose(stdout);
111 #endif
113 * If a write of zero bytes to stderr returns non-zero, i.e. -1,
114 * then writing to stderr failed, and we'll write somewhere else
115 * instead. (Apparently this never happens in the Real World.)
117 if (write (2, fname, 0) == -1)
119 FILE *err;
121 if (strlen (display) + strlen (admpath) + 1 < sizeof fname)
122 sprintf (fname, admpath, display);
123 else
124 strcpy (fname, devnull);
126 * uses stdio to avoid os dependencies here,
127 * a real os would use
128 * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
130 if (!(err = fopen (fname, "a+")))
131 err = fopen (devnull, "w");
132 if (err && (fileno(err) != 2)) {
133 dup2 (fileno (err), 2);
134 fclose (err);
136 #if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
138 static char buf[BUFSIZ];
139 setvbuf (stderr, buf, _IOLBF, BUFSIZ);
141 #else
142 setlinebuf(stderr);
143 #endif
146 #ifndef X_NOT_POSIX
147 if (getpgrp () == 0)
148 setpgid (0, 0);
149 #else
150 #if !defined(SYSV) && !defined(WIN32)
151 if (getpgrp (0) == 0)
152 setpgrp (0, getpid ());
153 #endif
154 #endif
156 #ifdef RLIMIT_DATA
157 if (limitDataSpace >= 0)
159 struct rlimit rlim;
161 if (!getrlimit(RLIMIT_DATA, &rlim))
163 if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
164 rlim.rlim_cur = limitDataSpace;
165 else
166 rlim.rlim_cur = rlim.rlim_max;
167 (void)setrlimit(RLIMIT_DATA, &rlim);
170 #endif
171 #ifdef RLIMIT_STACK
172 if (limitStackSpace >= 0)
174 struct rlimit rlim;
176 if (!getrlimit(RLIMIT_STACK, &rlim))
178 if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
179 rlim.rlim_cur = limitStackSpace;
180 else
181 rlim.rlim_cur = rlim.rlim_max;
182 (void)setrlimit(RLIMIT_STACK, &rlim);
185 #endif
186 #ifdef RLIMIT_NOFILE
187 if (limitNoFile >= 0)
189 struct rlimit rlim;
191 if (!getrlimit(RLIMIT_NOFILE, &rlim))
193 if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
194 rlim.rlim_cur = limitNoFile;
195 else
196 rlim.rlim_cur = rlim.rlim_max;
197 #if 0
198 if (rlim.rlim_cur > MAXSOCKS)
199 rlim.rlim_cur = MAXSOCKS;
200 #endif
201 (void)setrlimit(RLIMIT_NOFILE, &rlim);
204 #endif
205 #ifdef SERVER_LOCK
206 LockServer();
207 #endif
208 been_here = TRUE;
210 TimerInit();
211 #ifdef DDXOSINIT
212 OsVendorInit();
213 #endif
215 * No log file by default. OsVendorInit() should call LogInit() with the
216 * log file name if logging to a file is desired.
218 LogInit(NULL, NULL);
219 #ifdef SMART_SCHEDULE
220 if (!SmartScheduleDisable)
221 if (!SmartScheduleInit ())
222 SmartScheduleDisable = TRUE;
223 #endif
224 OsInitAllocator();
225 if (!OsDelayInitColors) OsInitColors();
228 void
229 OsCleanup(Bool terminating)
231 #ifdef SERVER_LOCK
232 if (terminating)
234 UnlockServer();
236 #endif