open() not setting file permissions correctly
来源:互联网 发布:九城软件 编辑:程序博客网 时间:2024/06/02 07:06
http://stackoverflow.com/questions/9057419/open-not-setting-file-permissions-correctly
open() not setting file permissions correctly
I create a file using the code below:
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>int main(){ const char* filename = "./test.out"; int fd; if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666))) { perror("Error"); errno = 0; } else puts("File opened"); if(-1 == (close(fd))) { perror("Error"); errno = 0; } else puts("File closed"); return 0;}
I specify the mode
argument as 0666
, which should grant read,write access to everyone. However, anls -l
shows
-rw-r--r-- 1 kmehta users 0 2012-01-29 16:29 test.out
As you can see, write permissions are only granted to the owner of the file. I do not know why everyone else is not granted permissions correctly.chmod a+w test.out
sets the permissions correctly though.
Code compiled as gcc -Wall test.c
Specs: gcc v 4.5.0 on Opensuse 11.3 64 bit
2 Answers
The mode
argument to open
specifies the maximum allowed permissions. Theumask
setting is then applied to further restrict the permissions.
If you need to make the permissions be 0666 specifically you will need to use fchmod
on the file handle after the open succeeds.
Executing this code :
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(void){ int fd; if((fd = open("new.file",O_CREAT,S_IRWXU | S_IRWXG | S_IRWXO)) == -1) { perror("open"); return 1; } close(fd); return 0;}
on my Linux box, where umask
returns 0022
, gives me a file with the following attributes :
-rwxr-xr-x 1 daniel daniel 0 Jan 29 23:46 new.file
So, as you can see, the umask masks out the write bits in my case. It looks like it's the same on your system, too.
Not the answer you're looking for? Browse other questions taggedc posix or ask your own question.
- open() not setting file permissions correctly
- Setting file and folder permissions
- You do not have permissions to open this file on Excel Services
- Error - .netrc file not correct permissions.
- File Permissions
- linux下安装PHP的CI框架,出现Your view folder path does not appear to be set correctly. Please open the following file and correct this: i
- could not open hex file!!
- Wrong permissions on configuration file, should not be world writable!
- Wrong permissions on configuration file, should not be world writable!
- Wrong permissions on configuration file, should not be world writable!
- Setting NTFS Permissions with C#
- Setting NTFS Permissions with C#
- Setting NTFS Permissions with C#[
- LRM-00109: could not open parameter file
- ADS: (Fatal)L6002U:Could not open file:.....
- ADS: (Fatal)L6002U:Could not open file:.....
- could not open parameter file initOrcl.ora
- could not open parameter file init**.ora
- Hibernate使用Spring进行单元测试时的事务问题
- windows 消息队列
- jquery操作select(取值,设置选中)
- bign 类 代码
- c# DES加密类
- open() not setting file permissions correctly
- statpack
- 基于mini2440的Linux音频驱动完全解读(三)——ASoc层的初始化(2)
- UBUNTU动态桌面墙纸配置文件脚本,配置步骤
- Javascript 什么是系统对象?
- STL中map与hash_map容器的选择
- Asp.net生成Excel文件并下载(更新:解决使用迅雷下载页面而不是文件的问题)
- simple-spring-memcached简介
- atoi()函数的升级版本strtol()