summaryrefslogtreecommitdiff
path: root/openbsd-compat/bsd-closefrom.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/bsd-closefrom.c')
-rw-r--r--openbsd-compat/bsd-closefrom.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/openbsd-compat/bsd-closefrom.c b/openbsd-compat/bsd-closefrom.c
index 5b7b94ae4..9380b33a7 100644
--- a/openbsd-compat/bsd-closefrom.c
+++ b/openbsd-compat/bsd-closefrom.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2004 Todd C. Miller <Todd.Miller@courtesan.com> 2 * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.com>
3 * 3 *
4 * Permission to use, copy, modify, and distribute this software for any 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 5 * purpose with or without fee is hereby granted, provided that the above
@@ -22,9 +22,14 @@
22#include <sys/param.h> 22#include <sys/param.h>
23#include <unistd.h> 23#include <unistd.h>
24#include <stdio.h> 24#include <stdio.h>
25#ifdef HAVE_FCNTL_H
26# include <fcntl.h>
27#endif
25#include <limits.h> 28#include <limits.h>
26#include <stdlib.h> 29#include <stdlib.h>
27#include <stddef.h> 30#include <stddef.h>
31#include <string.h>
32#include <unistd.h>
28#ifdef HAVE_DIRENT_H 33#ifdef HAVE_DIRENT_H
29# include <dirent.h> 34# include <dirent.h>
30# define NAMLEN(dirent) strlen((dirent)->d_name) 35# define NAMLEN(dirent) strlen((dirent)->d_name)
@@ -46,15 +51,20 @@
46# define OPEN_MAX 256 51# define OPEN_MAX 256
47#endif 52#endif
48 53
49RCSID("$Id: bsd-closefrom.c,v 1.2 2005/11/10 08:29:13 dtucker Exp $"); 54#if 0
50 55__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
51#ifndef lint
52static const char sudorcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $";
53#endif /* lint */ 56#endif /* lint */
54 57
55/* 58/*
56 * Close all file descriptors greater than or equal to lowfd. 59 * Close all file descriptors greater than or equal to lowfd.
57 */ 60 */
61#ifdef HAVE_FCNTL_CLOSEM
62void
63closefrom(int lowfd)
64{
65 (void) fcntl(lowfd, F_CLOSEM, 0);
66}
67#else
58void 68void
59closefrom(int lowfd) 69closefrom(int lowfd)
60{ 70{
@@ -67,7 +77,7 @@ closefrom(int lowfd)
67 77
68 /* Check for a /proc/$$/fd directory. */ 78 /* Check for a /proc/$$/fd directory. */
69 len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid()); 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) { 81 while ((dent = readdir(dirp)) != NULL) {
72 fd = strtol(dent->d_name, &endp, 10); 82 fd = strtol(dent->d_name, &endp, 10);
73 if (dent->d_name != endp && *endp == '\0' && 83 if (dent->d_name != endp && *endp == '\0' &&
@@ -95,6 +105,5 @@ closefrom(int lowfd)
95 (void) close((int) fd); 105 (void) close((int) fd);
96 } 106 }
97} 107}
98 108#endif /* !HAVE_FCNTL_CLOSEM */
99#endif /* HAVE_CLOSEFROM */ 109#endif /* HAVE_CLOSEFROM */
100