This question was migrated from Super User because it can be answered on Stack Overflow. Migrated4个月前。Learn more
我的环境是运行nfs的FreeBSD 12服务器。我尝试过的客户机包括CentOS 7和Artix Linux。
当我尝试以普通用户的身份聚集一个文件时,flock系统调用在ENOLCK中失败。然而,当我试图以根的身份聚集时,它成功了,并且表现出了预期的效果。也就是说,如果我从两个客户机中聚集相同的文件,第二个客户机将阻塞,直到第一个客户机释放锁为止。
有趣的是,当客户端是另一个FreeBSD 12安装时,锁定确实有效。
编辑:下面是一个例子:
user@client$ flock /projects/testfile ls
flock: /projects/testfile: No locks available

下面是战略:
execve("/usr/bin/flock", ["flock", "/projects/testfile", "ls"], 0x7ffe68f60370 /* 34 vars */) = 0
brk(NULL)                               = 0x560012532000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffc4bd04660) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/haswell/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/haswell", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/tls", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/haswell/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/haswell", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/mpi/openmpi/lib", {st_mode=S_IFDIR|0775, st_size=1454, ...}) = 0
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/tls/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/tls/haswell/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/tls/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/tls/haswell", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/tls/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/tls", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/haswell/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/haswell/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/haswell/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/haswell", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib/x86_64", 0x7ffc4bd038a0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/pgi/linux86-64/18.4/lib", {st_mode=S_IFDIR|0755, st_size=3480, ...}) = 0
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=259146, ...}) = 0
mmap(NULL, 259146, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff2a1391000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/librt.so.1", 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\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=35096, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff2a138f000
mmap(NULL, 39416, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff2a1385000
mmap(0x7ff2a1387000, 16384, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7ff2a1387000
mmap(0x7ff2a138b000, 8192, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7ff2a138b000
mmap(0x7ff2a138d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7000) = 0x7ff2a138d000
close(3)                                = 0
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/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\360=\2\0\0\0\0\0"..., 832) = 832
lseek(3, 792, SEEK_SET)                 = 792
read(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\37\245\323=\353Gzs\267\5\27\265R\0\7U"..., 68) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=2141696, ...}) = 0
lseek(3, 792, SEEK_SET)                 = 792
read(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\37\245\323=\353Gzs\267\5\27\265R\0\7U"..., 68) = 68
lseek(3, 864, SEEK_SET)                 = 864
read(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32) = 32
mmap(NULL, 1852992, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff2a11c0000
mprotect(0x7ff2a11e2000, 1675264, PROT_NONE) = 0
mmap(0x7ff2a11e2000, 1359872, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7ff2a11e2000
mmap(0x7ff2a132e000, 311296, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16e000) = 0x7ff2a132e000
mmap(0x7ff2a137b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ba000) = 0x7ff2a137b000
mmap(0x7ff2a1381000, 13888, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff2a1381000
close(3)                                = 0
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/mpi/openmpi/lib/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/pgi/linux86-64/18.4/lib/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libpthread.so.0", 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\340f\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=155600, ...}) = 0
lseek(3, 808, SEEK_SET)                 = 808
read(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32) = 32
mmap(NULL, 131528, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff2a119f000
mmap(0x7ff2a11a5000, 61440, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7ff2a11a5000
mmap(0x7ff2a11b4000, 24576, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15000) = 0x7ff2a11b4000
mmap(0x7ff2a11ba000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a000) = 0x7ff2a11ba000
mmap(0x7ff2a11bc000, 12744, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff2a11bc000
close(3)                                = 0
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff2a119c000
arch_prctl(ARCH_SET_FS, 0x7ff2a119c740) = 0
mprotect(0x7ff2a137b000, 16384, PROT_READ) = 0
mprotect(0x7ff2a11ba000, 4096, PROT_READ) = 0
mprotect(0x7ff2a138d000, 4096, PROT_READ) = 0
mprotect(0x56001243e000, 4096, PROT_READ) = 0
mprotect(0x7ff2a13fb000, 4096, PROT_READ) = 0
munmap(0x7ff2a1391000, 259146)          = 0
set_tid_address(0x7ff2a119ca10)         = 5376
set_robust_list(0x7ff2a119ca20, 24)     = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7ff2a11a5130, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff2a11b14d0}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7ff2a11a51d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7ff2a11b14d0}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL)                               = 0x560012532000
brk(0x560012553000)                     = 0x560012553000
openat(AT_FDCWD, "/projects/testfile", O_RDONLY|O_CREAT|O_NOCTTY, 0666) = 3
flock(3, LOCK_EX)                       = -1 ENOLCK (No locks available)
write(2, "flock: ", 7flock: )                  = 7
write(2, "/projects/testfile", 18/projects/testfile)      = 18
write(2, ": No locks available\n", 21: No locks available
)  = 21
close(1)                                = 0
close(2)                                = 0
exit_group(71)                          = ?
+++ exited with 71 +++

这是它的根:
在客户机上,我尝试锁定文件(vim只是为了通过退出来控制何时轻松解锁):
root@client1$ flock /projects/testfile vim

客户一号拿到锁。成功,vim启动。对于客户二,我也会这样做:
root@client2$ flock /projects/testfile vim

客户端2个街区,直到我关闭客户端1上的vim。

最佳答案

结果我犯了个愚蠢的错误。文件归root所有。我的测试文件在某个时候被删除了(它已经被单独锁定了),我一定是不假思索地将它重新创建为根文件。
普通用户无法锁定没有权限的文件。

10-08 14:11