系统调用 fchownat

fchownat 是从 Linux kernel 2.6.16 开始引入的系统调用,扩展了传统的系统调用功能,允许通过文件描述符(df)指定的目录路径进行操作。与传统调用不同,*at 调用提供了更灵活的路径处理方式。示例中展示了 openat 和 unlinkat 的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fchownat 是linux kernel 2.6.16 以后添加的系统调用

linux kernel 2.6.16 新增了系列 at 系统调用( openat, linkat ..... )

原型:include/linux/syscalls.h

asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group, int flag);

说明:

The fchownat() function sets the owner ID and  group  ID  of
     the named  file  in the same manner as chown(). If, however,
     the path argument is relative, the path is resolved relative
     to  the  fildes  argument  rather  than  the current working
     directory.  If the fildes argument  has  the  special  value
     FDCWD,  the  path  path  resolution  reverts back to current
     working directory relative.  If the flag argument is set  to
     SYMLNK,  the  function behaves like lchown() with respect to
     symbolic links. If the path argument is absolute, the fildes
     argument  is  ignored.   If  the  path  argument  is  a null
     pointer, the function behaves like fchown().


这些 *at 系统调用相对与之前的系统调用增加了个文件描述符参数(上面的 int dfd)

功能是相对与相对与此文件描述符(目录)的路径而操作, 之前的系统调用的操作都是相对与进程当前位置的

当然,如果此文件描述符未设置或者不是个有效的目录文件描述符, *at 系统调用就和老版系统调用功能相同了


一个例子展示了 openat 系统调用的用法:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
    int dfd,ffd;
    dfd = open("/home", O_RDONLY|00200000); // 00200000其实就是 O_DIRECTORY
    if(dfd < 1)
    {
        perror("open home");
        return -1;
    }
    ffd = openat(dfd, "../opt/openat", O_CREAT, 777);
    if(ffd < 1)
    {
        perror("open new file");
        return -1;
    }
    return 0;
}

该程序创建了文件 /opt/openat

下段代码则演示了 unlinkat 的用法:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
    int dfd,ffd;
    dfd = open("/opt/cc", O_RDONLY|00200000);
    if(dfd < 1)
    {
        perror("open home");
        return -1;
    }
    //ffd = openat(dfd, "../opt/deny/openat", O_CREAT, 777);
    ffd = unlinkat(dfd,"../deny/openat", 0);
    if(ffd  != 0)
    {
        perror("unlinkat new file");
        return -1;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值