[v2,4/4] tools/nolibc: sys.h: apply __syscall() helper

Message ID ee86e33d9f0031da5932b0de798f188535308dd7.1686036862.git.falcon@tinylab.org
State New
Headers
Series tools/nolibc: add two new syscall helpers |

Commit Message

Zhangjin Wu June 6, 2023, 8:17 a.m. UTC
  Use __syscall() helper to shrink 252 lines of code.

    $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
    1425
    $ git show HEAD:tools/include/nolibc/sys.h | wc -l
    1173
    $ echo "1425-1173" | bc -l
    252

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/sys.h | 336 +++++--------------------------------
 1 file changed, 42 insertions(+), 294 deletions(-)
  

Comments

Thomas Weißschuh June 6, 2023, 6:36 p.m. UTC | #1
Hi Zhangjin,

On 2023-06-06 16:17:38+0800, Zhangjin Wu wrote:
> Use __syscall() helper to shrink 252 lines of code.
> 
>     $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
>     1425
>     $ git show HEAD:tools/include/nolibc/sys.h | wc -l
>     1173
>     $ echo "1425-1173" | bc -l
>     252
> 
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  tools/include/nolibc/sys.h | 336 +++++--------------------------------
>  1 file changed, 42 insertions(+), 294 deletions(-)
> 
> diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> index f6e3168b3e50..0cfc5157845a 100644
> --- a/tools/include/nolibc/sys.h
> +++ b/tools/include/nolibc/sys.h
> @@ -108,13 +108,7 @@ int sys_chdir(const char *path)
>  static __attribute__((unused))
>  int chdir(const char *path)
>  {
> -	int ret = sys_chdir(path);
> -
> -	if (ret < 0) {
> -		SET_ERRNO(-ret);
> -		ret = -1;
> -	}
> -	return ret;
> +	return __syscall(chdir, path);

To be honest I'm still not a big fan of the __syscall macro.
It's a bit too magic for too little gain.

The commit message argues that the patches make the code shorter.

However doing 

__sysret(sys_chdir(path));

instead of

__syscall(chdir, path);

is only three characters longer and the same amout of lines.

Otherwise we would have syscall() _syscall() and __syscall() each doing
different things.

And __syscall does not behave like a regular function.

The rest of the patchset looks great.

Maybe Willy can break the tie?


Thomas


Note: If we figure out a way to build syscall() without macros I would
like that also :-)
  
Zhangjin Wu June 7, 2023, 12:34 a.m. UTC | #2
> Hi Zhangjin,
> 
> On 2023-06-06 16:17:38+0800, Zhangjin Wu wrote:
> > Use __syscall() helper to shrink 252 lines of code.
> > 
> >     $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
> >     1425
> >     $ git show HEAD:tools/include/nolibc/sys.h | wc -l
> >     1173
> >     $ echo "1425-1173" | bc -l
> >     252
> > 
> > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > ---
> >  tools/include/nolibc/sys.h | 336 +++++--------------------------------
> >  1 file changed, 42 insertions(+), 294 deletions(-)
> > 
> > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> > index f6e3168b3e50..0cfc5157845a 100644
> > --- a/tools/include/nolibc/sys.h
> > +++ b/tools/include/nolibc/sys.h
> > @@ -108,13 +108,7 @@ int sys_chdir(const char *path)
> >  static __attribute__((unused))
> >  int chdir(const char *path)
> >  {
> > -	int ret = sys_chdir(path);
> > -
> > -	if (ret < 0) {
> > -		SET_ERRNO(-ret);
> > -		ret = -1;
> > -	}
> > -	return ret;
> > +	return __syscall(chdir, path);
> 
> To be honest I'm still not a big fan of the __syscall macro.
> It's a bit too magic for too little gain.
> 
> The commit message argues that the patches make the code shorter.
> 
> However doing 
> 
> __sysret(sys_chdir(path));
> 
> instead of
> 
> __syscall(chdir, path);
> 
> is only three characters longer and the same amout of lines.
>

Yeah, I do like your version too, it looks consise too, the only not
comfortable part is there are dual calls in one line.

> Otherwise we would have syscall() _syscall() and __syscall() each doing
> different things.
>

Yes, I'm worried about this too, although the compilers may help a
little, but it is too later.

Just brain storming, What about another non-similar name, for example,
__syswrap() or __sysin() ?

Or even convert __sysret() to __sysout() and __syscall() to __sysin(),
do you like it? or even __sysexit(), __sysentry(), but the __sysexit()
may be misused with sys_exit().

    /* Syscall return helper, set errno as -ret when ret < 0 */
    static __inline__ __attribute__((unused, always_inline))
    long __sysout(long ret)
    {
    	if (ret < 0) {
    		SET_ERRNO(-ret);
    		ret = -1;
    	}
    	return ret;
    }
    
    /* Syscall call helper, use syscall name instead of syscall number */
    #define __sysin(name, ...) __sysout(sys_##name(__VA_ARGS__))

    static __attribute__((unused))
    int brk(void *addr)
    {
    	return __sysout(sys_brk(addr) ? 0 : -ENOMEM);
    }

    static __attribute__((unused))
    int chdir(const char *path)
    {
    	return __sysin(chdir, path);
    }

If we really want something like __syscall()/__sysret(), I do think they
should be a pair ;-)

> And __syscall does not behave like a regular function.
> 
> The rest of the patchset looks great.
>

Thanks for your nice review.

> Maybe Willy can break the tie?
>

If there is no better solution, I think your version is also a first
step to go.

> 
> Thomas
> 
> 
> Note: If we figure out a way to build syscall() without macros I would
> like that also :-)

Yes, but it is not easy to cope with the variable number of arguments
without a macro.

BTW, do you like to convert the my_syscallN() of sys.h to the syscall()
you added? I do worry about it will make the checking of arguments
mismatch, exspecially, the checking of number of them hardly.

Best regards,
Zhangjin
  
Willy Tarreau June 7, 2023, 4:05 a.m. UTC | #3
Hi,

On Wed, Jun 07, 2023 at 08:34:06AM +0800, Zhangjin Wu wrote:
> > Hi Zhangjin,
> > 
> > On 2023-06-06 16:17:38+0800, Zhangjin Wu wrote:
> > > Use __syscall() helper to shrink 252 lines of code.
> > > 
> > >     $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
> > >     1425
> > >     $ git show HEAD:tools/include/nolibc/sys.h | wc -l
> > >     1173
> > >     $ echo "1425-1173" | bc -l
> > >     252
> > > 
> > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > > ---
> > >  tools/include/nolibc/sys.h | 336 +++++--------------------------------
> > >  1 file changed, 42 insertions(+), 294 deletions(-)
> > > 
> > > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> > > index f6e3168b3e50..0cfc5157845a 100644
> > > --- a/tools/include/nolibc/sys.h
> > > +++ b/tools/include/nolibc/sys.h
> > > @@ -108,13 +108,7 @@ int sys_chdir(const char *path)
> > >  static __attribute__((unused))
> > >  int chdir(const char *path)
> > >  {
> > > -	int ret = sys_chdir(path);
> > > -
> > > -	if (ret < 0) {
> > > -		SET_ERRNO(-ret);
> > > -		ret = -1;
> > > -	}
> > > -	return ret;
> > > +	return __syscall(chdir, path);
> > 
> > To be honest I'm still not a big fan of the __syscall macro.
> > It's a bit too magic for too little gain.
> > 
> > The commit message argues that the patches make the code shorter.
> > 
> > However doing 
> > 
> > __sysret(sys_chdir(path));
> > 
> > instead of
> > 
> > __syscall(chdir, path);
> > 
> > is only three characters longer and the same amout of lines.
> >
> 
> Yeah, I do like your version too, it looks consise too, the only not
> comfortable part is there are dual calls in one line.

For those who want to debug, having less macros or magic stuff is always
better, and in this essence I too find that Thomas' version is more
expressive about what is being done. Also, if some syscalls require a
specific handling (e.g. mmap() needs to return MAP_FAILED instead), it's
much easier to change only the code dealing with the return value and
errno setting than having to guess how to reimplement what was magically
done in a macro.

> > Otherwise we would have syscall() _syscall() and __syscall() each doing
> > different things.
> >
> 
> Yes, I'm worried about this too, although the compilers may help a
> little, but it is too later.

The issue is for the person who remembers "I need to use 'syscall'" but
never remembering the number of underscores nor the variations.

> Just brain storming, What about another non-similar name, for example,
> __syswrap() or __sysin() ?
> 
> Or even convert __sysret() to __sysout() and __syscall() to __sysin(),
> do you like it? or even __sysexit(), __sysentry(), but the __sysexit()
> may be misused with sys_exit().

I'd rather use "__set_errno()" to explicitly mention that it's only
used to set errno, but sysret would be fine as well IMHO as if we're
purist, it also normalizes the return value.

>     /* Syscall return helper, set errno as -ret when ret < 0 */
>     static __inline__ __attribute__((unused, always_inline))
>     long __sysout(long ret)
>     {
>     	if (ret < 0) {
>     		SET_ERRNO(-ret);
>     		ret = -1;
>     	}
>     	return ret;
>     }
>     
>     /* Syscall call helper, use syscall name instead of syscall number */
>     #define __sysin(name, ...) __sysout(sys_##name(__VA_ARGS__))
> 
>     static __attribute__((unused))
>     int brk(void *addr)
>     {
>     	return __sysout(sys_brk(addr) ? 0 : -ENOMEM);
>     }
> 
>     static __attribute__((unused))
>     int chdir(const char *path)
>     {
>     	return __sysin(chdir, path);
>     }

I still don't find this intuitive at all.

> If we really want something like __syscall()/__sysret(), I do think they
> should be a pair ;-)

Then one being called "call" while the other one being "ret" do form a
pair, no ?

Thanks,
Willy
  
Zhangjin Wu June 7, 2023, 5:39 a.m. UTC | #4
> On Wed, Jun 07, 2023 at 08:34:06AM +0800, Zhangjin Wu wrote:
> > > Hi Zhangjin,
> > > 
> > > On 2023-06-06 16:17:38+0800, Zhangjin Wu wrote:
> > > > Use __syscall() helper to shrink 252 lines of code.
> > > > 
> > > >     $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
> > > >     1425
> > > >     $ git show HEAD:tools/include/nolibc/sys.h | wc -l
> > > >     1173
> > > >     $ echo "1425-1173" | bc -l
> > > >     252
> > > > 
> > > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > > > ---
> > > >  tools/include/nolibc/sys.h | 336 +++++--------------------------------
> > > >  1 file changed, 42 insertions(+), 294 deletions(-)
> > > > 
> > > > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> > > > index f6e3168b3e50..0cfc5157845a 100644
> > > > --- a/tools/include/nolibc/sys.h
> > > > +++ b/tools/include/nolibc/sys.h
> > > > @@ -108,13 +108,7 @@ int sys_chdir(const char *path)
> > > >  static __attribute__((unused))
> > > >  int chdir(const char *path)
> > > >  {
> > > > -	int ret = sys_chdir(path);
> > > > -
> > > > -	if (ret < 0) {
> > > > -		SET_ERRNO(-ret);
> > > > -		ret = -1;
> > > > -	}
> > > > -	return ret;
> > > > +	return __syscall(chdir, path);
> > > 
> > > To be honest I'm still not a big fan of the __syscall macro.
> > > It's a bit too magic for too little gain.
> > > 
> > > The commit message argues that the patches make the code shorter.
> > > 
> > > However doing 
> > > 
> > > __sysret(sys_chdir(path));
> > > 
> > > instead of
> > > 
> > > __syscall(chdir, path);
> > > 
> > > is only three characters longer and the same amout of lines.
> > >
> > 
> > Yeah, I do like your version too, it looks consise too, the only not
> > comfortable part is there are dual calls in one line.
> 
> For those who want to debug, having less macros or magic stuff is always
> better, and in this essence I too find that Thomas' version is more
> expressive about what is being done. Also, if some syscalls require a
> specific handling (e.g. mmap() needs to return MAP_FAILED instead), it's
> much easier to change only the code dealing with the return value and
> errno setting than having to guess how to reimplement what was magically
> done in a macro.
>

Ok, so, let's go with Thomas' version ;-)

> > > Otherwise we would have syscall() _syscall() and __syscall() each doing
> > > different things.
> > >
> > 
> > Yes, I'm worried about this too, although the compilers may help a
> > little, but it is too later.
> 
> The issue is for the person who remembers "I need to use 'syscall'" but
> never remembering the number of underscores nor the variations.

Yeah, it is hard to remember.

> 
> > Just brain storming, What about another non-similar name, for example,
> > __syswrap() or __sysin() ?
> > 
> > Or even convert __sysret() to __sysout() and __syscall() to __sysin(),
> > do you like it? or even __sysexit(), __sysentry(), but the __sysexit()
> > may be misused with sys_exit().
> 
> I'd rather use "__set_errno()" to explicitly mention that it's only
> used to set errno, but sysret would be fine as well IMHO as if we're
> purist, it also normalizes the return value.
>

Ok, let's take the shorter sysret() seems no similar sys_xxx calls.

> >     /* Syscall return helper, set errno as -ret when ret < 0 */
> >     static __inline__ __attribute__((unused, always_inline))
> >     long __sysout(long ret)
> >     {
> >     	if (ret < 0) {
> >     		SET_ERRNO(-ret);
> >     		ret = -1;
> >     	}
> >     	return ret;
> >     }
> >     
> >     /* Syscall call helper, use syscall name instead of syscall number */
> >     #define __sysin(name, ...) __sysout(sys_##name(__VA_ARGS__))
> > 
> >     static __attribute__((unused))
> >     int brk(void *addr)
> >     {
> >     	return __sysout(sys_brk(addr) ? 0 : -ENOMEM);
> >     }
> > 
> >     static __attribute__((unused))
> >     int chdir(const char *path)
> >     {
> >     	return __sysin(chdir, path);
> >     }
> 
> I still don't find this intuitive at all.
> 
> > If we really want something like __syscall()/__sysret(), I do think they
> > should be a pair ;-)
> 
> Then one being called "call" while the other one being "ret" do form a
> pair, no ?

The 'ret' currently is a part of our old '__syscall', seems not that like a
'pair', it differs from entry/exit ;-)

As a summary, will use 'sysret()' and something like:

   static __attribute__((unused))
   int chdir(const char *path)
   {
   	return sysret(chdir(path));
   }

to renew the syscall helper patchset, Thanks you very much.

Best regards,
Zhangjin

> 
> Thanks,
> Willy
  
Thomas Weißschuh June 7, 2023, 6:05 a.m. UTC | #5
Hi Zhangjin,

On 2023-06-07 13:39:20+0800, Zhangjin Wu wrote:
> 
> As a summary, will use 'sysret()' and something like:
> 
>    static __attribute__((unused))
>    int chdir(const char *path)
>    {
>    	return sysret(chdir(path));
>    }
> 
> to renew the syscall helper patchset, Thanks you very much.

But please to use the "__" prefix.
Otherwise it could conflict with user code.

Thomas
  
Zhangjin Wu June 7, 2023, 6:38 a.m. UTC | #6
> Hi Zhangjin,
> 
> On 2023-06-07 13:39:20+0800, Zhangjin Wu wrote:
> > 
> > As a summary, will use 'sysret()' and something like:
> > 
> >    static __attribute__((unused))
> >    int chdir(const char *path)
> >    {
> >    	return sysret(chdir(path));
> >    }
> > 
> > to renew the syscall helper patchset, Thanks you very much.
> 
> But please to use the "__" prefix.
> Otherwise it could conflict with user code.
>

Ok, not yet to modify the code, will reserve the '__sysret()' and convert the
left parts to this style: '__sysret(chdir(path))', a simple sed script may help
to do so.

Thanks a lot.
Zhangjin

> Thomas
  
David Laight June 10, 2023, 4:34 p.m. UTC | #7
From: Zhangjin Wu
> Sent: 07 June 2023 06:39
...
> As a summary, will use 'sysret()' and something like:
> 
>    static __attribute__((unused))
>    int chdir(const char *path)
>    {
>    	return sysret(chdir(path));
>    }
> 
> to renew the syscall helper patchset, Thanks you very much.

While I'm all for using 'cpp-magic' to abstract and (hopefully)
simplify things. Token-pasting the sys_ here doesn't seem to gain
anything.
Anyone grepping the code for 'sys_chdir' is also going to
wonder where it is used.

There might be scope for something like:
#define syscall_wrapper(func, type) \
	static __attribute__((unused)) \
	int func(type *arg) \
	{ \
		return sysret(sys_#func(arg)); \
	}
and then:
syscall_wrapper(chdir, const char *)
would expand to the code above.

I think you'd need separate defines for each number of arguments.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  
David Laight June 10, 2023, 4:58 p.m. UTC | #8
From: David Laight
> Sent: 10 June 2023 17:34
> 
> From: Zhangjin Wu
> > Sent: 07 June 2023 06:39
> ...
> > As a summary, will use 'sysret()' and something like:
> >
> >    static __attribute__((unused))
> >    int chdir(const char *path)
> >    {
> >    	return sysret(chdir(path));
> >    }
> >
> > to renew the syscall helper patchset, Thanks you very much.
> 
> While I'm all for using 'cpp-magic' to abstract and (hopefully)
> simplify things. Token-pasting the sys_ here doesn't seem to gain
> anything.
> Anyone grepping the code for 'sys_chdir' is also going to
> wonder where it is used.

I think I've spotted a later version of the patch
that doesn't paste the sys_

> There might be scope for something like:
> #define syscall_wrapper(func, type) \
> 	static __attribute__((unused)) \
> 	int func(type *arg) \
> 	{ \
> 		return sysret(sys_#func(arg)); \
> 	}
> and then:
> syscall_wrapper(chdir, const char *)
> would expand to the code above.
> 
> I think you'd need separate defines for each number of arguments.

It is also worth pointing out that once the 'static unused'
functions have been defined, all the #define 'goop' used to
define them can be summarily #undef'ed.
That (mostly) solves the namespace problem.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  

Patch

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index f6e3168b3e50..0cfc5157845a 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -108,13 +108,7 @@  int sys_chdir(const char *path)
 static __attribute__((unused))
 int chdir(const char *path)
 {
-	int ret = sys_chdir(path);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(chdir, path);
 }
 
 
@@ -137,13 +131,7 @@  int sys_chmod(const char *path, mode_t mode)
 static __attribute__((unused))
 int chmod(const char *path, mode_t mode)
 {
-	int ret = sys_chmod(path, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(chmod, path, mode);
 }
 
 
@@ -166,13 +154,7 @@  int sys_chown(const char *path, uid_t owner, gid_t group)
 static __attribute__((unused))
 int chown(const char *path, uid_t owner, gid_t group)
 {
-	int ret = sys_chown(path, owner, group);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(chown, path, owner, group);
 }
 
 
@@ -189,13 +171,7 @@  int sys_chroot(const char *path)
 static __attribute__((unused))
 int chroot(const char *path)
 {
-	int ret = sys_chroot(path);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(chroot, path);
 }
 
 
@@ -212,13 +188,7 @@  int sys_close(int fd)
 static __attribute__((unused))
 int close(int fd)
 {
-	int ret = sys_close(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(close, fd);
 }
 
 
@@ -235,13 +205,7 @@  int sys_dup(int fd)
 static __attribute__((unused))
 int dup(int fd)
 {
-	int ret = sys_dup(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(dup, fd);
 }
 
 
@@ -264,13 +228,7 @@  int sys_dup2(int old, int new)
 static __attribute__((unused))
 int dup2(int old, int new)
 {
-	int ret = sys_dup2(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(dup2, old, new);
 }
 
 
@@ -288,13 +246,7 @@  int sys_dup3(int old, int new, int flags)
 static __attribute__((unused))
 int dup3(int old, int new, int flags)
 {
-	int ret = sys_dup3(old, new, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(dup3, old, new, flags);
 }
 #endif
 
@@ -312,13 +264,7 @@  int sys_execve(const char *filename, char *const argv[], char *const envp[])
 static __attribute__((unused))
 int execve(const char *filename, char *const argv[], char *const envp[])
 {
-	int ret = sys_execve(filename, argv, envp);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(execve, filename, argv, envp);
 }
 
 
@@ -365,13 +311,7 @@  pid_t sys_fork(void)
 static __attribute__((unused))
 pid_t fork(void)
 {
-	pid_t ret = sys_fork();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(fork);
 }
 
 
@@ -388,13 +328,7 @@  int sys_fsync(int fd)
 static __attribute__((unused))
 int fsync(int fd)
 {
-	int ret = sys_fsync(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(fsync, fd);
 }
 
 
@@ -411,13 +345,7 @@  int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
 static __attribute__((unused))
 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
 {
-	int ret = sys_getdents64(fd, dirp, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(getdents64, fd, dirp, count);
 }
 
 
@@ -455,13 +383,7 @@  pid_t sys_getpgid(pid_t pid)
 static __attribute__((unused))
 pid_t getpgid(pid_t pid)
 {
-	pid_t ret = sys_getpgid(pid);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(getpgid, pid);
 }
 
 
@@ -562,13 +484,7 @@  int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
 static __attribute__((unused))
 int gettimeofday(struct timeval *tv, struct timezone *tz)
 {
-	int ret = sys_gettimeofday(tv, tz);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(gettimeofday, tv, tz);
 }
 
 
@@ -606,13 +522,7 @@  int sys_ioctl(int fd, unsigned long req, void *value)
 static __attribute__((unused))
 int ioctl(int fd, unsigned long req, void *value)
 {
-	int ret = sys_ioctl(fd, req, value);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(ioctl, fd, req, value);
 }
 
 /*
@@ -628,13 +538,7 @@  int sys_kill(pid_t pid, int signal)
 static __attribute__((unused))
 int kill(pid_t pid, int signal)
 {
-	int ret = sys_kill(pid, signal);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(kill, pid, signal);
 }
 
 
@@ -657,13 +561,7 @@  int sys_link(const char *old, const char *new)
 static __attribute__((unused))
 int link(const char *old, const char *new)
 {
-	int ret = sys_link(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(link, old, new);
 }
 
 
@@ -684,13 +582,7 @@  off_t sys_lseek(int fd, off_t offset, int whence)
 static __attribute__((unused))
 off_t lseek(int fd, off_t offset, int whence)
 {
-	off_t ret = sys_lseek(fd, offset, whence);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(lseek, fd, offset, whence);
 }
 
 
@@ -713,13 +605,7 @@  int sys_mkdir(const char *path, mode_t mode)
 static __attribute__((unused))
 int mkdir(const char *path, mode_t mode)
 {
-	int ret = sys_mkdir(path, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(mkdir, path, mode);
 }
 
 
@@ -742,13 +628,7 @@  long sys_mknod(const char *path, mode_t mode, dev_t dev)
 static __attribute__((unused))
 int mknod(const char *path, mode_t mode, dev_t dev)
 {
-	int ret = sys_mknod(path, mode, dev);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(mknod, path, mode, dev);
 }
 
 #ifndef MAP_SHARED
@@ -806,13 +686,7 @@  int sys_munmap(void *addr, size_t length)
 static __attribute__((unused))
 int munmap(void *addr, size_t length)
 {
-	int ret = sys_munmap(addr, length);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(munmap, addr, length);
 }
 
 /*
@@ -832,13 +706,7 @@  int mount(const char *src, const char *tgt,
           const char *fst, unsigned long flags,
           const void *data)
 {
-	int ret = sys_mount(src, tgt, fst, flags, data);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(mount, src, tgt, fst, flags, data);
 }
 
 
@@ -872,13 +740,7 @@  int open(const char *path, int flags, ...)
 		va_end(args);
 	}
 
-	ret = sys_open(path, flags, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(open, path, flags, mode);
 }
 
 
@@ -898,13 +760,7 @@  static __attribute__((unused))
 int prctl(int option, unsigned long arg2, unsigned long arg3,
 		      unsigned long arg4, unsigned long arg5)
 {
-	int ret = sys_prctl(option, arg2, arg3, arg4, arg5);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(prctl, option, arg2, arg3, arg4, arg5);
 }
 
 
@@ -921,13 +777,7 @@  int sys_pivot_root(const char *new, const char *old)
 static __attribute__((unused))
 int pivot_root(const char *new, const char *old)
 {
-	int ret = sys_pivot_root(new, old);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(pivot_root, new, old);
 }
 
 
@@ -956,13 +806,7 @@  int sys_poll(struct pollfd *fds, int nfds, int timeout)
 static __attribute__((unused))
 int poll(struct pollfd *fds, int nfds, int timeout)
 {
-	int ret = sys_poll(fds, nfds, timeout);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(poll, fds, nfds, timeout);
 }
 
 
@@ -979,13 +823,7 @@  ssize_t sys_read(int fd, void *buf, size_t count)
 static __attribute__((unused))
 ssize_t read(int fd, void *buf, size_t count)
 {
-	ssize_t ret = sys_read(fd, buf, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(read, fd, buf, count);
 }
 
 
@@ -1003,13 +841,7 @@  ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
 static __attribute__((unused))
 int reboot(int cmd)
 {
-	int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
 }
 
 
@@ -1026,13 +858,7 @@  int sys_sched_yield(void)
 static __attribute__((unused))
 int sched_yield(void)
 {
-	int ret = sys_sched_yield();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(sched_yield);
 }
 
 
@@ -1072,13 +898,7 @@  int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva
 static __attribute__((unused))
 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
 {
-	int ret = sys_select(nfds, rfds, wfds, efds, timeout);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(select, nfds, rfds, wfds, efds, timeout);
 }
 
 
@@ -1095,13 +915,7 @@  int sys_setpgid(pid_t pid, pid_t pgid)
 static __attribute__((unused))
 int setpgid(pid_t pid, pid_t pgid)
 {
-	int ret = sys_setpgid(pid, pgid);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(setpgid, pid, pgid);
 }
 
 
@@ -1118,13 +932,7 @@  pid_t sys_setsid(void)
 static __attribute__((unused))
 pid_t setsid(void)
 {
-	pid_t ret = sys_setsid();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(setsid);
 }
 
 #if defined(__NR_statx)
@@ -1141,13 +949,7 @@  int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct sta
 static __attribute__((unused))
 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
 {
-	int ret = sys_statx(fd, path, flags, mask, buf);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(statx, fd, path, flags, mask, buf);
 }
 #endif
 
@@ -1227,13 +1029,7 @@  int sys_stat(const char *path, struct stat *buf)
 static __attribute__((unused))
 int stat(const char *path, struct stat *buf)
 {
-	int ret = sys_stat(path, buf);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(stat, path, buf);
 }
 
 
@@ -1256,13 +1052,7 @@  int sys_symlink(const char *old, const char *new)
 static __attribute__((unused))
 int symlink(const char *old, const char *new)
 {
-	int ret = sys_symlink(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(symlink, old, new);
 }
 
 
@@ -1296,13 +1086,7 @@  int sys_umount2(const char *path, int flags)
 static __attribute__((unused))
 int umount2(const char *path, int flags)
 {
-	int ret = sys_umount2(path, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(umount2, path, flags);
 }
 
 
@@ -1325,13 +1109,7 @@  int sys_unlink(const char *path)
 static __attribute__((unused))
 int unlink(const char *path)
 {
-	int ret = sys_unlink(path);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(unlink, path);
 }
 
 
@@ -1354,38 +1132,20 @@  pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 static __attribute__((unused))
 pid_t wait(int *status)
 {
-	pid_t ret = sys_wait4(-1, status, 0, NULL);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(wait4, -1, status, 0, NULL);
 }
 
 static __attribute__((unused))
 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 {
-	pid_t ret = sys_wait4(pid, status, options, rusage);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(wait4, pid, status, options, rusage);
 }
 
 
 static __attribute__((unused))
 pid_t waitpid(pid_t pid, int *status, int options)
 {
-	pid_t ret = sys_wait4(pid, status, options, NULL);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(wait4, pid, status, options, NULL);
 }
 
 
@@ -1402,13 +1162,7 @@  ssize_t sys_write(int fd, const void *buf, size_t count)
 static __attribute__((unused))
 ssize_t write(int fd, const void *buf, size_t count)
 {
-	ssize_t ret = sys_write(fd, buf, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(write, fd, buf, count);
 }
 
 
@@ -1425,13 +1179,7 @@  int sys_memfd_create(const char *name, unsigned int flags)
 static __attribute__((unused))
 int memfd_create(const char *name, unsigned int flags)
 {
-	ssize_t ret = sys_memfd_create(name, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __syscall(memfd_create, name, flags);
 }
 
 /* make sure to include all global symbols */