* [PATCH 0/3] Handle lack of F_DUPFD_CLOEXEC support
@ 2015-06-06 4:19 Guillem Jover
2015-06-06 4:19 ` [PATCH 1/3] lib/fileutils: Add new dup_fd_cloexec function Guillem Jover
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Guillem Jover @ 2015-06-06 4:19 UTC (permalink / raw)
To: util-linux; +Cc: Steven Chamberlain
Hi!
These patches make the code gracefully handle missing F_DUPFD_CLOEXEC
support at build and run-time. This affects at least current GNU/kFreeBSD
systems. I've included the last patch so that GNU/kFreeBSD systems can
immediately benefit from F_DUPFD_CLOEXEC before glibc is updated there,
but if you prefer to omit that one out of disgust, that would be fine I
guess.
While the actual problem is that sysfs code is being built and used on
non-Linux systems, it is too entrenched to get rid of it w/o substantial
amounts of work. Also GNU/kFreeBSD does have limited sysfs support via
the compat linsysfs, so it might not be entirely useless there. But it
certainly is not useful on GNU/Hurd though, at least not currently.
The patches were tested by Steven Chamberlain.
Thanks,
Guillem
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
include/c.h | 6 ++++++
include/fileutils.h | 1 +
lib/fileutils.c | 30 ++++++++++++++++++++++++++++++
lib/sysfs.c | 2 +-
4 files changed, 38 insertions(+), 1 deletion(-)
--
2.2.1.209.g41e5f3a
^ permalink raw reply [flat|nested] 5+ messages in thread
* [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
end of thread, other threads:[~2015-06-08 10:21 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox