删除一个空文件,为什么有这么多系统调用?

删除一个空文件,为什么有这么多系统调用?

尝试创建一个空文件并跟踪删除它所涉及的操作。名单很长:

touch a
strace rm a
execve("/bin/rm", ["rm", "a"], [/* 20 vars */]) = 0
brk(0)                                  = 0x65f000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd579b94000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=25132, ...}) = 0
mmap(NULL, 25132, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd579b8d000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1840928, ...}) = 0
mmap(NULL, 3949248, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd5795af000
mprotect(0x7fd57976a000, 2093056, PROT_NONE) = 0
mmap(0x7fd579969000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ba000) = 0x7fd579969000
mmap(0x7fd57996f000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd57996f000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd579b8c000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd579b8a000
arch_prctl(ARCH_SET_FS, 0x7fd579b8a740) = 0
mprotect(0x7fd579969000, 16384, PROT_READ) = 0
mprotect(0x60d000, 4096, PROT_READ)     = 0
mprotect(0x7fd579b96000, 4096, PROT_READ) = 0
munmap(0x7fd579b8d000, 25132)           = 0
brk(0)                                  = 0x65f000
brk(0x680000)                           = 0x680000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2919792, ...}) = 0
mmap(NULL, 2919792, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd5792e6000
close(3)                                = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
newfstatat(AT_FDCWD, "a", {st_mode=S_IFREG|0664, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
geteuid()                               = 1000
newfstatat(AT_FDCWD, "a", {st_mode=S_IFREG|0664, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
faccessat(AT_FDCWD, "a", W_OK)          = 0
unlinkat(AT_FDCWD, "a", 0)              = 0
lseek(0, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

这是为什么 ?

答案1

调用上方出现的所有系统调用close(3)都是由于动态库加载造成的。看起来它也在检查任何缓存的动态可链接库。

实际删除文件的系统调用以该newfstatat()调用开始,并包括 4 个其他系统调用。这 3 个close()调用和exit_group()是 C 运行时在main()函数rm返回或退出后进行清理。

稍微编辑一下,这就是人们希望静态链接的可执行文件的原因,并尝试各种类似的事情斯塔里或者睡眠

相关内容