下面的幾個chown函數可用於更改文件的用戶ID和組ID。若是兩個參數owner或group中的任意一個是-1,則對應的ID不變。git
#include<unistd.h> int chown(const char *pathname,uid_t owner,gid_t group); int fchown(int fd,uid_t owner,gid_t group); int fchownat(int fd,const char *pathname,uid_t owner,gid_t group,int flag); int lchown(const char *pathname,uid_t owner,gid_t group); 若成功返回0;若出錯返回-1
在符號連接下,lchown和fchownat(設置了AT_SYMLINK_NOFOLLOW標誌)更改符號連接自己的全部者,而不是該符號連接所指向的文件的全部者。github
看下面的例子:函數
#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #define RWRWRW (S_IRUSR |S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) //#define _POSIX_CHOWN_RESTRICTED -1 int main(void) { umask(0);//remove the mask int rv = creat("source.txt",RWRWRW);//creat a file whose mode is -rw-rw-rw- system("ln -s source.txt source_l.txt");//create a soft link to "source.txt" whose mode is lrwxrwxrwx rv = lchown("source_l.txt",0,0);//update the user ID and group ID to 0 printf("rv:%d\n",rv); printf("errno:%d\n",errno); return 0; }
首先建立一個文件source.txt,而後建立一個符號連接source_1.txt,最後修改此符號連接的全部者和所屬組。ui
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ gcc 4-11.c harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ./a.out rv:-1 errno:1 harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll 總用量 29 -rw-r----- 1 harlan harlan 282 6月 5 22:52 4-10.c -rw-rw-r-- 1 harlan harlan 516 6月 6 21:51 4-11.c -rw-rw-r-- 1 harlan harlan 290 6月 1 22:13 4-7.c -rw-rw-r-- 1 harlan harlan 1168 6月 5 22:15 4-9.c -rw-rw-r-- 1 harlan harlan 4950 6月 1 09:07 4.c -rwxrwxr-x 1 harlan harlan 8788 6月 6 21:51 a.out lrwxrwxrwx 1 harlan harlan 10 6月 6 22:00 source_l.txt -> source.txt -rw-rw-rw- 1 harlan harlan 0 6月 6 22:00 source.txt
在harlan用戶下生成可執行文件,並執行,文件和符號連接生成成功,可是lchown執行失敗,errno爲1,表示「Operation not permitted」。code
緣由由於以下:進程
基於BSD的系統一直規定只有超級用戶才能更改一個文件的全部者。rem
切換到root用戶下執行,最後成功:it
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ su 密碼: root@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples# rm source* root@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples# ./a.out rv:0 errno:0 root@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples# ll 總用量 37 drwxrwsr-t 2 harlan harlan 0 6月 6 22:03 ./ drwxrwxrwx 2 harlan harlan 0 5月 18 21:16 ../ -rw-r----- 1 harlan harlan 282 6月 5 22:52 4-10.c -rw-rw-r-- 1 harlan harlan 516 6月 6 21:51 4-11.c -rw-rw-r-- 1 harlan harlan 290 6月 1 22:13 4-7.c -rw-rw-r-- 1 harlan harlan 1168 6月 5 22:15 4-9.c -rw-rw-r-- 1 harlan harlan 4950 6月 1 09:07 4.c -rwxrwxr-x 1 harlan harlan 8788 6月 6 21:51 a.out* lrwxrwxrwx 1 root root 10 6月 6 22:03 source_l.txt -> source.txt -rw-rw-rw- 1 root harlan 0 6月 6 22:03 source.txt
接下來看一下怎麼才能修改文件的組,知足以下條件就可以修改文件的組:
若是進程擁有此文件(有效用戶ID等於該文件的用戶ID),參數owner等於-1或文件的用戶ID,而且參數group等於進程的有效組ID或進程的附屬組ID之一,那麼一個非超級用戶進程能夠更改該文件的組ID。
看下面的例子:io
首先咱們爲當前用戶harlan添加一個附屬組ID,改以前:test
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ id harlan uid=1000(harlan) gid=1000(harlan) group=1000(harlan)
改以後:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ sudo usermod -G zexu harlan [sudo] password for harlan: harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ id harlan uid=1000(harlan) gid=1000(harlan) group=1000(harlan),1001(zexu)
爲了是當前設置生效,執行以下命令:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ newgrp zexu
最後看下面的代碼:
#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #define RWRWRW (S_IRUSR |S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) void test_chownGID() { int rv = creat("testGID.txt",RWRWRW); if(rv<0) { printf("create file error!\n"); } struct stat statbuf; if(stat("testGID.txt",&statbuf)<0) { printf("stat error!\n"); } else { printf("The current user's group ID is %d\n",statbuf.st_gid); } rv = chown("testGID.txt",-1,1001); printf("rv:%d\n",rv); if(stat("testGID.txt",&statbuf)<0) { printf("stat error!\n"); } else { printf("After chown,the user's group ID is %d\n",statbuf.st_gid); } } int main(void) { test_chownGID(); return 0; }
執行結果:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ./a.out The current user's group ID is 1000 rv:0 After chown,the user's group ID is 1001