zoukankan      html  css  js  c++  java
  • UNIX环境编程学习笔记(9)——文件I/O之文件访问权限的屏蔽和更改

    lienhua34
    2014-09-10

    1 文件访问权限

    文件访问权限和进程访问控制中,我们已经讲述过文件访问权限位,为了方便,我们重新列在下面,

    表 1: 文件的 9 个访问权限位
     st_mode 屏蔽  意义
     S_IRUSR  用户 -读
     S_IWUSR  用户 -写
     S_IXUSR  用户 -执行
     S_IRGRP   组 -读
     S_IWGRP  组 -写
     S_IXGRP  组 -执行
     S_IROTH  其他 -读
     S_IWOTH  其他 -写
     S_IXOTH  其他 -执行


    2 新文件访问权限的屏蔽

    每个进程都有一个单独的跟进程相关联的文件模式创建屏蔽字(file creation mask). 每个进程的文件模式创建屏蔽字跟该进程的父进程没有关系,不继承于父进程的文件模式创建屏蔽字,本进程文件模式创建屏蔽字变更对父进程的文件模式进程也不会产生任何影响。

    在进程创建一个新文件时,会通过文件模式创建屏蔽字和创建新文件时指定的文件访问权限位一起决定新文件最终的文件访问权限。对于任何在文件模式创建屏蔽字中为 1 的位,在文件 mode 中的相对应位则一定被关闭。

    umask 函数为进程设置文件模式创建屏蔽字,并返回以前的值。

    #include <sys/stat.h>

    mode_t umask(mode_t cmask);

    返回值:以前的文件模式创建屏蔽字。

    其中,参数 cmask 是由表 1 中列出的 9 个常量中的若干个按位或构成的。下面程序创建两个文件,创建第一个文件 foo 时将进程的文件模式创建屏蔽字清空,而创建第二个文件之前,则屏蔽了所有组和其他用户的读写权限。

    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
    int
    main(void)
    {
        umask(0);
        if (creat("foo", RWRWRW) < 0) {
        printf("creat error for foo");
            exit(-1);
        }
        umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
        if (creat("bar", RWRWRW) < 0) {
            printf("creat error for bar");
            exit(-1);
        }
        exit(0);
    }

    编译该程序,生成文件 umaskdemo,然后运行之,

    lienhua34:demo$ gcc -o umaskdemo umaskdemo.c
    lienhua34:demo$ ./umaskdemo
    lienhua34:demo$ ls -l bar foo
    -rw------- 1 lienhua34 lienhua34 0 9月 3 23:51 bar
    -rw-rw-rw- 1 lienhua34 lienhua34 0 9月 3 23:51 foo

    3 文件访问权限的更改

    UNIX 提供了两个函数 chmod 和 fchmod 实现对现有文件访问权限的更改。

    #include <sys/stat.h>

    int chmod(const char *pathname, mode_t mode);

    int fchmod(int filedes, mode_t mode);

    两个函数返回值:若成功则返回0,若出错则返回-1。

    chmod 函数在指定的文件上进行操作,而 fchmod 函数则对已打开的文件进行操作。

    要改变一个文件的访问权限,进程的有效用户 ID 必须等于文件的所有者 ID,或者该进程必须具有超级用户权限。

    参数 mode 是表 2 中所示常量的某种按位或运算构成的。

    表 2: chmod 函数的 mode 常量
    mode 说明
    S_ISUID 执行时设置用户 ID
    S_ISGID 执行时设置组 ID
    S_IRWXU 用户读、写和执行
      S_IRUSR 用户 -读
      S_IWUSR 用户 -写
      S_IXUSR 用户 -执行
    S_IRWXG 组读、写和执行
      S_IRGRP 组 -读
      S_IWGRP 组 -写
      S_IXGRP 组 -执行
    S_IRWXO 其他读、写和执行
      S_IROTH 其他 -读
      S_IWOTH 其他 -写
      S_IXOTH 其他 -执行

    实例:

    下面程序打开了文件 foo 的设置组 ID 位、关闭了组执行位。而对于文件 bar 则强制设置为某个指定的访问权限。

    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/stat.h>
    int
    main(void)
    {
        struct stat statbuf;
        if (stat("foo", &statbuf) < 0) {
            printf("stat error for foo");
            exit(-1);
        }
        if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
            printf("chmod error for foo");
            exit(-1);
        }
        if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
            printf("chmod error for bar");
            exit(-1);
        }
        exit(0);
    }

    编译该程序文件,生成文件 chmoddemo,然后运行该文件,

    lienhua34:demo$ gcc -o chmoddemo chmoddemo.c
    lienhua34:demo$ ls -l foo bar
    -rw------- 1 lienhua34 lienhua34 0 9月 3 23:51 bar
    -rw-rw-r-- 1 lienhua34 lienhua34 0 9月 3 23:51 foo
    lienhua34:demo$ ./chmoddemo
    lienhua34:demo$ ls -l foo bar
    -rw-r--r-- 1 lienhua34 lienhua34 0 9月 3 23:51 bar -rw-rwSr-- 1 lienhua34 lienhua34 0 9月 3 23:51 foo

    (done)

  • 相关阅读:
    Java++:常用工具|性能监测|服务监控
    Vue++:Vuecli4.x 的配置文件配置方法
    Vue++: gyp ERR! find Python
    Node++:Windows 环境搭建 Vue 开发环境
    linux++:free h 详解
    踩坑纪:windows 系统下,nodejs版本管理器无法使用n来管理
    Node++:NodeJs 版本管理工具(n | nvm)n教程
    Vue++:Error: 'XXX' is defined but never used (nounusedvars)报错的解决方案
    [MOSS]关闭弹出窗口
    安装 64 位版本的 Office 2010 后,无法查看数据表视图中的列表
  • 原文地址:https://www.cnblogs.com/lienhua34/p/3965267.html
Copyright © 2011-2022 走看看