* [PATCH 1/3] lib/fileutils: Add new dup_fd_cloexec function
2015-06-06 4:19 [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Guillem Jover
@ 2015-06-06 4:19 ` Guillem Jover
2015-06-06 4:19 ` [PATCH 2/3] lib/sysfs: Use dup_fd_cloexec instead of direct call to fcntl Guillem Jover
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Guillem Jover @ 2015-06-06 4:19 UTC (permalink / raw)
To: util-linux; +Cc: Steven Chamberlain
This function duplicates and marks a file descriptor as close-on-exec.
Takes care of build and run-time support for the fcntl F_DUPFD_CLOEXEC
command, and other errors.
Signed-off-by: Guillem Jover <guillem@hadrons.org>
---
include/fileutils.h | 1 +
lib/fileutils.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/include/fileutils.h b/include/fileutils.h
index 3353f69..e0e2ece 100644
--- a/include/fileutils.h
+++ b/include/fileutils.h
@@ -25,6 +25,7 @@ static inline FILE *xfmkstemp(char **tmpname, char *dir)
return ret;
}
+extern int dup_fd_cloexec(int oldfd, int lowfd);
extern int get_fd_tabsize(void);
extern int mkdir_p(const char *path, mode_t mode);
diff --git a/lib/fileutils.c b/lib/fileutils.c
index bffa8ff..81b38ad 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -50,6 +50,36 @@ int xmkstemp(char **tmpname, char *dir)
return fd;
}
+int dup_fd_cloexec(int oldfd, int lowfd)
+{
+ int fd, flags, errno_save;
+
+#ifdef F_DUPFD_CLOEXEC
+ fd = fcntl(oldfd, F_DUPFD_CLOEXEC, lowfd);
+ if (fd >= 0)
+ return fd;
+#endif
+
+ fd = dup(oldfd);
+ if (fd < 0)
+ return fd;
+
+ flags = fcntl(fd, F_GETFD);
+ if (flags < 0)
+ goto unwind;
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
+ goto unwind;
+
+ return fd;
+
+unwind:
+ errno_save = errno;
+ close(fd);
+ errno = errno_save;
+
+ return -1;
+}
+
/*
* portable getdtablesize()
*/
--
2.2.1.209.g41e5f3a
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 2/3] lib/sysfs: Use dup_fd_cloexec instead of direct call to fcntl
2015-06-06 4:19 [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Guillem Jover
2015-06-06 4:19 ` [PATCH 1/3] lib/fileutils: Add new dup_fd_cloexec function Guillem Jover
@ 2015-06-06 4:19 ` Guillem Jover
2015-06-06 4:19 ` [PATCH 3/3] include/c: Define F_DUPFD_CLOEXEC on kFreeBSD systems if missing Guillem Jover
2015-06-08 10:21 ` [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Karel Zak
3 siblings, 0 replies; 5+ messages in thread
From: Guillem Jover @ 2015-06-06 4:19 UTC (permalink / raw)
To: util-linux; +Cc: Steven Chamberlain
Signed-off-by: Guillem Jover <guillem@hadrons.org>
---
lib/sysfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/sysfs.c b/lib/sysfs.c
index 07b6b56..caf89bf 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -267,7 +267,7 @@ DIR *sysfs_opendir(struct sysfs_cxt *cxt, const char *attr)
* -- we cannot use cxt->sysfs_fd directly, because closedir()
* will close this our persistent file descriptor.
*/
- fd = fcntl(cxt->dir_fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
+ fd = dup_fd_cloexec(cxt->dir_fd, STDERR_FILENO + 1);
if (fd < 0)
return NULL;
--
2.2.1.209.g41e5f3a
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] include/c: Define F_DUPFD_CLOEXEC on kFreeBSD systems if missing
2015-06-06 4:19 [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Guillem Jover
2015-06-06 4:19 ` [PATCH 1/3] lib/fileutils: Add new dup_fd_cloexec function Guillem Jover
2015-06-06 4:19 ` [PATCH 2/3] lib/sysfs: Use dup_fd_cloexec instead of direct call to fcntl Guillem Jover
@ 2015-06-06 4:19 ` Guillem Jover
2015-06-08 10:21 ` [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Karel Zak
3 siblings, 0 replies; 5+ messages in thread
From: Guillem Jover @ 2015-06-06 4:19 UTC (permalink / raw)
To: util-linux; +Cc: Steven Chamberlain
The kernel of FreeBSD version 10 and higher supports this fcntl command,
but the system libc, in this case glibc, might not yet know about it.
Signed-off-by: Guillem Jover <guillem@hadrons.org>
---
include/c.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/include/c.h b/include/c.h
index a72e264..1eda75f 100644
--- a/include/c.h
+++ b/include/c.h
@@ -231,6 +231,12 @@ static inline int dirfd(DIR *d)
#define O_CLOEXEC 0
#endif
+#ifdef __FreeBSD_kernel__
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
+#endif
+#endif
+
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0x0020
--
2.2.1.209.g41e5f3a
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support
2015-06-06 4:19 [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support Guillem Jover
` (2 preceding siblings ...)
2015-06-06 4:19 ` [PATCH 3/3] include/c: Define F_DUPFD_CLOEXEC on kFreeBSD systems if missing Guillem Jover
@ 2015-06-08 10:21 ` Karel Zak
3 siblings, 0 replies; 5+ messages in thread
From: Karel Zak @ 2015-06-08 10:21 UTC (permalink / raw)
To: Guillem Jover; +Cc: util-linux, Steven Chamberlain
> Guillem Jover (3):
> lib/fileutils: Add new dup_fd_cloexec function
> lib/sysfs: Use dup_fd_cloexec instead of direct call to fcntl
> include/c: Define F_DUPFD_CLOEXEC on kFreeBSD systems if missing
Applied, thanks.
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 5+ messages in thread