chmod/fchmod

Header Files

#include <sys/types.h>
#include <sys/stat.h>

Function Prototype

int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
  • mode = bitwise-OR S_I[RWX]USR, S_ISVTX (sticky bit), S_IS[UG]ID, etc

Function

更新 i-node table 上的權限(所以不會馬上被寫到 disk 上)

Remark: UNIX will automatically chmod for security reason

  • 清除 sticky bit:當嘗試在普通檔案上設置 sticky bit (S_ISVTX),但沒有 superuser 權限時,sticky bit 會被自動關閉
  • 清除 set-group-ID bit:如果新創建檔案的 GID 不等於呼叫程序的 effective GID,且該程序不是 superuser,則清除 set-group-ID 位元
  • 清除 set-user/group-ID bits:當非 superuser 寫入 set-uid/gid 檔案時,清除這些 bits