1 /* $TOG: utils.c /main/128 1997/06/01 13:50:39 sekhar $ */
4 Copyright (c) 1987 X Consortium
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of the X Consortium shall
26 not be used in advertising or otherwise to promote the sale, use or
27 other dealings in this Software without prior written authorization
28 from the X Consortium.
31 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
32 Copyright 1994 Quarterdeck Office Systems.
36 Permission to use, copy, modify, and distribute this software and its
37 documentation for any purpose and without fee is hereby granted,
38 provided that the above copyright notice appear in all copies and that
39 both that copyright notice and this permission notice appear in
40 supporting documentation, and that the names of Digital and
41 Quarterdeck not be used in advertising or publicity pertaining to
42 distribution of the software without specific, written prior
45 DIGITAL AND QUARTERDECK DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
46 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
47 FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
48 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
49 OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
50 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
51 OR PERFORMANCE OF THIS SOFTWARE.
54 /* $XFree86: xc/programs/Xserver/os/utils.c,v 3.27.2.6 1998/02/20 15:13:58 robin Exp $ */
57 #include <X11/Xwinsock.h>
65 #ifdef X_POSIX_C_SOURCE
66 #define _POSIX_C_SOURCE X_POSIX_C_SOURCE
68 #undef _POSIX_C_SOURCE
70 #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
78 #if !defined(SYSV) && !defined(AMOEBA) && !defined(_MINIX) && !defined(WIN32) && !defined(Lynx) || defined(hpux) || defined(AIXV4)
79 #include <sys/resource.h>
83 #include <ctype.h> /* for isspace */
84 #if NeedVarargsPrototypes
91 #include <module/mutex.h>
93 static mutex print_lock;
96 #if defined(__STDC__) || defined(AMOEBA)
97 /* DHD: SVR4.0 has a prototype for abs() in stdlib.h */
98 /* DHD: might be better to move this include higher up? */
103 #include <stdlib.h> /* for malloc() */
107 extern char *display;
109 extern CARD32 defaultScreenSaverTime; /* for parsing command line */
110 extern CARD32 defaultScreenSaverInterval;
111 extern int defaultScreenSaverBlanking;
112 extern int defaultBackingStore;
113 extern Bool disableBackingStore;
114 extern Bool disableSaveUnders;
115 extern Bool PartialNetwork;
117 extern int logoScreenSaver;
120 extern int limitDataSpace;
123 extern int limitStackSpace;
126 extern int limitNoFile;
128 extern int defaultColorVisualClass;
129 extern Bool permitOldBugs;
130 extern int monitorResolution;
131 extern Bool defeatAccessControl;
133 static Bool nolock = FALSE;
136 extern char* protNoListen;
139 Bool noTestExtensions;
141 int auditTrailLevel = 1;
144 #if NeedVarargsPrototypes
145 void VErrorF(char*, va_list);
149 #ifndef SPECIAL_MALLOC
155 #define MEM_FAIL_SCALE 100000
156 long Memory_fail = 0;
158 #include <stdlib.h> /* for random() */
163 int userdefinedfontpath = 0;
166 Bool Must_have_memory = FALSE;
168 char *dev_tty_from_init = NULL; /* since we need to parse it anyway */
171 OsSignal(sig, handler)
173 OsSigHandlerPtr handler;
176 return signal(sig, handler);
178 struct sigaction act, oact;
180 sigemptyset(&act.sa_mask);
181 if (handler != SIG_IGN)
182 sigaddset(&act.sa_mask, sig);
184 act.sa_handler = handler;
185 sigaction(sig, &act, &oact);
186 return oact.sa_handler;
195 * Explicit support for a server lock file like the ones used for UUCP.
196 * For architectures with virtual terminals that can run more than one
197 * server at a time. This keeps the servers from stomping on each other
198 * if the user forgets to give them different display numbers.
201 #define LOCK_DIR "/tmp"
202 #define LOCK_TMP_PREFIX "/.tX"
203 #define LOCK_PREFIX "/.X"
204 #define LOCK_SUFFIX "-lock"
206 #define LOCK_TMP_PREFIX "/xf86$"
207 #define LOCK_PREFIX "/xf86_"
208 #define LOCK_SUFFIX ".lck"
212 #include <limits.h> /* For PATH_MAX */
221 #include <sys/param.h>
227 #define PATH_MAX MAXPATHLEN
229 #define PATH_MAX 1024
234 static Bool StillLocking = FALSE;
235 static char LockFile[PATH_MAX];
239 * Check if the server lock file exists. If so, check if the PID
240 * contained inside is valid. If so, then die. Otherwise, create
241 * the lock file containing the PID.
247 char tmp[PATH_MAX], pid_str[12];
248 int lfd, i, haslock, l_pid, t;
249 char *tmppath = NULL;
259 /* OS/2 uses TMP directory, must also prepare for 8.3 names */
260 tmppath = getenv("TMP");
262 FatalError("No TMP dir found\n");
265 len = strlen(LOCK_PREFIX) > strlen(LOCK_TMP_PREFIX) ? strlen(LOCK_PREFIX) :
266 strlen(LOCK_TMP_PREFIX);
267 len += strlen(tmppath) + strlen(display) + strlen(LOCK_SUFFIX) + 1;
268 if (len > sizeof(LockFile))
269 FatalError("Display name `%s' is too long\n");
270 (void)sprintf(tmp, "%s" LOCK_TMP_PREFIX "%s" LOCK_SUFFIX, tmppath, display);
271 (void)sprintf(LockFile, "%s" LOCK_PREFIX "%s" LOCK_SUFFIX, tmppath, display);
274 * Create a temporary file containing our PID. Attempt three times
275 * to create the file.
281 lfd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0644);
292 lfd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0644);
300 FatalError("Could not create lock file in %s\n", tmp);
301 (void) sprintf(pid_str, "%10d\n", getpid());
302 (void) write(lfd, pid_str, 11);
305 (void) fchmod(lfd, 0444);
307 (void) chmod(tmp, 0444);
313 * OK. Now the tmp file exists. Try three times to move it in place
318 while ((!haslock) && (i++ < 3)) {
319 haslock = (link(tmp,LockFile) == 0);
328 * Read the pid from the existing file
330 lfd = open(LockFile, O_RDONLY);
333 FatalError("Can't read lock file %s\n", LockFile);
336 if (read(lfd, pid_str, 11) != 11) {
345 sscanf(pid_str, "%d", &l_pid);
349 * Now try to kill the PID to see if it exists.
353 if ((t< 0) && (errno == ESRCH)) {
360 else if (((t < 0) && (errno == EPERM)) || (t == 0)) {
362 * Process is still active.
365 FatalError("Server is already active for display %s\n%s %s\n%s\n",
366 display, "\tIf this server is no longer running, remove",
367 LockFile, "\tand start again.");
373 FatalError("Could not create server lock file: %s\n", LockFile);
374 StillLocking = FALSE;
380 * Remove the server lock file.
391 (void) chmod(LockFile,S_IREAD|S_IWRITE);
393 (void) unlink(LockFile);
398 #endif /* SERVER_LOCK */
400 /* Force connections to close on SIGHUP from init */
404 AutoResetServer (sig)
407 dispatchException |= DE_RESET;
408 isItTimeToYield = TRUE;
413 #if defined(SYSV) && defined(X_NOT_POSIX)
414 OsSignal (SIGHUP, AutoResetServer);
421 /* Force connections to close and then exit on SIGTERM, SIGINT */
428 dispatchException |= DE_TERMINATE;
429 isItTimeToYield = TRUE;
430 #if defined(SYSV) && defined(X_NOT_POSIX)
432 OsSignal(sig, SIG_IGN);
440 static void AbortServer() __attribute__((noreturn));
446 extern void AbortDDX();
464 mu_lock(&print_lock);
468 mu_unlock(&print_lock);
480 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
487 AdjustWaitForDelay (waitTime, newdelay)
489 unsigned long newdelay;
491 static struct timeval delay_val;
492 struct timeval **wt = (struct timeval **) waitTime;
493 unsigned long olddelay;
497 delay_val.tv_sec = newdelay / 1000;
498 delay_val.tv_usec = 1000 * (newdelay % 1000);
503 olddelay = (*wt)->tv_sec * 1000 + (*wt)->tv_usec / 1000;
504 if (newdelay < olddelay)
506 (*wt)->tv_sec = newdelay / 1000;
507 (*wt)->tv_usec = 1000 * (newdelay % 1000);
514 #if !defined(AIXrt) && !defined(AIX386)
516 ErrorF("use: X [:<display>] [option]\n");
518 ErrorF("use: X [[<host>]:<display>] [option]\n");
520 ErrorF("-a # mouse acceleration (pixels)\n");
521 ErrorF("-ac disable access control restrictions\n");
523 ErrorF("-alloc int chance alloc should fail\n");
525 ErrorF("-audit int set audit trail level\n");
526 ErrorF("-auth file select authorization file\n");
527 ErrorF("bc enable bug compatibility\n");
528 ErrorF("-bs disable any backing store support\n");
529 ErrorF("-c turns off key-click\n");
530 ErrorF("c # key-click volume (0-100)\n");
531 ErrorF("-cc int default color visual class\n");
532 ErrorF("-co file color database file\n");
534 ErrorF("-config file read options from file\n");
536 ErrorF("-core generate core dump on fatal error\n");
537 ErrorF("-dpi int screen resolution in dots per inch\n");
539 ErrorF("dpms enables VESA DPMS monitor control\n");
540 ErrorF("-dpms disables VESA DPMS monitor control\n");
542 ErrorF("-deferglyphs [none|all|16] defer loading of [no|all|16-bit] glyphs\n");
543 ErrorF("-f # bell base (0-100)\n");
544 ErrorF("-fc string cursor font\n");
545 ErrorF("-fn string default font name\n");
546 ErrorF("-fp string default font path\n");
547 ErrorF("-help prints message with these options\n");
548 ErrorF("-I ignore all remaining arguments\n");
550 ErrorF("-ld int limit data space to N Kb\n");
553 ErrorF("-lf int limit number of open files to N\n");
556 ErrorF("-ls int limit stack space to N Kb\n");
559 ErrorF("-nolock disable the locking mechanism\n");
562 ErrorF("-logo enable logo in screen saver\n");
563 ErrorF("nologo disable logo in screen saver\n");
565 ErrorF("-nolisten string don't listen on protocol\n");
566 ErrorF("-p # screen-saver pattern duration (minutes)\n");
567 ErrorF("-pn accept failure to listen on all ports\n");
568 ErrorF("-nopn reject failure to listen on all ports\n");
569 ErrorF("-r turns off auto-repeat\n");
570 ErrorF("r turns on auto-repeat \n");
571 ErrorF("-s # screen-saver timeout (minutes)\n");
573 ErrorF("-sp file security policy file\n");
575 ErrorF("-su disable any save under support\n");
576 ErrorF("-t # mouse threshold (pixels)\n");
577 ErrorF("-terminate terminate at server reset\n");
578 ErrorF("-to # connection time out\n");
579 ErrorF("-tst disable testing extensions\n");
580 ErrorF("ttyxx server started from init on /dev/ttyxx\n");
581 ErrorF("v video blanking for screen-saver\n");
582 ErrorF("-v screen-saver without video blanking\n");
583 ErrorF("-wm WhenMapped default backing-store\n");
584 ErrorF("-x string loads named extension at init time \n");
586 ErrorF("-tcp capability specify TCP/IP server capability\n");
591 #endif /* !AIXrt && ! AIX386 */
599 * This function parses the command line. Handles device-independent fields
600 * and allows ddx to handle additional fields. It is not allowed to modify
601 * argc or any of the strings pointed to by argv.
604 ProcessCommandLine ( argc, argv )
612 mu_init(&print_lock);
615 defaultKeyboardControl.autoRepeat = TRUE;
618 PartialNetwork = TRUE;
621 for ( i = 1; i < argc; i++ )
623 /* call ddx first, so it can peek/override if it wants */
624 if(skip = ddxProcessArgument(argc, argv, i))
628 else if(argv[i][0] == ':')
630 /* initialize display */
635 else if (strchr(argv[i], ':') != NULL) {
638 XServerHostName = argv[i];
639 if ((p = strchr(argv[i], ':')) != NULL) {
643 } else if (strcmp( argv[i], "-tcp") == 0) {
645 XTcpServerName = argv[i];
650 else if ( strcmp( argv[i], "-a") == 0)
653 defaultPointerControl.num = atoi(argv[i]);
657 else if ( strcmp( argv[i], "-ac") == 0)
659 defeatAccessControl = TRUE;
662 else if ( strcmp( argv[i], "-alloc") == 0)
665 Memory_fail = atoi(argv[i]);
670 else if ( strcmp( argv[i], "-audit") == 0)
673 auditTrailLevel = atoi(argv[i]);
677 else if ( strcmp( argv[i], "-auth") == 0)
680 InitAuthorization (argv[i]);
684 else if ( strcmp( argv[i], "bc") == 0)
685 permitOldBugs = TRUE;
686 else if ( strcmp( argv[i], "-bs") == 0)
687 disableBackingStore = TRUE;
688 else if ( strcmp( argv[i], "c") == 0)
691 defaultKeyboardControl.click = atoi(argv[i]);
695 else if ( strcmp( argv[i], "-c") == 0)
697 defaultKeyboardControl.click = 0;
699 else if ( strcmp( argv[i], "-cc") == 0)
702 defaultColorVisualClass = atoi(argv[i]);
706 else if ( strcmp( argv[i], "-co") == 0)
713 else if ( strcmp( argv[i], "-core") == 0)
715 else if ( strcmp( argv[i], "-dpi") == 0)
718 monitorResolution = atoi(argv[i]);
723 else if ( strcmp( argv[i], "dpms") == 0)
724 DPMSEnabledSwitch = TRUE;
725 else if ( strcmp( argv[i], "-dpms") == 0)
726 DPMSDisabledSwitch = TRUE;
728 else if ( strcmp( argv[i], "-deferglyphs") == 0)
730 if(++i >= argc || !ParseGlyphCachingMode(argv[i]))
733 else if ( strcmp( argv[i], "-f") == 0)
736 defaultKeyboardControl.bell = atoi(argv[i]);
740 else if ( strcmp( argv[i], "-fc") == 0)
743 defaultCursorFont = argv[i];
747 else if ( strcmp( argv[i], "-fn") == 0)
750 defaultTextFont = argv[i];
754 else if ( strcmp( argv[i], "-fp") == 0)
759 userdefinedfontpath = 1;
761 defaultFontPath = argv[i];
766 else if ( strcmp( argv[i], "-help") == 0)
772 else if ( (skip=XkbProcessArguments(argc,argv,i))!=0 ) {
779 else if ( strcmp( argv[i], "-ld") == 0)
783 limitDataSpace = atoi(argv[i]);
784 if (limitDataSpace > 0)
785 limitDataSpace *= 1024;
792 else if ( strcmp( argv[i], "-lf") == 0)
795 limitNoFile = atoi(argv[i]);
801 else if ( strcmp( argv[i], "-ls") == 0)
805 limitStackSpace = atoi(argv[i]);
806 if (limitStackSpace > 0)
807 limitStackSpace *= 1024;
814 else if ( strcmp ( argv[i], "-nolock") == 0)
820 else if ( strcmp( argv[i], "-logo") == 0)
824 else if ( strcmp( argv[i], "nologo") == 0)
829 else if ( strcmp( argv[i], "-nolisten") == 0)
832 protNoListen = argv[i];
836 else if ( strcmp( argv[i], "-p") == 0)
839 defaultScreenSaverInterval = ((CARD32)atoi(argv[i])) *
844 else if ( strcmp( argv[i], "-pn") == 0)
845 PartialNetwork = TRUE;
846 else if ( strcmp( argv[i], "-nopn") == 0)
847 PartialNetwork = FALSE;
848 else if ( strcmp( argv[i], "r") == 0)
849 defaultKeyboardControl.autoRepeat = TRUE;
850 else if ( strcmp( argv[i], "-r") == 0)
851 defaultKeyboardControl.autoRepeat = FALSE;
852 else if ( strcmp( argv[i], "-s") == 0)
855 defaultScreenSaverTime = ((CARD32)atoi(argv[i])) *
860 else if ( strcmp( argv[i], "-su") == 0)
861 disableSaveUnders = TRUE;
862 else if ( strcmp( argv[i], "-t") == 0)
865 defaultPointerControl.threshold = atoi(argv[i]);
869 else if ( strcmp( argv[i], "-terminate") == 0)
871 extern Bool terminateAtReset;
873 terminateAtReset = TRUE;
875 else if ( strcmp( argv[i], "-to") == 0)
878 TimeOutValue = ((CARD32)atoi(argv[i])) * MILLI_PER_SECOND;
882 else if ( strcmp( argv[i], "-tst") == 0)
884 noTestExtensions = TRUE;
886 else if ( strcmp( argv[i], "v") == 0)
887 defaultScreenSaverBlanking = PreferBlanking;
888 else if ( strcmp( argv[i], "-v") == 0)
889 defaultScreenSaverBlanking = DontPreferBlanking;
890 else if ( strcmp( argv[i], "-wm") == 0)
891 defaultBackingStore = WhenMapped;
892 else if ( strcmp( argv[i], "-x") == 0)
896 /* For U**x, which doesn't support dynamic loading, there's nothing
897 * to do when we see a -x. Either the extension is linked in or
900 else if ( strcmp( argv[i], "-I") == 0)
902 /* ignore all remaining arguments */
905 else if (strncmp (argv[i], "tty", 3) == 0)
907 /* just in case any body is interested */
908 dev_tty_from_init = argv[i];
911 else if ((skip = XdmcpOptions(argc, argv, i)) != i)
917 else if ((skip = XprintOptions(argc, argv, i)) != i)
923 else if ((skip = XSecurityOptions(argc, argv, i)) != i)
930 ErrorF("Unrecognized option: %s\n", argv[i]);
939 InsertFileIntoCommandLine(resargc, resargv, prefix_argc, prefix_argv,
940 filename, suffix_argc, suffix_argv)
958 f = fopen(filename, "r");
960 FatalError("Can't open option file %s\n", filename);
962 fstat(fileno(f), &st);
964 buf = (char *) xalloc((unsigned) st.st_size + 1);
966 FatalError("Out of Memory\n");
968 len = fread(buf, 1, (unsigned) st.st_size, f);
973 FatalError("Error reading option file %s\n", filename);
989 while (*p && *p != '\n')
993 while (*p && !isspace(*p))
995 /* Since p and q might still be pointing at the same place, we */
996 /* need to step p over the whitespace now before we add the null. */
1004 buf = (char *) xrealloc(buf, q - buf);
1006 FatalError("Out of memory reallocing option buf\n");
1008 *resargc = prefix_argc + insert_argc + suffix_argc;
1009 *resargv = (char **) xalloc((*resargc + 1) * sizeof(char *));
1011 memcpy(*resargv, prefix_argv, prefix_argc * sizeof(char *));
1014 for (i = 0; i < insert_argc; i++)
1016 (*resargv)[prefix_argc + i] = p;
1020 memcpy(*resargv + prefix_argc + insert_argc,
1021 suffix_argv, suffix_argc * sizeof(char *));
1023 (*resargv)[*resargc] = NULL;
1024 } /* end InsertFileIntoCommandLine */
1027 ExpandCommandLine(pargc, pargv)
1033 for (i = 1; i < *pargc; i++)
1035 if ( (0 == strcmp((*pargv)[i], "-config")) && (i < (*pargc - 1)) )
1037 InsertFileIntoCommandLine(pargc, pargv,
1039 (*pargv)[i+1], /* filename */
1040 *pargc - i - 2, *pargv + i + 2);
1044 } /* end ExpandCommandLine */
1047 #if defined(TCPCONN) || defined(STREAMSCONN)
1053 /* Implement a simple-minded font authorization scheme. The authorization
1054 name is "hp-hostname-1", the contents are simply the host name. */
1056 set_font_authorizations(authorizations, authlen, client)
1057 char **authorizations;
1061 #define AUTHORIZATION_NAME "hp-hostname-1"
1062 #if defined(TCPCONN) || defined(STREAMSCONN)
1063 static char result[1024];
1064 static char *p = NULL;
1068 char hname[1024], *hnameptr;
1069 struct hostent *host;
1072 gethostname(hname, 1024);
1073 host = gethostbyname(hname);
1077 hnameptr = host->h_name;
1080 *p++ = sizeof(AUTHORIZATION_NAME) >> 8;
1081 *p++ = sizeof(AUTHORIZATION_NAME) & 0xff;
1082 *p++ = (len = strlen(hnameptr) + 1) >> 8;
1083 *p++ = (len & 0xff);
1085 memmove(p, AUTHORIZATION_NAME, sizeof(AUTHORIZATION_NAME));
1086 p += sizeof(AUTHORIZATION_NAME);
1087 memmove(p, hnameptr, len);
1090 *authlen = p - result;
1091 *authorizations = result;
1095 #endif /* TCPCONN */
1098 /* XALLOC -- X's internal memory allocator. Why does it return unsigned
1099 * long * instead of the more common char *? Well, if you read K&R you'll
1100 * see they say that alloc must return a pointer "suitable for conversion"
1101 * to whatever type you really want. In a full-blown generic allocator
1102 * there's no way to solve the alignment problems without potentially
1103 * wasting lots of space. But we have a more limited problem. We know
1104 * we're only ever returning pointers to structures which will have to
1105 * be long word aligned. So we are making a stronger guarantee. It might
1106 * have made sense to make Xalloc return char * to conform with people's
1107 * expectations of malloc, but this makes lint happier.
1110 #ifndef INTERNAL_MALLOC
1114 unsigned long amount;
1116 #if !defined(__STDC__) && !defined(AMOEBA)
1119 register pointer ptr;
1121 if ((long)amount <= 0) {
1122 return (unsigned long *)NULL;
1124 /* aligned extra on long word boundary */
1125 amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
1127 if (!Must_have_memory && Memory_fail &&
1128 ((random() % MEM_FAIL_SCALE) < Memory_fail))
1129 return (unsigned long *)NULL;
1131 if (ptr = (pointer)malloc(amount)) {
1132 return (unsigned long *)ptr;
1134 if (Must_have_memory)
1135 FatalError("Out of memory");
1136 return (unsigned long *)NULL;
1141 * "no failure" realloc, alternate interface to Xalloc w/o Must_have_memory
1146 unsigned long amount;
1148 #if !defined(__STDC__) && !defined(AMOEBA)
1151 register pointer ptr;
1153 if ((long)amount <= 0)
1155 return (unsigned long *)NULL;
1157 /* aligned extra on long word boundary */
1158 amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
1159 ptr = (pointer)malloc(amount);
1162 FatalError("Out of memory");
1164 return ((unsigned long *)ptr);
1173 unsigned long amount;
1177 ret = Xalloc (amount);
1179 bzero ((char *) ret, (int) amount);
1188 Xrealloc (ptr, amount)
1189 register pointer ptr;
1190 unsigned long amount;
1192 #if !defined(__STDC__) && !defined(AMOEBA)
1198 if (!Must_have_memory && Memory_fail &&
1199 ((random() % MEM_FAIL_SCALE) < Memory_fail))
1200 return (unsigned long *)NULL;
1202 if ((long)amount <= 0)
1206 return (unsigned long *)NULL;
1208 amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
1210 ptr = (pointer)realloc((char *)ptr, amount);
1212 ptr = (pointer)malloc(amount);
1214 return (unsigned long *)ptr;
1215 if (Must_have_memory)
1216 FatalError("Out of memory");
1217 return (unsigned long *)NULL;
1222 * "no failure" realloc, alternate interface to Xrealloc w/o Must_have_memory
1226 XNFrealloc (ptr, amount)
1227 register pointer ptr;
1228 unsigned long amount;
1230 if (( ptr = (pointer)Xrealloc( ptr, amount ) ) == NULL)
1232 FatalError( "Out of memory" );
1234 return ((unsigned long *)ptr);
1244 register pointer ptr;
1254 static int been_here;
1256 /* Check the memory system after each generation */
1269 #ifdef X_NOT_STDC_ENV
1278 autime = ctime(&tm);
1279 if (s = strchr(autime, '\n'))
1281 if (s = strrchr(argvGlobal[0], '/'))
1285 ErrorF("AUDIT: %s: %d %s: ", autime, getpid(), s);
1292 #if NeedVarargsPrototypes
1295 f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
1297 char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
1300 #if NeedVarargsPrototypes
1306 #if NeedVarargsPrototypes
1311 ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
1318 #if NeedVarargsPrototypes
1321 f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
1323 char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
1326 #if NeedVarargsPrototypes
1329 ErrorF("\nFatal server error:\n");
1330 #if NeedVarargsPrototypes
1335 ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
1338 #ifdef DDXOSFATALERROR
1339 OsVendorFatalError();
1345 #if NeedVarargsPrototypes
1351 vfprintf(stderr, f, args);
1358 #if NeedVarargsPrototypes
1361 f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
1363 char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
1366 #if NeedVarargsPrototypes
1373 mu_lock(&print_lock);
1375 fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
1377 mu_unlock(&print_lock);
1382 #if !defined(WIN32) && !defined(__EMX__)
1384 * "safer" versions of system(3), popen(3) and pclose(3) which give up
1385 * all privs before running a command.
1387 * This is based on the code in FreeBSD 2.2 libc.
1402 csig = signal(SIGCHLD, SIG_DFL);
1405 ErrorF("System: `%s'\n", command);
1407 switch (pid = fork()) {
1408 case -1: /* error */
1413 execl("/bin/sh", "sh", "-c", command, (char *)NULL);
1415 default: /* parent */
1417 p = waitpid(pid, &status, 0);
1418 } while (p == -1 && errno == EINTR);
1423 signal(SIGCHLD, csig);
1426 return p == -1 ? -1 : status;
1436 Popen(command, type)
1445 if (command == NULL || type == NULL)
1448 if ((*type != 'r' && *type != 'w') || type[1])
1451 if ((cur = (struct pid *)xalloc(sizeof(struct pid))) == NULL)
1454 if (pipe(pdes) < 0) {
1459 switch (pid = fork()) {
1460 case -1: /* error */
1483 execl("/bin/sh", "sh", "-c", command, (char *)NULL);
1489 iop = fdopen(pdes[0], type);
1492 iop = fdopen(pdes[1], type);
1498 cur->next = pidlist;
1502 ErrorF("Popen: `%s', fp = %p\n", command, iop);
1512 struct pid *cur, *last;
1518 ErrorF("Pclose: fp = %p\n", iop);
1523 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
1530 pid = waitpid(cur->pid, &pstat, 0);
1531 } while (pid == -1 && errno == EINTR);
1534 pidlist = cur->next;
1536 last->next = cur->next;
1539 return pid == -1 ? -1 : pstat;
1541 #endif /* !WIN32 && !__EMX__ */