Welcome 微信登录

首页 / 操作系统 / Linux / Unix下文件的访问权限

access Function:    When we open a file, the kernel performs its access tests based on the effective user and group IDs. There are times when a process wants to test accessibility based on the real user and group IDs. This is useful when a process is running as someone else, using either the set-user-ID or the set-group-ID feature. Even though a process might beset-user-ID to root, it could still want to verify that the read user can access a given file. The access function bases its tests on the real user and group IDs.
  1. #include <fcntl.h>   
  2. #include "apue.h"   
  3. #include "my_err.h"    
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.         if( argc != 2 )  
  8.         {  
  9.                 err_quit("usage: a .out <pathname>");  
  10.         }  
  11.   
  12.         if( access( argv[1], R_OK) < 0 )  
  13.         {  
  14.                 err_ret("access error for %s", argv[1]);  
  15.         }  
  16.         else  
  17.         {  
  18.                 printf("read access ok ");  
  19.         }  
  20.   
  21.         if( open( argv[1], O_RDONLY) < 0 )  
  22.         {  
  23.                 err_ret("open error for %s", argv[1]);  
  24.         }  
  25.         else  
  26.         {  
  27.                 printf("open for reading OK ");  
  28.         }  
  29.   
  30.         exit(0);  
  31. }  


说明:        a.out本来没有权限访问/etc/shadow文件,该文件只有root用户才有权限,但是我们把a.out改成root的文件,再增加S属性,虽然用的是普通的用户去执行root用户的文件,由于a.out 文件有S属性,所以,它的有效用户id还是root的id,也只有这样,a.out 才能访问/etc/shadow文件。AIX克隆和镜像补充rp_filter及Linux下多网卡接收多播的问题相关资讯      UNIX 
  • 一个涵盖 Unix 44 年进化史的版本  (12/23/2015 19:08:29)
  • 2002年,程序员和Unix大神们的桌面  (11/10/2015 08:18:44)
  • 20个 Unix/Linux 命令技巧  (03/22/2015 08:09:34)
  • UNIX 家族小史  (12/01/2015 12:03:06)
  • 从Unix 和 PC机:重释Linux的起源  (05/17/2015 21:13:04)
  • 什么是Unix以及它为什么这么重要?  (07/23/2014 16:47:39)
本文评论 查看全部评论 (0)
表情: 姓名: 字数