Linux umask 函数
文章目录
- 一、函数声明
- 二、实例
- 2.1 运行结果
- 2.2 验证umask不影响chmod
- 2.3 验证umask不影响chmod(运行结果)
一、函数声明
#include <sys/types.h>
#include <sys/stat.h>mode_t umask(mode_t mask);
umask() sets the calling process’s file mode creation mask (umask) to mask & 0777
The umask is used by open(2), mkdir(2), and other system calls that create files to modify the permissions placed on newly created files or directories.即所有改变权限或者使用权限的地方,都会用到umask设置的屏蔽位。也就是例如umask了S_IXOTH那么该进程创建,(文件、文件夹)。都无法让文件或文件夹获得S_IXOTH。
【简单说就是,只要S_IXOTH设置了屏蔽位置1,这一位在创建文件或文件夹时都不会为1】如果用chmod去改变文件权限是可以成功,并不受umask影响的
二、实例
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>int main(int argc, char **args)
{mode_t oldmask;int fd;if (argc < 2) {printf("Usage:\n"" umask [filename]\n");exit(-1);}oldmask = umask(S_IXOTH | S_IROTH | S_IWOTH);fd = open(args[1], O_CREAT, S_IRUSR | S_IWUSR | S_IXOTH | S_IROTH | S_IWOTH);close(fd);umask(oldmask);return 0;
}
2.1 运行结果
hotice0@ubuntu:~/Documents/Unix_Program$ ls kkk
ls: cannot access 'kkk': No such file or directoryhotice0@ubuntu:~/Documents/Unix_Program$ ./umask kkkhotice0@ubuntu:~/Documents/Unix_Program$ ll kkk
-rw------- 1 hotice0 hotice0 0 Aug 3 15:53 kkk
2.2 验证umask不影响chmod
#ifndef __USE_XOPEN_EXTENDED
#define __USE_XOPEN_EXTENDED
#endif#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>int main(int argc, char **args)
{mode_t oldmask;int fd;if (argc < 2) {printf("Usage:\n"" umask [filename]\n");exit(-1);}oldmask = umask(S_IXOTH | S_IROTH | S_IWOTH);fd = open(args[1], O_CREAT, S_IRUSR | S_IWUSR | S_IXOTH | S_IROTH | S_IWOTH);if (fchmod(fd, S_IRWXU | S_IRWXO) < 0) {perror("fchmod with fail");exit(-1);}close(fd);umask(oldmask);return 0;
}
2.3 验证umask不影响chmod(运行结果)
hotice0@ubuntu:~/Documents/Unix_Program$ rm aaa
hotice0@ubuntu:~/Documents/Unix_Program$ ./umask aaa
hotice0@ubuntu:~/Documents/Unix_Program$ ll aaa
-rwx---rwx 1 hotice0 hotice0 0 Aug 3 16:21 aaa*
可以看到,aaa文件的其他用户的所有权限都处于开启状态。chmod成功,而umask此时无效了。
Linux umask 函数相关推荐
- linux中的umask 函数
我们创建文件的默认权限是怎么来的?如何改变这个默认权限呢? umask是什么? 当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是umask干的事情.umask设置了 ...
- Linux系统函数之IO函数
技术交流 QQ 群:1027579432,欢迎你的加入! 欢迎关注我的微信公众号:CurryCoder的程序人生 1.标准C库IO函数工作流程 IO缓冲区的作用? 大部分硬盘都是机械硬盘,读取寻道时间 ...
- Linux 系统函数
Linux 系统函数 可以使用 man 2 函数名 查看 系统函数 也可以在 文件内 使用 2+ shift +k 进入指定函数 (前提是光标停在指定函数上) open 函数 int open(con ...
- Linux 高并发学习笔记 - Linux 文件属性函数
1.6.3 Linux 文件属性函数 Linux 高并发学习笔记 - 笔记索引 Linux中一切皆文件,你同样可以用下面这些函数操作目录等特殊文件. 前言 关于文件操作函数这一块主要用英文文档的形势书 ...
- linux计时函数的使用
linux计时函数的使用 linux下计时函数的使用目的是测量程序运行的时间 头文件 #include <sys/time.h> 计时函数 int gettimeofday(struct ...
- linux C函数之strdup函数分析【转】
本文转载自:http://blog.csdn.net/tigerjibo/article/details/12784823 linux C函数之strdup函数分析 一.函数分析 1.函数原型: [c ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
- linux shell函数
linux shell函数
- linux fork函数的精辟解说
此文原文来源于一个blog,文章的名称为:linux fork函数的精辟解说原文地址:http://blog.chinaunix.net/space.php?uid=12461657&do=b ...
最新文章
- 用了N年的接口,你知道接口是什么吗?——一个简单实例说明接口的伟大意义...
- 长此以往的发展,以BCH为代表的数字货币终将会为自己正名
- mysql出现unblock with 'mysqladmin flush-hosts'
- 模拟input type=file
- Misc-----[2021祥云杯初赛]鸣雏恋 提取压缩包文件图片名8个为一组 转ASC码base64转图片
- 三十四、Scrapy中的CrawlSpider
- 解决 There are no resources that can be added or removed from the server
- 20.网页卷去的距离与偏移量
- 【概率论】3-4:二维分布(Bivariate Distribution)
- Python安装OpenGL包
- jbpm知识点——state
- 文件另存为GBK编码格式
- Unity 动态编辑Terrain地形(五)贴图
- ThreadPoolExecutor参数解析
- 为什么SAST和SCA在SDLC中很重要?
- echarts配合google地图,并自定义google地图的样式
- 「YGG Japan」宣布已完成约 295 万美元的新股权私募
- matlab 光平面标定代码,线结构光视觉传感器/线激光深度传感器标定工具(matlab)...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
- 淘宝客 WebView打开淘宝链接失败的解决方法