Skip to contentSkip to conversation

[7118] linux-user: prefer glibc over direct syscalls

Qemu-devel Aurelien Jarno 6 participants 11 messages Apr 15, 2009 – Apr 21, 2009

lore
Share & tools
lore
#0 lore raw

Revision: 7118

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7118
Author:   aurel32
Date:     2009-04-15 16:12:13 +0000 (Wed, 15 Apr 2009)

Log Message:
-----------
linux-user: prefer glibc over direct syscalls

The openat/*at syscalls are incredibly common with modern coreutils, calling them directly via syscalls breaks for example fakeroot. Use glibc stubs whenever directly available and provide old syscall calling for people still using older libc.

Patch originally from Mika Westerberg, Adapted to apply to current trunk and cleaned up by Riku Voipio.

Modified Paths:
--------------
trunk/configure
trunk/linux-user/syscall.c

Modified: trunk/configure
===================================================================

configure +53 -0
@@ -1156,6 +1156,53 @@
   fi
 fi
 
+#
+# Check for xxxat() functions when we are building linux-user
+# emulator.  This is done because older glibc versions don't
+# have syscall stubs for these implemented.
+#
+atfile=no
+if [ "$linux_user" = "yes" ] ; then
+  cat > $TMPC << EOF
+#define _ATFILE_SOURCE
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+	/* try to unlink nonexisting file */
+	return (unlinkat(AT_FDCWD, "nonexistent_file", 0));
+}
+EOF
+  if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
+    atfile=yes
+  fi
+fi
+
+# Check for initofy functions when we are building linux-user
+# emulator.  This is done because older glibc versions don't
+# have syscall stubs for these implemented.  In that case we
+# don't provide them even if kernel supports them.
+#
+inotify=no
+if [ "$linux_user" = "yes" ] ; then
+  cat > $TMPC << EOF
+#include <sys/inotify.h>
+
+int
+main(void)
+{
+	/* try to start inotify */
+	return inotify_init(void);
+}
+EOF
+  if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
+    inotify=yes
+  fi
+fi
+
 # Check if tools are available to build documentation.
 if [ -x "`which texi2html 2>/dev/null`" ] && \
    [ -x "`which pod2man 2>/dev/null`" ]; then
@@ -1544,6 +1591,12 @@
   echo "CONFIG_CURSES=yes" >> $config_mak
   echo "CURSES_LIBS=-lcurses" >> $config_mak
 fi
+if test "$atfile" = "yes" ; then
+  echo "#define CONFIG_ATFILE 1" >> $config_h
+fi
+if test "$inotify" = "yes" ; then
+  echo "#define CONFIG_INOTIFY 1" >> $config_h
+fi
 if test "$brlapi" = "yes" ; then
   echo "CONFIG_BRLAPI=yes" >> $config_mak
   echo "#define CONFIG_BRLAPI 1" >> $config_h

Modified: trunk/linux-user/syscall.c
===================================================================

linux-user/syscall.c +253 -61

370 lines not on this page · raw message

#1 lore raw

Review

Quoted 11 lines · Aurelien Jarno

Revision: 7118

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7118
Author:   aurel32
Date:     2009-04-15 16:12:13 +0000 (Wed, 15 Apr 2009)

Log Message:
-----------
linux-user: prefer glibc over direct syscalls
...

(quoted diff) +2 -0
+
+# Check for initofy functions when we are building linux-user

initofy => inotify?

Quoted 17 lines
configure +16 -0
+# emulator.  This is done because older glibc versions don't
+# have syscall stubs for these implemented.  In that case we
+# don't provide them even if kernel supports them.
+#
+inotify=no
+if [ "$linux_user" = "yes" ] ; then
+  cat > $TMPC << EOF
+#include <sys/inotify.h>
+
+int
+main(void)
+{
+	/* try to start inotify */
+	return inotify_init(void);
+}
+EOF
#2 lore raw
Quoted 15 lines · Stefan Weil

Review

Quoted 11 lines · Aurelien Jarno

Revision: 7118

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7118
Author:   aurel32
Date:     2009-04-15 16:12:13 +0000 (Wed, 15 Apr 2009)

Log Message:
-----------
linux-user: prefer glibc over direct syscalls
...

(quoted diff) +2 -0
+
+# Check for initofy functions when we are building linux-user

initofy => inotify?

Thanks, fixed.

Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#3 lore raw
In reply to Aurelien Jarno
Quoted 23 lines · Aurelien Jarno

Revision: 7118

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7118
Author:   aurel32
Date:     2009-04-15 16:12:13 +0000 (Wed, 15 Apr 2009)

Log Message:
-----------
linux-user: prefer glibc over direct syscalls

The openat/*at syscalls are incredibly common with modern coreutils, calling them directly via syscalls breaks for example fakeroot. Use glibc stubs whenever directly available and provide old syscall calling for people still using older libc.

Patch originally from Mika Westerberg, Adapted to apply to current trunk and cleaned up by Riku Voipio.

Modified Paths:
--------------
trunk/configure
trunk/linux-user/syscall.c

[...]

Quoted 4 lines

Modified: trunk/linux-user/syscall.c
===================================================================

linux-user/syscall.c 2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c 2009-04-15 16:12:06 UTC (rev 7117) +0 -0

[...]

Quoted 18 lines
linux-user/syscall.c 2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c 2009-04-15 16:12:06 UTC (rev 7117) +9 -2
@@ -285,22 +482,37 @@
 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
          const struct timespec *,tsp,int,flags)
 #endif
+
+#endif /* CONFIG_ATFILE */
+
+#ifdef CONFIG_INOTIFY
+
 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
-_syscall0(int,sys_inotify_init)
+static int sys_inotify_init(void)
+{
+  return (inotify_init());
+}
 #endif
 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
-_syscall3(int,sys_inotify_add_watch,int,fd,const char *,pathname,uint32_t,mask)

linux-user/syscall.c 2009-04-15 16:12:13 UTC (rev 7118) line 505

Quoted 10 lines
linux-user/syscall.c 2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c 2009-04-15 16:12:06 UTC (rev 7117) +7 -1
+static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
+{
+  return (inotify_add_watch(fd, pathname, mask));
+}
 #endif
 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
-_syscall2(int,sys_inotify_rm_watch,int,fd,uint32_t,wd)
+static int sys_inotify_rm_watch(int fd, int32_t wd)
+{
+  return (inotify_rm_watch(fd,pathname, wd));

Isn't pathname spurious?

Laurent

#4 lore raw
Quoted 7 lines · Laurent Desnogues

Review

Quoted 5 lines

#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)

(quoted diff) +3 -1
-_syscall2(int,sys_inotify_rm_watch,int,fd,uint32_t,wd)
+static int sys_inotify_rm_watch(int fd, int32_t wd)
+{
+  return (inotify_rm_watch(fd,pathname, wd));

Isn't pathname spurious?

It is broken. Try this.

Quoted 1 line

From 448cfe737b614ec44a8c4a9a4d8c2542c3eac165 Mon Sep 17 00:00:00 2001

From: Riku Voipio <[email protected]>
Date: Thu, 16 Apr 2009 19:38:14 +0300
Subject: linux-user: fix inotify syscalls

Configure test was broken, so the breakage of the #ifdef'd code was not noticed.

---
 configure            |    2 +-
 linux-user/syscall.c |    3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
configure +1 -1
index 5b705a0..251d271 100755
@@ -1200,7 +1200,7 @@ int
 main(void)
 {
 	/* try to start inotify */
-	return inotify_init(void);
+	return inotify_init();
 }
 EOF
   if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
linux-user/syscall.c +2 -1
index 898f58c..c94efe6 100644
@@ -480,6 +480,7 @@ _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
 #endif /* CONFIG_ATFILE */
 
 #ifdef CONFIG_INOTIFY
+#include <sys/inotify.h>
 
 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
 static int sys_inotify_init(void)
@@ -496,7 +497,7 @@ static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
 static int sys_inotify_rm_watch(int fd, int32_t wd)
 {
-  return (inotify_rm_watch(fd,pathname, wd));
+  return (inotify_rm_watch(fd, wd));
 }
 #endif
 #else
Signature · 5 lines
1.6.2.1


-- 
"rm -rf" only sounds scary if you don't have backups
#6 lore raw
In reply to Aurelien Jarno

configure line 1165

Quoted 3 lines · Aurelien Jarno
configure +3 -0
+# Check for xxxat() functions when we are building linux-user
+# emulator.
+if [ "$linux_user" = "yes" ] ; then

This is wrong. $linux_user only indicates whether the linux usermode targets are built by default. It can be overridden by --target-list.

Paul

#7 lore raw
Quoted 7 lines · Paul Brook

configure line 1165

Quoted 3 lines · Aurelien Jarno
configure +3 -0
+# Check for xxxat() functions when we are building linux-user
+# emulator.
+if [ "$linux_user" = "yes" ] ; then

This is wrong. $linux_user only indicates whether the linux usermode targets are built by default. It can be overridden by --target-list.

linux_user is default yes linux host and is only removed with --disable-linux-user. So, to expose this wrongness one would have to configure with
--disable-linux-user --target-list="foo-linux-user"

I guess we can still drop the $linux_user test and run it anyway, same way tests only needed for system-qemu (gnutls, sasl..) are run even when configuring for a linux-user targets build.

author Applied
#5 lore raw
In reply to Riku Voipio
Quoted 10 lines · Riku Voipio
Quoted 7 lines · Laurent Desnogues

Review

Quoted 5 lines

#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)

(quoted diff) +3 -1
-_syscall2(int,sys_inotify_rm_watch,int,fd,uint32_t,wd)
+static int sys_inotify_rm_watch(int fd, int32_t wd)
+{
+  return (inotify_rm_watch(fd,pathname, wd));

Isn't pathname spurious?

It is broken. Try this.

Thanks, applied.

Quoted 57 lines

From 448cfe737b614ec44a8c4a9a4d8c2542c3eac165 Mon Sep 17 00:00:00 2001
From: Riku Voipio <[email protected]>
Date: Thu, 16 Apr 2009 19:38:14 +0300
Subject: linux-user: fix inotify syscalls

Configure test was broken, so the breakage of the #ifdef'd code was not noticed.

---
 configure            |    2 +-
 linux-user/syscall.c |    3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
configure +1 -1
index 5b705a0..251d271 100755
@@ -1200,7 +1200,7 @@ int
 main(void)
 {
 	/* try to start inotify */
-	return inotify_init(void);
+	return inotify_init();
 }
 EOF
   if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
linux-user/syscall.c +2 -1
index 898f58c..c94efe6 100644
@@ -480,6 +480,7 @@ _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
 #endif /* CONFIG_ATFILE */
 
 #ifdef CONFIG_INOTIFY
+#include <sys/inotify.h>
 
 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
 static int sys_inotify_init(void)
@@ -496,7 +497,7 @@ static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
 static int sys_inotify_rm_watch(int fd, int32_t wd)
 {
-  return (inotify_rm_watch(fd,pathname, wd));
+  return (inotify_rm_watch(fd, wd));
 }
 #endif
 #else
Signature · 5 lines
1.6.2.1


-- 
"rm -rf" only sounds scary if you don't have backups
Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#8 lore raw
In reply to Aurelien Jarno
Quoted 23 lines · Aurelien Jarno

Revision: 7118
         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7118
Author:   aurel32
Date:     2009-04-15 16:12:13 +0000 (Wed, 15 Apr 2009) Log Message:
-----------
linux-user: prefer glibc over direct syscalls

The openat/*at syscalls are incredibly common with modern coreutils, calling them directly via syscalls breaks for example fakeroot. Use glibc stubs whenever directly available and provide old syscall calling for people still using older libc.

Patch originally from Mika Westerberg, Adapted to apply to current trunk and cleaned up by Riku Voipio.

Modified Paths:
--------------
   trunk/configure
   trunk/linux-user/syscall.c

[...]

Quoted 11 lines
configure +11 -0
+int
+main(void)
+{
+       /* try to unlink nonexisting file */
+       return (unlinkat(AT_FDCWD, "nonexistent_file", 0));
+}
+EOF
+  if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
+    atfile=yes
+  fi
+fi

[...]

Quoted 4 lines

Modified: trunk/linux-user/syscall.c
===================================================================

linux-user/syscall.c  2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c  2009-04-15 16:12:06 UTC (rev 7117) +0 -0

[...]

Quoted 113 lines
linux-user/syscall.c  2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c  2009-04-15 16:12:06 UTC (rev 7117) +113 -0
+#ifdef CONFIG_ATFILE
+/*
+ * Host system seems to have atfile syscall stubs available.  We
+ * now enable them one by one as specified by target syscall_nr.h.
+ */
+
+#ifdef TARGET_NR_faccessat
+static int sys_faccessat(int dirfd, const char *pathname, int mode, int flags)
+{
+  return (faccessat(dirfd, pathname, mode, flags));
+}
+#endif
+#ifdef TARGET_NR_fchmodat
+static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode, int flags)
+{
+  return (fchmodat(dirfd, pathname, mode, flags));
+}
+#endif
+#ifdef TARGET_NR_fchownat
+static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
+    gid_t group, int flags)
+{
+  return (fchownat(dirfd, pathname, owner, group, flags));
+}
+#endif
+#ifdef __NR_fstatat64
+static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
+    int flags)
+{
+  return (fstatat(dirfd, pathname, buf, flags));
+}
+#endif
+#ifdef __NR_newfstatat
+static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
+    int flags)
+{
+  return (fstatat(dirfd, pathname, buf, flags));
+}
+#endif
+#ifdef TARGET_NR_futimesat
+static int sys_futimesat(int dirfd, const char *pathname,
+    const struct timeval times[2])
+{
+  return (futimesat(dirfd, pathname, times));
+}
+#endif
+#ifdef TARGET_NR_linkat
+static int sys_linkat(int olddirfd, const char *oldpath,
+    int newdirfd, const char *newpath, int flags)
+{
+  return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
+}
+#endif
+#ifdef TARGET_NR_mkdirat
+static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
+{
+  return (mkdirat(dirfd, pathname, mode));
+}
+#endif
+#ifdef TARGET_NR_mknodat
+static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
+    dev_t dev)
+{
+  return (mknodat(dirfd, pathname, mode, dev));
+}
+#endif
+#ifdef TARGET_NR_openat
+static int sys_openat(int dirfd, const char *pathname, int flags, ...)
+{
+  /*
+   * open(2) has extra parameter 'mode' when called with
+   * flag O_CREAT.
+   */
+  if ((flags & O_CREAT) != 0) {
+      va_list ap;
+      mode_t mode;
+
+      /*
+       * Get the 'mode' parameter and translate it to
+       * host bits.
+       */
+      va_start(ap, flags);
+      mode = va_arg(ap, mode_t);
+      mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
+      va_end(ap);
+
+      return (openat(dirfd, pathname, flags, mode));
+  }
+  return (openat(dirfd, pathname, flags));
+}
+#endif
+#ifdef TARGET_NR_readlinkat
+static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
+{
+  return (readlinkat(dirfd, pathname, buf, bufsiz));
+}
+#endif
+#ifdef TARGET_NR_renameat
+static int sys_renameat(int olddirfd, const char *oldpath,
+    int newdirfd, const char *newpath)
+{
+  return (renameat(olddirfd, oldpath, newdirfd, newpath));
+}
+#endif
+#ifdef TARGET_NR_symlinkat
+static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
+{
+  return (symlinkat(oldpath, newdirfd, newpath));
+}
+#endif
+#ifdef TARGET_NR_unlinkat
+static int sys_unlinkat(int dirfd, const char *pathname, int flags)
+{

linux-user/syscall.c  2009-04-15 16:12:13 UTC (rev 7118)

Quoted 10 lines
linux-user/syscall.c  2009-04-15 16:12:13 UTC (rev 7118) renamed from linux-user/syscall.c  2009-04-15 16:12:06 UTC (rev 7117) +10 -0
+  return (unlinkat(dirfd, pathname, flags));
+}
+#endif
+#ifdef TARGET_NR_utimensat
+static int sys_utimensat(int dirfd, const char *pathname,
+    const struct timespec times[2], int flags)
+{
+  return (utimensat(dirfd, pathname, times, flags));
+}
+#endif

Just to point that my system has all *at functions except for utimensat. This breaks compilation. Shouldn't all *at functions be tested in the configure script?

For the record I am running CentOS 5.3 x86_64.

Laurent

#9 lore raw

Laurent Desnogues <[email protected]> writes:

Hi,

Quoted 11 lines

linux-user/syscall.c line 421

Quoted 7 lines
linux-user/syscall.c +7 -0
+#ifdef TARGET_NR_utimensat
+static int sys_utimensat(int dirfd, const char *pathname,
+    const struct timespec times[2], int flags)
+{
+  return (utimensat(dirfd, pathname, times, flags));
+}
+#endif

Just to point that my system has all *at functions except for utimensat. This breaks compilation. Shouldn't all *at functions be tested in the configure script?

This utimensat change is broken, I've sent a patch to revert to the syscall interface. As a side effect, it'll bring back the check :

#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)

It will likely solve your trouble. Lets hope the patch will be merged :)

Quoted 2 lines

For the record I am running CentOS 5.3 x86_64.

hmm... glibc older than 2.6 or kernel older than 2.6.22 ?

Arnaud

#10 lore raw
Quoted 7 lines · Arnaud Patard

This utimensat change is broken, I've sent a patch to revert to the syscall interface. As a side effect, it'll bring back the check :

#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)

It will likely solve your trouble. Lets hope the patch will be merged :)

Yes, that fixes it. Thanks.

Quoted 4 lines
Quoted 2 lines

For the record I am running CentOS 5.3 x86_64.

hmm... glibc older than 2.6 or kernel older than 2.6.22 ?

Indeed: glibc 2.5 and kernel 2.6.18.

Laurent