bsd-closefrom.c (157016) | bsd-closefrom.c (162852) |
---|---|
1/* | 1/* |
2 * Copyright (c) 2004 Todd C. Miller | 2 * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.com> |
3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR --- 6 unchanged lines hidden (view full) --- 17#include "includes.h" 18 19#ifndef HAVE_CLOSEFROM 20 21#include <sys/types.h> 22#include <sys/param.h> 23#include <unistd.h> 24#include <stdio.h> | 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR --- 6 unchanged lines hidden (view full) --- 17#include "includes.h" 18 19#ifndef HAVE_CLOSEFROM 20 21#include <sys/types.h> 22#include <sys/param.h> 23#include <unistd.h> 24#include <stdio.h> |
25#ifdef HAVE_FCNTL_H 26# include <fcntl.h> 27#endif |
|
25#include <limits.h> 26#include <stdlib.h> 27#include <stddef.h> | 28#include <limits.h> 29#include <stdlib.h> 30#include <stddef.h> |
31#include <string.h> 32#include <unistd.h> |
|
28#ifdef HAVE_DIRENT_H 29# include <dirent.h> 30# define NAMLEN(dirent) strlen((dirent)->d_name) 31#else 32# define dirent direct 33# define NAMLEN(dirent) (dirent)->d_namlen 34# ifdef HAVE_SYS_NDIR_H 35# include <sys/ndir.h> --- 5 unchanged lines hidden (view full) --- 41# include <ndir.h> 42# endif 43#endif 44 45#ifndef OPEN_MAX 46# define OPEN_MAX 256 47#endif 48 | 33#ifdef HAVE_DIRENT_H 34# include <dirent.h> 35# define NAMLEN(dirent) strlen((dirent)->d_name) 36#else 37# define dirent direct 38# define NAMLEN(dirent) (dirent)->d_namlen 39# ifdef HAVE_SYS_NDIR_H 40# include <sys/ndir.h> --- 5 unchanged lines hidden (view full) --- 46# include <ndir.h> 47# endif 48#endif 49 50#ifndef OPEN_MAX 51# define OPEN_MAX 256 52#endif 53 |
49RCSID("$Id: bsd-closefrom.c,v 1.2 2005/11/10 08:29:13 dtucker Exp $"); 50 51#ifndef lint 52static const char sudorcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $"; | 54#if 0 55__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $"; |
53#endif /* lint */ 54 55/* 56 * Close all file descriptors greater than or equal to lowfd. 57 */ | 56#endif /* lint */ 57 58/* 59 * Close all file descriptors greater than or equal to lowfd. 60 */ |
61#ifdef HAVE_FCNTL_CLOSEM |
|
58void 59closefrom(int lowfd) 60{ | 62void 63closefrom(int lowfd) 64{ |
65 (void) fcntl(lowfd, F_CLOSEM, 0); 66} 67#else 68void 69closefrom(int lowfd) 70{ |
|
61 long fd, maxfd; 62#if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID) 63 char fdpath[PATH_MAX], *endp; 64 struct dirent *dent; 65 DIR *dirp; 66 int len; 67 68 /* Check for a /proc/$$/fd directory. */ 69 len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid()); | 71 long fd, maxfd; 72#if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID) 73 char fdpath[PATH_MAX], *endp; 74 struct dirent *dent; 75 DIR *dirp; 76 int len; 77 78 /* Check for a /proc/$$/fd directory. */ 79 len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid()); |
70 if (len >= 0 && (u_int)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) { | 80 if (len > 0 && (size_t)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) { |
71 while ((dent = readdir(dirp)) != NULL) { 72 fd = strtol(dent->d_name, &endp, 10); 73 if (dent->d_name != endp && *endp == '\0' && 74 fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp)) 75 (void) close((int) fd); 76 } 77 (void) closedir(dirp); 78 } else --- 11 unchanged lines hidden (view full) --- 90#endif /* HAVE_SYSCONF */ 91 if (maxfd < 0) 92 maxfd = OPEN_MAX; 93 94 for (fd = lowfd; fd < maxfd; fd++) 95 (void) close((int) fd); 96 } 97} | 81 while ((dent = readdir(dirp)) != NULL) { 82 fd = strtol(dent->d_name, &endp, 10); 83 if (dent->d_name != endp && *endp == '\0' && 84 fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp)) 85 (void) close((int) fd); 86 } 87 (void) closedir(dirp); 88 } else --- 11 unchanged lines hidden (view full) --- 100#endif /* HAVE_SYSCONF */ 101 if (maxfd < 0) 102 maxfd = OPEN_MAX; 103 104 for (fd = lowfd; fd < maxfd; fd++) 105 (void) close((int) fd); 106 } 107} |
98 | 108#endif /* !HAVE_FCNTL_CLOSEM */ |
99#endif /* HAVE_CLOSEFROM */ | 109#endif /* HAVE_CLOSEFROM */ |
100 | |