【问题标题】:Beside umask what else affects permissions of new directories?除了 umask 还有什么会影响新目录的权限?
【发布时间】:2016-01-28 17:12:59
【问题描述】:

我注意到,如果你中断rsync,一些新目录仍然拥有drwx------ 的权限,尽管当前的umask 是0022

我启动了gdb 并尝试在调用mkdir() 之前显式调用umask(0),但它没有效果:我希望新目录有drwxrwxrwx,但他们仍然有drwx------

(在较新版本的 rsync 中,它们不再是 drwx------,但仍然不受 umask 影响)

如果我将命令更改为/bin/mkdir,调用umask() 开始工作。 rsync 似乎在使用某种魔法。

这是一个测试脚本: (由于某种原因,mkdir() 上的断点仅在从 ssh 复制时有效)

(
rm -rf /tmp/3 && mkdir -p /tmp/3 && cd /tmp/3 &&
#gdb -q -nx --args /bin/mkdir foo <<EOF
gdb -q -nx --args rsync -r --include=/profile.d --exclude="*" localhost:/etc/ ./ <<'EOF'
set width 0
set height 0
set pagination no
set breakpoint pending on
b mkdir
b mkdirat
run
del
print (char*)$rdi
call umask(0)
call mkdir("test")
fin
shell ls -l
p/o umask(0)
k
EOF
)

_

Reading symbols from /usr/bin/rsync...Reading symbols from /usr/bin/rsync...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Missing separate debuginfos, use: debuginfo-install rsync-3.0.9-15.el7.x86_64
(gdb) (gdb) (gdb) (gdb) (gdb) Breakpoint 1 at 0x6c70
(gdb) Function "mkdirat" not defined.
Breakpoint 2 (mkdirat) pending.
(gdb) Starting program: /usr/bin/rsync -r --include=/profile.d --exclude=\* localhost:/etc/ ./
Detaching after fork from child process 15444.
Detaching after fork from child process 15464.

Breakpoint 1, 0x00007ffff76ee720 in mkdir () from /lib64/libc.so.6
(gdb) Delete all breakpoints? (y or n) [answered Y; input not from terminal]

mkdir 参数:

(gdb) $1 = 0x7fffffff9330 "profile.d"

之前的umask值:

(gdb) $2 = 0

mkdir("test")的结果

(gdb) $3 = 0

_

(gdb) Run till exit from #0  0x00007ffff76ee720 in mkdir () from /lib64/libc.so.6
0x000055555556845f in recv_generator ()
(gdb) total 8
drwx------ 2 il il 4096 Jan 28 20:02 profile.d
drwx------ 2 il il 4096 Jan 28 20:02 test
(gdb) $4 = 0
(gdb) Kill the program being debugged? (y or n) [answered Y; input not from terminal]
rsync: connection unexpectedly closed (31 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]
(gdb) quit
[il@basinsrv ~]$ rsync: connection unexpectedly closed (51 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [receiver=3.0.9]

【问题讨论】:

    标签: c linux rsync umask


    【解决方案1】:

    创建目录时请求的权限被umask屏蔽,以获得最终的权限。

    Rsync 最有可能使用远程目录的权限来应用到本地目录。此外,rsync 可能在使用 chmod() 系统调用创建目录后设置权限,根本不应用 umask。

    umask 设置旨在在创建目录或文件时允许默认权限,而无需专门设置文件的权限。大多数创建文件的简单实用程序只是请求权限模式 666(每个人的完全权限),然后让 umask 修剪它。

    请注意,使用 gdb 可能无法按预期工作。 GDB 可以中断用户空间中的函数调用,但不能中断系统调用。尝试使用strace(如果这是 Linux/Unix) - 它会显示所有系统调用(对于这种情况非常非常方便)。

    【讨论】:

    • 在调用 ls -l 之前,我不允许 rsync 调用除 mkdir() 之外的任何其他函数。它无法更改权限
    • mkdir(const char *path, mode_t mode);第二个参数是请求的模式,对其应用 umask 以使其更具限制性。
    • 除了 strace,ltrace man7.org/linux/man-pages/man1/ltrace.1.html 可能有助于理解 rsync 正在做什么
    猜你喜欢
    • 1970-01-01
    • 2018-03-10
    • 2023-03-10
    • 1970-01-01
    • 2012-05-25
    • 2014-11-26
    • 2013-02-13
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多