编辑

编辑

当我位于 gentoo-guest 上的已安装目录 (9p) 中并执行以下操作:

TMPDIR='./foo' # The directory './foo' exists and the name 'TMPDIR' is important
cat <<EOF
hello world
EOF

我得到以下输出:

cat: -: No such file or directory

一旦我取消设置$TMPDIR和/或删除./foo一切,就会按预期工作。

看来猫依赖于$TMPDIR.这是一个错误吗?

编辑

看起来strace像这样:

execve("/bin/cat", ["cat"], 0x7fffdbbfeb20 /* 23 vars */) = 0
brk(NULL)                               = 0x55eca4cce000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=22796, ...}) = 0
mmap(NULL, 22796, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7166997000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1857312, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7166995000
mmap(NULL, 3963464, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f71663af000
mprotect(0x7f716656d000, 2097152, PROT_NONE) = 0
mmap(0x7f716676d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1be000) = 0x7f716676d000
mmap(0x7f7166773000, 14920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7166773000
close(3)                                = 0
arch_prctl(ARCH_SET_FS, 0x7f7166996540) = 0
mprotect(0x7f716676d000, 16384, PROT_READ) = 0
mprotect(0x55eca40ce000, 4096, PROT_READ) = 0
mprotect(0x7f716699d000, 4096, PROT_READ) = 0
munmap(0x7f7166997000, 22796)           = 0
brk(NULL)                               = 0x55eca4cce000
brk(0x55eca4cef000)                     = 0x55eca4cef000
fstat(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(229, 0), ...}) = 0
fstat(0, 0x7ffe16cc1ec0)                = -1 ENOENT (No such file or directory)
write(2, "cat: ", 5)                    = 5
write(2, "-", 1)                        = 1
write(2, ": No such file or directory", 27) = 27
write(2, "\n", 1)                       = 1
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(1)                           = ?
+++ exited with 1 +++

答案1

实际上cat不依赖于$TMPDIR,但 shell 重定向<<依赖于 。

在这种情况下,似乎cat尝试执行fstat()on stdin(fd 0),但由于某种原因失败(来自 strace):

fstat(0, 0x7ffe16cc1ec0)                = -1 ENOENT (No such file or directory)

在正常环境下这不是问题:

strace -f cat <<FOO
> bar
> FOO
execve("/usr/bin/cat", ["cat"], [/* 91 vars */]) = 0

...

fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
fstat(0, {st_mode=S_IFREG|0600, st_size=4, ...}) = 0
fadvise64(0, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
mmap(NULL, 139264, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4844156000
read(0, "bar\n", 131072)                = 4
write(1, "bar\n", 4bar
)                    = 4
read(0, "", 131072)                     = 0
munmap(0x7f4844156000, 139264)          = 0
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

再说一遍:这不是 中的错误cat,而是虚拟环境中的一些错误。

相关内容