Skip to contentSkip to conversation

iproute2: Auto-detect the presence of setns in libc

PATCH In review Eric W. Biederman 3 participants 3 messages Jul 16, 2011 – Jul 20, 2011

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

PATCH 1/1 iproute2: Auto-detect the presence of setns in libc

If libc has setns present use that version instead of rolling the syscall wrapper by hand.

Dan McGee found the following compile error:

gcc -D_GNU_SOURCE -O2 -Wstrict-prototypes -Wall -I../include
-DRESOLVE_HOSTNAMES -DLIBDIR=\"/usr/lib/\" -c -o ipnetns.o ipnetns.c
ipnetns.c:31:12: error: static declaration of ‘setns’ follows non-static declaration
/usr/include/bits/sched.h:93:12: note: previous declaration of ‘setns’ was here
make[1]: *** [ipnetns.o] Error 1

---
 configure    |   24 ++++++++++++++++++++++++
 ip/Makefile  |    6 ++++++
 ip/ipnetns.c |    2 ++
 3 files changed, 32 insertions(+), 0 deletions(-)
configure +24 -0
index 69797ab..cfcc82a 100755
@@ -163,6 +163,27 @@ check_ipt_lib_dir()
 	echo "not found!"
 }
 
+check_setns()
+{
+cat >/tmp/setnstest.c <<EOF
+#include <sched.h>
+int main(int argc, char **argv) 
+{
+	(void)setns(0,0);
+	return 0;
+}
+EOF
+gcc -I$INCLUDE -o /tmp/setnstest /tmp/setnstest.c >/dev/null 2>&1
+if [ $? -eq 0 ]
+then
+	echo "IP_CONFIG_SETNS:=y" >>Config
+	echo "yes"
+else
+	echo "no"
+fi
+rm -f /tmp/setnstest.c /tmp/setnstest
+}
+
 echo "# Generated config based on" $INCLUDE >Config
 
 echo "TC schedulers"
@@ -178,3 +199,6 @@ check_ipt
 
 echo -n "iptables modules directory: "
 check_ipt_lib_dir
+
+echo -n "libc has setns: "
+check_setns
ip/Makefile +6 -0
index 2ee4e7c..8d03993 100644
@@ -7,6 +7,12 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
 
 RTMONOBJ=rtmon.o
 
+include ../Config
+
+ifeq ($(IP_CONFIG_SETNS),y)
+	CFLAGS += -DHAVE_SETNS
+endif
+
 ALLOBJ=$(IPOBJ) $(RTMONOBJ)
 SCRIPTS=ifcfg rtpr routel routef
 TARGETS=ip rtmon
ip/ipnetns.c +2 -0
index db7007c..dff3497 100644
@@ -28,6 +28,7 @@
 #define MNT_DETACH	0x00000002	/* Just detach from the tree */
 #endif /* MNT_DETACH */
 
+#ifndef HAVE_SETNS
 static int setns(int fd, int nstype)
 {
 #ifdef __NR_setns
@@ -37,6 +38,7 @@ static int setns(int fd, int nstype)
 	return -1;
 #endif
 }
+#endif /* HAVE_SETNS */
 
 
 static int touch(const char *path, mode_t mode)
Signature · 1 line
1.7.5.1.217.g4e3aa
#1 lore raw
Quoted 14 lines · Eric W. Biederman

If libc has setns present use that version instead of rolling the syscall wrapper by hand.

Dan McGee found the following compile error:

   gcc -D_GNU_SOURCE -O2 -Wstrict-prototypes -Wall -I../include -DRESOLVE_HOSTNAMES -DLIBDIR=\"/usr/lib/\"   -c -o ipnetns.o ipnetns.c ipnetns.c:31:12: error: static declaration of ‘setns’ follows non-static declaration
   /usr/include/bits/sched.h:93:12: note: previous declaration of ‘setns’ was here
   make[1]: *** [ipnetns.o] Error 1

Quoted 2 lines

Should mention that this was tested on glibc 2.14, where the setns function was introduced.

Quoted 1 line

And I forgot to be explicit, but:

Quoted 86 lines

---
 configure    |   24 ++++++++++++++++++++++++
 ip/Makefile  |    6 ++++++
 ip/ipnetns.c |    2 ++

 3 files changed, 32 insertions(+), 0 deletions(-)
configure +0 -0
index 69797ab..cfcc82a 100755
@@ -163,6 +163,27 @@ check_ipt_lib_dir()

       echo "not found!"
 }

configure +21 -0
+check_setns()
+{
+cat >/tmp/setnstest.c <<EOF
+#include <sched.h>
+int main(int argc, char **argv)
+{
+       (void)setns(0,0);
+       return 0;
+}
+EOF
+gcc -I$INCLUDE -o /tmp/setnstest /tmp/setnstest.c >/dev/null 2>&1
+if [ $? -eq 0 ]
+then
+       echo "IP_CONFIG_SETNS:=y" >>Config
+       echo "yes"
+else
+       echo "no"
+fi
+rm -f /tmp/setnstest.c /tmp/setnstest
+}
+

 echo "# Generated config based on" $INCLUDE >Config

 echo "TC schedulers"

configure +0 -0
@@ -178,3 +199,6 @@ check_ipt
 

 echo -n "iptables modules directory: "
 check_ipt_lib_dir

configure +3 -0
+
+echo -n "libc has setns: "
+check_setns
ip/Makefile +0 -0
index 2ee4e7c..8d03993 100644
@@ -7,6 +7,12 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
 

 RTMONOBJ=rtmon.o

ip/Makefile +6 -0
+include ../Config
+
+ifeq ($(IP_CONFIG_SETNS),y)
+       CFLAGS += -DHAVE_SETNS
+endif
+

 ALLOBJ=$(IPOBJ) $(RTMONOBJ)
 SCRIPTS=ifcfg rtpr routel routef
 TARGETS=ip rtmon

ip/ipnetns.c +2 -1
index db7007c..dff3497 100644
@@ -28,6 +28,7 @@
  #define MNT_DETACH     0x00000002      /* Just detach from the tree */
  #endif /* MNT_DETACH */
 
+#ifndef HAVE_SETNS
  static int setns(int fd, int nstype)
  {
  #ifdef __NR_setns
@@ -37,6 +38,7 @@ static int setns(int fd, int nstype)
        return -1;
  #endif
  }
+#endif /* HAVE_SETNS */
 
 
  static int touch(const char *path, mode_t mode)
--

1.7.5.1.217.g4e3aa

#2 lore raw
Quoted 6 lines · On Fri, 15 Jul 2011 19:34:04 -0500 Dan McGee <[email protected]> wrote:
Quoted 4 lines · Eric W. Biederman

If libc has setns present use that version instead of rolling the syscall wrapper by hand.

I am more inclined to get rid of the wrapper completely and just not put setns support in if libc doesn't support it.