简体   繁体   中英

Linux bare system calls, not glibc

I'm reading an article that explains how to call bare syscalls without passing through glibc. To call chmod and exit , use:

#include <linux/unistd.h>
_syscall2(int,chmod,char*,f,int,m)
_syscall1(int,exit,int,r)

My gcc complains about them. What are their use, how do they work?

$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
$ gcc e.c 
e.c:2:15: error: unknown type name ‘setresuid’; did you mean ‘__NR_setresuid’?
 _syscall3(int,setresuid,int,r,int,e,int,s)
               ^~~~~~~~~
               __NR_setresuid
e.c:2:29: error: unknown type name ‘r’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                             ^
e.c:2:35: error: unknown type name ‘e’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                                   ^
e.c:2:41: error: unknown type name ‘s’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                                         ^

Your article is probably obsolete.

If you code in C, there is no reason to avoid using the syscalls(2) (notice the plural) as documented. Be also aware of the vdso(7) . You could use some other C standard library than the glibc (eg musl-libc , dietlibc , etc...) and you might (but that is not recommended) statically link it.

You might use syscall(2) (notice the singular) instead. I see no reason to do that, eg use read(2) or mmap(2) without syscall .

The Assembly HowTo might be an interesting read (beware, it might be too 32 bits centric, most Linux PCs today are 64 bits x86-64).

See also osdev.org

BTW, some old Unixes (eg Solaris) had a libsys providing just the syscalls, and their libc linked to it. I would like a libsys too! But on current Linux systems, it does not really matter, since almost every process (running some dynamically linked ELF executable) is mmap(2) -ing, after ld-linux.so(8) , several segments and sections of your libc.so.6 ; for details, read Drepper's How to write a shared library (since it also explains in details how shared libraries actually work). Use also pmap(1) on some running process (eg pmap $$ in a shell).

Some rare syscalls (eg userfaultfd(2) today 2Q2019) are not known by the glibc . They are an exception, because most system calls are wrapped by your libc (the wrapping usually just deals with errno(3) setting on failure). Be aware of strace(1) .

And you also should read Operating Systems: Three Easy Pieces (it is a freely downloadable book, explaining the role of, and reason for, system calls)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM