summaryrefslogtreecommitdiffstats
authornicm <nicm>2010-10-27 20:21:00 (GMT)
committer Raghavendra D Prabhu <[email protected]>2011-01-02 18:59:31 (GMT)
commit777fd509c28dc97c9c70c72bfd5c2132fff848e7 (patch) (side-by-side diff)
tree487563790c51fb947fdf6cd53a1d0e7d74d8bbdb
parent06ab4c63755237dbc76b3e278b3990eff3241982 (diff)
downloadtmux-old-777fd509c28dc97c9c70c72bfd5c2132fff848e7.zip
tmux-old-777fd509c28dc97c9c70c72bfd5c2132fff848e7.tar.gz
tmux-old-777fd509c28dc97c9c70c72bfd5c2132fff848e7.tar.bz2
Compat for closefrom().
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--compat.h10
-rw-r--r--compat/closefrom.c111
-rwxr-xr-xconfigure25
3 files changed, 140 insertions, 6 deletions
diff --git a/compat.h b/compat.h
index 6db22fa..bf459ac 100644
--- a/compat.h
+++ b/compat.h
@@ -1,4 +1,4 @@
-/* $Id: compat.h,v 1.26 2010/09/07 19:32:58 nicm Exp $ */
+/* $Id: compat.h,v 1.27 2010/10/27 20:21:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <[email protected]>
@@ -163,6 +163,14 @@ typedef uint64_t u_int64_t;
#define bzero(buf, len) memset(buf, 0, len);
#endif
+#ifndef HAVE_CLOSEFROM
+/* closefrom.c */
+#define HAVE_FCNTL_H
+#define HAVE_DIRENT_H
+#define HAVE_SYSCONF
+void closefrom(int);
+#endif
+
#ifndef HAVE_STRCASESTR
/* strcasestr.c */
char *strcasestr(const char *, const char *);
diff --git a/compat/closefrom.c b/compat/closefrom.c
new file mode 100644
index 0000000..981958e
--- a/dev/null
+++ b/compat/closefrom.c
@@ -0,0 +1,111 @@
+/* $Id: closefrom.c,v 1.1 2010/10/27 20:21:01 nicm Exp $ */
+
+/*
+ * Copyright (c) 2004-2005 Todd C. Miller <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "tmux.h"
+
+#ifndef HAVE_CLOSEFROM
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <unistd.h>
+#include <stdio.h>
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+#include <limits.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+#ifdef HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen((dirent)->d_name)
+#else
+# define dirent direct
+# define NAMLEN(dirent) (dirent)->d_namlen
+# ifdef HAVE_SYS_NDIR_H
+# include <sys/ndir.h>
+# endif
+# ifdef HAVE_SYS_DIR_H
+# include <sys/dir.h>
+# endif
+# ifdef HAVE_NDIR_H
+# include <ndir.h>
+# endif
+#endif
+
+#ifndef OPEN_MAX
+# define OPEN_MAX 256
+#endif
+
+#if 0
+__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
+#endif /* lint */
+
+/*
+ * Close all file descriptors greater than or equal to lowfd.
+ */
+#ifdef HAVE_FCNTL_CLOSEM
+void
+closefrom(int lowfd)
+{
+ (void) fcntl(lowfd, F_CLOSEM, 0);
+}
+#else
+void
+closefrom(int lowfd)
+{
+ long fd, maxfd;
+#if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
+ char fdpath[PATH_MAX], *endp;
+ struct dirent *dent;
+ DIR *dirp;
+ int len;
+
+ /* Check for a /proc/$$/fd directory. */
+ len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
+ if (len > 0 && (size_t)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
+ while ((dent = readdir(dirp)) != NULL) {
+ fd = strtol(dent->d_name, &endp, 10);
+ if (dent->d_name != endp && *endp == '\0' &&
+ fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
+ (void) close((int) fd);
+ }
+ (void) closedir(dirp);
+ } else
+#endif
+ {
+ /*
+ * Fall back on sysconf() or getdtablesize(). We avoid checking
+ * resource limits since it is possible to open a file descriptor
+ * and then drop the rlimit such that it is below the open fd.
+ */
+#ifdef HAVE_SYSCONF
+ maxfd = sysconf(_SC_OPEN_MAX);
+#else
+ maxfd = getdtablesize();
+#endif /* HAVE_SYSCONF */
+ if (maxfd < 0)
+ maxfd = OPEN_MAX;
+
+ for (fd = lowfd; fd < maxfd; fd++)
+ (void) close((int) fd);
+ }
+}
+#endif /* !HAVE_FCNTL_CLOSEM */
+#endif /* HAVE_CLOSEFROM */
diff --git a/configure b/configure
index 7a9af9f..b9f4e19 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
#!/bin/sh
-# $Id: configure,v 1.54 2010/09/07 19:32:58 nicm Exp $
+# $Id: configure,v 1.55 2010/10/27 20:21:00 nicm Exp $
#
# Copyright (c) 2009 Nicholas Marriott <[email protected]>
#
@@ -33,14 +33,18 @@ cat <<EOF >>$CONFIG_H
#undef HAVE_BROKEN_KQUEUE
#undef HAVE_BROKEN_POLL
#undef HAVE_BZERO
+#undef HAVE_CLOSEFROM
#undef HAVE_CRYPT_H
#undef HAVE_DAEMON
+#undef HAVE_DIRFD
+#undef HAVE_FCNTL_CLOSEM
#undef HAVE_FGETLN
#undef HAVE_FORKPTY
#undef HAVE_GETOPT
#undef HAVE_IMSG
#undef HAVE_LIBUTIL_H
#undef HAVE_PATHS_H
+#undef HAVE_PROC_PID
#undef HAVE_PROGNAME
#undef HAVE_PTY_H
#undef HAVE_QUEUE_H
@@ -64,6 +68,7 @@ case $TMUX_PLATFORM in
#define HAVE_ASPRINTF
#define HAVE_BITSTRING_H
#define HAVE_BZERO
+#define HAVE_CLOSEFROM
#define HAVE_DAEMON
#define HAVE_FGETLN
#define HAVE_FORKPTY
@@ -96,8 +101,10 @@ EOF
#define HAVE_ASPRINTF
#define HAVE_BZERO
#define HAVE_DAEMON
+#define HAVE_DIRFD
#define HAVE_FORKPTY
#define HAVE_PATHS_H
+#define HAVE_PROC_PID
#define HAVE_PROGNAME
#define HAVE_PTY_H
#define HAVE_SETENV
@@ -110,11 +117,12 @@ EOF
CFLAGS+= -std=c99 -D_GNU_SOURCE -D_POSIX_SOURCE
LIBS+= -lncurses -lcrypt -lutil -levent -lrt
SRCS+= osdep-linux.c \
+ compat/closefrom.c \
compat/fgetln.c \
compat/strlcat.c \
compat/strlcpy.c \
compat/strtonum.c \
- compat/getopt.c \
+ compat/getopt.c \
compat/vis.c \
compat/unvis.c \
compat/imsg-buffer.c \
@@ -133,6 +141,7 @@ EOF
LIBS+= -lcurses -levent
SRCS+= osdep-unknown.c \
compat/asprintf.c \
+ compat/closefrom.c \
compat/daemon.c \
compat/forkpty-aix.c \
compat/strcasestr.c \
@@ -151,6 +160,7 @@ EOF
# ------------------------------------------------------------------------------
SunOS)
cat <<EOF >>$CONFIG_H
+#define HAVE_CLOSEFROM
#define HAVE_CRYPT_H
#define HAVE_STRLCAT
#define HAVE_STRLCPY
@@ -183,13 +193,15 @@ EOF
#define HAVE_BROKEN_POLL
#define HAVE_BZERO
#define HAVE_DAEMON
+#define HAVE_DIRFD
#define HAVE_FGETLN
#define HAVE_FORKPTY
#define HAVE_GETOPT
#define HAVE_PATHS_H
+#define HAVE_PROC_PID
#define HAVE_PROGNAME
-#define HAVE_STDINT_H
#define HAVE_SETENV
+#define HAVE_STDINT_H
#define HAVE_STRCASESTR
#define HAVE_STRLCAT
#define HAVE_STRLCPY
@@ -202,6 +214,7 @@ CPPFLAGS+= -I/opt/local/include
LDFLAGS+= -L/opt/local/lib
LIBS+= -lcurses -levent
SRCS+= osdep-darwin.c \
+ compat/closefrom.c \
compat/strtonum.c \
compat/vis.c \
compat/unvis.c \
@@ -215,6 +228,7 @@ EOF
#define HAVE_ASPRINTF
#define HAVE_BROKEN_KQUEUE
#define HAVE_BZERO
+#define HAVE_CLOSEFROM
#define HAVE_DAEMON
#define HAVE_FGETLN
#define HAVE_FORKPTY
@@ -228,8 +242,8 @@ EOF
#define HAVE_STRCASESTR
#define HAVE_STRLCAT
#define HAVE_STRLCPY
-#define HAVE_STRTONUM
#define HAVE_STRSEP
+#define HAVE_STRTONUM
#define HAVE_U_INT
EOF
cat <<EOF >>$CONFIG_MK
@@ -246,14 +260,15 @@ EOF
cat <<EOF >>$CONFIG_H
#define HAVE_ASPRINTF
#define HAVE_BZERO
+#define HAVE_CLOSEFROM
#define HAVE_DAEMON
#define HAVE_FGETLN
#define HAVE_FORKPTY
#define HAVE_GETOPT
#define HAVE_PATHS_H
#define HAVE_PROGNAME
-#define HAVE_SETPROCTITLE
#define HAVE_SETENV
+#define HAVE_SETPROCTITLE
#define HAVE_STDINT_H
#define HAVE_STRCASESTR
#define HAVE_STRLCAT