APUE-文件和目录(三)函数chown 和lchown
下面的几个chown函数可用于更改文件的用户ID和组ID。如果两个参数owner或group中的任意一个是-1,则对应的ID不变。
#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标志)更改符号链接本身的所有者,而不是该符号链接所指向的文件的所有者。
看下面的例子:
#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,最后修改此符号链接的所有者和所属组。
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”。
原因因为如下:
基于BSD的系统一直规定只有超级用户才能更改一个文件的所有者。
切换到root用户下执行,最后成功:
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。
看下面的例子:
首先我们为当前用户harlan添加一个附属组ID,改之前:
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
APUE-文件和目录(三)函数chown 和lchown的更多相关文章
- windows中操作文件和目录的函数
1.文件操作函数 CreateFile();//创建或打开文件 ReadFile();//从文件读 WriteFile();//向文件写 SetFilePoi ...
- [APUE]文件和目录(上)
一.文件权限 1. 各种ID 我在读这一章时遇到了各种ID,根据名字完全不清楚什么意思,幸好看到了这篇文章,http://blog.csdn.net/ccjjnn19890720/article/de ...
- [APUE]文件和目录(下)
一.mkdir和rmdir函数 #include <sys/types.h> #include <sys/stat.h> int mkdir(const char *pathn ...
- [APUE]文件和目录(中)
一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int ...
- PHP 文件与目录操作函数总结
>>>文件操作 打开 fopen(); 打开文件 读取内容 fread(); 从文件指针 handle 读取最多 length 个字节 readfile(); 读入 ...
- APUE 文件和目录
文件和目录 Unix 所有的文件都对应一个 struct stat,包含了一个文件所有的信息. #include <sys/stat.h> struct stat { mode_t st_ ...
- php 文件、目录操作函数
目录 opendir readdir closedir mkdir rmdir : 只能删除空目录 文件 filetype filesize is_file basename dirname pat ...
- APUE ☞ 文件和目录
粘着位(Sticky Bit) S_ISVTX位被称为粘着位.如果一个可执行程序文件的这一位被设置了,程序第一次运行完之后,程序的正文部分的一个副本仍被保存在交换区(程序的正文部分是机器指令).这使得 ...
- php文件和目录操作函数
文件:打开和关闭:fopen(), fclose()读:readfile(), file(), file_get_contents(), fgets(), fgetss(), fgetc()写:fwr ...
随机推荐
- php 启动过程 - sapi MINIT 过程
php 启动过程 - sapi MINIT 过程 sapi 概念 sapi 是 php 的应用编程接口, server 端接收请求通过 sapi 接口层交给 php 处理 不同的 server 端底层 ...
- c的旅程---字符
输出菱形实验: /***************** 2017年四月十六. 第一个例子,输出菱形.就是找关系. 这是当n= 3时, * *** * ******************/ #inclu ...
- 《阿里巴巴Java开发手册(正式版》读记
前几天,阿里巴巴发布了<阿里巴巴Java开发手册(正式版>,第一时间下载阅读了一番. 不同于一般大厂内部的代码规范,阿里巴巴的这本Java开发手册,可谓包罗万象,几乎日常Java开发中方方 ...
- _getch() 函数,应用于输入密码敲入回车前修改
body,table { font-family: 微软雅黑; font-size: 10pt } table { border-collapse: collapse; border: solid g ...
- php-fpm死机解决办法,脚本后台自动重启
本人用nginx+php7搭建了一台服务器,因为请求量太大,而且php里面又有挂起的任务,导致php-fpm在高峰期的时候经常死掉,吧php-fpm的最大进程数已经改到1000了,还是吃不消,cpu也 ...
- python编程总结
1.signal.signal(signal.SIGCHLD,signal.SIG_IGN) 这句话的作用是 防止产生僵尸进程.详细解释看 http://www.cnblogs.com/Anker/p ...
- 使用PhpIniDir加载php配置文件出错,不能解析。
我也是在apache服务器上配置php,在加载了php的配置文件之后,就出问题了. PHPIniDir "H:/php54/"增加了类似这句话之后,服务器就启动不了了,或者.php ...
- office web apps 部署-搭建office web apps服务器
二.搭建office web apps服务器 相关文件可以去焰尾迭分享的百度网盘下载,下载地址:http://pan.baidu.com/s/1o6tCo8y#path=%252Foffice%252 ...
- 【Android Widget】FragmentTabHost
android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...
- 【SoDiaoEditor电子病历编辑器】阶段性更新啦
转眼距离上一次v2正式发布已经过去一个半月了.github期间不定期push了二十几次,同时感谢分布在广州.福建.上海.北京的一众小伙伴,正是你们给出的建议,才让SoDiaoEditor不断完善. 我 ...