So I was looking through the linux glibc source and I don't see where it actually does anything. The following is from io/chdir.c
but it is indicative of many of the source files. What's going on here? Obviously I am missing something. What's the secret, where does it make a system call or actually do something?
stub_warning
is some legacy craziness. __set_errno
seems to be a simple macro that sets errno
. And while I find a million usages of weak_alias
I don't see it defined anywhere.
Is there a helpful guide to understanding how glibc works somewhere?
#include <errno.h> #include <stddef.h> #include <unistd.h> /* Change the current directory to PATH. */ int __chdir (path) const char *path; { if (path == NULL) { __set_errno (EINVAL); return -1; } __set_errno (ENOSYS); return -1; } stub_warning (chdir) weak_alias (__chdir, chdir) #include <stub-tag.h>
The system call we are looking at is implemented in the sys_read function, and it must (like several other system call) dispatch execution to a file object. The file object itself must first be looked up based on the file descriptor that the user application passed to the system call.
The glibc wrapper is only copying arguments and unique system call number to the registers where the kernel expects them, then trapping to kernel mode and setting the errno if the system call returns an error number [man 2 intro]. It is possible to invoke system calls directly by using syscall [man 2 syscall].
fopen is a function call, but it may sometimes be referred to as a system call because it is ultimately handled by the "system" (the OS). fopen is built into the C runtime library.
A system call is a function that allows a process to communicate with the Linux kernel. It's just a programmatic way for a computer program to order a facility from the operating system's kernel. System calls expose the operating system's resources to user programs through an API (Application Programming Interface).
What you've found is a stub function for systems it's not implemented on. You need to look under the sysdeps
tree for the actual implementation. The following may be of interest:
sysdeps/unix/sysv/linux
sysdeps/posix
sysdeps/i386
(or x86_64
or whatever your cpu arch is)The actual system call code for chdir()
is auto-generated on most systems supported by glibc
, by the script make-syscalls.sh
. That's why you can't find it in the source tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With