VirtualBox

source: kBuild/trunk/src/kmk/misc.c@ 1536

Last change on this file since 1536 was 1535, checked in by bird, 17 years ago

Made the allocated and allocated-sum optional as they made xmalloc and xfree very expensive. New argument: --statistics / KMK_OPTS_STATISTICS=[1|0]

  • Property svn:eol-style set to native
File size: 22.7 KB
Line 
1/* Miscellaneous generic support functions for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20#include "dep.h"
21#include "debug.h"
22
23/* All bcopy calls in this file can be replaced by memcpy and save a tick or two. */
24#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
25# undef bcopy
26# if defined(__GNUC__) && defined(CONFIG_WITH_OPTIMIZATION_HACKS)
27# define bcopy(src, dst, size) __builtin_memcpy ((dst), (src), (size))
28# else
29# define bcopy(src, dst, size) memcpy ((dst), (src), (size))
30# endif
31#endif
32
33/* Variadic functions. We go through contortions to allow proper function
34 prototypes for both ANSI and pre-ANSI C compilers, and also for those
35 which support stdarg.h vs. varargs.h, and finally those which have
36 vfprintf(), etc. and those who have _doprnt... or nothing.
37
38 This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
39 VA_END macros used here since we have multiple print functions. */
40
41#if USE_VARIADIC
42# if HAVE_STDARG_H
43# include <stdarg.h>
44# define VA_START(args, lastarg) va_start(args, lastarg)
45# else
46# include <varargs.h>
47# define VA_START(args, lastarg) va_start(args)
48# endif
49# if HAVE_VPRINTF
50# define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
51# else
52# define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
53# endif
54# define VA_END(args) va_end(args)
55#else
56/* We can't use any variadic interface! */
57# define va_alist a1, a2, a3, a4, a5, a6, a7, a8
58# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
59# define VA_START(args, lastarg)
60# define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
61# define VA_END(args)
62#endif
63
64
65/* Compare strings *S1 and *S2.
66 Return negative if the first is less, positive if it is greater,
67 zero if they are equal. */
68
69int
70alpha_compare (const void *v1, const void *v2)
71{
72 const char *s1 = *((char **)v1);
73 const char *s2 = *((char **)v2);
74
75 if (*s1 != *s2)
76 return *s1 - *s2;
77 return strcmp (s1, s2);
78}
79
80
81/* Discard each backslash-newline combination from LINE.
82 Backslash-backslash-newline combinations become backslash-newlines.
83 This is done by copying the text at LINE into itself. */
84
85void
86collapse_continuations (char *line)
87{
88 register char *in, *out, *p;
89 register int backslash;
90 register unsigned int bs_write;
91
92 in = strchr (line, '\n');
93 if (in == 0)
94 return;
95
96 out = in;
97 while (out > line && out[-1] == '\\')
98 --out;
99
100 while (*in != '\0')
101 {
102 /* BS_WRITE gets the number of quoted backslashes at
103 the end just before IN, and BACKSLASH gets nonzero
104 if the next character is quoted. */
105 backslash = 0;
106 bs_write = 0;
107 for (p = in - 1; p >= line && *p == '\\'; --p)
108 {
109 if (backslash)
110 ++bs_write;
111 backslash = !backslash;
112
113 /* It should be impossible to go back this far without exiting,
114 but if we do, we can't get the right answer. */
115 if (in == out - 1)
116 abort ();
117 }
118
119 /* Output the appropriate number of backslashes. */
120 while (bs_write-- > 0)
121 *out++ = '\\';
122
123 /* Skip the newline. */
124 ++in;
125
126 /* If the newline is quoted, discard following whitespace
127 and any preceding whitespace; leave just one space. */
128 if (backslash)
129 {
130 in = next_token (in);
131 while (out > line && isblank ((unsigned char)out[-1]))
132 --out;
133 *out++ = ' ';
134 }
135 else
136 /* If the newline isn't quoted, put it in the output. */
137 *out++ = '\n';
138
139 /* Now copy the following line to the output.
140 Stop when we find backslashes followed by a newline. */
141 while (*in != '\0')
142 if (*in == '\\')
143 {
144 p = in + 1;
145 while (*p == '\\')
146 ++p;
147 if (*p == '\n')
148 {
149 in = p;
150 break;
151 }
152 while (in < p)
153 *out++ = *in++;
154 }
155 else
156 *out++ = *in++;
157 }
158
159 *out = '\0';
160}
161
162
163/* Print N spaces (used in debug for target-depth). */
164
165void
166print_spaces (unsigned int n)
167{
168 while (n-- > 0)
169 putchar (' ');
170}
171
172
173
174/* Return a string whose contents concatenate those of s1, s2, s3.
175 This string lives in static, re-used memory. */
176
177char *
178concat (const char *s1, const char *s2, const char *s3)
179{
180 unsigned int len1, len2, len3;
181 static unsigned int rlen = 0;
182 static char *result = NULL;
183
184 len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0;
185 len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0;
186 len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0;
187
188 if (len1 + len2 + len3 + 1 > rlen)
189 result = xrealloc (result, (rlen = len1 + len2 + len3 + 10));
190
191 if (len1)
192 memcpy (result, s1, len1);
193 if (len2)
194 memcpy (result + len1, s2, len2);
195 if (len3)
196 memcpy (result + len1 + len2, s3, len3);
197
198 result[len1+len2+len3] = '\0';
199
200 return result;
201}
202
203
204/* Print a message on stdout. */
205
206void
207#if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
208message (int prefix, const char *fmt, ...)
209#else
210message (prefix, fmt, va_alist)
211 int prefix;
212 const char *fmt;
213 va_dcl
214#endif
215{
216#if USE_VARIADIC
217 va_list args;
218#endif
219
220 log_working_directory (1);
221
222 if (fmt != 0)
223 {
224 if (prefix)
225 {
226 if (makelevel == 0)
227 printf ("%s: ", program);
228 else
229 printf ("%s[%u]: ", program, makelevel);
230 }
231 VA_START (args, fmt);
232 VA_PRINTF (stdout, fmt, args);
233 VA_END (args);
234 putchar ('\n');
235 }
236
237 fflush (stdout);
238}
239
240/* Print an error message. */
241
242void
243#if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
244error (const struct floc *flocp, const char *fmt, ...)
245#else
246error (flocp, fmt, va_alist)
247 const struct floc *flocp;
248 const char *fmt;
249 va_dcl
250#endif
251{
252#if USE_VARIADIC
253 va_list args;
254#endif
255
256 log_working_directory (1);
257
258 if (flocp && flocp->filenm)
259 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
260 else if (makelevel == 0)
261 fprintf (stderr, "%s: ", program);
262 else
263 fprintf (stderr, "%s[%u]: ", program, makelevel);
264
265 VA_START(args, fmt);
266 VA_PRINTF (stderr, fmt, args);
267 VA_END (args);
268
269 putc ('\n', stderr);
270 fflush (stderr);
271}
272
273/* Print an error message and exit. */
274
275void
276#if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
277fatal (const struct floc *flocp, const char *fmt, ...)
278#else
279fatal (flocp, fmt, va_alist)
280 const struct floc *flocp;
281 const char *fmt;
282 va_dcl
283#endif
284{
285#if USE_VARIADIC
286 va_list args;
287#endif
288
289 log_working_directory (1);
290
291 if (flocp && flocp->filenm)
292 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
293 else if (makelevel == 0)
294 fprintf (stderr, "%s: *** ", program);
295 else
296 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
297
298 VA_START(args, fmt);
299 VA_PRINTF (stderr, fmt, args);
300 VA_END (args);
301
302 fputs (_(". Stop.\n"), stderr);
303
304 die (2);
305}
306
307#ifndef HAVE_STRERROR
308
309#undef strerror
310
311char *
312strerror (int errnum)
313{
314 extern int errno, sys_nerr;
315#ifndef __DECC
316 extern char *sys_errlist[];
317#endif
318 static char buf[] = "Unknown error 12345678901234567890";
319
320 if (errno < sys_nerr)
321 return sys_errlist[errnum];
322
323 sprintf (buf, _("Unknown error %d"), errnum);
324 return buf;
325}
326#endif
327
328/* Print an error message from errno. */
329
330void
331perror_with_name (const char *str, const char *name)
332{
333 error (NILF, _("%s%s: %s"), str, name, strerror (errno));
334}
335
336/* Print an error message from errno and exit. */
337
338void
339pfatal_with_name (const char *name)
340{
341 fatal (NILF, _("%s: %s"), name, strerror (errno));
342
343 /* NOTREACHED */
344}
345
346
347/* Like malloc but get fatal error if memory is exhausted. */
348/* Don't bother if we're using dmalloc; it provides these for us. */
349
350#if !defined(HAVE_DMALLOC_H) && !defined(ELECTRIC_HEAP) /* bird */
351
352#undef xmalloc
353#undef xrealloc
354#undef xstrdup
355
356void *
357xmalloc (unsigned int size)
358{
359 /* Make sure we don't allocate 0, for pre-ANSI libraries. */
360 void *result = malloc (size ? size : 1);
361 if (result == 0)
362 fatal (NILF, _("virtual memory exhausted"));
363#ifdef CONFIG_WITH_MAKE_STATS
364 make_stats_allocations++;
365 if (make_expensive_statistics)
366 {
367 unsigned int actual_size = SIZE_OF_HEAP_BLOCK (result);
368 make_stats_allocated += actual_size;
369 make_stats_allocated_sum += actual_size;
370 }
371#endif
372 return result;
373}
374
375
376void *
377xrealloc (void *ptr, unsigned int size)
378{
379 void *result;
380#ifdef CONFIG_WITH_MAKE_STATS
381 if (make_expensive_statistics && ptr != NULL)
382 {
383 unsigned int actual_size = SIZE_OF_HEAP_BLOCK (ptr);
384 make_stats_allocated -= actual_size;
385 make_stats_allocated_sum -= actual_size;
386 }
387#endif
388
389 /* Some older implementations of realloc() don't conform to ANSI. */
390 if (! size)
391 size = 1;
392 result = ptr ? realloc (ptr, size) : malloc (size);
393 if (result == 0)
394 fatal (NILF, _("virtual memory exhausted"));
395#ifdef CONFIG_WITH_MAKE_STATS
396 if (!ptr)
397 make_stats_allocations++;
398 if (make_expensive_statistics)
399 {
400 unsigned int actual_size = SIZE_OF_HEAP_BLOCK (result);
401 make_stats_allocated += actual_size;
402 make_stats_allocated_sum += actual_size;
403 }
404#endif
405 return result;
406}
407
408
409char *
410xstrdup (const char *ptr)
411{
412 char *result;
413
414#ifdef HAVE_STRDUP
415 result = strdup (ptr);
416#else
417 result = malloc (strlen (ptr) + 1);
418#endif
419
420 if (result == 0)
421 fatal (NILF, _("virtual memory exhausted"));
422
423#ifdef CONFIG_WITH_MAKE_STATS
424 make_stats_allocations++;
425 if (make_expensive_statistics)
426 {
427 unsigned int actual_size = SIZE_OF_HEAP_BLOCK (result);
428 make_stats_allocated += actual_size;
429 make_stats_allocated_sum += actual_size;
430 }
431#endif
432#ifdef HAVE_STRDUP
433 return result;
434#else
435 return strcpy (result, ptr);
436#endif
437}
438
439#endif /* HAVE_DMALLOC_H */
440
441char *
442savestring (const char *str, unsigned int length)
443{
444 char *out = xmalloc (length + 1);
445 if (length > 0)
446 memcpy (out, str, length);
447 out[length] = '\0';
448 return out;
449}
450
451
452
453#ifndef CONFIG_WITH_OPTIMIZATION_HACKS /* This is really a reimplemntation of
454 memchr, only slower. It's been replaced by a macro in the header file. */
455
456/* Limited INDEX:
457 Search through the string STRING, which ends at LIMIT, for the character C.
458 Returns a pointer to the first occurrence, or nil if none is found.
459 Like INDEX except that the string searched ends where specified
460 instead of at the first null. */
461
462char *
463lindex (const char *s, const char *limit, int c)
464{
465 while (s < limit)
466 if (*s++ == c)
467 return (char *)(s - 1);
468
469 return 0;
470}
471#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
472
473
474/* Return the address of the first whitespace or null in the string S. */
475
476char *
477end_of_token (const char *s)
478{
479 while (*s != '\0' && !isblank ((unsigned char)*s))
480 ++s;
481 return (char *)s;
482}
483
484#ifdef WINDOWS32
485/*
486 * Same as end_of_token, but take into account a stop character
487 */
488char *
489end_of_token_w32 (const char *s, char stopchar)
490{
491 const char *p = s;
492 int backslash = 0;
493
494 while (*p != '\0' && *p != stopchar
495 && (backslash || !isblank ((unsigned char)*p)))
496 {
497 if (*p++ == '\\')
498 {
499 backslash = !backslash;
500 while (*p == '\\')
501 {
502 backslash = !backslash;
503 ++p;
504 }
505 }
506 else
507 backslash = 0;
508 }
509
510 return (char *)p;
511}
512#endif
513
514/* Return the address of the first nonwhitespace or null in the string S. */
515
516char *
517next_token (const char *s)
518{
519 while (isblank ((unsigned char)*s))
520 ++s;
521 return (char *)s;
522}
523
524/* Find the next token in PTR; return the address of it, and store the length
525 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
526 of the token, so this function can be called repeatedly in a loop. */
527
528char *
529find_next_token (const char **ptr, unsigned int *lengthptr)
530{
531 const char *p = next_token (*ptr);
532
533 if (*p == '\0')
534 return 0;
535
536 *ptr = end_of_token (p);
537 if (lengthptr != 0)
538 *lengthptr = *ptr - p;
539
540 return (char *)p;
541}
542
543
544
545/* Allocate a new `struct dep' with all fields initialized to 0. */
546
547struct dep *
548alloc_dep ()
549{
550 struct dep *d = xmalloc (sizeof (struct dep));
551 memset (d, '\0', sizeof (struct dep));
552 return d;
553}
554
555
556/* Free `struct dep' along with `name' and `stem'. */
557
558void
559free_dep (struct dep *d)
560{
561 free (d);
562}
563
564/* Copy a chain of `struct dep', making a new chain
565 with the same contents as the old one. */
566
567struct dep *
568copy_dep_chain (const struct dep *d)
569{
570 struct dep *firstnew = 0;
571 struct dep *lastnew = 0;
572
573 while (d != 0)
574 {
575 struct dep *c = xmalloc (sizeof (struct dep));
576 memcpy (c, d, sizeof (struct dep));
577
578 c->next = 0;
579 if (firstnew == 0)
580 firstnew = lastnew = c;
581 else
582 lastnew = lastnew->next = c;
583
584 d = d->next;
585 }
586
587 return firstnew;
588}
589
590/* Free a chain of 'struct dep'. */
591
592void
593free_dep_chain (struct dep *d)
594{
595 while (d != 0)
596 {
597 struct dep *df = d;
598 d = d->next;
599 free_dep (df);
600 }
601}
602
603/* Free a chain of struct nameseq.
604 For struct dep chains use free_dep_chain. */
605
606void
607free_ns_chain (struct nameseq *ns)
608{
609 while (ns != 0)
610 {
611 struct nameseq *t = ns;
612 ns = ns->next;
613 free (t);
614 }
615}
616
617
618
619#if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
620
621/* If we don't have strcasecmp() (from POSIX), or anything that can substitute
622 for it, define our own version. */
623
624int
625strcasecmp (const char *s1, const char *s2)
626{
627 while (1)
628 {
629 int c1 = (int) *(s1++);
630 int c2 = (int) *(s2++);
631
632 if (isalpha (c1))
633 c1 = tolower (c1);
634 if (isalpha (c2))
635 c2 = tolower (c2);
636
637 if (c1 != '\0' && c1 == c2)
638 continue;
639
640 return (c1 - c2);
641 }
642}
643#endif
644
645
646#ifdef GETLOADAVG_PRIVILEGED
647
648#ifdef POSIX
649
650/* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
651 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
652 for example) which claim to be POSIX.1 also have the BSD setreuid and
653 setregid functions, but they don't work as in BSD and only the POSIX.1
654 way works. */
655
656#undef HAVE_SETREUID
657#undef HAVE_SETREGID
658
659#else /* Not POSIX. */
660
661/* Some POSIX.1 systems have the seteuid and setegid functions. In a
662 POSIX-like system, they are the best thing to use. However, some
663 non-POSIX systems have them too but they do not work in the POSIX style
664 and we must use setreuid and setregid instead. */
665
666#undef HAVE_SETEUID
667#undef HAVE_SETEGID
668
669#endif /* POSIX. */
670
671#ifndef HAVE_UNISTD_H
672extern int getuid (), getgid (), geteuid (), getegid ();
673extern int setuid (), setgid ();
674#ifdef HAVE_SETEUID
675extern int seteuid ();
676#else
677#ifdef HAVE_SETREUID
678extern int setreuid ();
679#endif /* Have setreuid. */
680#endif /* Have seteuid. */
681#ifdef HAVE_SETEGID
682extern int setegid ();
683#else
684#ifdef HAVE_SETREGID
685extern int setregid ();
686#endif /* Have setregid. */
687#endif /* Have setegid. */
688#endif /* No <unistd.h>. */
689
690/* Keep track of the user and group IDs for user- and make- access. */
691static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
692#define access_inited (user_uid != -1)
693static enum { make, user } current_access;
694
695
696/* Under -d, write a message describing the current IDs. */
697
698static void
699log_access (const char *flavor)
700{
701 if (! ISDB (DB_JOBS))
702 return;
703
704 /* All the other debugging messages go to stdout,
705 but we write this one to stderr because it might be
706 run in a child fork whose stdout is piped. */
707
708 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
709 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
710 (unsigned long) getegid (), (unsigned long) getgid ());
711 fflush (stderr);
712}
713
714
715static void
716init_access (void)
717{
718#ifndef VMS
719 user_uid = getuid ();
720 user_gid = getgid ();
721
722 make_uid = geteuid ();
723 make_gid = getegid ();
724
725 /* Do these ever fail? */
726 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
727 pfatal_with_name ("get{e}[gu]id");
728
729 log_access (_("Initialized access"));
730
731 current_access = make;
732#endif
733}
734
735#endif /* GETLOADAVG_PRIVILEGED */
736
737/* Give the process appropriate permissions for access to
738 user data (i.e., to stat files, or to spawn a child process). */
739void
740user_access (void)
741{
742#ifdef GETLOADAVG_PRIVILEGED
743
744 if (!access_inited)
745 init_access ();
746
747 if (current_access == user)
748 return;
749
750 /* We are in "make access" mode. This means that the effective user and
751 group IDs are those of make (if it was installed setuid or setgid).
752 We now want to set the effective user and group IDs to the real IDs,
753 which are the IDs of the process that exec'd make. */
754
755#ifdef HAVE_SETEUID
756
757 /* Modern systems have the seteuid/setegid calls which set only the
758 effective IDs, which is ideal. */
759
760 if (seteuid (user_uid) < 0)
761 pfatal_with_name ("user_access: seteuid");
762
763#else /* Not HAVE_SETEUID. */
764
765#ifndef HAVE_SETREUID
766
767 /* System V has only the setuid/setgid calls to set user/group IDs.
768 There is an effective ID, which can be set by setuid/setgid.
769 It can be set (unless you are root) only to either what it already is
770 (returned by geteuid/getegid, now in make_uid/make_gid),
771 the real ID (return by getuid/getgid, now in user_uid/user_gid),
772 or the saved set ID (what the effective ID was before this set-ID
773 executable (make) was exec'd). */
774
775 if (setuid (user_uid) < 0)
776 pfatal_with_name ("user_access: setuid");
777
778#else /* HAVE_SETREUID. */
779
780 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
781 They may be set to themselves or each other. So you have two alternatives
782 at any one time. If you use setuid/setgid, the effective will be set to
783 the real, leaving only one alternative. Using setreuid/setregid, however,
784 you can toggle between your two alternatives by swapping the values in a
785 single setreuid or setregid call. */
786
787 if (setreuid (make_uid, user_uid) < 0)
788 pfatal_with_name ("user_access: setreuid");
789
790#endif /* Not HAVE_SETREUID. */
791#endif /* HAVE_SETEUID. */
792
793#ifdef HAVE_SETEGID
794 if (setegid (user_gid) < 0)
795 pfatal_with_name ("user_access: setegid");
796#else
797#ifndef HAVE_SETREGID
798 if (setgid (user_gid) < 0)
799 pfatal_with_name ("user_access: setgid");
800#else
801 if (setregid (make_gid, user_gid) < 0)
802 pfatal_with_name ("user_access: setregid");
803#endif
804#endif
805
806 current_access = user;
807
808 log_access (_("User access"));
809
810#endif /* GETLOADAVG_PRIVILEGED */
811}
812
813/* Give the process appropriate permissions for access to
814 make data (i.e., the load average). */
815void
816make_access (void)
817{
818#ifdef GETLOADAVG_PRIVILEGED
819
820 if (!access_inited)
821 init_access ();
822
823 if (current_access == make)
824 return;
825
826 /* See comments in user_access, above. */
827
828#ifdef HAVE_SETEUID
829 if (seteuid (make_uid) < 0)
830 pfatal_with_name ("make_access: seteuid");
831#else
832#ifndef HAVE_SETREUID
833 if (setuid (make_uid) < 0)
834 pfatal_with_name ("make_access: setuid");
835#else
836 if (setreuid (user_uid, make_uid) < 0)
837 pfatal_with_name ("make_access: setreuid");
838#endif
839#endif
840
841#ifdef HAVE_SETEGID
842 if (setegid (make_gid) < 0)
843 pfatal_with_name ("make_access: setegid");
844#else
845#ifndef HAVE_SETREGID
846 if (setgid (make_gid) < 0)
847 pfatal_with_name ("make_access: setgid");
848#else
849 if (setregid (user_gid, make_gid) < 0)
850 pfatal_with_name ("make_access: setregid");
851#endif
852#endif
853
854 current_access = make;
855
856 log_access (_("Make access"));
857
858#endif /* GETLOADAVG_PRIVILEGED */
859}
860
861/* Give the process appropriate permissions for a child process.
862 This is like user_access, but you can't get back to make_access. */
863void
864child_access (void)
865{
866#ifdef GETLOADAVG_PRIVILEGED
867
868 if (!access_inited)
869 abort ();
870
871 /* Set both the real and effective UID and GID to the user's.
872 They cannot be changed back to make's. */
873
874#ifndef HAVE_SETREUID
875 if (setuid (user_uid) < 0)
876 pfatal_with_name ("child_access: setuid");
877#else
878 if (setreuid (user_uid, user_uid) < 0)
879 pfatal_with_name ("child_access: setreuid");
880#endif
881
882#ifndef HAVE_SETREGID
883 if (setgid (user_gid) < 0)
884 pfatal_with_name ("child_access: setgid");
885#else
886 if (setregid (user_gid, user_gid) < 0)
887 pfatal_with_name ("child_access: setregid");
888#endif
889
890 log_access (_("Child access"));
891
892#endif /* GETLOADAVG_PRIVILEGED */
893}
894
895
896#ifdef NEED_GET_PATH_MAX
897unsigned int
898get_path_max (void)
899{
900 static unsigned int value;
901
902 if (value == 0)
903 {
904 long int x = pathconf ("/", _PC_PATH_MAX);
905 if (x > 0)
906 value = x;
907 else
908 return MAXPATHLEN;
909 }
910
911 return value;
912}
913#endif
914
915
916
917/* This code is stolen from gnulib.
918 If/when we abandon the requirement to work with K&R compilers, we can
919 remove this (and perhaps other parts of GNU make!) and migrate to using
920 gnulib directly.
921
922 This is called only through atexit(), which means die() has already been
923 invoked. So, call exit() here directly. Apparently that works...?
924*/
925
926/* Close standard output, exiting with status 'exit_failure' on failure.
927 If a program writes *anything* to stdout, that program should close
928 stdout and make sure that it succeeds before exiting. Otherwise,
929 suppose that you go to the extreme of checking the return status
930 of every function that does an explicit write to stdout. The last
931 printf can succeed in writing to the internal stream buffer, and yet
932 the fclose(stdout) could still fail (due e.g., to a disk full error)
933 when it tries to write out that buffered data. Thus, you would be
934 left with an incomplete output file and the offending program would
935 exit successfully. Even calling fflush is not always sufficient,
936 since some file systems (NFS and CODA) buffer written/flushed data
937 until an actual close call.
938
939 Besides, it's wasteful to check the return value from every call
940 that writes to stdout -- just let the internal stream state record
941 the failure. That's what the ferror test is checking below.
942
943 It's important to detect such failures and exit nonzero because many
944 tools (most notably `make' and other build-management systems) depend
945 on being able to detect failure in other tools via their exit status. */
946
947void
948close_stdout (void)
949{
950 int prev_fail = ferror (stdout);
951 int fclose_fail = fclose (stdout);
952
953 if (prev_fail || fclose_fail)
954 {
955 if (fclose_fail)
956 error (NILF, _("write error: %s"), strerror (errno));
957 else
958 error (NILF, _("write error"));
959 exit (EXIT_FAILURE);
960 }
961}
962
963#if defined(CONFIG_WITH_MAKE_STATS) && !defined(ELECTRIC_HEAP)
964#undef free
965void xfree(void *ptr)
966{
967 if (ptr)
968 {
969 make_stats_allocations--;
970 if (make_expensive_statistics)
971 make_stats_allocated -= SIZE_OF_HEAP_BLOCK (ptr);
972 free (ptr);
973 }
974}
975#endif
976
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette