4 ms·
Cool "anthropology" method. This triggered my interest to go look at the C source code for linux[1]. Turns out the command actually relies on a few different fi
by acbart 8y ago
Cool "anthropology" method. This triggered my interest to go look at the C source code for linux[1]. Turns out the command actually relies on a few different files, including a "remove.h"[2]. I was surprised I didn't immediately see the call to unlink in the source code, but obviously there's a lot of useful infrastructure to build on. Further investigation that the unlinkat[3] syscall was actually being used. Minorly fascinating :)
[1] https://github.com/coreutils/coreutils/blob/master/src/rm.c https://github.com/coreutils/coreutils/blob/master/src/rm.c
[2] https://github.com/coreutils/coreutils/blob/master/src/remove.c https://github.com/coreutils/coreutils/blob/master/src/remov...
[3] https://linux.die.net/man/2/unlinkat https://linux.die.net/man/2/unlinkat
- zokier 8y ago> This triggered my interest to go look at the C source code for linux[1]. Coreutils is not Linux, and GNU tools are notoriously heavyweight. For contrast, here is Toybox implementation of rm: https://github.com/landley/toybox/blob/master/toys/posix/rm.c https://github.com/landley/toybox/blob/master/toys/posix/rm.... busybox: https://git.busybox.net/busybox/tree/coreutils/rm.c https://git.busybox.net/busybox/tree/coreutils/rm.c https://git.busybox.net/busybox/tree/libbb/remove_file.c https://git.busybox.net/busybox/tree/libbb/remove_file.c And finally openbsd: https://github.com/openbsd/src/blob/master/bin/rm/rm.c https://github.com/openbsd/src/blob/master/bin/rm/rm.c
- masklinn 8y agoAnd the actual code for OSX: https://opensource.apple.com/source/file_cmds/file_cmds-264.50.1/rm/rm.c.auto.html https://opensource.apple.com/source/file_cmds/file_cmds-264....
- dfox 8y agoI was somewhat surprised by the small number of process startup/libc initialization syscalls in the trace in original article. On Linux (Debian unstable in my case) you will get order of magnitude more, because rm is dynamically linked (although it seems that only with libc and nothing else), because of libc startup (did you know that linux has amd64-specific syscall arch_prctl(PRCTL_SET_FS), that does exactly what it sounds like?). And then because core utils rm cares about such things as whether stdin is terminal (probably because -f/-i behavior changes depending on that) and does the actual unlink in somewhat convoluted way that involves fstatat() (called twice, for some reason) and only then unlinkat(). Somewhat notably last thing that rm does is trying to lseek() stdin only to get ESPIPE...