From 5539c50f95a17ff108b1139b488933287e9c4f45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
<grubba@grubba.org>
Date: Mon, 8 Jun 1998 14:48:30 +0200
Subject: [PATCH] Added fallback for nonexistant/nonworking F_SETFD.
Rev: src/acconfig.h:1.27
Rev: src/configure.in:1.202
Rev: src/fd_control.c:1.18
Rev: src/fd_control.h:1.4
Rev: src/fdlib.h:1.18
Rev: src/modules/files/efuns.c:1.50
Rev: src/signal_handler.c:1.68
---
src/acconfig.h | 5 +++-
src/configure.in | 46 +++++++++++++++++++++++++++++++++++-
src/fd_control.c | 49 +++++++++++++++++++++++++++++++++++----
src/fd_control.h | 6 ++++-
src/fdlib.h | 10 +++++++-
src/modules/files/efuns.c | 7 +++++-
src/signal_handler.c | 6 ++++-
7 files changed, 118 insertions(+), 11 deletions(-)
diff --git a/src/acconfig.h b/src/acconfig.h
index 524d70e278..5bfcbd4a45 100644
--- a/src/acconfig.h
+++ b/src/acconfig.h
@@ -1,5 +1,5 @@
/*
- * $Id: acconfig.h,v 1.26 1998/06/06 03:11:48 hubbe Exp $
+ * $Id: acconfig.h,v 1.27 1998/06/08 12:48:21 grubba Exp $
*/
#ifndef MACHINE_H
#define MACHINE_H
@@ -195,6 +195,9 @@
/* Define if your cpp supports the ANSI concatenation operator ## */
#undef HAVE_ANSI_CONCAT
+/* Define if you don't have F_SETFD, or it doesn't work */
+#undef HAVE_BROKEN_F_SETFD
+
/* Define if your cpp supports K&R-style concatenation */
#undef HAVE_KR_CONCAT
diff --git a/src/configure.in b/src/configure.in
index b369cfc718..cf78fb2dea 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -1,4 +1,4 @@
-AC_REVISION("$Id: configure.in,v 1.201 1998/06/07 20:40:30 grubba Exp $")
+AC_REVISION("$Id: configure.in,v 1.202 1998/06/08 12:48:23 grubba Exp $")
AC_INIT(interpret.c)
AC_CONFIG_HEADER(machine.h)
@@ -983,6 +983,7 @@ AC_CHECK_FUNCS( \
memcpy \
memset \
mktime \
+ perror \
rindex \
setlocale \
setrlimit \
@@ -1057,6 +1058,49 @@ if test $ac_cv_func_crypt$ac_cv_func__crypt = nono ; then
fi
+#############################################################################
+
+# FreeBSD 3.0 has broken F_SETFD when running with threads.
+
+AC_MSG_CHECKING(whether F_SETFD exists and works)
+
+AC_CACHE_VAL(pike_cv_broken_f_setfd, [
+ AC_TRY_RUN([
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif /* HAVE_SYS_TYPES_H */
+#include <sys/stat.h>
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
+#include <stdio.h>
+#ifndef FD_CLOEXEC
+#define FD_CLOEXEC 1
+#endif /* FD_CLOEXEC */
+int main(int argc, char **argv)
+{
+ int fd = 0;
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
+ exit(1);
+ }
+ if (fcntl(fd, F_GETFD) != FD_CLOEXEC) {
+ exit(1);
+ }
+ exit(0);
+}
+], pike_cv_broken_f_setfd=no, pike_cv_broken_f_setfd=yes,
+ pike_cv_broken_f_setfd=no)
+])
+
+if test "x$pike_cv_broken_f_setfd" = "xyes"; then
+ AC_MSG_RESULT(no)
+ AC_DEFINE(HAVE_BROKEN_F_SETFD)
+else
+ AC_MSG_RESULT(yes)
+fi
+
+#############################################################################
+
AC_STRUCT_TM
#############################################################################
diff --git a/src/fd_control.c b/src/fd_control.c
index a4dd677b3b..68b0b7a014 100644
--- a/src/fd_control.c
+++ b/src/fd_control.c
@@ -9,7 +9,7 @@
#include "error.h"
#include "fdlib.h"
-RCSID("$Id: fd_control.c,v 1.17 1998/03/28 15:30:41 grubba Exp $");
+RCSID("$Id: fd_control.c,v 1.18 1998/06/08 12:48:24 grubba Exp $");
#else
#define _FILE_OFFSET_BITS 64
@@ -103,13 +103,52 @@ int query_nonblocking(int fd)
#endif
}
+#ifndef FD_CLOEXEC
+#define FD_CLOEXEC 1
+#endif /* FD_CLOEXEC */
+
+#ifdef HAVE_BROKEN_F_SETFD
+static int fds_to_close[MAX_OPEN_FILEDESCRIPTORS];
+static int num_fds_to_close = 0;
+
+void do_close_on_exec(void)
+{
+ int i;
+ for(i=0; i < num_fds_to_close; i++) {
+ close(fds_to_close[i]);
+ }
+ num_fds_to_close = 0;
+}
+#endif /* HAVE_BROKEN_F_SETFD */
+
int set_close_on_exec(int fd, int which)
{
-#ifdef F_SETFD
- return fcntl(fd, F_SETFD, !!which);
-#else
+#ifndef HAVE_BROKEN_F_SETFD
+ if (which) {
+ return fcntl(fd, F_SETFD, FD_CLOEXEC);
+ } else {
+ return fcntl(fd, F_SETFD, 0);
+ }
+#else /* HAVE_BROKEN_F_SETFD */
+ int i;
+ if (which) {
+ for(i = 0; i < num_fds_to_close; i++) {
+ if (fds_to_close[i] == fd) {
+ return(0); /* Already marked */
+ }
+ }
+ fds_to_close[num_fds_to_close++] = fd;
+ return(0);
+ } else {
+ for(i = 0; i < num_fds_to_close; i++) {
+ while (fds_to_close[i] == fd && (i < num_fds_to_close)) {
+ fds_to_close[i] = fds_to_close[--num_fds_to_close];
+ }
+ }
+ return(0);
+ }
return 0;
-#endif
+#endif /* !HAVE_BROKEN_F_SETFD */
}
#ifdef TESTING
diff --git a/src/fd_control.h b/src/fd_control.h
index 0306ad9fdc..7eeaf4b1fc 100644
--- a/src/fd_control.h
+++ b/src/fd_control.h
@@ -5,7 +5,7 @@
\*/
/*
- * $Id: fd_control.h,v 1.3 1998/03/28 15:30:13 grubba Exp $
+ * $Id: fd_control.h,v 1.4 1998/06/08 12:48:26 grubba Exp $
*/
#ifndef FD_CONTROL_H
#define FD_CONTROL_H
@@ -14,6 +14,10 @@
int set_nonblocking(int fd,int which);
int query_nonblocking(int fd);
int set_close_on_exec(int fd, int which);
+
+#ifdef HAVE_BROKEN_F_SETFD
+void do_close_on_exec(void);
+#endif /* HAVE_BROKEN_F_SETFD */
/* Prototypes end here */
#endif
diff --git a/src/fdlib.h b/src/fdlib.h
index 10ec39bf92..869c44781b 100644
--- a/src/fdlib.h
+++ b/src/fdlib.h
@@ -1,5 +1,5 @@
/*
- * $Id: fdlib.h,v 1.17 1998/05/28 04:24:47 hubbe Exp $
+ * $Id: fdlib.h,v 1.18 1998/06/08 12:48:27 grubba Exp $
*/
#ifndef FDLIB_H
#define FDLIB_H
@@ -231,7 +231,11 @@ typedef int FD;
#define fd_query_properties(X,Y) ( fd_INTERPROCESSABLE | (Y))
#define fd_open open
+#ifdef HAVE_BROKEN_F_SETFD
+#define fd_close(FD__) (set_close_on_exec(FD__,0), close(FD__))
+#else /* !HAVE_BROKEN_F_SETFD */
#define fd_close close
+#endif /* HAVE_BROKEN_F_SETFD */
#define fd_read read
#define fd_write write
#define fd_ioctl ioctl
@@ -253,7 +257,11 @@ typedef int FD;
#define fd_lseek lseek
#define fd_fstat fstat
#define fd_dup dup
+#ifdef HAVE_BROKEN_F_SETFD
+#define fd_dup2(FD__, FD2__) (set_close_on_exec(FD2__, 0), dup2(FD__, FD2__))
+#else /* !HAVE_BROKEN_F_SETFD */
#define fd_dup2 dup2
+#endif /* HAVE_BROKEN_F_SETFD */
#define fd_listen listen
#define fd_select select
diff --git a/src/modules/files/efuns.c b/src/modules/files/efuns.c
index 18391dd90c..cfc4f25d8a 100644
--- a/src/modules/files/efuns.c
+++ b/src/modules/files/efuns.c
@@ -22,7 +22,7 @@
#include "file_machine.h"
#include "file.h"
-RCSID("$Id: efuns.c,v 1.49 1998/05/17 21:06:48 grubba Exp $");
+RCSID("$Id: efuns.c,v 1.50 1998/06/08 12:48:30 grubba Exp $");
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
@@ -680,6 +680,11 @@ void f_exece(INT32 args)
#else
#define DOCAST(X) (X)
#endif
+
+#ifdef HAVE_BROKEN_F_SETFD
+ do_close_on_exec();
+#endif /* HAVE_BROKEN_F_SETFD */
+
execve(argv[0],DOCAST(argv),DOCAST(env));
free((char *)argv);
diff --git a/src/signal_handler.c b/src/signal_handler.c
index 20ef39965b..a11c856939 100644
--- a/src/signal_handler.c
+++ b/src/signal_handler.c
@@ -22,7 +22,7 @@
#include "builtin_functions.h"
#include <signal.h>
-RCSID("$Id: signal_handler.c,v 1.67 1998/06/06 03:11:48 hubbe Exp $");
+RCSID("$Id: signal_handler.c,v 1.68 1998/06/08 12:48:28 grubba Exp $");
#ifdef HAVE_PASSWD_H
# include <passwd.h>
@@ -1418,6 +1418,10 @@ void f_create_process(INT32 args)
set_close_on_exec(1,0);
set_close_on_exec(2,0);
+#ifdef HAVE_BROKEN_F_SETFD
+ do_close_on_exec();
+#endif /* HAVE_BROKEN_F_SETFD */
+
execvp(storage.argv[0],storage.argv);
exit(69);
}
--
GitLab