VirtualBox

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

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

_msize doesn't like NULL pointers.

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