下面的几个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的更多相关文章

  1. windows中操作文件和目录的函数

    1.文件操作函数       CreateFile();//创建或打开文件      ReadFile();//从文件读      WriteFile();//向文件写      SetFilePoi ...

  2. [APUE]文件和目录(上)

    一.文件权限 1. 各种ID 我在读这一章时遇到了各种ID,根据名字完全不清楚什么意思,幸好看到了这篇文章,http://blog.csdn.net/ccjjnn19890720/article/de ...

  3. [APUE]文件和目录(下)

    一.mkdir和rmdir函数 #include <sys/types.h> #include <sys/stat.h> int mkdir(const char *pathn ...

  4. [APUE]文件和目录(中)

    一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int ...

  5. PHP 文件与目录操作函数总结

    >>>文件操作 打开 fopen();    打开文件 读取内容 fread();    从文件指针 handle 读取最多 length 个字节 readfile();    读入 ...

  6. APUE 文件和目录

    文件和目录 Unix 所有的文件都对应一个 struct stat,包含了一个文件所有的信息. #include <sys/stat.h> struct stat { mode_t st_ ...

  7. php 文件、目录操作函数

    目录 opendir readdir closedir mkdir rmdir  : 只能删除空目录 文件 filetype filesize is_file basename dirname pat ...

  8. APUE ☞ 文件和目录

    粘着位(Sticky Bit) S_ISVTX位被称为粘着位.如果一个可执行程序文件的这一位被设置了,程序第一次运行完之后,程序的正文部分的一个副本仍被保存在交换区(程序的正文部分是机器指令).这使得 ...

  9. php文件和目录操作函数

    文件:打开和关闭:fopen(), fclose()读:readfile(), file(), file_get_contents(), fgets(), fgetss(), fgetc()写:fwr ...

随机推荐

  1. Swift try try! try?使用和区别

    Swift try try! try?使用和区别 一.异常处理try catch的使用 1. swift异常处理 历史由来 Swift1.0版本 Cocoa Touch 的 NSError ,Swif ...

  2. Angular2.js——数据显示

    显示数据,即属性绑定机制把数据显示到用户界面上. 在Angular中最典型的数据显示方式,就是把HTML模板中的控件绑定到Angular组件的属性. 接下来介绍几种数据显示的语法和代码片段. 使用插值 ...

  3. MAMP 环境下为 php 添加 pcntl 扩展

    前言: pcntl 介绍 pcntl 扩展可以支持 PHP 的多线程操作.(非Unix类系统不支持此模块) phpize 介绍 phpize 可以用来给 PHP 动态的添加扩展.比如编译 PHP 时忘 ...

  4. 开始使用gentoo linux——gentoo安装笔记(上)

    gentoo linux安装笔记(上) 家里有一台破旧的富士通笔记本,08年至今质量依然杠杠的,但是性能已经不能和现代超极本同日而语,装上了ubuntu更是不敢恭维,别提gnome和kde的linux ...

  5. centos系统修改网络配置注意事项

    这也是无意之中发现的,我在做一个远程修改工控机网络配置的程序, 网络配置参数/etc/sysconfig/network-scripts/ifcfg-enp1s0下面,当然名字可能不一样ifcfg-e ...

  6. CTF中怎看phpinfo

    CTF中怎么看phpinfo 在比赛中经常遇到phpinfo,这个页面可以看到很多配置信息,我们需要在这么多信息中,着重看一下几个内容: 1.allow_url_fopen和allow_url_inc ...

  7. Hadoop集群

    你可以用以下三种支持的模式中的一种启动Hadoop集群: 单机模式 伪分布式模式 完全分布式模式 单机模式的操作方法 默认情况下,Hadoop被配置成以非分布式模式运行的一个独立Java进程.这对调试 ...

  8. HTML标签元素分类(HTML基础知识)

    HTML标签元素分类 一.按照块级元素还是行内元素分类 块级元素(block-level)和行内元素(inline-level,也叫作"内联"元素). a.块级元素(独占一行) 块 ...

  9. Python 与 C/C++ 交互的几种方式

    python作为一门脚本语言,其好处是语法简单,很多东西都已经封装好了,直接拿过来用就行,所以实现同样一个功能,用Python写要比用C/C++代码量会少得多.但是优点也必然也伴随着缺点(这是肯定的, ...

  10. coding.net及git的使用方式

    一般部分测试的公司里可能会用到代码管理工具,这里可能不得不推荐coding.net和git这个工具,类似于svn,不过用命令行的情况多点 这里简单介绍下 1.建立coding.net 首先新建一个co ...