home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / osinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  3.7 KB  |  159 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: osinit.c,v 1.40 91/11/29 15:21:07 rws Exp $ */
  25. #include "X.h"
  26. #include "os.h"
  27. #include "osdep.h"
  28. #undef NULL
  29. #include <stdio.h>
  30. #include "Xos.h"
  31.  
  32. #ifndef PATH_MAX
  33. #ifdef MAXPATHLEN
  34. #define PATH_MAX MAXPATHLEN
  35. #else
  36. #define PATH_MAX 1024
  37. #endif
  38. #endif
  39.  
  40. #ifndef SYSV
  41. #include <sys/resource.h>
  42. #endif
  43.  
  44. #ifndef ADMPATH
  45. #define ADMPATH "/usr/adm/X%smsgs"
  46. #endif
  47.  
  48. extern char *display;
  49. #ifdef RLIMIT_DATA
  50. int limitDataSpace = -1;
  51. #endif
  52. #ifdef RLIMIT_STACK
  53. int limitStackSpace = -1;
  54. #endif
  55. #ifdef RLIMIT_NOFILE
  56. int limitNoFile = -1;
  57. #endif
  58.  
  59. OsInit()
  60. {
  61.     static Bool been_here = FALSE;
  62.     char fname[PATH_MAX];
  63.  
  64. #ifdef macII
  65.     set42sig();
  66. #endif
  67.  
  68.     if (!been_here) {
  69.     fclose(stdin);
  70.     fclose(stdout);
  71.     /* hack test to decide where to log errors */
  72.     if (write (2, fname, 0)) 
  73.     {
  74.         FILE *err;
  75.         sprintf (fname, ADMPATH, display);
  76.         /*
  77.          * uses stdio to avoid os dependencies here,
  78.          * a real os would use
  79.           *  open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
  80.          */
  81.         if (!(err = fopen (fname, "a+")))
  82.         err = fopen ("/dev/null", "w");
  83.         if (err && (fileno(err) != 2)) {
  84.         dup2 (fileno (err), 2);
  85.         fclose (err);
  86.         }
  87. #if defined(SYSV) || defined(SVR4)
  88.         {
  89.         static char buf[BUFSIZ];
  90.         setvbuf (stderr, buf, _IOLBF, BUFSIZ);
  91.         }
  92. #else
  93.         setlinebuf(stderr);
  94. #endif
  95.     }
  96.  
  97. #ifndef X_NOT_POSIX
  98.     if (getpgrp () == 0)
  99.         setpgid (0, 0);
  100. #else
  101. #ifndef SYSV
  102.     if (getpgrp (0) == 0)
  103.         setpgrp (0, getpid ());
  104. #endif
  105. #endif
  106.  
  107. #ifdef RLIMIT_DATA
  108.     if (limitDataSpace >= 0)
  109.     {
  110.         struct rlimit    rlim;
  111.  
  112.         if (!getrlimit(RLIMIT_DATA, &rlim))
  113.         {
  114.         if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
  115.             rlim.rlim_cur = limitDataSpace;
  116.         else
  117.             rlim.rlim_cur = rlim.rlim_max;
  118.         (void)setrlimit(RLIMIT_DATA, &rlim);
  119.         }
  120.     }
  121. #endif
  122. #ifdef RLIMIT_STACK
  123.     if (limitStackSpace >= 0)
  124.     {
  125.         struct rlimit    rlim;
  126.  
  127.         if (!getrlimit(RLIMIT_STACK, &rlim))
  128.         {
  129.         if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
  130.             rlim.rlim_cur = limitStackSpace;
  131.         else
  132.             rlim.rlim_cur = rlim.rlim_max;
  133.         (void)setrlimit(RLIMIT_STACK, &rlim);
  134.         }
  135.     }
  136. #endif
  137. #ifdef RLIMIT_NOFILE
  138.     if (limitNoFile >= 0)
  139.     {
  140.         struct rlimit    rlim;
  141.  
  142.         if (!getrlimit(RLIMIT_NOFILE, &rlim))
  143.         {
  144.         if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
  145.             rlim.rlim_cur = limitNoFile;
  146.         else
  147.             rlim.rlim_cur = rlim.rlim_max;
  148.         if (rlim.rlim_cur > MAXSOCKS)
  149.             rlim.rlim_cur = MAXSOCKS;
  150.         (void)setrlimit(RLIMIT_NOFILE, &rlim);
  151.         }
  152.     }
  153. #endif
  154.     been_here = TRUE;
  155.     }
  156.  
  157.     OsInitColors();
  158. }
  159.