Skip to contentSkip to conversation

libbpf: ensure F_DUPFD_CLOEXEC is defined

PATCH In review James Hilliard 3 participants 7 messages Feb 27, 2022 – Mar 8, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: ensure F_DUPFD_CLOEXEC is defined

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

   fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                  ^~~~~~~~~~~~~~~
                  FD_CLOEXEC
---
 tools/lib/bpf/libbpf_internal.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +4 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644
@@ -31,6 +31,10 @@
 #define EM_BPF 247
 #endif
 
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif
+
 #ifndef R_BPF_64_64
 #define R_BPF_64_64 1
 #endif
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi James,

Quoted 5 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Quoted 7 lines

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

    fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                   ^~~~~~~~~~~~~~~
                   FD_CLOEXEC

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

Quoted 10 lines
---
  tools/lib/bpf/libbpf_internal.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +0 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644

tools/lib/bpf/libbpf_internal.h line 40

Quoted 10 lines
tools/lib/bpf/libbpf_internal.h +4 -0
@@ -31,6 +31,10 @@
  #define EM_BPF 247
  #endif
 
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif
+
  #ifndef R_BPF_64_64
  #define R_BPF_64_64 1
  #endif

Thanks,
Daniel

#2 lore raw
Quoted 15 lines · Daniel Borkmann

Hi James,

Quoted 11 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

    fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                   ^~~~~~~~~~~~~~~
                   FD_CLOEXEC
Quoted 4 lines
Quoted 1 line

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

It seems like it's guarded by __USE_XOPEN2K8 in glibc (from a quick glance at glibc code). But it's been there since 2010 or so, at the very least.

Quoted 3 lines

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

yep, this is UAPI header so we can use it easily (we'll need to sync it into Github repo, but that's not a problem)

Quoted 24 lines
Quoted 11 lines
---
  tools/lib/bpf/libbpf_internal.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +0 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644

tools/lib/bpf/libbpf_internal.h line 40

Quoted 10 lines
tools/lib/bpf/libbpf_internal.h +4 -0
@@ -31,6 +31,10 @@
  #define EM_BPF 247
  #endif
 
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif
+
  #ifndef R_BPF_64_64
  #define R_BPF_64_64 1
  #endif

Thanks,
Daniel

maintainer
#3 lore raw
Quoted 22 lines · Andrii Nakryiko
Quoted 12 lines · Daniel Borkmann
Quoted 11 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

     fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                    ^~~~~~~~~~~~~~~
                    FD_CLOEXEC
Quoted 4 lines
Quoted 1 line

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

It seems like it's guarded by __USE_XOPEN2K8 in glibc (from a quick glance at glibc code). But it's been there since 2010 or so, at the very least.

Quoted 5 lines
Quoted 2 lines

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

yep, this is UAPI header so we can use it easily (we'll need to sync it into Github repo, but that's not a problem)

Sgtm, James, could you respin with using the include?

Thanks,
Daniel

#4 lore raw
In reply to Andrii Nakryiko
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Daniel Borkmann

Hi James,

Quoted 11 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

    fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                   ^~~~~~~~~~~~~~~
                   FD_CLOEXEC
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

It seems like it's guarded by __USE_XOPEN2K8 in glibc (from a quick glance at glibc code). But it's been there since 2010 or so, at the very least.

The toolchain that hit this issue appears to be uclibc based which seems to have had some bugs with the F_DUPFD_CLOEXEC definition.

Quoted 33 lines
Quoted 4 lines

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

yep, this is UAPI header so we can use it easily (we'll need to sync it into Github repo, but that's not a problem)

Quoted 24 lines
Quoted 11 lines
---
  tools/lib/bpf/libbpf_internal.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +0 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644

tools/lib/bpf/libbpf_internal.h line 40

Quoted 10 lines
tools/lib/bpf/libbpf_internal.h +4 -0
@@ -31,6 +31,10 @@
  #define EM_BPF 247
  #endif
 
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif
+
  #ifndef R_BPF_64_64
  #define R_BPF_64_64 1
  #endif

Thanks,
Daniel

#5 lore raw
In reply to Daniel Borkmann
Quoted 15 lines · Daniel Borkmann

Hi James,

Quoted 11 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

    fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                   ^~~~~~~~~~~~~~~
                   FD_CLOEXEC
Quoted 7 lines
Quoted 1 line

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

That seems to cause a conflict: error: redefinition of ‘struct flock’

Quoted 24 lines
Quoted 11 lines
---
  tools/lib/bpf/libbpf_internal.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +0 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644

tools/lib/bpf/libbpf_internal.h line 40

Quoted 10 lines
tools/lib/bpf/libbpf_internal.h +4 -0
@@ -31,6 +31,10 @@
  #define EM_BPF 247
  #endif
 
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif
+
  #ifndef R_BPF_64_64
  #define R_BPF_64_64 1
  #endif

Thanks,
Daniel

#6 lore raw
Quoted 41 lines · James Hilliard
Quoted 15 lines · Daniel Borkmann

Hi James,

Quoted 11 lines · James Hilliard

This definition seems to be missing from some older toolchains.

Note that the fcntl.h in libbpf_internal.h is not a kernel header but rather a toolchain libc header.

Fixes:
libbpf_internal.h:521:18: error: 'F_DUPFD_CLOEXEC' undeclared (first use in this function); did you mean 'FD_CLOEXEC'?

    fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                   ^~~~~~~~~~~~~~~
                   FD_CLOEXEC
Quoted 7 lines
Quoted 1 line

Do you have some more info on your env (e.g. libc)? Looks like F_DUPFD_CLOEXEC was added back in 2.6.24 kernel. When did libc add it?

Should we instead just add an include for <linux/fcntl.h> to libbpf_internal.h (given it defines F_DUPFD_CLOEXEC as well)?

That seems to cause a conflict: error: redefinition of ‘struct flock’

Quoted 13 lines
Quoted 13 lines
---
  tools/lib/bpf/libbpf_internal.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf_internal.h +0 -0
index 4fda8bdf0a0d..d2a86b5a457a 100644
@@ -31,6 +31,10 @@
  #define EM_BPF 247
  #endif
 
Quoted 3 lines
Quoted 3 lines

tools/lib/bpf/libbpf_internal.h line 36

Quoted 3 lines
tools/lib/bpf/libbpf_internal.h +3 -0
+#ifndef F_DUPFD_CLOEXEC
+#define F_DUPFD_CLOEXEC 1030
+#endif

Let's just do this then (assuming the value of F_DUPFD_CLOEXEC is architecture-independent)

Quoted 8 lines
Quoted 8 lines

tools/lib/bpf/libbpf_internal.h line 40

Quoted 5 lines
tools/lib/bpf/libbpf_internal.h +1 -0
+
  #ifndef R_BPF_64_64
  #define R_BPF_64_64 1
  #endif

Thanks,
Daniel