chmod,fchmod,and fchmodat Functions

chmod, fchmod, fchmodat - change permissions of a file

原型如下:

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
//Return: 0 if OK, -1 on error

chmod操作指定文件
fchmod操作已经打开的文件
fchmodat当路径为绝对路径或者fd参数为AT_FDCWD且路径为相对路径,这时候类似于chmod。此外,路径是相对于fd打开的目录。flag参数用于改变fchmodat的行为,当flag为AT_SYMLINK_NOFOLLOW被设置,fchmodat不跟随symbolic links

为了改变文件的权限位(permission bits),进程的effective user ID必须等于文件owner ID,或者进程拥有superuser permissions

mode用于位与(bitwise OR)常量如下:
figure 4.11
相对于9个文件权限常量增加了:
two set-ID 常量(S_ISUID, S_ISGID)
saved-text 常量(S_ISVTX)—不是POSIX.1的一部分,是单一UNIX规范的XSI选项。下一节讲解。
three combined 常量(S_IRWXU, S_IRWXG, S_IRWXO)

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
    struct stat stat_buf;

/*获取文件的信息*/
    if(stat("rwrwrw", &stat_buf) < 0)
    {
        fprintf(stderr, "stat error : %s \n", strerror(errno));
        exit(-1);
    }
/*关闭文件的group write权限,打开set-group-ID*/
    if(chmod("rwrwrw", stat_buf.st_mode & ~S_IWGRP | S_ISGID) < 0)
    {
        fprintf(stderr, "chmod error : %s \n", strerror(errno));
        exit(-1);
    }
/*将文件的权限改成 user 可读可写可执行*/
    if(chmod("nogrp", S_IRWXU) < 0)
    {
        fprintf(stderr, "chmod error : %s \n", strerror(errno));
        exit(-1);
    }


    return 0;
}

从执行前后的结果来看,文件权限正确修改。
Finally, note that the time and date listed by the ls command did not change after
we ran the program. We’ll see in Section 4.19 that the chmod function updates only the time that the i-node was last changed.

The chmod functions automatically clear two of the permission bits under the following conditions:

The group ID of a newly created file might potentially be a group that the calling process does not belong to. Recall from Section 4.6 that it’s possible for the group ID of the new file to be the group ID of the parent directory. Specifically, if the group ID of the new file does not equal either the effective group ID of the process or one of the process’s supplementary group IDs and if the process does not have superuser privileges, then the set-group-ID bit is automatically turned off. This prevents a user from creating a set-group-ID file owned by a group that the user doesn’t belong to.

Linux 3.2.0, Mac OS X 10.6.8, and Solaris 10 add another security feature to try to prevent misuse of some of the protection bits. If a process that does not have superuser privileges writes to a file, the set-user-ID and set-group-ID bits are automatically turned off. If malicious users find a set-group-ID or a set-user-ID file they can write to, even though they can modify the file, they lose the special privileges of the file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值