Linux bare system calls, not glibc

bicup

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)
                                         ^

Basile Starynkevitch

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 (e.g. 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, e.g. 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 (e.g. 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 (e.g. pmap $$ in a shell).

Some rare syscalls (e.g. 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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Concurrent system calls in Linux

From Dev

Linux:System calls for Who

From Dev

Linux glibc system call wrappers location

From Dev

Linux System calls in C on OSX

From Dev

Hoe does a bare metal hypervisor and the operating system it hosts cooridinate on system calls?

From Dev

Assembly Linux system calls vs assembly OS x system calls

From Dev

Does glibc work on bare metal or RTOS platforms?

From Java

Linux system calls vs C lib functions

From Dev

How to Mock Linux System Calls in C

From Java

How many system calls are there in linux kernel 2.6?

From Dev

Can I use Linux system calls in android?

From Dev

Why are linux system calls different across architectures

From Java

Are function calls like read() , write() actual system calls in linux?

From Dev

Installing Oracle Linux in bare metal

From Java

Why do some Linux system calls not have a wrapper, but are documented as if they do?

From Dev

Same programm works in C, but not in C++ (uses linux system calls)

From Java

What are the Windows and Linux native OS/system calls made from malloc()?

From Java

Why do some Linux system calls have two man pages?

From Java

What is the interface for ARM system calls and where is it defined in the Linux kernel?

From Java

How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

From Dev

How can I find the implementations of Linux kernel system calls?

From Dev

Copy files of directory into another using system calls on linux

From Dev

Linux terminal command for tracing threads and system calls in a process

From Dev

When do Linux system calls trigger a segfault vs returning EFAULT?

From Dev

C application that copy/moves a file & uses system calls in Linux

From Dev

How to correctly intercept system calls in the Linux kernel 5.*?

From Dev

exec() and system() system calls

From Dev

Compiling Mono (linux) with static glibc

From Dev

How to rebuild glibc on Arch Linux?

Related Related

  1. 1

    Concurrent system calls in Linux

  2. 2

    Linux:System calls for Who

  3. 3

    Linux glibc system call wrappers location

  4. 4

    Linux System calls in C on OSX

  5. 5

    Hoe does a bare metal hypervisor and the operating system it hosts cooridinate on system calls?

  6. 6

    Assembly Linux system calls vs assembly OS x system calls

  7. 7

    Does glibc work on bare metal or RTOS platforms?

  8. 8

    Linux system calls vs C lib functions

  9. 9

    How to Mock Linux System Calls in C

  10. 10

    How many system calls are there in linux kernel 2.6?

  11. 11

    Can I use Linux system calls in android?

  12. 12

    Why are linux system calls different across architectures

  13. 13

    Are function calls like read() , write() actual system calls in linux?

  14. 14

    Installing Oracle Linux in bare metal

  15. 15

    Why do some Linux system calls not have a wrapper, but are documented as if they do?

  16. 16

    Same programm works in C, but not in C++ (uses linux system calls)

  17. 17

    What are the Windows and Linux native OS/system calls made from malloc()?

  18. 18

    Why do some Linux system calls have two man pages?

  19. 19

    What is the interface for ARM system calls and where is it defined in the Linux kernel?

  20. 20

    How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

  21. 21

    How can I find the implementations of Linux kernel system calls?

  22. 22

    Copy files of directory into another using system calls on linux

  23. 23

    Linux terminal command for tracing threads and system calls in a process

  24. 24

    When do Linux system calls trigger a segfault vs returning EFAULT?

  25. 25

    C application that copy/moves a file & uses system calls in Linux

  26. 26

    How to correctly intercept system calls in the Linux kernel 5.*?

  27. 27

    exec() and system() system calls

  28. 28

    Compiling Mono (linux) with static glibc

  29. 29

    How to rebuild glibc on Arch Linux?

HotTag

Archive