系統調用 fchownat

來源:互聯網
上載者:User

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.