unix环境高级编程-4.11-chown,fchown和lchown函数

简介: 接下来讲的函数主要是用来更改文件的用户ID和,组ID。[cpp] view plaincopy#include     int chown(const char* pa...

接下来讲的函数主要是用来更改文件的用户ID和,组ID。

  1. #include <unistd.h>  
  2.   
  3. int chown(const char* pathname,uid_t owner,gid_t group);  
  4.   
  5. int fchown(int filedes,uid_t owner,gid_t growp);  
  6.   
  7. int lchown(const char* pathname,uid_t owner,gid_t group);  

成功返回0,若出错则返回-1
查看GNU C手册

int chown (const char *filename, uid t owner, gid t group) [Function]
The chown function changes the owner of the file filename to owner, and its group
owner to group.

Changing the owner of the file on certain systems clears the set-user-ID and set-group-
ID permission bits. (This is because those bits may not be appropriate for the new
owner.) Other file permission bits are not changed.

The return value is 0 on success and -1 on failure. In addition to the usual file name
errors (see Section 11.2.3 [File Name Errors], page 224), the following errno error
conditions are defined for this function:
EPERM This process lacks permission to make the requested change.
Only privileged users or the file’s owner can change the file’s group. On
most file systems, only privileged users can change the file owner; some
file systems allow you to change the owner if you are currently the owner.
When you access a remote file system, the behavior you encounter is
determined by the system that actually holds the file, not by the system
your program is running on.
See Section 31.7 [Optional Features in File Support], page 771, for information
about the _POSIX_CHOWN_RESTRICTED macro.
EROFS The file is on a read-only file system.

 

int fchown (int filedes, int owner, int group) [Function]
This is like chown, except that it changes the owner of the open file with descriptor
filedes.
The return value from fchown is 0 on success and -1 on failure. The following errno
error codes are defined for this function:
EBADF The filedes argument is not a valid file descriptor.
EINVAL The filedes argument corresponds to a pipe or socket, not an ordinary
file.
EPERM This process lacks permission to make the requested change. For details
see chmod above.
EROFS The file resides on a read-only file system.

其实 ,我们可以看到,大多数函数失败了是返回小余0的数,而并完全是-1.所以,当你判断一个函数是否出错的时候记得使用(<0),而不是-1;

 

特别说明一下lchown是改变符号链接本身的所有者,而不是该符号链接所指向的文件。

如果两个参数owner,和group中的任一一个是-1,则对应的ID不变。

基于BSD的系统规定,只有超级用户才能更改一个文件的所有者。这样做的原因是泛指用户改变其文件的所有者从而百度哦磁盘空间限额对他们的限制。系统V则允许任一用户更改他们所拥有的文件的所有者。

 

若_POSIX_CHOWN_RESTRICTED对指定的文件起作用。

(1)只有超级用户进程能更改文件的用户ID

(2) 若满足下列条件,一个非超级用户进程就可以更改文件的组ID:

      (a)进程拥有此文件(其有效用户ID,等于该文件的用户ID)

       (b)参数owner等于-1或者文件用户ID,并且参数group等于进程的有效组ID或者进程的附加组id之一。

这意味这,当_POSIX_CHOWN_RESTRICTED起作用的时你不能更改其他用户的文件的用户id,你可以更改你所拥有的文件的组ID,但只能改到你所属的组。

如果这些函数由超级用户进程调用。则在成功返回的时候,该文件的设置用户ID位和设置组id,为都会被清除。

 

shell中使用chown的时候,格式如下

chown 属主 文件名

更多请访问:http://blog.csdn.net/wallwind

目录
相关文章
|
8月前
|
Unix Shell API
组合思维:Unix 哲学到底给现代编程带来哪些重要启示?
Unix哲学提供了一套简洁而强大的设计理念,这些理念在现代编程中依然具有重要的指导意义。通过模块化设计、组合工具、避免过早优化以及注重可复用性和可扩展性,开发者可以构建出更高效、更健壮的软件系统。希望本文能够帮助读者深入理解Unix哲学,并在实际开发中应用这些宝贵的设计原则。
151 25
|
算法 Unix 数据安全/隐私保护
Python编程--UNIX口令破解机
Python编程--UNIX口令破解机
106 1
Unix环境高级编程(第三版)中apue.h头文件及其依赖安装教程
Unix环境高级编程(第三版)中apue.h头文件及其依赖安装教程
395 0
|
Unix Shell Python
unix高级编程-fork和execve
unix高级编程-fork和execve
127 0
|
Ubuntu Unix Shell
unix高级编程-fork之后父子进程共享文件
unix高级编程-fork之后父子进程共享文件
137 0
|
Unix Linux
unix高级编程-僵尸进程和孤儿进程
unix高级编程-僵尸进程和孤儿进程
114 0
|
Unix API C语言
《UNIX网络编程 卷1:套接字联网API(第3版)》——1.4 错误处理:包裹函数
任何现实世界的程序都必须检查每个函数调用是否返回错误。在图1-5所示的程序中,我们检查socket、inet_pton、connect、read和fputs函数是否返回错误,当发生错误时,就调用我们自己的err_quit或err_sys函数输出一个出错消息并终止程序的运行。
1365 0
|
Unix Shell Linux
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
173 2
|
8月前
|
安全 Unix Linux
Unix:Linux的“祖师爷”
Unix的诞生 Unix操作系统诞生于1969年,由肯·汤普逊(Kenneth Lane Thompson)和丹尼斯·里奇(Dennis MacAlistair Ritchie)在AT&T的贝尔实验室开发。其初衷是为了在闲置的PDP-7计算机上开发一个简单的操作系统,以便进行编程和游戏。最初的Unix是用汇编语言编写的,但随后为了更高效的开发和更好的可移植性,里奇和汤普逊用C语言重写了Unix的大部分代码,这奠定了Unix的基础,并促进了C语言的广泛应用。
178 2
|
10月前
|
Unix Linux 编译器
UNIX/Linux 上的安装
UNIX/Linux 上的安装。
126 2