util.c (73188) | util.c (77349) |
---|---|
1/* 2 * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14#ifndef lint | 1/* 2 * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14#ifndef lint |
15static char id[] = "@(#)$Id: util.c,v 8.225.2.1.2.19 2001/02/22 18:56:24 gshapiro Exp $"; | 15static char id[] = "@(#)$Id: util.c,v 8.225.2.1.2.23 2001/05/17 18:10:18 gshapiro Exp $"; |
16#endif /* ! lint */ 17 18#include <sendmail.h> 19#include <sysexits.h> 20 21 22static void readtimeout __P((time_t)); 23 --- 349 unchanged lines hidden (view full) --- 373 register int sz; 374{ 375 register char *p; 376 377 /* some systems can't handle size zero mallocs */ 378 if (sz <= 0) 379 sz = 1; 380 | 16#endif /* ! lint */ 17 18#include <sendmail.h> 19#include <sysexits.h> 20 21 22static void readtimeout __P((time_t)); 23 --- 349 unchanged lines hidden (view full) --- 373 register int sz; 374{ 375 register char *p; 376 377 /* some systems can't handle size zero mallocs */ 378 if (sz <= 0) 379 sz = 1; 380 |
381 ENTER_CRITICAL(); |
|
381 p = malloc((unsigned) sz); | 382 p = malloc((unsigned) sz); |
383 LEAVE_CRITICAL(); |
|
382 if (p == NULL) 383 { 384 syserr("!Out of memory!!"); | 384 if (p == NULL) 385 { 386 syserr("!Out of memory!!"); |
385 /* exit(EX_UNAVAILABLE); */ | 387 388 /* NOTREACHED */ 389 exit(EX_UNAVAILABLE); |
386 } 387 return p; 388} 389/* | 390 } 391 return p; 392} 393/* |
394** XREALLOC -- Reallocate memory and bitch wildly on failure. 395** 396** THIS IS A CLUDGE. This should be made to give a proper 397** error -- but after all, what can we do? 398** 399** Parameters: 400** ptr -- original area. 401** sz -- size of new area to allocate. 402** 403** Returns: 404** pointer to data region. 405** 406** Side Effects: 407** Memory is allocated. 408*/ 409 410char * 411xrealloc(ptr, sz) 412 void *ptr; 413 size_t sz; 414{ 415 register char *p; 416 417 /* some systems can't handle size zero mallocs */ 418 if (sz <= 0) 419 sz = 1; 420 421 ENTER_CRITICAL(); 422 p = realloc(ptr, (unsigned) sz); 423 LEAVE_CRITICAL(); 424 if (p == NULL) 425 { 426 syserr("!Out of memory!!"); 427 428 /* NOTREACHED */ 429 exit(EX_UNAVAILABLE); 430 } 431 return p; 432} 433/* 434** XCALLOC -- Allocate memory and bitch wildly on failure. 435** 436** THIS IS A CLUDGE. This should be made to give a proper 437** error -- but after all, what can we do? 438** 439** Parameters: 440** num -- number of items to allocate 441** sz -- size of new area to allocate. 442** 443** Returns: 444** pointer to data region. 445** 446** Side Effects: 447** Memory is allocated. 448*/ 449 450char * 451xcalloc(num, sz) 452 size_t num; 453 size_t sz; 454{ 455 register char *p; 456 457 /* some systems can't handle size zero mallocs */ 458 if (num <= 0) 459 num = 1; 460 if (sz <= 0) 461 sz = 1; 462 463 ENTER_CRITICAL(); 464 p = calloc((unsigned) num, (unsigned) sz); 465 LEAVE_CRITICAL(); 466 if (p == NULL) 467 { 468 syserr("!Out of memory!!"); 469 470 /* NOTREACHED */ 471 exit(EX_UNAVAILABLE); 472 } 473 return p; 474} 475/* 476** SM_FREE -- Free memory safely. 477** 478** Parameters: 479** ptr -- area to free 480** 481** Returns: 482** none. 483** 484** Side Effects: 485** Memory is freed. 486*/ 487 488void 489sm_free(ptr) 490 void *ptr; 491{ 492 ENTER_CRITICAL(); 493 free(ptr); 494 LEAVE_CRITICAL(); 495} 496/* |
|
390** COPYPLIST -- copy list of pointers. 391** 392** This routine is the equivalent of newstr for lists of 393** pointers. 394** 395** Parameters: 396** list -- list of pointers to copy. 397** Must be NULL terminated. --- 99 unchanged lines hidden (view full) --- 497 pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, 0644, sff); 498 if (pidf == NULL) 499 { 500 sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s", 501 pidpath, errstring(errno)); 502 } 503 else 504 { | 497** COPYPLIST -- copy list of pointers. 498** 499** This routine is the equivalent of newstr for lists of 500** pointers. 501** 502** Parameters: 503** list -- list of pointers to copy. 504** Must be NULL terminated. --- 99 unchanged lines hidden (view full) --- 604 pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, 0644, sff); 605 if (pidf == NULL) 606 { 607 sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s", 608 pidpath, errstring(errno)); 609 } 610 else 611 { |
505 long pid; | 612 pid_t pid; |
506 extern char *CommandLineArgs; 507 | 613 extern char *CommandLineArgs; 614 |
508 pid = (long) getpid(); | 615 pid = getpid(); |
509 510 /* write the process id on line 1 */ | 616 617 /* write the process id on line 1 */ |
511 fprintf(pidf, "%ld\n", pid); | 618 fprintf(pidf, "%ld\n", (long) pid); |
512 513 /* line 2 contains all command line flags */ 514 fprintf(pidf, "%s\n", CommandLineArgs); 515 516 /* flush and close */ 517 (void) fclose(pidf); 518 } 519} --- 687 unchanged lines hidden (view full) --- 1207 return buf; 1208} 1209 1210/* ARGSUSED */ 1211static void 1212readtimeout(timeout) 1213 time_t timeout; 1214{ | 619 620 /* line 2 contains all command line flags */ 621 fprintf(pidf, "%s\n", CommandLineArgs); 622 623 /* flush and close */ 624 (void) fclose(pidf); 625 } 626} --- 687 unchanged lines hidden (view full) --- 1314 return buf; 1315} 1316 1317/* ARGSUSED */ 1318static void 1319readtimeout(timeout) 1320 time_t timeout; 1321{ |
1322 /* 1323 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 1324 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 1325 ** DOING. 1326 */ 1327 1328 errno = ETIMEDOUT; |
|
1215 longjmp(CtxReadTimeout, 1); 1216} 1217/* 1218** FGETFOLDED -- like fgets, but know about folded lines. 1219** 1220** Parameters: 1221** buf -- place to put result. 1222** n -- bytes available. --- 43 unchanged lines hidden (view full) --- 1266 if (nn < MEMCHUNKSIZE) 1267 nn *= 2; 1268 else 1269 nn += MEMCHUNKSIZE; 1270 nbp = xalloc(nn); 1271 memmove(nbp, bp, p - bp); 1272 p = &nbp[p - bp]; 1273 if (bp != buf) | 1329 longjmp(CtxReadTimeout, 1); 1330} 1331/* 1332** FGETFOLDED -- like fgets, but know about folded lines. 1333** 1334** Parameters: 1335** buf -- place to put result. 1336** n -- bytes available. --- 43 unchanged lines hidden (view full) --- 1380 if (nn < MEMCHUNKSIZE) 1381 nn *= 2; 1382 else 1383 nn += MEMCHUNKSIZE; 1384 nbp = xalloc(nn); 1385 memmove(nbp, bp, p - bp); 1386 p = &nbp[p - bp]; 1387 if (bp != buf) |
1274 free(bp); | 1388 sm_free(bp); |
1275 bp = nbp; 1276 n = nn - (p - bp); 1277 } 1278 *p++ = i; 1279 if (i == '\n') 1280 { 1281 LineNumber++; 1282 i = getc(f); --- 548 unchanged lines hidden (view full) --- 1831** argv -- the argument list. 1832** pfd -- pointer to a place to store the file descriptor. 1833** e -- the current envelope. 1834** 1835** Returns: 1836** pid of the process -- -1 if it failed. 1837*/ 1838 | 1389 bp = nbp; 1390 n = nn - (p - bp); 1391 } 1392 *p++ = i; 1393 if (i == '\n') 1394 { 1395 LineNumber++; 1396 i = getc(f); --- 548 unchanged lines hidden (view full) --- 1945** argv -- the argument list. 1946** pfd -- pointer to a place to store the file descriptor. 1947** e -- the current envelope. 1948** 1949** Returns: 1950** pid of the process -- -1 if it failed. 1951*/ 1952 |
1839int | 1953pid_t |
1840prog_open(argv, pfd, e) 1841 char **argv; 1842 int *pfd; 1843 ENVELOPE *e; 1844{ | 1954prog_open(argv, pfd, e) 1955 char **argv; 1956 int *pfd; 1957 ENVELOPE *e; 1958{ |
1845 int pid; | 1959 pid_t pid; |
1846 int i; 1847 int save_errno; 1848 int fdv[2]; 1849 char *p, *q; 1850 char buf[MAXLINE + 1]; 1851 extern int DtableSize; 1852 1853 if (pipe(fdv) < 0) --- 15 unchanged lines hidden (view full) --- 1869 (void) close(fdv[1]); 1870 *pfd = fdv[0]; 1871 return pid; 1872 } 1873 1874 /* child -- close stdin */ 1875 (void) close(0); 1876 | 1960 int i; 1961 int save_errno; 1962 int fdv[2]; 1963 char *p, *q; 1964 char buf[MAXLINE + 1]; 1965 extern int DtableSize; 1966 1967 if (pipe(fdv) < 0) --- 15 unchanged lines hidden (view full) --- 1983 (void) close(fdv[1]); 1984 *pfd = fdv[0]; 1985 return pid; 1986 } 1987 1988 /* child -- close stdin */ 1989 (void) close(0); 1990 |
1991 /* Reset global flags */ 1992 RestartRequest = NULL; 1993 ShutdownRequest = NULL; 1994 PendingSignal = 0; 1995 |
|
1877 /* stdout goes back to parent */ 1878 (void) close(fdv[0]); 1879 if (dup2(fdv[1], 1) < 0) 1880 { 1881 syserr("%s: cannot dup2 for stdout", argv[0]); 1882 _exit(EX_OSERR); 1883 } 1884 (void) close(fdv[1]); --- 229 unchanged lines hidden (view full) --- 2114 if (p == NULL) 2115 return s; 2116 2117 l = strlen(s) + 1; 2118 if (bl < l) 2119 { 2120 /* allocate more space */ 2121 if (bp != NULL) | 1996 /* stdout goes back to parent */ 1997 (void) close(fdv[0]); 1998 if (dup2(fdv[1], 1) < 0) 1999 { 2000 syserr("%s: cannot dup2 for stdout", argv[0]); 2001 _exit(EX_OSERR); 2002 } 2003 (void) close(fdv[1]); --- 229 unchanged lines hidden (view full) --- 2233 if (p == NULL) 2234 return s; 2235 2236 l = strlen(s) + 1; 2237 if (bl < l) 2238 { 2239 /* allocate more space */ 2240 if (bp != NULL) |
2122 free(bp); | 2241 sm_free(bp); |
2123 bp = xalloc(l); 2124 bl = l; 2125 } 2126 (void) strlcpy(bp, s, l); 2127 for (p = bp; (p = strchr(p, '\n')) != NULL; ) 2128 *p++ = ' '; 2129 2130 if (logattacks) --- 63 unchanged lines hidden (view full) --- 2194** pid -- pid to add to list. 2195** task -- task of pid. 2196** type -- type of process. 2197** 2198** Returns: 2199** none 2200*/ 2201 | 2242 bp = xalloc(l); 2243 bl = l; 2244 } 2245 (void) strlcpy(bp, s, l); 2246 for (p = bp; (p = strchr(p, '\n')) != NULL; ) 2247 *p++ = ' '; 2248 2249 if (logattacks) --- 63 unchanged lines hidden (view full) --- 2313** pid -- pid to add to list. 2314** task -- task of pid. 2315** type -- type of process. 2316** 2317** Returns: 2318** none 2319*/ 2320 |
2202static struct procs *ProcListVec = NULL; | 2321static struct procs *volatile ProcListVec = NULL; |
2203static int ProcListSize = 0; 2204 2205void 2206proc_list_add(pid, task, type) 2207 pid_t pid; 2208 char *task; 2209 int type; 2210{ --- 22 unchanged lines hidden (view full) --- 2233 struct procs *npv; 2234 2235 npv = (struct procs *) xalloc((sizeof *npv) * 2236 (ProcListSize + PROC_LIST_SEG)); 2237 if (ProcListSize > 0) 2238 { 2239 memmove(npv, ProcListVec, 2240 ProcListSize * sizeof (struct procs)); | 2322static int ProcListSize = 0; 2323 2324void 2325proc_list_add(pid, task, type) 2326 pid_t pid; 2327 char *task; 2328 int type; 2329{ --- 22 unchanged lines hidden (view full) --- 2352 struct procs *npv; 2353 2354 npv = (struct procs *) xalloc((sizeof *npv) * 2355 (ProcListSize + PROC_LIST_SEG)); 2356 if (ProcListSize > 0) 2357 { 2358 memmove(npv, ProcListVec, 2359 ProcListSize * sizeof (struct procs)); |
2241 free(ProcListVec); | 2360 sm_free(ProcListVec); |
2242 } 2243 for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++) 2244 { 2245 npv[i].proc_pid = NO_PID; 2246 npv[i].proc_task = NULL; 2247 npv[i].proc_type = PROC_NONE; 2248 } 2249 i = ProcListSize; 2250 ProcListSize += PROC_LIST_SEG; 2251 ProcListVec = npv; 2252 } 2253 ProcListVec[i].proc_pid = pid; 2254 if (ProcListVec[i].proc_task != NULL) | 2361 } 2362 for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++) 2363 { 2364 npv[i].proc_pid = NO_PID; 2365 npv[i].proc_task = NULL; 2366 npv[i].proc_type = PROC_NONE; 2367 } 2368 i = ProcListSize; 2369 ProcListSize += PROC_LIST_SEG; 2370 ProcListVec = npv; 2371 } 2372 ProcListVec[i].proc_pid = pid; 2373 if (ProcListVec[i].proc_task != NULL) |
2255 free(ProcListVec[i].proc_task); | 2374 sm_free(ProcListVec[i].proc_task); |
2256 ProcListVec[i].proc_task = newstr(task); 2257 ProcListVec[i].proc_type = type; 2258 2259 /* if process adding itself, it's not a child */ 2260 if (pid != getpid()) 2261 CurChildren++; 2262} 2263/* --- 14 unchanged lines hidden (view full) --- 2278{ 2279 int i; 2280 2281 for (i = 0; i < ProcListSize; i++) 2282 { 2283 if (ProcListVec[i].proc_pid == pid) 2284 { 2285 if (ProcListVec[i].proc_task != NULL) | 2375 ProcListVec[i].proc_task = newstr(task); 2376 ProcListVec[i].proc_type = type; 2377 2378 /* if process adding itself, it's not a child */ 2379 if (pid != getpid()) 2380 CurChildren++; 2381} 2382/* --- 14 unchanged lines hidden (view full) --- 2397{ 2398 int i; 2399 2400 for (i = 0; i < ProcListSize; i++) 2401 { 2402 if (ProcListVec[i].proc_pid == pid) 2403 { 2404 if (ProcListVec[i].proc_task != NULL) |
2286 free(ProcListVec[i].proc_task); | 2405 sm_free(ProcListVec[i].proc_task); |
2287 ProcListVec[i].proc_task = newstr(task); 2288 break; 2289 } 2290 } 2291} 2292/* 2293** PROC_LIST_DROP -- drop pid from process list 2294** 2295** Parameters: 2296** pid -- pid to drop 2297** 2298** Returns: 2299** type of process | 2406 ProcListVec[i].proc_task = newstr(task); 2407 break; 2408 } 2409 } 2410} 2411/* 2412** PROC_LIST_DROP -- drop pid from process list 2413** 2414** Parameters: 2415** pid -- pid to drop 2416** 2417** Returns: 2418** type of process |
2419** 2420** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 2421** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 2422** DOING. |
|
2300*/ 2301 2302int 2303proc_list_drop(pid) 2304 pid_t pid; 2305{ 2306 int i; 2307 int type = PROC_NONE; --- 216 unchanged lines hidden --- | 2423*/ 2424 2425int 2426proc_list_drop(pid) 2427 pid_t pid; 2428{ 2429 int i; 2430 int type = PROC_NONE; --- 216 unchanged lines hidden --- |