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