How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

john01dav

I am looking to write a program that will need to do low level work with processes (ie. using the fork system call, among others). This program is to be written in C++ and is to run only on Linux. Ideally, it will be portable across CPU architectures (ie. x86, x86_64, and arm) with nothing more than a recompile, but I only really need x86_64 support.

As each Linux system call takes a number of arguments and returns a number of arguments in cpu registers (often only 1 return value), then a C function wrapper for each system call is likely easy to make. Also, because, AFAIK, system calls, being implemented in the kernel, have identical arguments and return values, if different assembly-level implementations, the same C interface can be exposed.

Does such a thing exist? If so, how can I access it?

Where is its documentation (list of available functions, their arguments with an explanation, and an explanation of exactly what the function does)?

Peter Cordes

libc already includes the wrapper functions you're looking for. The prototypes for many of them are in #include <unistd.h>, as specified by POSIX.

C is the language of low-level systems program on Unix (and Linux), so this has been a thing since Unix existed. (Providing wrapper functions in libc is easier than teaching compilers the difference between function call and system calls, and allows for setting errno on errors. It also allows for tricks like LD_PRELOAD to intercept system calls in user-space.)


The man pages for system calls are in section 2, vs. section 3 for library functions (which might or might not use system calls as part of their implementation: math.h cos(3), ISO C stdio printf(3) and fwrite(3), vs. POSIX write(2)).

execve(2) is the system call.

See execl(3) and friends are also part of libc, and eventually call execve(2). They are convenience wrappers on top of it for constructing the argv array, doing $PATH lookup, and passing along the current process's environment. Thus they're classed as functions, not system calls.

See syscalls(2) for an overview, and complete list of system Linux calls with links to their man-page wrappers. (I've linked the Linux man pages, but there are also POSIX man pages for all of the standard system calls.)


In the unlikely case that you're not linking libc, you can use macros like MUSL's syscall2 / syscall3 / etc. macros (the number is the arg count) to inline the right asm on whatever platform. You use __NR_write from asm/unistd.h to get system call numbers.

But note that the raw Linux system calls might have small differences from the interface provided by the libc wrappers. For example, they won't check for pthreads cancellation points, and brk / sbrk requires bookkeeping in user-space by libc.

See SYSCALL_INLINE in Android for a portable raw sys_write() inline wrapper using MUSL macros.

But if you are using libc like a normal person for functions like malloc and printf, you should just use its system call wrapper functions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Java

Is it possible to create threads without system calls in Linux x86 GAS assembly?

From Java

How can I make a class not extendable without using the final keyword?

From Java

How can I iterate over (system) properties in a thread safe manner?

From Dev

How can I make .change() fire when the text changes without using input from a keyboard?

From Dev

Assembly Linux system calls vs assembly OS x system calls

From Dev

How can I protect my application's register API from spammers without using CAPTCHA?

From Dev

How can I make equal height system without using any javascript or jquery plugin

From Dev

JavaFX app appears under javaw.exe in windows Task Manager. How can i make it appear as an independent application

From Dev

How do I make raycast from the cursor without using the physics system?

From Dev

How can i make a portable C++ program for Windows with GUI, independent from .NET or other Libs?

From Dev

Can I use Linux system calls in android?

From Dev

How to separate application from data in a good manner?

From Dev

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

From Dev

How can I make atomic incremental backups of a running linux system using freeware?

From Dev

How can I make a POST request from HTML form without browser using Python?

From Dev

How can I make ma webbrowser working independent from the System.Thread.Sleep?

From Dev

How can I show installed CSP's on my Linux system without using the cpconfig util?

From Dev

How can I run an independent express.js application using a 2nd node server?

From Dev

How can I make a similar application using street view?

From Dev

How to make System.out calls without affecting current users input (System.in) in console application?

From Dev

how can I consume rest api of one module from other module in same application using spring boot application without module dependency

From Dev

Movement is dependent to frame-rate. How can i make it independent from frame-rate

From Dev

How can I make the system empty value from the guid null?

From Dev

How can I make JavaScript calls to scrape data from a website?

From Dev

How do you make faster A.P.I calls using multithreading without using requests in Python?

From Dev

How can I make this code faster (without using a loop)?

From Dev

How can I make my Checkbuttons independent of each other on Tkinter?

From Dev

How can I make the radio buttons independent?

Related Related

  1. 1

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

  2. 2

    Is it possible to create threads without system calls in Linux x86 GAS assembly?

  3. 3

    How can I make a class not extendable without using the final keyword?

  4. 4

    How can I iterate over (system) properties in a thread safe manner?

  5. 5

    How can I make .change() fire when the text changes without using input from a keyboard?

  6. 6

    Assembly Linux system calls vs assembly OS x system calls

  7. 7

    How can I protect my application's register API from spammers without using CAPTCHA?

  8. 8

    How can I make equal height system without using any javascript or jquery plugin

  9. 9

    JavaFX app appears under javaw.exe in windows Task Manager. How can i make it appear as an independent application

  10. 10

    How do I make raycast from the cursor without using the physics system?

  11. 11

    How can i make a portable C++ program for Windows with GUI, independent from .NET or other Libs?

  12. 12

    Can I use Linux system calls in android?

  13. 13

    How to separate application from data in a good manner?

  14. 14

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

  15. 15

    How can I make atomic incremental backups of a running linux system using freeware?

  16. 16

    How can I make a POST request from HTML form without browser using Python?

  17. 17

    How can I make ma webbrowser working independent from the System.Thread.Sleep?

  18. 18

    How can I show installed CSP's on my Linux system without using the cpconfig util?

  19. 19

    How can I run an independent express.js application using a 2nd node server?

  20. 20

    How can I make a similar application using street view?

  21. 21

    How to make System.out calls without affecting current users input (System.in) in console application?

  22. 22

    how can I consume rest api of one module from other module in same application using spring boot application without module dependency

  23. 23

    Movement is dependent to frame-rate. How can i make it independent from frame-rate

  24. 24

    How can I make the system empty value from the guid null?

  25. 25

    How can I make JavaScript calls to scrape data from a website?

  26. 26

    How do you make faster A.P.I calls using multithreading without using requests in Python?

  27. 27

    How can I make this code faster (without using a loop)?

  28. 28

    How can I make my Checkbuttons independent of each other on Tkinter?

  29. 29

    How can I make the radio buttons independent?

HotTag

Archive