1 | /* Miscellaneous generic support functions for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
---|
4 | 2010 Free Software Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License along with
|
---|
17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 | #include "dep.h"
|
---|
21 | #include "debug.h"
|
---|
22 | #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_ALLOC_CACHES)
|
---|
23 | # include <assert.h>
|
---|
24 | #endif
|
---|
25 | #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
|
---|
26 | # ifdef __APPLE__
|
---|
27 | # include <malloc/malloc.h>
|
---|
28 | # endif
|
---|
29 | # if defined(__GLIBC__) || defined(HAVE_MALLINFO)
|
---|
30 | # include <malloc.h>
|
---|
31 | # endif
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #if defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_PRINT_TIME_SWITCH)
|
---|
35 | # ifdef WINDOWS32
|
---|
36 | # include <Windows.h>
|
---|
37 | # endif
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | /* All bcopy calls in this file can be replaced by memcpy and save a tick or two. */
|
---|
41 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
42 | # undef bcopy
|
---|
43 | # if defined(__GNUC__) && defined(CONFIG_WITH_OPTIMIZATION_HACKS)
|
---|
44 | # define bcopy(src, dst, size) __builtin_memcpy ((dst), (src), (size))
|
---|
45 | # else
|
---|
46 | # define bcopy(src, dst, size) memcpy ((dst), (src), (size))
|
---|
47 | # endif
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /* Variadic functions. We go through contortions to allow proper function
|
---|
51 | prototypes for both ANSI and pre-ANSI C compilers, and also for those
|
---|
52 | which support stdarg.h vs. varargs.h, and finally those which have
|
---|
53 | vfprintf(), etc. and those who have _doprnt... or nothing.
|
---|
54 |
|
---|
55 | This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
|
---|
56 | VA_END macros used here since we have multiple print functions. */
|
---|
57 |
|
---|
58 | #if USE_VARIADIC
|
---|
59 | # if HAVE_STDARG_H
|
---|
60 | # include <stdarg.h>
|
---|
61 | # define VA_START(args, lastarg) va_start(args, lastarg)
|
---|
62 | # else
|
---|
63 | # include <varargs.h>
|
---|
64 | # define VA_START(args, lastarg) va_start(args)
|
---|
65 | # endif
|
---|
66 | # if HAVE_VPRINTF
|
---|
67 | # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
|
---|
68 | # else
|
---|
69 | # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
|
---|
70 | # endif
|
---|
71 | # define VA_END(args) va_end(args)
|
---|
72 | #else
|
---|
73 | /* We can't use any variadic interface! */
|
---|
74 | # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
|
---|
75 | # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
|
---|
76 | # define VA_START(args, lastarg)
|
---|
77 | # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
|
---|
78 | # define VA_END(args)
|
---|
79 | #endif
|
---|
80 |
|
---|
81 |
|
---|
82 | /* Compare strings *S1 and *S2.
|
---|
83 | Return negative if the first is less, positive if it is greater,
|
---|
84 | zero if they are equal. */
|
---|
85 |
|
---|
86 | int
|
---|
87 | alpha_compare (const void *v1, const void *v2)
|
---|
88 | {
|
---|
89 | const char *s1 = *((char **)v1);
|
---|
90 | const char *s2 = *((char **)v2);
|
---|
91 |
|
---|
92 | if (*s1 != *s2)
|
---|
93 | return *s1 - *s2;
|
---|
94 | return strcmp (s1, s2);
|
---|
95 | }
|
---|
96 | |
---|
97 |
|
---|
98 | /* Discard each backslash-newline combination from LINE.
|
---|
99 | Backslash-backslash-newline combinations become backslash-newlines.
|
---|
100 | This is done by copying the text at LINE into itself. */
|
---|
101 |
|
---|
102 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
103 | void
|
---|
104 | collapse_continuations (char *line)
|
---|
105 | #else
|
---|
106 | char *
|
---|
107 | collapse_continuations (char *line, unsigned int linelen)
|
---|
108 | #endif
|
---|
109 | {
|
---|
110 | register char *in, *out, *p;
|
---|
111 | register int backslash;
|
---|
112 | register unsigned int bs_write;
|
---|
113 |
|
---|
114 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
115 | in = strchr (line, '\n');
|
---|
116 | if (in == 0)
|
---|
117 | return;
|
---|
118 | #else
|
---|
119 | assert (strlen (line) == linelen);
|
---|
120 | in = memchr (line, '\n', linelen);
|
---|
121 | if (in == 0)
|
---|
122 | return line + linelen;
|
---|
123 | if (in == line || in[-1] != '\\')
|
---|
124 | {
|
---|
125 | do
|
---|
126 | {
|
---|
127 | unsigned int off_in = in - line;
|
---|
128 | if (off_in == linelen)
|
---|
129 | return in;
|
---|
130 | in = memchr (in + 1, '\n', linelen - off_in - 1);
|
---|
131 | if (in == 0)
|
---|
132 | return line + linelen;
|
---|
133 | }
|
---|
134 | while (in[-1] != '\\');
|
---|
135 | }
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | out = in;
|
---|
139 | while (out > line && out[-1] == '\\')
|
---|
140 | --out;
|
---|
141 |
|
---|
142 | while (*in != '\0')
|
---|
143 | {
|
---|
144 | /* BS_WRITE gets the number of quoted backslashes at
|
---|
145 | the end just before IN, and BACKSLASH gets nonzero
|
---|
146 | if the next character is quoted. */
|
---|
147 | backslash = 0;
|
---|
148 | bs_write = 0;
|
---|
149 | for (p = in - 1; p >= line && *p == '\\'; --p)
|
---|
150 | {
|
---|
151 | if (backslash)
|
---|
152 | ++bs_write;
|
---|
153 | backslash = !backslash;
|
---|
154 |
|
---|
155 | /* It should be impossible to go back this far without exiting,
|
---|
156 | but if we do, we can't get the right answer. */
|
---|
157 | if (in == out - 1)
|
---|
158 | abort ();
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* Output the appropriate number of backslashes. */
|
---|
162 | while (bs_write-- > 0)
|
---|
163 | *out++ = '\\';
|
---|
164 |
|
---|
165 | /* Skip the newline. */
|
---|
166 | ++in;
|
---|
167 |
|
---|
168 | /* If the newline is escaped, discard following whitespace leaving just
|
---|
169 | one space. POSIX requires that each backslash/newline/following
|
---|
170 | whitespace sequence be reduced to a single space. */
|
---|
171 | if (backslash)
|
---|
172 | {
|
---|
173 | in = next_token (in);
|
---|
174 | /* Removing this loop will fix Savannah bug #16670: do we want to? */
|
---|
175 | while (out > line && isblank ((unsigned char)out[-1]))
|
---|
176 | --out;
|
---|
177 | *out++ = ' ';
|
---|
178 | }
|
---|
179 | else
|
---|
180 | /* If the newline isn't quoted, put it in the output. */
|
---|
181 | *out++ = '\n';
|
---|
182 |
|
---|
183 | /* Now copy the following line to the output.
|
---|
184 | Stop when we find backslashes followed by a newline. */
|
---|
185 | while (*in != '\0')
|
---|
186 | if (*in == '\\')
|
---|
187 | {
|
---|
188 | p = in + 1;
|
---|
189 | while (*p == '\\')
|
---|
190 | ++p;
|
---|
191 | if (*p == '\n')
|
---|
192 | {
|
---|
193 | in = p;
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | while (in < p)
|
---|
197 | *out++ = *in++;
|
---|
198 | }
|
---|
199 | else
|
---|
200 | *out++ = *in++;
|
---|
201 | }
|
---|
202 |
|
---|
203 | *out = '\0';
|
---|
204 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
205 | assert (strchr (line, '\0') == out);
|
---|
206 | return out;
|
---|
207 | #endif
|
---|
208 | }
|
---|
209 | |
---|
210 |
|
---|
211 | /* Print N spaces (used in debug for target-depth). */
|
---|
212 |
|
---|
213 | void
|
---|
214 | print_spaces (unsigned int n)
|
---|
215 | {
|
---|
216 | while (n-- > 0)
|
---|
217 | putchar (' ');
|
---|
218 | }
|
---|
219 |
|
---|
220 | |
---|
221 |
|
---|
222 | /* Return a string whose contents concatenate the NUM strings provided
|
---|
223 | This string lives in static, re-used memory. */
|
---|
224 |
|
---|
225 | const char *
|
---|
226 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
227 | concat (unsigned int num, ...)
|
---|
228 | #else
|
---|
229 | concat (num, va_alist)
|
---|
230 | unsigned int num;
|
---|
231 | va_dcl
|
---|
232 | #endif
|
---|
233 | {
|
---|
234 | static unsigned int rlen = 0;
|
---|
235 | static char *result = NULL;
|
---|
236 | unsigned int ri = 0; /* bird: must be unsigned */
|
---|
237 |
|
---|
238 | #if USE_VARIADIC
|
---|
239 | va_list args;
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | VA_START (args, num);
|
---|
243 |
|
---|
244 | while (num-- > 0)
|
---|
245 | {
|
---|
246 | const char *s = va_arg (args, const char *);
|
---|
247 | unsigned int l = s ? strlen (s) : 0;
|
---|
248 |
|
---|
249 | if (l == 0)
|
---|
250 | continue;
|
---|
251 |
|
---|
252 | if (ri + l > rlen)
|
---|
253 | {
|
---|
254 | rlen = ((rlen ? rlen : 60) + l) * 2;
|
---|
255 | result = xrealloc (result, rlen);
|
---|
256 | }
|
---|
257 |
|
---|
258 | memcpy (result + ri, s, l);
|
---|
259 | ri += l;
|
---|
260 | }
|
---|
261 |
|
---|
262 | VA_END (args);
|
---|
263 |
|
---|
264 | /* Get some more memory if we don't have enough space for the
|
---|
265 | terminating '\0'. */
|
---|
266 | if (ri == rlen)
|
---|
267 | {
|
---|
268 | rlen = (rlen ? rlen : 60) * 2;
|
---|
269 | result = xrealloc (result, rlen);
|
---|
270 | }
|
---|
271 |
|
---|
272 | result[ri] = '\0';
|
---|
273 |
|
---|
274 | return result;
|
---|
275 | }
|
---|
276 | |
---|
277 |
|
---|
278 | /* Print a message on stdout. */
|
---|
279 |
|
---|
280 | void
|
---|
281 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
282 | message (int prefix, const char *fmt, ...)
|
---|
283 | #else
|
---|
284 | message (prefix, fmt, va_alist)
|
---|
285 | int prefix;
|
---|
286 | const char *fmt;
|
---|
287 | va_dcl
|
---|
288 | #endif
|
---|
289 | {
|
---|
290 | #if USE_VARIADIC
|
---|
291 | va_list args;
|
---|
292 | #endif
|
---|
293 |
|
---|
294 | log_working_directory (1);
|
---|
295 |
|
---|
296 | if (fmt != 0)
|
---|
297 | {
|
---|
298 | if (prefix)
|
---|
299 | {
|
---|
300 | if (makelevel == 0)
|
---|
301 | printf ("%s: ", program);
|
---|
302 | else
|
---|
303 | printf ("%s[%u]: ", program, makelevel);
|
---|
304 | }
|
---|
305 | VA_START (args, fmt);
|
---|
306 | VA_PRINTF (stdout, fmt, args);
|
---|
307 | VA_END (args);
|
---|
308 | putchar ('\n');
|
---|
309 | }
|
---|
310 |
|
---|
311 | fflush (stdout);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* Print an error message. */
|
---|
315 |
|
---|
316 | void
|
---|
317 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
318 | error (const struct floc *flocp, const char *fmt, ...)
|
---|
319 | #else
|
---|
320 | error (flocp, fmt, va_alist)
|
---|
321 | const struct floc *flocp;
|
---|
322 | const char *fmt;
|
---|
323 | va_dcl
|
---|
324 | #endif
|
---|
325 | {
|
---|
326 | #if USE_VARIADIC
|
---|
327 | va_list args;
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | log_working_directory (1);
|
---|
331 |
|
---|
332 | if (flocp && flocp->filenm)
|
---|
333 | fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
|
---|
334 | else if (makelevel == 0)
|
---|
335 | fprintf (stderr, "%s: ", program);
|
---|
336 | else
|
---|
337 | fprintf (stderr, "%s[%u]: ", program, makelevel);
|
---|
338 |
|
---|
339 | VA_START(args, fmt);
|
---|
340 | VA_PRINTF (stderr, fmt, args);
|
---|
341 | VA_END (args);
|
---|
342 |
|
---|
343 | putc ('\n', stderr);
|
---|
344 | fflush (stderr);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* Print an error message and exit. */
|
---|
348 |
|
---|
349 | void
|
---|
350 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
351 | fatal (const struct floc *flocp, const char *fmt, ...)
|
---|
352 | #else
|
---|
353 | fatal (flocp, fmt, va_alist)
|
---|
354 | const struct floc *flocp;
|
---|
355 | const char *fmt;
|
---|
356 | va_dcl
|
---|
357 | #endif
|
---|
358 | {
|
---|
359 | #if USE_VARIADIC
|
---|
360 | va_list args;
|
---|
361 | #endif
|
---|
362 |
|
---|
363 | log_working_directory (1);
|
---|
364 |
|
---|
365 | if (flocp && flocp->filenm)
|
---|
366 | fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
|
---|
367 | else if (makelevel == 0)
|
---|
368 | fprintf (stderr, "%s: *** ", program);
|
---|
369 | else
|
---|
370 | fprintf (stderr, "%s[%u]: *** ", program, makelevel);
|
---|
371 |
|
---|
372 | VA_START(args, fmt);
|
---|
373 | VA_PRINTF (stderr, fmt, args);
|
---|
374 | VA_END (args);
|
---|
375 |
|
---|
376 | fputs (_(". Stop.\n"), stderr);
|
---|
377 |
|
---|
378 | die (2);
|
---|
379 | }
|
---|
380 |
|
---|
381 | #ifndef HAVE_STRERROR
|
---|
382 |
|
---|
383 | #undef strerror
|
---|
384 |
|
---|
385 | char *
|
---|
386 | strerror (int errnum)
|
---|
387 | {
|
---|
388 | extern int errno, sys_nerr;
|
---|
389 | #ifndef __DECC
|
---|
390 | extern char *sys_errlist[];
|
---|
391 | #endif
|
---|
392 | static char buf[] = "Unknown error 12345678901234567890";
|
---|
393 |
|
---|
394 | if (errno < sys_nerr)
|
---|
395 | return sys_errlist[errnum];
|
---|
396 |
|
---|
397 | sprintf (buf, _("Unknown error %d"), errnum);
|
---|
398 | return buf;
|
---|
399 | }
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | /* Print an error message from errno. */
|
---|
403 |
|
---|
404 | void
|
---|
405 | perror_with_name (const char *str, const char *name)
|
---|
406 | {
|
---|
407 | error (NILF, _("%s%s: %s"), str, name, strerror (errno));
|
---|
408 | }
|
---|
409 |
|
---|
410 | /* Print an error message from errno and exit. */
|
---|
411 |
|
---|
412 | void
|
---|
413 | pfatal_with_name (const char *name)
|
---|
414 | {
|
---|
415 | fatal (NILF, _("%s: %s"), name, strerror (errno));
|
---|
416 |
|
---|
417 | /* NOTREACHED */
|
---|
418 | }
|
---|
419 | |
---|
420 |
|
---|
421 | /* Like malloc but get fatal error if memory is exhausted. */
|
---|
422 | /* Don't bother if we're using dmalloc; it provides these for us. */
|
---|
423 |
|
---|
424 | #if !defined(HAVE_DMALLOC_H) && !defined(ELECTRIC_HEAP) /* bird */
|
---|
425 |
|
---|
426 | #undef xmalloc
|
---|
427 | #undef xcalloc
|
---|
428 | #undef xrealloc
|
---|
429 | #undef xstrdup
|
---|
430 |
|
---|
431 | void *
|
---|
432 | xmalloc (unsigned int size)
|
---|
433 | {
|
---|
434 | /* Make sure we don't allocate 0, for pre-ISO implementations. */
|
---|
435 | void *result = malloc (size ? size : 1);
|
---|
436 | if (result == 0)
|
---|
437 | fatal (NILF, _("virtual memory exhausted"));
|
---|
438 |
|
---|
439 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
440 | make_stats_allocations++;
|
---|
441 | if (make_expensive_statistics)
|
---|
442 | make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
|
---|
443 | else
|
---|
444 | make_stats_allocated += size;
|
---|
445 | #endif
|
---|
446 | return result;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | void *
|
---|
451 | xcalloc (unsigned int size)
|
---|
452 | {
|
---|
453 | /* Make sure we don't allocate 0, for pre-ISO implementations. */
|
---|
454 | void *result = calloc (size ? size : 1, 1);
|
---|
455 | if (result == 0)
|
---|
456 | fatal (NILF, _("virtual memory exhausted"));
|
---|
457 | return result;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | void *
|
---|
462 | xrealloc (void *ptr, unsigned int size)
|
---|
463 | {
|
---|
464 | void *result;
|
---|
465 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
466 | if (make_expensive_statistics && ptr != NULL)
|
---|
467 | make_stats_allocated -= SIZE_OF_HEAP_BLOCK (ptr);
|
---|
468 | if (ptr)
|
---|
469 | make_stats_reallocations++;
|
---|
470 | else
|
---|
471 | make_stats_allocations++;
|
---|
472 | #endif
|
---|
473 |
|
---|
474 | /* Some older implementations of realloc() don't conform to ISO. */
|
---|
475 | if (! size)
|
---|
476 | size = 1;
|
---|
477 | result = ptr ? realloc (ptr, size) : malloc (size);
|
---|
478 | if (result == 0)
|
---|
479 | fatal (NILF, _("virtual memory exhausted"));
|
---|
480 |
|
---|
481 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
482 | if (make_expensive_statistics)
|
---|
483 | make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
|
---|
484 | else
|
---|
485 | make_stats_allocated += size;
|
---|
486 | #endif
|
---|
487 | return result;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | char *
|
---|
492 | xstrdup (const char *ptr)
|
---|
493 | {
|
---|
494 | char *result;
|
---|
495 |
|
---|
496 | #ifdef HAVE_STRDUP
|
---|
497 | result = strdup (ptr);
|
---|
498 | #else
|
---|
499 | result = malloc (strlen (ptr) + 1);
|
---|
500 | #endif
|
---|
501 |
|
---|
502 | if (result == 0)
|
---|
503 | fatal (NILF, _("virtual memory exhausted"));
|
---|
504 |
|
---|
505 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
506 | make_stats_allocations++;
|
---|
507 | if (make_expensive_statistics)
|
---|
508 | make_stats_allocated += SIZE_OF_HEAP_BLOCK (result);
|
---|
509 | else
|
---|
510 | make_stats_allocated += strlen (ptr) + 1;
|
---|
511 | #endif
|
---|
512 | #ifdef HAVE_STRDUP
|
---|
513 | return result;
|
---|
514 | #else
|
---|
515 | return strcpy (result, ptr);
|
---|
516 | #endif
|
---|
517 | }
|
---|
518 |
|
---|
519 | #endif /* HAVE_DMALLOC_H */
|
---|
520 |
|
---|
521 | char *
|
---|
522 | xstrndup (const char *str, unsigned int length)
|
---|
523 | {
|
---|
524 | char *result;
|
---|
525 |
|
---|
526 | #if defined(HAVE_STRNDUP) && !defined(KMK)
|
---|
527 | result = strndup (str, length);
|
---|
528 | if (result == 0)
|
---|
529 | fatal (NILF, _("virtual memory exhausted"));
|
---|
530 | #else
|
---|
531 | result = xmalloc (length + 1);
|
---|
532 | if (length > 0)
|
---|
533 | strncpy (result, str, length);
|
---|
534 | result[length] = '\0';
|
---|
535 | #endif
|
---|
536 |
|
---|
537 | return result;
|
---|
538 | }
|
---|
539 | |
---|
540 |
|
---|
541 |
|
---|
542 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS /* This is really a reimplemntation of
|
---|
543 | memchr, only slower. It's been replaced by a macro in the header file. */
|
---|
544 |
|
---|
545 | /* Limited INDEX:
|
---|
546 | Search through the string STRING, which ends at LIMIT, for the character C.
|
---|
547 | Returns a pointer to the first occurrence, or nil if none is found.
|
---|
548 | Like INDEX except that the string searched ends where specified
|
---|
549 | instead of at the first null. */
|
---|
550 |
|
---|
551 | char *
|
---|
552 | lindex (const char *s, const char *limit, int c)
|
---|
553 | {
|
---|
554 | while (s < limit)
|
---|
555 | if (*s++ == c)
|
---|
556 | return (char *)(s - 1);
|
---|
557 |
|
---|
558 | return 0;
|
---|
559 | }
|
---|
560 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
561 | |
---|
562 |
|
---|
563 | /* Return the address of the first whitespace or null in the string S. */
|
---|
564 |
|
---|
565 | char *
|
---|
566 | end_of_token (const char *s)
|
---|
567 | {
|
---|
568 | #ifdef KMK
|
---|
569 | for (;;)
|
---|
570 | {
|
---|
571 | unsigned char ch0, ch1, ch2, ch3;
|
---|
572 |
|
---|
573 | ch0 = *s;
|
---|
574 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch0)))
|
---|
575 | return (char *)s;
|
---|
576 | ch1 = s[1];
|
---|
577 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch1)))
|
---|
578 | return (char *)s + 1;
|
---|
579 | ch2 = s[2];
|
---|
580 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch2)))
|
---|
581 | return (char *)s + 2;
|
---|
582 | ch3 = s[3];
|
---|
583 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch3)))
|
---|
584 | return (char *)s + 3;
|
---|
585 |
|
---|
586 | s += 4;
|
---|
587 | }
|
---|
588 |
|
---|
589 | #else
|
---|
590 | while (*s != '\0' && !isblank ((unsigned char)*s))
|
---|
591 | ++s;
|
---|
592 | return (char *)s;
|
---|
593 | #endif
|
---|
594 | }
|
---|
595 |
|
---|
596 | #ifdef WINDOWS32
|
---|
597 | /*
|
---|
598 | * Same as end_of_token, but take into account a stop character
|
---|
599 | */
|
---|
600 | char *
|
---|
601 | end_of_token_w32 (const char *s, char stopchar)
|
---|
602 | {
|
---|
603 | const char *p = s;
|
---|
604 | int backslash = 0;
|
---|
605 |
|
---|
606 | while (*p != '\0' && *p != stopchar
|
---|
607 | && (backslash || !isblank ((unsigned char)*p)))
|
---|
608 | {
|
---|
609 | if (*p++ == '\\')
|
---|
610 | {
|
---|
611 | backslash = !backslash;
|
---|
612 | while (*p == '\\')
|
---|
613 | {
|
---|
614 | backslash = !backslash;
|
---|
615 | ++p;
|
---|
616 | }
|
---|
617 | }
|
---|
618 | else
|
---|
619 | backslash = 0;
|
---|
620 | }
|
---|
621 |
|
---|
622 | return (char *)p;
|
---|
623 | }
|
---|
624 | #endif
|
---|
625 |
|
---|
626 | /* Return the address of the first nonwhitespace or null in the string S. */
|
---|
627 |
|
---|
628 | char *
|
---|
629 | next_token (const char *s)
|
---|
630 | {
|
---|
631 | #ifdef KMK
|
---|
632 | for (;;)
|
---|
633 | {
|
---|
634 | unsigned char ch0, ch1, ch2, ch3;
|
---|
635 |
|
---|
636 | ch0 = *s;
|
---|
637 | if (MY_PREDICT_FALSE(!MY_IS_BLANK(ch0)))
|
---|
638 | return (char *)s;
|
---|
639 | ch1 = s[1];
|
---|
640 | if (MY_PREDICT_TRUE(!MY_IS_BLANK(ch1)))
|
---|
641 | return (char *)s + 1;
|
---|
642 | ch2 = s[2];
|
---|
643 | if (MY_PREDICT_FALSE(!MY_IS_BLANK(ch2)))
|
---|
644 | return (char *)s + 2;
|
---|
645 | ch3 = s[3];
|
---|
646 | if (MY_PREDICT_TRUE(!MY_IS_BLANK(ch3)))
|
---|
647 | return (char *)s + 3;
|
---|
648 |
|
---|
649 | s += 4;
|
---|
650 | }
|
---|
651 |
|
---|
652 | #else /* !KMK */
|
---|
653 | while (isblank ((unsigned char)*s))
|
---|
654 | ++s;
|
---|
655 | return (char *)s;
|
---|
656 | #endif /* !KMK */
|
---|
657 | }
|
---|
658 |
|
---|
659 | /* Find the next token in PTR; return the address of it, and store the length
|
---|
660 | of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
|
---|
661 | of the token, so this function can be called repeatedly in a loop. */
|
---|
662 |
|
---|
663 | char *
|
---|
664 | find_next_token (const char **ptr, unsigned int *lengthptr)
|
---|
665 | {
|
---|
666 | #ifdef KMK
|
---|
667 | const char *p = *ptr;
|
---|
668 | const char *e;
|
---|
669 |
|
---|
670 | /* skip blanks */
|
---|
671 | # if 0 /* a moderate version */
|
---|
672 | for (;; p++)
|
---|
673 | {
|
---|
674 | unsigned char ch = *p;
|
---|
675 | if (!MY_IS_BLANK(ch))
|
---|
676 | {
|
---|
677 | if (!ch)
|
---|
678 | return NULL;
|
---|
679 | break;
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | # else /* (too) big unroll */
|
---|
684 | for (;; p += 4)
|
---|
685 | {
|
---|
686 | unsigned char ch0, ch1, ch2, ch3;
|
---|
687 |
|
---|
688 | ch0 = *p;
|
---|
689 | if (MY_PREDICT_FALSE(!MY_IS_BLANK(ch0)))
|
---|
690 | {
|
---|
691 | if (!ch0)
|
---|
692 | return NULL;
|
---|
693 | break;
|
---|
694 | }
|
---|
695 | ch1 = p[1];
|
---|
696 | if (MY_PREDICT_TRUE(!MY_IS_BLANK(ch1)))
|
---|
697 | {
|
---|
698 | if (!ch1)
|
---|
699 | return NULL;
|
---|
700 | p += 1;
|
---|
701 | break;
|
---|
702 | }
|
---|
703 | ch2 = p[2];
|
---|
704 | if (MY_PREDICT_FALSE(!MY_IS_BLANK(ch2)))
|
---|
705 | {
|
---|
706 | if (!ch2)
|
---|
707 | return NULL;
|
---|
708 | p += 2;
|
---|
709 | break;
|
---|
710 | }
|
---|
711 | ch3 = p[3];
|
---|
712 | if (MY_PREDICT_TRUE(!MY_IS_BLANK(ch3)))
|
---|
713 | {
|
---|
714 | if (!ch3)
|
---|
715 | return NULL;
|
---|
716 | p += 3;
|
---|
717 | break;
|
---|
718 | }
|
---|
719 | }
|
---|
720 | # endif
|
---|
721 |
|
---|
722 | /* skip ahead until EOS or blanks. */
|
---|
723 | # if 0 /* a moderate version */
|
---|
724 | for (e = p + 1; ; e++)
|
---|
725 | {
|
---|
726 | unsigned char ch = *e;
|
---|
727 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch)))
|
---|
728 | break;
|
---|
729 | }
|
---|
730 | # else /* (too) big unroll */
|
---|
731 | for (e = p + 1; ; e += 4)
|
---|
732 | {
|
---|
733 | unsigned char ch0, ch1, ch2, ch3;
|
---|
734 |
|
---|
735 | ch0 = *e;
|
---|
736 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch0)))
|
---|
737 | break;
|
---|
738 | ch1 = e[1];
|
---|
739 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch1)))
|
---|
740 | {
|
---|
741 | e += 1;
|
---|
742 | break;
|
---|
743 | }
|
---|
744 | ch2 = e[2];
|
---|
745 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch2)))
|
---|
746 | {
|
---|
747 | e += 2;
|
---|
748 | break;
|
---|
749 | }
|
---|
750 | ch3 = e[3];
|
---|
751 | if (MY_PREDICT_FALSE(MY_IS_BLANK_OR_EOS(ch3)))
|
---|
752 | {
|
---|
753 | e += 3;
|
---|
754 | break;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | # endif
|
---|
758 | *ptr = e;
|
---|
759 |
|
---|
760 | if (lengthptr != 0)
|
---|
761 | *lengthptr = e - p;
|
---|
762 |
|
---|
763 | return (char *)p;
|
---|
764 |
|
---|
765 | #else
|
---|
766 | const char *p = next_token (*ptr);
|
---|
767 |
|
---|
768 | if (*p == '\0')
|
---|
769 | return 0;
|
---|
770 |
|
---|
771 | *ptr = end_of_token (p);
|
---|
772 | if (lengthptr != 0)
|
---|
773 | *lengthptr = *ptr - p;
|
---|
774 |
|
---|
775 | return (char *)p;
|
---|
776 | #endif
|
---|
777 | }
|
---|
778 | #ifdef KMK
|
---|
779 |
|
---|
780 | /* Same as find_next_token with two exception:
|
---|
781 | - The string ends at EOS or '\0'.
|
---|
782 | - We keep track of $() and ${}, allowing functions to be used. */
|
---|
783 |
|
---|
784 | char *
|
---|
785 | find_next_token_eos (const char **ptr, const char *eos, unsigned int *lengthptr)
|
---|
786 | {
|
---|
787 | const char *p = *ptr;
|
---|
788 | const char *e;
|
---|
789 | int level = 0;
|
---|
790 |
|
---|
791 | /* skip blanks */
|
---|
792 | for (; p != eos; p++)
|
---|
793 | {
|
---|
794 | unsigned char ch = *p;
|
---|
795 | if (!MY_IS_BLANK(ch))
|
---|
796 | {
|
---|
797 | if (!ch)
|
---|
798 | return NULL;
|
---|
799 | break;
|
---|
800 | }
|
---|
801 | }
|
---|
802 | if (p == eos)
|
---|
803 | return NULL;
|
---|
804 |
|
---|
805 | /* skip ahead until EOS or blanks. */
|
---|
806 | for (e = p; e != eos; e++)
|
---|
807 | {
|
---|
808 | unsigned char ch = *e;
|
---|
809 | if (MY_IS_BLANK_OR_EOS(ch))
|
---|
810 | {
|
---|
811 | if (!ch || level == 0)
|
---|
812 | break;
|
---|
813 | }
|
---|
814 | else if (ch == '$')
|
---|
815 | {
|
---|
816 | if (&e[1] != eos && (e[1] == '(' || e[1] == '{'))
|
---|
817 | {
|
---|
818 | level++;
|
---|
819 | e++;
|
---|
820 | }
|
---|
821 | }
|
---|
822 | else if ((ch == ')' || ch == '}') && level > 0)
|
---|
823 | level--;
|
---|
824 | }
|
---|
825 |
|
---|
826 | *ptr = e;
|
---|
827 | if (lengthptr != 0)
|
---|
828 | *lengthptr = e - p;
|
---|
829 |
|
---|
830 | return (char *)p;
|
---|
831 | }
|
---|
832 |
|
---|
833 | #endif /* KMK */
|
---|
834 | |
---|
835 |
|
---|
836 |
|
---|
837 | /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */
|
---|
838 |
|
---|
839 | struct dep *
|
---|
840 | copy_dep_chain (const struct dep *d)
|
---|
841 | {
|
---|
842 | struct dep *firstnew = 0;
|
---|
843 | struct dep *lastnew = 0;
|
---|
844 |
|
---|
845 | while (d != 0)
|
---|
846 | {
|
---|
847 | #ifndef CONFIG_WITH_ALLOC_CACHES
|
---|
848 | struct dep *c = xmalloc (sizeof (struct dep));
|
---|
849 | #else
|
---|
850 | struct dep *c = alloccache_alloc(&dep_cache);
|
---|
851 | #endif
|
---|
852 | memcpy (c, d, sizeof (struct dep));
|
---|
853 |
|
---|
854 | /** @todo KMK: Check if we need this duplication! */
|
---|
855 | if (c->need_2nd_expansion)
|
---|
856 | c->name = xstrdup (c->name);
|
---|
857 |
|
---|
858 | c->next = 0;
|
---|
859 | if (firstnew == 0)
|
---|
860 | firstnew = lastnew = c;
|
---|
861 | else
|
---|
862 | lastnew = lastnew->next = c;
|
---|
863 |
|
---|
864 | d = d->next;
|
---|
865 | }
|
---|
866 |
|
---|
867 | return firstnew;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /* Free a chain of 'struct dep'. */
|
---|
871 |
|
---|
872 | void
|
---|
873 | free_dep_chain (struct dep *d)
|
---|
874 | {
|
---|
875 | while (d != 0)
|
---|
876 | {
|
---|
877 | struct dep *df = d;
|
---|
878 | d = d->next;
|
---|
879 | free_dep (df);
|
---|
880 | }
|
---|
881 | }
|
---|
882 |
|
---|
883 | /* Free a chain of struct nameseq.
|
---|
884 | For struct dep chains use free_dep_chain. */
|
---|
885 |
|
---|
886 | void
|
---|
887 | free_ns_chain (struct nameseq *ns)
|
---|
888 | {
|
---|
889 | while (ns != 0)
|
---|
890 | {
|
---|
891 | struct nameseq *t = ns;
|
---|
892 | ns = ns->next;
|
---|
893 | #ifndef CONFIG_WITH_ALLOC_CACHES
|
---|
894 | free (t);
|
---|
895 | #else
|
---|
896 | alloccache_free (&nameseq_cache, t);
|
---|
897 | #endif
|
---|
898 | }
|
---|
899 | }
|
---|
900 | |
---|
901 |
|
---|
902 |
|
---|
903 | #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
|
---|
904 |
|
---|
905 | /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
|
---|
906 | for it, define our own version. */
|
---|
907 |
|
---|
908 | int
|
---|
909 | strcasecmp (const char *s1, const char *s2)
|
---|
910 | {
|
---|
911 | while (1)
|
---|
912 | {
|
---|
913 | int c1 = (int) *(s1++);
|
---|
914 | int c2 = (int) *(s2++);
|
---|
915 |
|
---|
916 | if (isalpha (c1))
|
---|
917 | c1 = tolower (c1);
|
---|
918 | if (isalpha (c2))
|
---|
919 | c2 = tolower (c2);
|
---|
920 |
|
---|
921 | if (c1 != '\0' && c1 == c2)
|
---|
922 | continue;
|
---|
923 |
|
---|
924 | return (c1 - c2);
|
---|
925 | }
|
---|
926 | }
|
---|
927 | #endif
|
---|
928 |
|
---|
929 | #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
|
---|
930 |
|
---|
931 | /* If we don't have strncasecmp() (from POSIX), or anything that can
|
---|
932 | substitute for it, define our own version. */
|
---|
933 |
|
---|
934 | int
|
---|
935 | strncasecmp (const char *s1, const char *s2, int n)
|
---|
936 | {
|
---|
937 | while (n-- > 0)
|
---|
938 | {
|
---|
939 | int c1 = (int) *(s1++);
|
---|
940 | int c2 = (int) *(s2++);
|
---|
941 |
|
---|
942 | if (isalpha (c1))
|
---|
943 | c1 = tolower (c1);
|
---|
944 | if (isalpha (c2))
|
---|
945 | c2 = tolower (c2);
|
---|
946 |
|
---|
947 | if (c1 != '\0' && c1 == c2)
|
---|
948 | continue;
|
---|
949 |
|
---|
950 | return (c1 - c2);
|
---|
951 | }
|
---|
952 |
|
---|
953 | return 0;
|
---|
954 | }
|
---|
955 | #endif
|
---|
956 | |
---|
957 |
|
---|
958 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
959 |
|
---|
960 | #ifdef POSIX
|
---|
961 |
|
---|
962 | /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
|
---|
963 | functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
|
---|
964 | for example) which claim to be POSIX.1 also have the BSD setreuid and
|
---|
965 | setregid functions, but they don't work as in BSD and only the POSIX.1
|
---|
966 | way works. */
|
---|
967 |
|
---|
968 | #undef HAVE_SETREUID
|
---|
969 | #undef HAVE_SETREGID
|
---|
970 |
|
---|
971 | #else /* Not POSIX. */
|
---|
972 |
|
---|
973 | /* Some POSIX.1 systems have the seteuid and setegid functions. In a
|
---|
974 | POSIX-like system, they are the best thing to use. However, some
|
---|
975 | non-POSIX systems have them too but they do not work in the POSIX style
|
---|
976 | and we must use setreuid and setregid instead. */
|
---|
977 |
|
---|
978 | #undef HAVE_SETEUID
|
---|
979 | #undef HAVE_SETEGID
|
---|
980 |
|
---|
981 | #endif /* POSIX. */
|
---|
982 |
|
---|
983 | #ifndef HAVE_UNISTD_H
|
---|
984 | extern int getuid (), getgid (), geteuid (), getegid ();
|
---|
985 | extern int setuid (), setgid ();
|
---|
986 | #ifdef HAVE_SETEUID
|
---|
987 | extern int seteuid ();
|
---|
988 | #else
|
---|
989 | #ifdef HAVE_SETREUID
|
---|
990 | extern int setreuid ();
|
---|
991 | #endif /* Have setreuid. */
|
---|
992 | #endif /* Have seteuid. */
|
---|
993 | #ifdef HAVE_SETEGID
|
---|
994 | extern int setegid ();
|
---|
995 | #else
|
---|
996 | #ifdef HAVE_SETREGID
|
---|
997 | extern int setregid ();
|
---|
998 | #endif /* Have setregid. */
|
---|
999 | #endif /* Have setegid. */
|
---|
1000 | #endif /* No <unistd.h>. */
|
---|
1001 |
|
---|
1002 | /* Keep track of the user and group IDs for user- and make- access. */
|
---|
1003 | static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
|
---|
1004 | #define access_inited (user_uid != -1)
|
---|
1005 | static enum { make, user } current_access;
|
---|
1006 |
|
---|
1007 |
|
---|
1008 | /* Under -d, write a message describing the current IDs. */
|
---|
1009 |
|
---|
1010 | static void
|
---|
1011 | log_access (const char *flavor)
|
---|
1012 | {
|
---|
1013 | if (! ISDB (DB_JOBS))
|
---|
1014 | return;
|
---|
1015 |
|
---|
1016 | /* All the other debugging messages go to stdout,
|
---|
1017 | but we write this one to stderr because it might be
|
---|
1018 | run in a child fork whose stdout is piped. */
|
---|
1019 |
|
---|
1020 | fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
|
---|
1021 | flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
|
---|
1022 | (unsigned long) getegid (), (unsigned long) getgid ());
|
---|
1023 | fflush (stderr);
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 |
|
---|
1027 | static void
|
---|
1028 | init_access (void)
|
---|
1029 | {
|
---|
1030 | #ifndef VMS
|
---|
1031 | user_uid = getuid ();
|
---|
1032 | user_gid = getgid ();
|
---|
1033 |
|
---|
1034 | make_uid = geteuid ();
|
---|
1035 | make_gid = getegid ();
|
---|
1036 |
|
---|
1037 | /* Do these ever fail? */
|
---|
1038 | if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
|
---|
1039 | pfatal_with_name ("get{e}[gu]id");
|
---|
1040 |
|
---|
1041 | log_access (_("Initialized access"));
|
---|
1042 |
|
---|
1043 | current_access = make;
|
---|
1044 | #endif
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
1048 |
|
---|
1049 | /* Give the process appropriate permissions for access to
|
---|
1050 | user data (i.e., to stat files, or to spawn a child process). */
|
---|
1051 | void
|
---|
1052 | user_access (void)
|
---|
1053 | {
|
---|
1054 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
1055 |
|
---|
1056 | if (!access_inited)
|
---|
1057 | init_access ();
|
---|
1058 |
|
---|
1059 | if (current_access == user)
|
---|
1060 | return;
|
---|
1061 |
|
---|
1062 | /* We are in "make access" mode. This means that the effective user and
|
---|
1063 | group IDs are those of make (if it was installed setuid or setgid).
|
---|
1064 | We now want to set the effective user and group IDs to the real IDs,
|
---|
1065 | which are the IDs of the process that exec'd make. */
|
---|
1066 |
|
---|
1067 | #ifdef HAVE_SETEUID
|
---|
1068 |
|
---|
1069 | /* Modern systems have the seteuid/setegid calls which set only the
|
---|
1070 | effective IDs, which is ideal. */
|
---|
1071 |
|
---|
1072 | if (seteuid (user_uid) < 0)
|
---|
1073 | pfatal_with_name ("user_access: seteuid");
|
---|
1074 |
|
---|
1075 | #else /* Not HAVE_SETEUID. */
|
---|
1076 |
|
---|
1077 | #ifndef HAVE_SETREUID
|
---|
1078 |
|
---|
1079 | /* System V has only the setuid/setgid calls to set user/group IDs.
|
---|
1080 | There is an effective ID, which can be set by setuid/setgid.
|
---|
1081 | It can be set (unless you are root) only to either what it already is
|
---|
1082 | (returned by geteuid/getegid, now in make_uid/make_gid),
|
---|
1083 | the real ID (return by getuid/getgid, now in user_uid/user_gid),
|
---|
1084 | or the saved set ID (what the effective ID was before this set-ID
|
---|
1085 | executable (make) was exec'd). */
|
---|
1086 |
|
---|
1087 | if (setuid (user_uid) < 0)
|
---|
1088 | pfatal_with_name ("user_access: setuid");
|
---|
1089 |
|
---|
1090 | #else /* HAVE_SETREUID. */
|
---|
1091 |
|
---|
1092 | /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
|
---|
1093 | They may be set to themselves or each other. So you have two alternatives
|
---|
1094 | at any one time. If you use setuid/setgid, the effective will be set to
|
---|
1095 | the real, leaving only one alternative. Using setreuid/setregid, however,
|
---|
1096 | you can toggle between your two alternatives by swapping the values in a
|
---|
1097 | single setreuid or setregid call. */
|
---|
1098 |
|
---|
1099 | if (setreuid (make_uid, user_uid) < 0)
|
---|
1100 | pfatal_with_name ("user_access: setreuid");
|
---|
1101 |
|
---|
1102 | #endif /* Not HAVE_SETREUID. */
|
---|
1103 | #endif /* HAVE_SETEUID. */
|
---|
1104 |
|
---|
1105 | #ifdef HAVE_SETEGID
|
---|
1106 | if (setegid (user_gid) < 0)
|
---|
1107 | pfatal_with_name ("user_access: setegid");
|
---|
1108 | #else
|
---|
1109 | #ifndef HAVE_SETREGID
|
---|
1110 | if (setgid (user_gid) < 0)
|
---|
1111 | pfatal_with_name ("user_access: setgid");
|
---|
1112 | #else
|
---|
1113 | if (setregid (make_gid, user_gid) < 0)
|
---|
1114 | pfatal_with_name ("user_access: setregid");
|
---|
1115 | #endif
|
---|
1116 | #endif
|
---|
1117 |
|
---|
1118 | current_access = user;
|
---|
1119 |
|
---|
1120 | log_access (_("User access"));
|
---|
1121 |
|
---|
1122 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /* Give the process appropriate permissions for access to
|
---|
1126 | make data (i.e., the load average). */
|
---|
1127 | void
|
---|
1128 | make_access (void)
|
---|
1129 | {
|
---|
1130 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
1131 |
|
---|
1132 | if (!access_inited)
|
---|
1133 | init_access ();
|
---|
1134 |
|
---|
1135 | if (current_access == make)
|
---|
1136 | return;
|
---|
1137 |
|
---|
1138 | /* See comments in user_access, above. */
|
---|
1139 |
|
---|
1140 | #ifdef HAVE_SETEUID
|
---|
1141 | if (seteuid (make_uid) < 0)
|
---|
1142 | pfatal_with_name ("make_access: seteuid");
|
---|
1143 | #else
|
---|
1144 | #ifndef HAVE_SETREUID
|
---|
1145 | if (setuid (make_uid) < 0)
|
---|
1146 | pfatal_with_name ("make_access: setuid");
|
---|
1147 | #else
|
---|
1148 | if (setreuid (user_uid, make_uid) < 0)
|
---|
1149 | pfatal_with_name ("make_access: setreuid");
|
---|
1150 | #endif
|
---|
1151 | #endif
|
---|
1152 |
|
---|
1153 | #ifdef HAVE_SETEGID
|
---|
1154 | if (setegid (make_gid) < 0)
|
---|
1155 | pfatal_with_name ("make_access: setegid");
|
---|
1156 | #else
|
---|
1157 | #ifndef HAVE_SETREGID
|
---|
1158 | if (setgid (make_gid) < 0)
|
---|
1159 | pfatal_with_name ("make_access: setgid");
|
---|
1160 | #else
|
---|
1161 | if (setregid (user_gid, make_gid) < 0)
|
---|
1162 | pfatal_with_name ("make_access: setregid");
|
---|
1163 | #endif
|
---|
1164 | #endif
|
---|
1165 |
|
---|
1166 | current_access = make;
|
---|
1167 |
|
---|
1168 | log_access (_("Make access"));
|
---|
1169 |
|
---|
1170 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /* Give the process appropriate permissions for a child process.
|
---|
1174 | This is like user_access, but you can't get back to make_access. */
|
---|
1175 | void
|
---|
1176 | child_access (void)
|
---|
1177 | {
|
---|
1178 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
1179 |
|
---|
1180 | if (!access_inited)
|
---|
1181 | abort ();
|
---|
1182 |
|
---|
1183 | /* Set both the real and effective UID and GID to the user's.
|
---|
1184 | They cannot be changed back to make's. */
|
---|
1185 |
|
---|
1186 | #ifndef HAVE_SETREUID
|
---|
1187 | if (setuid (user_uid) < 0)
|
---|
1188 | pfatal_with_name ("child_access: setuid");
|
---|
1189 | #else
|
---|
1190 | if (setreuid (user_uid, user_uid) < 0)
|
---|
1191 | pfatal_with_name ("child_access: setreuid");
|
---|
1192 | #endif
|
---|
1193 |
|
---|
1194 | #ifndef HAVE_SETREGID
|
---|
1195 | if (setgid (user_gid) < 0)
|
---|
1196 | pfatal_with_name ("child_access: setgid");
|
---|
1197 | #else
|
---|
1198 | if (setregid (user_gid, user_gid) < 0)
|
---|
1199 | pfatal_with_name ("child_access: setregid");
|
---|
1200 | #endif
|
---|
1201 |
|
---|
1202 | log_access (_("Child access"));
|
---|
1203 |
|
---|
1204 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
1205 | }
|
---|
1206 | |
---|
1207 |
|
---|
1208 | #ifdef NEED_GET_PATH_MAX
|
---|
1209 | unsigned int
|
---|
1210 | get_path_max (void)
|
---|
1211 | {
|
---|
1212 | static unsigned int value;
|
---|
1213 |
|
---|
1214 | if (value == 0)
|
---|
1215 | {
|
---|
1216 | long int x = pathconf ("/", _PC_PATH_MAX);
|
---|
1217 | if (x > 0)
|
---|
1218 | value = x;
|
---|
1219 | else
|
---|
1220 | return MAXPATHLEN;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | return value;
|
---|
1224 | }
|
---|
1225 | #endif
|
---|
1226 | |
---|
1227 |
|
---|
1228 |
|
---|
1229 | /* This code is stolen from gnulib.
|
---|
1230 | If/when we abandon the requirement to work with K&R compilers, we can
|
---|
1231 | remove this (and perhaps other parts of GNU make!) and migrate to using
|
---|
1232 | gnulib directly.
|
---|
1233 |
|
---|
1234 | This is called only through atexit(), which means die() has already been
|
---|
1235 | invoked. So, call exit() here directly. Apparently that works...?
|
---|
1236 | */
|
---|
1237 |
|
---|
1238 | /* Close standard output, exiting with status 'exit_failure' on failure.
|
---|
1239 | If a program writes *anything* to stdout, that program should close
|
---|
1240 | stdout and make sure that it succeeds before exiting. Otherwise,
|
---|
1241 | suppose that you go to the extreme of checking the return status
|
---|
1242 | of every function that does an explicit write to stdout. The last
|
---|
1243 | printf can succeed in writing to the internal stream buffer, and yet
|
---|
1244 | the fclose(stdout) could still fail (due e.g., to a disk full error)
|
---|
1245 | when it tries to write out that buffered data. Thus, you would be
|
---|
1246 | left with an incomplete output file and the offending program would
|
---|
1247 | exit successfully. Even calling fflush is not always sufficient,
|
---|
1248 | since some file systems (NFS and CODA) buffer written/flushed data
|
---|
1249 | until an actual close call.
|
---|
1250 |
|
---|
1251 | Besides, it's wasteful to check the return value from every call
|
---|
1252 | that writes to stdout -- just let the internal stream state record
|
---|
1253 | the failure. That's what the ferror test is checking below.
|
---|
1254 |
|
---|
1255 | It's important to detect such failures and exit nonzero because many
|
---|
1256 | tools (most notably `make' and other build-management systems) depend
|
---|
1257 | on being able to detect failure in other tools via their exit status. */
|
---|
1258 |
|
---|
1259 | void
|
---|
1260 | close_stdout (void)
|
---|
1261 | {
|
---|
1262 | int prev_fail = ferror (stdout);
|
---|
1263 | int fclose_fail = fclose (stdout);
|
---|
1264 |
|
---|
1265 | if (prev_fail || fclose_fail)
|
---|
1266 | {
|
---|
1267 | if (fclose_fail)
|
---|
1268 | error (NILF, _("write error: %s"), strerror (errno));
|
---|
1269 | else
|
---|
1270 | error (NILF, _("write error"));
|
---|
1271 | exit (EXIT_FAILURE);
|
---|
1272 | }
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
|
---|
1276 | /* Print heap statistics if supported by the platform. */
|
---|
1277 | void
|
---|
1278 | print_heap_stats (void)
|
---|
1279 | {
|
---|
1280 | /* Darwin / Mac OS X */
|
---|
1281 | # ifdef __APPLE__
|
---|
1282 | malloc_statistics_t s;
|
---|
1283 |
|
---|
1284 | malloc_zone_statistics (NULL, &s);
|
---|
1285 | printf (_("\n# CRT Heap: %u bytes in use, in %u blocks, avg %u bytes/block\n"),
|
---|
1286 | (unsigned)s.size_in_use, (unsigned)s.blocks_in_use,
|
---|
1287 | (unsigned)(s.size_in_use / s.blocks_in_use));
|
---|
1288 | printf (_("# %u bytes max in use (high water mark)\n"),
|
---|
1289 | (unsigned)s.max_size_in_use);
|
---|
1290 | printf (_("# %u bytes reserved, %u bytes free (estimate)\n"),
|
---|
1291 | (unsigned)s.size_allocated,
|
---|
1292 | (unsigned)(s.size_allocated - s.size_in_use));
|
---|
1293 | # endif /* __APPLE__ */
|
---|
1294 |
|
---|
1295 | /* MSC / Windows */
|
---|
1296 | # ifdef _MSC_VER
|
---|
1297 | unsigned int blocks_used = 0;
|
---|
1298 | unsigned int bytes_used = 0;
|
---|
1299 | unsigned int blocks_avail = 0;
|
---|
1300 | unsigned int bytes_avail = 0;
|
---|
1301 | _HEAPINFO hinfo;
|
---|
1302 |
|
---|
1303 | memset (&hinfo, '\0', sizeof (hinfo));
|
---|
1304 | while (_heapwalk(&hinfo) == _HEAPOK)
|
---|
1305 | {
|
---|
1306 | if (hinfo._useflag == _USEDENTRY)
|
---|
1307 | {
|
---|
1308 | blocks_used++;
|
---|
1309 | bytes_used += hinfo._size;
|
---|
1310 | }
|
---|
1311 | else
|
---|
1312 | {
|
---|
1313 | blocks_avail++;
|
---|
1314 | bytes_avail += hinfo._size;
|
---|
1315 | }
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | printf (_("\n# CRT Heap: %u bytes in use, in %u blocks, avg %u bytes/block\n"),
|
---|
1319 | bytes_used, blocks_used, bytes_used / blocks_used);
|
---|
1320 | printf (_("# %u bytes avail, in %u blocks, avg %u bytes/block\n"),
|
---|
1321 | bytes_avail, blocks_avail, bytes_avail / blocks_avail);
|
---|
1322 | # endif /* _MSC_VER */
|
---|
1323 |
|
---|
1324 | /* Darwin Libc sources indicates that something like this may be
|
---|
1325 | found in GLIBC, however, it's not in any current one... */
|
---|
1326 | # if 0 /* ??? */
|
---|
1327 | struct mstats m;
|
---|
1328 |
|
---|
1329 | m = mstats();
|
---|
1330 | printf (_("\n# CRT Heap: %zu blocks / %zu bytes in use, %zu blocks / %zu bytes free\n"),
|
---|
1331 | m.chunks_used, m.bytes_used, m.chunks_free, m.bytes_free);
|
---|
1332 | printf (_("# %zu bytes reserved\n"),
|
---|
1333 | m.bytes_total);
|
---|
1334 | # endif /* ??? */
|
---|
1335 |
|
---|
1336 | /* XVID2/XPG mallinfo (displayed per GLIBC documentation). */
|
---|
1337 | # if defined(__GLIBC__) || defined(HAVE_MALLINFO)
|
---|
1338 | struct mallinfo m;
|
---|
1339 |
|
---|
1340 | m = mallinfo();
|
---|
1341 | printf (_("\n# CRT Heap: %d bytes in use, %d bytes free\n"),
|
---|
1342 | m.uordblks, m.fordblks);
|
---|
1343 |
|
---|
1344 | printf (_("# # free chunks=%d, # fastbin blocks=%d\n"),
|
---|
1345 | m.ordblks, m.smblks);
|
---|
1346 | printf (_("# # mapped regions=%d, space in mapped regions=%d\n"),
|
---|
1347 | m.hblks, m.hblkhd);
|
---|
1348 | printf (_("# non-mapped space allocated from system=%d\n"),
|
---|
1349 | m.arena);
|
---|
1350 | printf (_("# maximum total allocated space=%d\n"),
|
---|
1351 | m.usmblks);
|
---|
1352 | printf (_("# top-most releasable space=%d\n"),
|
---|
1353 | m.keepcost);
|
---|
1354 | # endif /* __GLIBC__ || HAVE_MALLINFO */
|
---|
1355 |
|
---|
1356 | # ifdef CONFIG_WITH_MAKE_STATS
|
---|
1357 | printf(_("# %lu malloc calls, %lu realloc calls\n"),
|
---|
1358 | make_stats_allocations, make_stats_reallocations);
|
---|
1359 | printf(_("# %lu MBs alloc sum, not counting freed, add pinch of salt\n"), /* XXX: better wording */
|
---|
1360 | make_stats_allocated / (1024*1024));
|
---|
1361 | # endif
|
---|
1362 |
|
---|
1363 | /* XXX: windows */
|
---|
1364 | }
|
---|
1365 | #endif /* CONFIG_WITH_PRINT_STATS_SWITCH */
|
---|
1366 |
|
---|
1367 | #ifdef CONFIG_WITH_PRINT_TIME_SWITCH
|
---|
1368 | /* Get a nanosecond timestamp, from a monotonic time source if
|
---|
1369 | possible. Returns -1 after calling error() on failure. */
|
---|
1370 |
|
---|
1371 | big_int
|
---|
1372 | nano_timestamp (void)
|
---|
1373 | {
|
---|
1374 | big_int ts;
|
---|
1375 | #if defined (WINDOWS32)
|
---|
1376 | static int s_state = -1;
|
---|
1377 | static LARGE_INTEGER s_freq;
|
---|
1378 |
|
---|
1379 | if (s_state == -1)
|
---|
1380 | s_state = QueryPerformanceFrequency (&s_freq);
|
---|
1381 | if (s_state)
|
---|
1382 | {
|
---|
1383 | LARGE_INTEGER pc;
|
---|
1384 | if (!QueryPerformanceCounter (&pc))
|
---|
1385 | {
|
---|
1386 | s_state = 0;
|
---|
1387 | return nano_timestamp ();
|
---|
1388 | }
|
---|
1389 | ts = (big_int)((long double)pc.QuadPart / (long double)s_freq.QuadPart * 1000000000);
|
---|
1390 | }
|
---|
1391 | else
|
---|
1392 | {
|
---|
1393 | /* fall back to low resolution system time. */
|
---|
1394 | LARGE_INTEGER bigint;
|
---|
1395 | FILETIME ft = {0,0};
|
---|
1396 | GetSystemTimeAsFileTime (&ft);
|
---|
1397 | bigint.u.LowPart = ft.dwLowDateTime;
|
---|
1398 | bigint.u.HighPart = ft.dwLowDateTime;
|
---|
1399 | ts = bigint.QuadPart * 100;
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | #elif HAVE_GETTIMEOFDAY
|
---|
1403 | /* FIXME: Linux and others have the realtime clock_* api, detect and use it. */
|
---|
1404 | struct timeval tv;
|
---|
1405 | if (!gettimeofday (&tv, NULL))
|
---|
1406 | ts = (big_int)tv.tv_sec * 1000000000
|
---|
1407 | + tv.tv_usec * 1000;
|
---|
1408 | else
|
---|
1409 | {
|
---|
1410 | error (NILF, _("gettimeofday failed"));
|
---|
1411 | ts = -1;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | #else
|
---|
1415 | # error "PORTME"
|
---|
1416 | #endif
|
---|
1417 |
|
---|
1418 | return ts;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | /* Formats the elapsed time (nano seconds) in the manner easiest
|
---|
1422 | to read, with millisecond percision for larger numbers. */
|
---|
1423 |
|
---|
1424 | int
|
---|
1425 | format_elapsed_nano (char *buf, size_t size, big_int ts)
|
---|
1426 | {
|
---|
1427 | unsigned sz;
|
---|
1428 | if (ts < 1000)
|
---|
1429 | sz = sprintf (buf, "%uns", (unsigned)ts);
|
---|
1430 | else if (ts < 100000)
|
---|
1431 | sz = sprintf (buf, "%u.%03uus",
|
---|
1432 | (unsigned)(ts / 1000),
|
---|
1433 | (unsigned)(ts % 1000));
|
---|
1434 | else
|
---|
1435 | {
|
---|
1436 | ts /= 1000;
|
---|
1437 | if (ts < 1000)
|
---|
1438 | sz = sprintf (buf, "%uus", (unsigned)ts);
|
---|
1439 | else if (ts < 100000)
|
---|
1440 | sz = sprintf (buf, "%u.%03ums",
|
---|
1441 | (unsigned)(ts / 1000),
|
---|
1442 | (unsigned)(ts % 1000));
|
---|
1443 | else
|
---|
1444 | {
|
---|
1445 | ts /= 1000;
|
---|
1446 | if (ts < BIG_INT_C(60000))
|
---|
1447 | sz = sprintf (buf,
|
---|
1448 | "%u.%03us",
|
---|
1449 | (unsigned)(ts / 1000),
|
---|
1450 | (unsigned)(ts % 1000));
|
---|
1451 | else
|
---|
1452 | sz = sprintf (buf,
|
---|
1453 | "%um%u.%03us",
|
---|
1454 | (unsigned)( ts / BIG_INT_C(60000)),
|
---|
1455 | (unsigned)((ts % BIG_INT_C(60000)) / 1000),
|
---|
1456 | (unsigned)((ts % BIG_INT_C(60000)) % 1000));
|
---|
1457 | }
|
---|
1458 | }
|
---|
1459 | if (sz >= size)
|
---|
1460 | fatal (NILF, _("format_elapsed_nano buffer overflow: %u written, %lu buffer"),
|
---|
1461 | sz, (unsigned long)size);
|
---|
1462 | return sz;
|
---|
1463 | }
|
---|
1464 | #endif /* CONFIG_WITH_PRINT_TIME_SWITCH */
|
---|