Linux system calls vs C lib functions

josh :

I have bit bit of confusion regarding these two so here are my questions;

The Linux man-pages project lists all these functions: https://www.kernel.org/doc/man-pages/

Looking at recvfrom as an example, this function exists both as a Linux system call as well as a C library function. Their documentation seems different but they are both reachable using #include <sys/socket.h>. I don't understand their difference?

I also thought systems calls are defined using hex values which can be implemented in assembly directly, their list is here: https://syscalls.kernelgrok.com/

However I cannot find recvfrom in the above link. I'm a bit confused between Linux system calls vs C lib functions at this point!

Edit: To add to the questions, alot of functions are under (3) but not (2), i.e clean. Does this mean these are done by C runtime directly without relying on system calls and the underlying OS?

S.S. Anne :

First, understand that that C functions and system calls are two completely different things.

System calls not wrapped in the C library must be called through the syscall function. One example of such a call is gettid.

To create a gettid system call wrapper with syscall, do this:

#define _GNU_SOURCE
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

pid_t gettid(void)
{
    pid_t tid = (pid_t)syscall(SYS_gettid);
    return tid;
}

Here's an excerpt from the NOTES section of the man-page, which explicitly states that this function is not defined within the C library:

NOTES
Glibc does not provide a wrapper for this system call; call it using syscall(2).


recvfrom is a C library wrapper around a system call.
Everything in section (2) is a system call. Everything in section (3) is not. Everything in section (3) (with a few notable exceptions, such as getumask) has a definition in the C library. About half of everything in section (2) does not have a definition (or wrapper) within the C library (with the exception of functions mandated by POSIX, and some other extensions, which all do), as with gettid.

When calling recvfrom in C, the C library calls the kernel to do the syscall.

The syscall function is the function that puts the system call number in the %eax register and uses int $0x80.

The reason you don't see recvfrom in https://syscalls.kernelgrok.com/ is because https://syscalls.kernelgrok.com/ is very, very incomplete.

The reason there are many functions in (3) that you don't see in (2) is because many functions on (3) don't have a system call. They may or may not rely on system calls, they just don't have a system call with that specific name that backs them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assembly Linux system calls vs assembly OS x system calls

From Dev

Linux System calls in C on OSX

From Java

System calls vs C/C++ system calls

From Dev

How to Mock Linux System Calls in C

From Dev

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

From Dev

Concurrent system calls in Linux

From Dev

Linux:System calls for Who

From Dev

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

From Java

system("linux_command") vs. Perl library functions

From Dev

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

From Dev

Software interrupts VS System calls

From Dev

C system calls fails

From Dev

C system calls open()

From Dev

Linux bare system calls, not glibc

From Dev

Difference between system calls and library functions

From Java

PHP calls to system vs Bash scripts Security

From Dev

File system: Kernel calls vs. Caching

From Java

What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64

From Dev

Packaging: /usr/lib vs. /usr/lib/*-linux-gnu

From Dev

Storing System Calls in C Programming

From Dev

Are system() C functions ok?

From Dev

System functions call 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 Dev

Basics questions regarding File and I/O System Calls in C (on Linux/UNIX)

From Java

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

From Dev

Using GSL lib functions on data in a struct C

From Dev

functions in c on linux

Related Related

HotTag

Archive