0

I have recently encountered remove() in C which removes files, however i was interested on how it worked. After doing some digging i found its function definition:

#include <errno.h>
#include <stdio.h>

int
remove (const char *file)
{
  __set_errno (ENOSYS);
  return -1;
}
libc_hidden_def (remove)

stub_warning (remove)

However i am failing to understand what this code does. I know that the function takes a char * and returns a int and returns -1 however the rest of it looks new to me. How does this function work? and what do __set_errno (ENOSYS);, libc_hidden_def (remove) and stub_warning (remove) mean?

  • 2
    This definition is a _stub_ that always fails. It is not actually used on any fully supported configuration of GNU libc. Look for other definitions in the sysdeps/ directory. – zwol Sep 24 '21 at 17:41
  • 1
    Take a look at [this SO question](https://stackoverflow.com/questions/6515583/where-are-syscalls-located-in-glibc-source). – Luca Polito Sep 24 '21 at 17:41
  • 1
    Take a look at [`ENOSYS`](https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html). – Oka Sep 24 '21 at 17:42
  • 1
    Note that `remove` must do most of its work by calling into the operating system; you may find [this code](https://github.com/mit-pdos/xv6-public/blob/eeb7b415dbcb12cc362d0783e41c3d1f44066b17/sysfile.c#L185) more informative. – zwol Sep 24 '21 at 17:43
  • 1
    The real `remove()` function calls the operating system's system call that deletes files. – Barmar Sep 24 '21 at 17:43
  • 1
    If you want to see an implementation of `remove()`, you can take a look at the [musl libc source code for `remove()`](https://git.musl-libc.org/cgit/musl/tree/src/stdio/remove.c). But it just makes a syscall to the kernel. Because it's the kernel that implements the logic behind `remove()`. – Luca Polito Sep 24 '21 at 17:45
  • @zwol https://github.com/lattera/glibc/blob/master/sysdeps/posix/remove.c would this be more appropriate? – programme3219873 Sep 24 '21 at 17:45
  • @LucaPolito is that how ```remove()``` is normally implemented (making syscalls to the kernel)? – programme3219873 Sep 24 '21 at 17:48
  • @Barmar ah ok i see – programme3219873 Sep 24 '21 at 17:49
  • 1
    @programme3219873 Most of the time, yes. You have to look at the kernel's source code to see more logic. – Luca Polito Sep 24 '21 at 17:52
  • @LucaPolito ah i see and im assuming these syscalls are pretty low level so are written in assembly – programme3219873 Sep 24 '21 at 17:55
  • @programme3219873 Not exactly: making a syscall does require some lines of assembly, but the implementation of `remove()` in the kernel may be 100% C. You may look at some small hobbyist Unix-like OSes, if you want to see a small (and relatively simple) implementation of `remove()` syscall in a kernel. – Luca Polito Sep 24 '21 at 17:58
  • 2
    @programme3219873 I believe `sysdeps/posix/remove.c` is the definition of `remove` that actually gets used on Linux, yes, but notice that it just calls `unlink` or `rmdir`, both of which are system calls. – zwol Sep 24 '21 at 18:10
  • ah i see thank you for the help ! – programme3219873 Sep 25 '21 at 11:38

0 Answers0