VirtualBox

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

Last change on this file since 1453 was 1438, checked in by bird, 17 years ago

Added $(make-stats ) that provides access to memory and hash stats (if CONFIG_WITH_MAKE_STATS is defined).

  • Property svn:eol-style set to native
File size: 22.3 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 make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
366 make_stats_allocated_sum += SIZE_OF_HEAP_BLOCK (result);
367#endif
368 return result;
369}
370
371
372void *
373xrealloc (void *ptr, unsigned int size)
374{
375 void *result;
376#ifdef CONFIG_WITH_MAKE_STATS
377 make_stats_allocated -= SIZE_OF_HEAP_BLOCK (ptr);
378 make_stats_allocated_sum -= SIZE_OF_HEAP_BLOCK (ptr);
379#endif
380
381 /* Some older implementations of realloc() don't conform to ANSI. */
382 if (! size)
383 size = 1;
384 result = ptr ? realloc (ptr, size) : malloc (size);
385 if (result == 0)
386 fatal (NILF, _("virtual memory exhausted"));
387#ifdef CONFIG_WITH_MAKE_STATS
388 make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
389 make_stats_allocated_sum += SIZE_OF_HEAP_BLOCK (result);
390#endif
391 return result;
392}
393
394
395char *
396xstrdup (const char *ptr)
397{
398 char *result;
399
400#ifdef HAVE_STRDUP
401 result = strdup (ptr);
402#else
403 result = malloc (strlen (ptr) + 1);
404#endif
405
406 if (result == 0)
407 fatal (NILF, _("virtual memory exhausted"));
408
409#ifdef CONFIG_WITH_MAKE_STATS
410 make_stats_allocations++;
411 make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
412 make_stats_allocated_sum += SIZE_OF_HEAP_BLOCK (result);
413#endif
414#ifdef HAVE_STRDUP
415 return result;
416#else
417 return strcpy (result, ptr);
418#endif
419}
420
421#endif /* HAVE_DMALLOC_H */
422
423char *
424savestring (const char *str, unsigned int length)
425{
426 char *out = xmalloc (length + 1);
427 if (length > 0)
428 memcpy (out, str, length);
429 out[length] = '\0';
430 return out;
431}
432
433
434
435#ifndef CONFIG_WITH_OPTIMIZATION_HACKS /* This is really a reimplemntation of
436 memchr, only slower. It's been replaced by a macro in the header file. */
437
438/* Limited INDEX:
439 Search through the string STRING, which ends at LIMIT, for the character C.
440 Returns a pointer to the first occurrence, or nil if none is found.
441 Like INDEX except that the string searched ends where specified
442 instead of at the first null. */
443
444char *
445lindex (const char *s, const char *limit, int c)
446{
447 while (s < limit)
448 if (*s++ == c)
449 return (char *)(s - 1);
450
451 return 0;
452}
453#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
454
455
456/* Return the address of the first whitespace or null in the string S. */
457
458char *
459end_of_token (const char *s)
460{
461 while (*s != '\0' && !isblank ((unsigned char)*s))
462 ++s;
463 return (char *)s;
464}
465
466#ifdef WINDOWS32
467/*
468 * Same as end_of_token, but take into account a stop character
469 */
470char *
471end_of_token_w32 (const char *s, char stopchar)
472{
473 const char *p = s;
474 int backslash = 0;
475
476 while (*p != '\0' && *p != stopchar
477 && (backslash || !isblank ((unsigned char)*p)))
478 {
479 if (*p++ == '\\')
480 {
481 backslash = !backslash;
482 while (*p == '\\')
483 {
484 backslash = !backslash;
485 ++p;
486 }
487 }
488 else
489 backslash = 0;
490 }
491
492 return (char *)p;
493}
494#endif
495
496/* Return the address of the first nonwhitespace or null in the string S. */
497
498char *
499next_token (const char *s)
500{
501 while (isblank ((unsigned char)*s))
502 ++s;
503 return (char *)s;
504}
505
506/* Find the next token in PTR; return the address of it, and store the length
507 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
508 of the token, so this function can be called repeatedly in a loop. */
509
510char *
511find_next_token (const char **ptr, unsigned int *lengthptr)
512{
513 const char *p = next_token (*ptr);
514
515 if (*p == '\0')
516 return 0;
517
518 *ptr = end_of_token (p);
519 if (lengthptr != 0)
520 *lengthptr = *ptr - p;
521
522 return (char *)p;
523}
524
525
526
527/* Allocate a new `struct dep' with all fields initialized to 0. */
528
529struct dep *
530alloc_dep ()
531{
532 struct dep *d = xmalloc (sizeof (struct dep));
533 memset (d, '\0', sizeof (struct dep));
534 return d;
535}
536
537
538/* Free `struct dep' along with `name' and `stem'. */
539
540void
541free_dep (struct dep *d)
542{
543 free (d);
544}
545
546/* Copy a chain of `struct dep', making a new chain
547 with the same contents as the old one. */
548
549struct dep *
550copy_dep_chain (const struct dep *d)
551{
552 struct dep *firstnew = 0;
553 struct dep *lastnew = 0;
554
555 while (d != 0)
556 {
557 struct dep *c = xmalloc (sizeof (struct dep));
558 memcpy (c, d, sizeof (struct dep));
559
560 c->next = 0;
561 if (firstnew == 0)
562 firstnew = lastnew = c;
563 else
564 lastnew = lastnew->next = c;
565
566 d = d->next;
567 }
568
569 return firstnew;
570}
571
572/* Free a chain of 'struct dep'. */
573
574void
575free_dep_chain (struct dep *d)
576{
577 while (d != 0)
578 {
579 struct dep *df = d;
580 d = d->next;
581 free_dep (df);
582 }
583}
584
585/* Free a chain of struct nameseq.
586 For struct dep chains use free_dep_chain. */
587
588void
589free_ns_chain (struct nameseq *ns)
590{
591 while (ns != 0)
592 {
593 struct nameseq *t = ns;
594 ns = ns->next;
595 free (t);
596 }
597}
598
599
600
601#if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
602
603/* If we don't have strcasecmp() (from POSIX), or anything that can substitute
604 for it, define our own version. */
605
606int
607strcasecmp (const char *s1, const char *s2)
608{
609 while (1)
610 {
611 int c1 = (int) *(s1++);
612 int c2 = (int) *(s2++);
613
614 if (isalpha (c1))
615 c1 = tolower (c1);
616 if (isalpha (c2))
617 c2 = tolower (c2);
618
619 if (c1 != '\0' && c1 == c2)
620 continue;
621
622 return (c1 - c2);
623 }
624}
625#endif
626
627
628#ifdef GETLOADAVG_PRIVILEGED
629
630#ifdef POSIX
631
632/* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
633 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
634 for example) which claim to be POSIX.1 also have the BSD setreuid and
635 setregid functions, but they don't work as in BSD and only the POSIX.1
636 way works. */
637
638#undef HAVE_SETREUID
639#undef HAVE_SETREGID
640
641#else /* Not POSIX. */
642
643/* Some POSIX.1 systems have the seteuid and setegid functions. In a
644 POSIX-like system, they are the best thing to use. However, some
645 non-POSIX systems have them too but they do not work in the POSIX style
646 and we must use setreuid and setregid instead. */
647
648#undef HAVE_SETEUID
649#undef HAVE_SETEGID
650
651#endif /* POSIX. */
652
653#ifndef HAVE_UNISTD_H
654extern int getuid (), getgid (), geteuid (), getegid ();
655extern int setuid (), setgid ();
656#ifdef HAVE_SETEUID
657extern int seteuid ();
658#else
659#ifdef HAVE_SETREUID
660extern int setreuid ();
661#endif /* Have setreuid. */
662#endif /* Have seteuid. */
663#ifdef HAVE_SETEGID
664extern int setegid ();
665#else
666#ifdef HAVE_SETREGID
667extern int setregid ();
668#endif /* Have setregid. */
669#endif /* Have setegid. */
670#endif /* No <unistd.h>. */
671
672/* Keep track of the user and group IDs for user- and make- access. */
673static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
674#define access_inited (user_uid != -1)
675static enum { make, user } current_access;
676
677
678/* Under -d, write a message describing the current IDs. */
679
680static void
681log_access (const char *flavor)
682{
683 if (! ISDB (DB_JOBS))
684 return;
685
686 /* All the other debugging messages go to stdout,
687 but we write this one to stderr because it might be
688 run in a child fork whose stdout is piped. */
689
690 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
691 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
692 (unsigned long) getegid (), (unsigned long) getgid ());
693 fflush (stderr);
694}
695
696
697static void
698init_access (void)
699{
700#ifndef VMS
701 user_uid = getuid ();
702 user_gid = getgid ();
703
704 make_uid = geteuid ();
705 make_gid = getegid ();
706
707 /* Do these ever fail? */
708 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
709 pfatal_with_name ("get{e}[gu]id");
710
711 log_access (_("Initialized access"));
712
713 current_access = make;
714#endif
715}
716
717#endif /* GETLOADAVG_PRIVILEGED */
718
719/* Give the process appropriate permissions for access to
720 user data (i.e., to stat files, or to spawn a child process). */
721void
722user_access (void)
723{
724#ifdef GETLOADAVG_PRIVILEGED
725
726 if (!access_inited)
727 init_access ();
728
729 if (current_access == user)
730 return;
731
732 /* We are in "make access" mode. This means that the effective user and
733 group IDs are those of make (if it was installed setuid or setgid).
734 We now want to set the effective user and group IDs to the real IDs,
735 which are the IDs of the process that exec'd make. */
736
737#ifdef HAVE_SETEUID
738
739 /* Modern systems have the seteuid/setegid calls which set only the
740 effective IDs, which is ideal. */
741
742 if (seteuid (user_uid) < 0)
743 pfatal_with_name ("user_access: seteuid");
744
745#else /* Not HAVE_SETEUID. */
746
747#ifndef HAVE_SETREUID
748
749 /* System V has only the setuid/setgid calls to set user/group IDs.
750 There is an effective ID, which can be set by setuid/setgid.
751 It can be set (unless you are root) only to either what it already is
752 (returned by geteuid/getegid, now in make_uid/make_gid),
753 the real ID (return by getuid/getgid, now in user_uid/user_gid),
754 or the saved set ID (what the effective ID was before this set-ID
755 executable (make) was exec'd). */
756
757 if (setuid (user_uid) < 0)
758 pfatal_with_name ("user_access: setuid");
759
760#else /* HAVE_SETREUID. */
761
762 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
763 They may be set to themselves or each other. So you have two alternatives
764 at any one time. If you use setuid/setgid, the effective will be set to
765 the real, leaving only one alternative. Using setreuid/setregid, however,
766 you can toggle between your two alternatives by swapping the values in a
767 single setreuid or setregid call. */
768
769 if (setreuid (make_uid, user_uid) < 0)
770 pfatal_with_name ("user_access: setreuid");
771
772#endif /* Not HAVE_SETREUID. */
773#endif /* HAVE_SETEUID. */
774
775#ifdef HAVE_SETEGID
776 if (setegid (user_gid) < 0)
777 pfatal_with_name ("user_access: setegid");
778#else
779#ifndef HAVE_SETREGID
780 if (setgid (user_gid) < 0)
781 pfatal_with_name ("user_access: setgid");
782#else
783 if (setregid (make_gid, user_gid) < 0)
784 pfatal_with_name ("user_access: setregid");
785#endif
786#endif
787
788 current_access = user;
789
790 log_access (_("User access"));
791
792#endif /* GETLOADAVG_PRIVILEGED */
793}
794
795/* Give the process appropriate permissions for access to
796 make data (i.e., the load average). */
797void
798make_access (void)
799{
800#ifdef GETLOADAVG_PRIVILEGED
801
802 if (!access_inited)
803 init_access ();
804
805 if (current_access == make)
806 return;
807
808 /* See comments in user_access, above. */
809
810#ifdef HAVE_SETEUID
811 if (seteuid (make_uid) < 0)
812 pfatal_with_name ("make_access: seteuid");
813#else
814#ifndef HAVE_SETREUID
815 if (setuid (make_uid) < 0)
816 pfatal_with_name ("make_access: setuid");
817#else
818 if (setreuid (user_uid, make_uid) < 0)
819 pfatal_with_name ("make_access: setreuid");
820#endif
821#endif
822
823#ifdef HAVE_SETEGID
824 if (setegid (make_gid) < 0)
825 pfatal_with_name ("make_access: setegid");
826#else
827#ifndef HAVE_SETREGID
828 if (setgid (make_gid) < 0)
829 pfatal_with_name ("make_access: setgid");
830#else
831 if (setregid (user_gid, make_gid) < 0)
832 pfatal_with_name ("make_access: setregid");
833#endif
834#endif
835
836 current_access = make;
837
838 log_access (_("Make access"));
839
840#endif /* GETLOADAVG_PRIVILEGED */
841}
842
843/* Give the process appropriate permissions for a child process.
844 This is like user_access, but you can't get back to make_access. */
845void
846child_access (void)
847{
848#ifdef GETLOADAVG_PRIVILEGED
849
850 if (!access_inited)
851 abort ();
852
853 /* Set both the real and effective UID and GID to the user's.
854 They cannot be changed back to make's. */
855
856#ifndef HAVE_SETREUID
857 if (setuid (user_uid) < 0)
858 pfatal_with_name ("child_access: setuid");
859#else
860 if (setreuid (user_uid, user_uid) < 0)
861 pfatal_with_name ("child_access: setreuid");
862#endif
863
864#ifndef HAVE_SETREGID
865 if (setgid (user_gid) < 0)
866 pfatal_with_name ("child_access: setgid");
867#else
868 if (setregid (user_gid, user_gid) < 0)
869 pfatal_with_name ("child_access: setregid");
870#endif
871
872 log_access (_("Child access"));
873
874#endif /* GETLOADAVG_PRIVILEGED */
875}
876
877
878#ifdef NEED_GET_PATH_MAX
879unsigned int
880get_path_max (void)
881{
882 static unsigned int value;
883
884 if (value == 0)
885 {
886 long int x = pathconf ("/", _PC_PATH_MAX);
887 if (x > 0)
888 value = x;
889 else
890 return MAXPATHLEN;
891 }
892
893 return value;
894}
895#endif
896
897
898
899/* This code is stolen from gnulib.
900 If/when we abandon the requirement to work with K&R compilers, we can
901 remove this (and perhaps other parts of GNU make!) and migrate to using
902 gnulib directly.
903
904 This is called only through atexit(), which means die() has already been
905 invoked. So, call exit() here directly. Apparently that works...?
906*/
907
908/* Close standard output, exiting with status 'exit_failure' on failure.
909 If a program writes *anything* to stdout, that program should close
910 stdout and make sure that it succeeds before exiting. Otherwise,
911 suppose that you go to the extreme of checking the return status
912 of every function that does an explicit write to stdout. The last
913 printf can succeed in writing to the internal stream buffer, and yet
914 the fclose(stdout) could still fail (due e.g., to a disk full error)
915 when it tries to write out that buffered data. Thus, you would be
916 left with an incomplete output file and the offending program would
917 exit successfully. Even calling fflush is not always sufficient,
918 since some file systems (NFS and CODA) buffer written/flushed data
919 until an actual close call.
920
921 Besides, it's wasteful to check the return value from every call
922 that writes to stdout -- just let the internal stream state record
923 the failure. That's what the ferror test is checking below.
924
925 It's important to detect such failures and exit nonzero because many
926 tools (most notably `make' and other build-management systems) depend
927 on being able to detect failure in other tools via their exit status. */
928
929void
930close_stdout (void)
931{
932 int prev_fail = ferror (stdout);
933 int fclose_fail = fclose (stdout);
934
935 if (prev_fail || fclose_fail)
936 {
937 if (fclose_fail)
938 error (NILF, _("write error: %s"), strerror (errno));
939 else
940 error (NILF, _("write error"));
941 exit (EXIT_FAILURE);
942 }
943}
944
945#if defined(CONFIG_WITH_MAKE_STATS) && !defined(ELECTRIC_HEAP)
946#undef free
947void xfree(void *ptr)
948{
949 if (ptr)
950 {
951 make_stats_allocations--;
952 make_stats_allocated -= SIZE_OF_HEAP_BLOCK (ptr);
953 free (ptr);
954 }
955}
956#endif
957
Note: See TracBrowser for help on using the repository browser.

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