首页 技术 正文
技术 2022年11月14日
0 收藏 683 点赞 2,244 浏览 3597 个字

下面的几个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 -1int 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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295