summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/Makefile.in4
-rw-r--r--openbsd-compat/bsd-closefrom.c100
-rw-r--r--openbsd-compat/bsd-misc.c19
-rw-r--r--openbsd-compat/bsd-misc.h6
-rw-r--r--openbsd-compat/openbsd-compat.h6
5 files changed, 110 insertions, 25 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index 5de20abbc..0f34f2240 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -1,4 +1,4 @@
1# $Id: Makefile.in,v 1.30 2004/01/21 06:07:23 djm Exp $ 1# $Id: Makefile.in,v 1.31 2004/08/15 08:41:00 djm Exp $
2 2
3sysconfdir=@sysconfdir@ 3sysconfdir=@sysconfdir@
4piddir=@piddir@ 4piddir=@piddir@
@@ -18,7 +18,7 @@ LDFLAGS=-L. @LDFLAGS@
18 18
19OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtoul.o vis.o 19OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtoul.o vis.o
20 20
21COMPAT=bsd-arc4random.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o 21COMPAT=bsd-arc4random.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o
22 22
23PORTS=port-irix.o port-aix.o 23PORTS=port-irix.o port-aix.o
24 24
diff --git a/openbsd-compat/bsd-closefrom.c b/openbsd-compat/bsd-closefrom.c
new file mode 100644
index 000000000..61a9fa391
--- /dev/null
+++ b/openbsd-compat/bsd-closefrom.c
@@ -0,0 +1,100 @@
1/*
2 * Copyright (c) 2004 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
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
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#include <limits.h>
26#include <stdlib.h>
27#include <stddef.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>
36# endif
37# ifdef HAVE_SYS_DIR_H
38# include <sys/dir.h>
39# endif
40# ifdef HAVE_NDIR_H
41# include <ndir.h>
42# endif
43#endif
44
45#ifndef OPEN_MAX
46# define OPEN_MAX 256
47#endif
48
49RCSID("$Id: bsd-closefrom.c,v 1.1 2004/08/15 08:41:00 djm Exp $");
50
51#ifndef lint
52static const char sudorcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $";
53#endif /* lint */
54
55/*
56 * Close all file descriptors greater than or equal to lowfd.
57 */
58void
59closefrom(int lowfd)
60{
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());
70 if (len != -1 && 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
79#endif
80 {
81 /*
82 * Fall back on sysconf() or getdtablesize(). We avoid checking
83 * resource limits since it is possible to open a file descriptor
84 * and then drop the rlimit such that it is below the open fd.
85 */
86#ifdef HAVE_SYSCONF
87 maxfd = sysconf(_SC_OPEN_MAX);
88#else
89 maxfd = getdtablesize();
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}
98
99#endif /* HAVE_CLOSEFROM */
100
diff --git a/openbsd-compat/bsd-misc.c b/openbsd-compat/bsd-misc.c
index 3a30b6e4f..1b276b4f4 100644
--- a/openbsd-compat/bsd-misc.c
+++ b/openbsd-compat/bsd-misc.c
@@ -1,3 +1,4 @@
1
1/* 2/*
2 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> 3 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
3 * 4 *
@@ -17,7 +18,7 @@
17#include "includes.h" 18#include "includes.h"
18#include "xmalloc.h" 19#include "xmalloc.h"
19 20
20RCSID("$Id: bsd-misc.c,v 1.24 2004/08/13 08:37:21 dtucker Exp $"); 21RCSID("$Id: bsd-misc.c,v 1.25 2004/08/15 08:41:00 djm Exp $");
21 22
22#ifndef HAVE___PROGNAME 23#ifndef HAVE___PROGNAME
23char *__progname; 24char *__progname;
@@ -196,22 +197,6 @@ tcsendbreak(int fd, int duration)
196} 197}
197#endif /* HAVE_TCSENDBREAK */ 198#endif /* HAVE_TCSENDBREAK */
198 199
199#ifndef HAVE_CLOSEFROM
200int
201closefrom(int fd)
202{
203 int i, result = 0, err = 0;
204
205 for (i = fd; i < 128; i++)
206 if (close(i) != 0) {
207 err = errno;
208 result = -1;
209 }
210 errno = err;
211 return result;
212}
213#endif /* HAVE_CLOSEFROM */
214
215mysig_t 200mysig_t
216mysignal(int sig, mysig_t act) 201mysignal(int sig, mysig_t act)
217{ 202{
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h
index 7027815d6..33a1d707f 100644
--- a/openbsd-compat/bsd-misc.h
+++ b/openbsd-compat/bsd-misc.h
@@ -1,4 +1,4 @@
1/* $Id: bsd-misc.h,v 1.16 2004/06/25 04:03:34 dtucker Exp $ */ 1/* $Id: bsd-misc.h,v 1.17 2004/08/15 08:41:00 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> 4 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
@@ -93,10 +93,6 @@ int tcsendbreak(int, int);
93void unsetenv(const char *); 93void unsetenv(const char *);
94#endif 94#endif
95 95
96#ifndef HAVE_CLOSEFROM
97int closefrom(int);
98#endif
99
100/* wrapper for signal interface */ 96/* wrapper for signal interface */
101typedef void (*mysig_t)(int); 97typedef void (*mysig_t)(int);
102mysig_t mysignal(int sig, mysig_t act); 98mysig_t mysignal(int sig, mysig_t act);
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
index 6be1bcda4..89d1454e0 100644
--- a/openbsd-compat/openbsd-compat.h
+++ b/openbsd-compat/openbsd-compat.h
@@ -1,4 +1,4 @@
1/* $Id: openbsd-compat.h,v 1.25 2004/01/21 06:07:23 djm Exp $ */ 1/* $Id: openbsd-compat.h,v 1.26 2004/08/15 08:41:00 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1999-2003 Damien Miller. All rights reserved. 4 * Copyright (c) 1999-2003 Damien Miller. All rights reserved.
@@ -48,6 +48,10 @@ char *basename(const char *path);
48int bindresvport_sa(int sd, struct sockaddr *sa); 48int bindresvport_sa(int sd, struct sockaddr *sa);
49#endif 49#endif
50 50
51#ifndef HAVE_CLOSEFROM
52void closefrom(int);
53#endif
54
51#ifndef HAVE_GETCWD 55#ifndef HAVE_GETCWD
52char *getcwd(char *pt, size_t size); 56char *getcwd(char *pt, size_t size);
53#endif 57#endif