APUE-文件和目录(二)函数access,mask,chmod和粘着位
4.7 函數(shù)access和faccessat
當一個進程使用了設(shè)置用戶ID和設(shè)置組ID作為另一個用戶(或者組)運行時,這時候有效用戶(組)ID和實際用戶(組)ID不一樣,但進程仍然希望測試實際用戶(組)ID的訪問能力。這時候就可以使用access和faccessat。測試步驟同4.5節(jié)一樣,但將有效改為實際。
#include <unistd.h> int access(const char *pathname,int mode); int faccessat(int fd,const char*pathname,int mode,int flag); //兩個函數(shù)的返回值:若成功煩怒I0;若出錯,返回-1其中,如果測試文件是否已經(jīng)存在,mode就為F_OK;否則mode是圖4-7中所列常量的按位或。
flag參數(shù)可以用于改變faccessat的行為,如果flag設(shè)置為AT_EACCESS,訪問檢查用的是調(diào)用進程的有效用戶ID和有效組ID,而不是實際用戶ID和實際組ID。
4.8 函數(shù)umask
umask函數(shù)為進程設(shè)置文件模式創(chuàng)建屏蔽字,并返回之前的值。
#include <sys/stat.h> mode_t umask(mode_t cmask);其中,參數(shù)cmask是由圖4-6中列出的9個常量中的若干個按位"或“構(gòu)成的。
在文件模式創(chuàng)建屏蔽字中為1的位,在文件mode中的相應(yīng)位一定被關(guān)閉。
看下面的例子:
首先將屏蔽位設(shè)為0,然后創(chuàng)建一個文件,發(fā)現(xiàn)默認創(chuàng)建的文件mode為u=rw,g=rw,o=rw(666)
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ umask 0 harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ umask -S u=rwx,g=rwx,o=rwx harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ touch test.txt harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll test.txt -rw-rw-rw- 1 harlan harlan 0 6月 5 21:02 test.txt下面更新文件模式屏蔽字,并創(chuàng)建文件:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ umask 027 harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ umask -S u=rwx,g=rx,o= harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ touch test2.txt harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll test2.txt -rw-r----- 1 harlan harlan 0 6月 5 21:05 test2.txt因為group的x和other的rwx被屏蔽掉了,因此得到上面的結(jié)果。
umask值表示為8進制數(shù),一位代表一種要屏蔽的權(quán)限,見下圖所示:
注意: 用數(shù)字和符號表示文件模式屏蔽字的不同,數(shù)字中的1表示的是要屏蔽的權(quán)限,而符號中顯示的是支持的權(quán)限。
4.9 函數(shù)chmod
chmod使得我們可以更改現(xiàn)有文件的訪問權(quán)限。
#include <sys/stat.h> int chmod(const char*pathname,mode_t name); 成功返回0;出錯返回-1為了改變一個文件的權(quán)限位,進程的有效用戶ID必須等于文件的所有者ID,或者該用戶必須具有超級用戶權(quán)限。
疑問:可讀可寫可執(zhí)行權(quán)限和chmod有關(guān)系么?
答:沒有任何關(guān)系。
看下面一個簡單的例子,創(chuàng)建一個其他用戶可讀可寫可執(zhí)行的文件所有者為root的文件:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll chmod.txt -rw-r--rwx 1 root root 0 6月 5 21:16 chmod.txt在harlan用戶下創(chuàng)建下面的程序:
#include <fcntl.h> #include <stdio.h> int main(void) {struct stat statbuf;if(stat("chmod.txt",&statbuf)<0)printf("stat error!\n");int rv = chmod("chmod.txt",statbuf.st_mode & ~S_IRWXO);//remove the other's read write and executeif(rv < 0){printf("chmod error!\n");}return 0; }上面的程序嘗試在harlan用戶下來修改chmod.txt文件的權(quán)限位,harlan用戶對chmod.txt是可讀可寫可執(zhí)行的。但是它不能修改此文件的權(quán)限位,結(jié)果打印:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ./a.out chmod error!參數(shù)mode是圖4-11中所示的常量的按位或:
注意:chmod函數(shù)修改的是i節(jié)點最近一次被更改的時間,而并不是最后修改文件內(nèi)容的時間。
注意:如果新文件的組ID不等于進程的有效組ID或者進程附屬組ID中的一個,并且進程沒有超級用戶權(quán)限,那么設(shè)置組ID位會被自動關(guān)閉。在linux3.2.0中,如果沒有超級用戶權(quán)限的進程寫一個文件,則設(shè)置用戶ID位和設(shè)置組ID位會被自動清除。看下面的例子:
對于文件chmod.txt,我們同時設(shè)置了設(shè)置用戶ID和設(shè)置組ID,
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll chmod.txt -rwSr-srwx 1 root root 4 6月 5 22:16 chmod.txt寫一段代碼來用普通用戶權(quán)限寫這個文件
#include <fcntl.h> #include <stdio.h>void PrintSetUserID(unsigned int st_mode) {if(st_mode&S_ISUID){printf("set-user-ID is 1.\n");}else{printf("set-user-ID is 0.\n");} }void PrintSetGroupID(unsigned int st_mode) {if(st_mode&S_ISGID){printf("set-group-ID is 1.\n");}else{printf("set-group-ID is 0.\n");} } void ClearSetUserID_SetGroupID() {struct stat statbuf;if(stat("chmod.txt",&statbuf)<0)printf("stat error!-");else{PrintSetUserID(statbuf.st_mode);PrintSetGroupID(statbuf.st_mode);}int fd = open("chmod.txt",O_RDWR);ssize_t num = write(fd,"test",4);printf("write num:%d\n",(int)num);if(stat("chmod.txt",&statbuf)<0)printf("stat error!--\n");else{PrintSetUserID(statbuf.st_mode);PrintSetGroupID(statbuf.st_mode);} } int main(void) { ClearSetUserID_SetGroupID(); return 0; }運行結(jié)果:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ./a.out set-user-ID is 1. set-group-ID is 1. write num:4 set-user-ID is 0. set-group-ID is 0.4.10 粘著位
- 對于文件的粘著位,因為現(xiàn)今的UNIX系統(tǒng)大多都配置了虛擬存儲系統(tǒng)以及快速文件系統(tǒng),所以不再需要這個設(shè)置。
- 對于目錄的粘著位,如果你為一個目錄設(shè)置了粘著位,只有對該目錄具有寫權(quán)限的用戶并且滿足下列條件之一,才能刪除或重命名該目錄下的文件:
看下面的例子:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ mkdir dir harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ chmod 777 dir harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll ... drwxrwsrwx 2 harlan harlan 0 6月 5 22:47 dir ... harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ chmod o+t dir harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ll ... drwxrwsrwt 2 harlan harlan 0 6月 5 22:47 dir ...疑問:粘著位和其他寫是否同時存在?見下面的代碼:
#include <fcntl.h> #include <stdio.h> void main() { struct stat statbuf; if(stat("dir",&statbuf)) printf("stat error!\n"); if(statbuf.st_mode & S_ISVTX) { printf("sticky bit is set!\n"); } if(statbuf.st_mode & S_IXOTH) { printf("other execute bit is set!\n"); } }輸出結(jié)果為:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ ./a.out sticky bit is set! other execute bit is set!可見是可以同時存在的。同時存在和只存在黏著位的顯示有什么區(qū)別呢?見下面的例子:
harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ chmod 776 dir drwxrwsrw- 2 harlan harlan 0 6月 5 22:47 dir harlan@DESKTOP-KU8C3K5:/github/APUE/chapter_4/myexamples$ chmod o+t dir drwxrwsrwT 2 harlan harlan 0 6月 5 22:47 dir可見如果只有粘著位其它的第三位為大寫的T,如果是小寫表示粘著位+可執(zhí)行。
/var/tmp 目錄設(shè)置了粘著位+任意用戶的可讀可寫可執(zhí)行
harlan@DESKTOP-KU8C3K5:/var$ ll 總用量 12 drwxr-xr-x 2 root root 0 4月 11 2014 backups drwxr-xr-x 2 root root 0 3月 30 14:52 cache drwxrwxrwt 2 root root 0 3月 30 14:52 crash drwxr-xr-x 2 root root 0 4月 27 22:17 lib drwxrwsr-x 2 root staff 0 4月 11 2014 local lrwxrwxrwx 1 root root 9 3月 30 14:50 lock -> /run/lock drwxrwxr-x 2 root syslog 0 5月 1 21:38 log drwxrwsr-x 2 root mail 0 3月 30 14:50 mail drwxr-xr-x 2 root root 0 3月 30 14:50 opt lrwxrwxrwx 1 root root 4 3月 30 14:50 run -> /run drwxr-xr-x 2 root root 0 3月 30 14:50 spool drwxrwxrwt 2 root root 0 6月 5 22:28 tmp我們用root用戶在tmp目錄下面創(chuàng)建一個文件:
root@DESKTOP-KU8C3K5:/var/tmp# ll 總用量 4 drwxrwxrwt 2 root root 0 6月 5 22:28 ./ drwxr-xr-x 2 root root 0 3月 30 14:53 ../ -rw-r--r-- 1 root root 0 6月 5 22:28 test.txt嘗試在harlan用戶下刪除:
harlan@DESKTOP-KU8C3K5:/var/tmp$ rm test.txt rm:是否刪除有寫保護的普通空文件 "test.txt"? y rm: 無法刪除"test.txt": 不允許的操作雖然harlan用戶對這個目錄具有寫權(quán)限,但因為所在目錄設(shè)置了粘著位,并且不滿足上述任意三個條件之一,因此不訥訥個刪除文件。
作者: HarlanC
博客地址: http://www.cnblogs.com/harlanc/
個人博客: http://www.harlancn.me/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出, 原文鏈接
如果覺的博主寫的可以,收到您的贊會是很大的動力,如果您覺的不好,您可以投反對票,但麻煩您留言寫下問題在哪里,這樣才能共同進步。謝謝!
總結(jié)
以上是生活随笔為你收集整理的APUE-文件和目录(二)函数access,mask,chmod和粘着位的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用gulp对项目html,js,css
- 下一篇: iOS11和机器学习CoreML库