osinit.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include <stdio.h>
  40. #include <X11/X.h>
  41. #include "os.h"
  42. #include "osdep.h"
  43. #include <X11/Xos.h>
  44. #ifdef SMART_SCHEDULE
  45. #include "dixstruct.h"
  46. #endif
  47. #ifndef PATH_MAX
  48. #ifdef MAXPATHLEN
  49. #define PATH_MAX MAXPATHLEN
  50. #else
  51. #define PATH_MAX 1024
  52. #endif
  53. #endif
  54. #include <sys/resource.h>
  55. #ifndef ADMPATH
  56. #define ADMPATH "/usr/adm/X%smsgs"
  57. #endif
  58. extern char *display;
  59. #ifdef RLIMIT_DATA
  60. int limitDataSpace = -1;
  61. #endif
  62. #ifdef RLIMIT_STACK
  63. int limitStackSpace = -1;
  64. #endif
  65. #ifdef RLIMIT_NOFILE
  66. int limitNoFile = -1;
  67. #endif
  68. void
  69. OsInit(void)
  70. {
  71. static Bool been_here = FALSE;
  72. static char* admpath = ADMPATH;
  73. static char* devnull = "/dev/null";
  74. char fname[PATH_MAX];
  75. if (!been_here) {
  76. fclose(stdin);
  77. fclose(stdout);
  78. /*
  79. * If a write of zero bytes to stderr returns non-zero, i.e. -1,
  80. * then writing to stderr failed, and we'll write somewhere else
  81. * instead. (Apparently this never happens in the Real World.)
  82. */
  83. if (write (2, fname, 0) == -1)
  84. {
  85. FILE *err;
  86. if (strlen (display) + strlen (admpath) + 1 < sizeof fname)
  87. sprintf (fname, admpath, display);
  88. else
  89. strcpy (fname, devnull);
  90. /*
  91. * uses stdio to avoid os dependencies here,
  92. * a real os would use
  93. * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
  94. */
  95. if (!(err = fopen (fname, "a+")))
  96. err = fopen (devnull, "w");
  97. if (err && (fileno(err) != 2)) {
  98. dup2 (fileno (err), 2);
  99. fclose (err);
  100. }
  101. setlinebuf(stderr);
  102. }
  103. if (getpgrp () == 0)
  104. setpgid (0, 0);
  105. #ifdef RLIMIT_DATA
  106. if (limitDataSpace >= 0)
  107. {
  108. struct rlimit rlim;
  109. if (!getrlimit(RLIMIT_DATA, &rlim))
  110. {
  111. if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
  112. rlim.rlim_cur = limitDataSpace;
  113. else
  114. rlim.rlim_cur = rlim.rlim_max;
  115. (void)setrlimit(RLIMIT_DATA, &rlim);
  116. }
  117. }
  118. #endif
  119. #ifdef RLIMIT_STACK
  120. if (limitStackSpace >= 0)
  121. {
  122. struct rlimit rlim;
  123. if (!getrlimit(RLIMIT_STACK, &rlim))
  124. {
  125. if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
  126. rlim.rlim_cur = limitStackSpace;
  127. else
  128. rlim.rlim_cur = rlim.rlim_max;
  129. (void)setrlimit(RLIMIT_STACK, &rlim);
  130. }
  131. }
  132. #endif
  133. #ifdef RLIMIT_NOFILE
  134. if (limitNoFile >= 0)
  135. {
  136. struct rlimit rlim;
  137. if (!getrlimit(RLIMIT_NOFILE, &rlim))
  138. {
  139. if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
  140. rlim.rlim_cur = limitNoFile;
  141. else
  142. rlim.rlim_cur = rlim.rlim_max;
  143. #if 0
  144. if (rlim.rlim_cur > MAXSOCKS)
  145. rlim.rlim_cur = MAXSOCKS;
  146. #endif
  147. (void)setrlimit(RLIMIT_NOFILE, &rlim);
  148. }
  149. }
  150. #endif
  151. LockServer();
  152. been_here = TRUE;
  153. }
  154. TimerInit();
  155. OsVendorInit();
  156. #ifdef SMART_SCHEDULE
  157. if (!SmartScheduleDisable)
  158. if (!SmartScheduleInit ())
  159. SmartScheduleDisable = TRUE;
  160. #endif
  161. OsInitAllocator();
  162. }
  163. void
  164. OsCleanup(Bool terminating)
  165. {
  166. if (terminating)
  167. {
  168. UnlockServer();
  169. }
  170. }