开篇废话
最近在看apue,把书中的代码详细的敲了一遍,并上传至了github,https://github.com/excel-bat/apuebegin
,部分概念参照了网上的结果。
疑难概念
chapter 4
文件权限位中特殊权限位:set_UID 和 set_GID 其实可以理解为设置用户所属者信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <unistd.h> // chown
#include <stdio.h> // printf, perror
#define ROOT_UID 0
#define ROOT_GID 0
int main(int argc, char *argv[]) {
// 1)
if (argc != 2) {
printf("usage: ./a.out filepath");
return 1;
}
// 2)
char *filepath = argv[1];
if (chown(filepath, ROOT_UID, ROOT_GID) == -1) {
perror("Change owner fail");
return 2;
}
// 3)
return 0;
}修改权限
1
2
3
4
5
6
7
8
9
10$ ls -l /tmp/ttt
-rw-rw-r-- 1 xxx xxx 0 1月 24 10:27 /tmp/ttt
$ ./a.out /tmp/ttt
Change owner fail: Operation not permitted
$ sudo ./a.out /tmp/ttt
$ ls -l /tmp/ttt
-rw-rw-r-- 1 root root 0 1月 24 10:27 /tmp/ttt其中 最后一行 ,root 就是特殊权限位
-rw-rw-r– 1
root root
0 1月 24 10:27 /tmp/tttumask 函数,文件权限掩码(权限屏蔽位),说白了就是原有权限减去umask值得到的就是真实的权限位。