2. 函數(shù)stat、fstat、fstatat、lstat
struct stat {mode_t st_mode; /* file type & mode (permissions), suid, sgid */ino_t st_ino; /* i-node number (serial number) */dev_t st_dev; /* device number (file system) */dev_t st_rdev; /* device number for special files */nlink_t st_nlink; /* number of links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */off_t st_size; /* size in bytes, for regular files */struct timespec st_atim; /* time of last access */struct timespec st_mtim; /* time of last modification */struct timespec st_ctim; /* time of last file status change */blksize_t st_blksize; /* best I/O block size */blkcnt_t st_blocks; /* number of disk blocks allocated , 512B */};struct timespec {time_t tv_sec;long tv_nsec;}
3. 文件類型
#include <sys/stat.h>int stat(const char *restrict pathname, struct stat *restrict buf );int fstat(int fd, struct stat *buf );int lstat(const char *restrict pathname, struct stat *restrict buf ); // 返回該符號(hào)鏈接本身的有關(guān)信息int fstatat(int fd, const char *restrict pathname, struct stat *restrict buf, int flag);All four return: 0 if OK, −1 on error
符號(hào)鏈接 l
文件類型宏:參數(shù)為stat結(jié)構(gòu)中的st_mode成員(文件模式字)
ipC類型宏:參數(shù)為stat結(jié)構(gòu)的指針
每個(gè)進(jìn)程關(guān)聯(lián)的用戶ID和組ID,包括實(shí)際用戶ID、有效用戶ID、保存的設(shè)置用戶ID。實(shí)際用戶ID指的是執(zhí)行該程序的用戶的ID。注意區(qū)別于文件的所有者。
每個(gè)文件有一個(gè)所有者和組所有者,由stat結(jié)構(gòu)中的st_uid, st_gid指定。
所有文件類型都有訪問(wèn)權(quán)限
進(jìn)程每次打開(kāi)、創(chuàng)建或刪除一個(gè)文件時(shí),內(nèi)核就進(jìn)行文件訪問(wèn)權(quán)限測(cè)試,這種測(cè)試可能涉及文件的所有者(st_uid和st_gid)、進(jìn)程的有效ID(有效用戶ID和有效組ID)、進(jìn)程的附屬組ID。兩個(gè)所有者ID是文件的性質(zhì),而兩個(gè)有效ID和附屬組ID則是進(jìn)程的性質(zhì)。
6. 新文件和新目錄的所有權(quán)
- 若進(jìn)程的有效用戶ID是0(超級(jí)用戶),則運(yùn)行訪問(wèn)
- 若進(jìn)程的有效用戶ID等于文件的所有者ID(即進(jìn)程擁有此文件),那么如果所有者適當(dāng)?shù)脑L問(wèn)權(quán)限位被設(shè)置,則運(yùn)行訪問(wèn);否則拒絕訪問(wèn)。適當(dāng)?shù)脑L問(wèn)權(quán)限位指的是:若進(jìn)程為讀而打開(kāi)該文件,則用戶讀位應(yīng)為1;若進(jìn)程為寫而打開(kāi)該文件,則用戶寫位應(yīng)為1;若進(jìn)程將執(zhí)行該文件,則用戶執(zhí)行位應(yīng)為1。
- 若進(jìn)程的有效組ID或進(jìn)程的附屬組ID之一等于文件的組ID,那么如果組適當(dāng)?shù)脑L問(wèn)權(quán)限位被設(shè)置,則允許訪問(wèn);否則拒絕訪問(wèn)。
- 若其他用戶適當(dāng)?shù)脑L問(wèn)權(quán)限位被設(shè)置,則允許訪問(wèn);否則拒絕訪問(wèn)。按順序執(zhí)行這4步。一旦前面的被拒絕了,即使后面的組、其他用戶擁有相應(yīng)權(quán)限也白搭。
linux下如果新目錄的父目錄的SUID被設(shè)置,則選擇2
當(dāng)open函數(shù)打開(kāi)一個(gè)文件時(shí),內(nèi)核以進(jìn)程的有效用戶ID和有效組ID為基礎(chǔ)執(zhí)行其訪問(wèn)權(quán)限測(cè)試。有時(shí),進(jìn)程希望以進(jìn)程的實(shí)際用戶ID和實(shí)際組ID為基礎(chǔ)來(lái)執(zhí)行其訪問(wèn)權(quán)限測(cè)試。這使用以下兩個(gè)函數(shù):
#include <unistd.h>int access(const char *pathname, int mode);int faccessat(int fd, const char *pathname, int mode, int flag);Both return: 0 if OK, −1 on error
測(cè)試文件是否存在,mode為F_OK;測(cè)試讀/寫/執(zhí)行權(quán)限,mode為R_OK、W_OK、X_OK的按位與
8. 函數(shù)umask
#include <sys/stat.h>mode_t umask(mode_t cmask);Returns: PRevious file mode creation mask

#include <sys/stat.h>
新聞熱點(diǎn)
疑難解答
圖片精選