没有模式的Unix O_CREAT标志指定 [英] Unix O_CREAT flag without mode specified

查看:182
本文介绍了没有模式的Unix O_CREAT标志指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与O_CREAT标志使用时,在UNIX的的open()的函数的定义是,它需要一个名为第三个参数的模式的以设置文​​件的权限。

The definition of the UNIX open() function when used with the O_CREAT flag is that it requires a third argument named mode in order to set the files' privileges.

如果说的 的没有指定什么模式?

What if that mode is not specified?

int file;
static const char filename[] = "test.test";

if ((file = open(filename, O_RDWR | O_CREAT | O_TRUNC)) == 1)
{
    perror("Error opening file.");
    exit(EXIT_FAILURE);
}

close(file);

与使用这些标志创建的文件会发生什么?在我的系统我得到:

What happens with the file that is created using those flags? On my system I get:

-r--r-s---  1 hyperboreean hyperboreean     0 2009-02-25 01:40 test.test

一个理论是,开放的功能看起来栈和检查模式参数上,并用它找到一个随机整数结束。

A theory is that the open function looks on the stack and checks for the mode parameter and ends up using a random integer it finds.

这是什么标准说这个?

推荐答案

POSIX标准(IEEE 1003.1:2008)的原型的open()为:

The POSIX standard (IEEE 1003.1:2008) prototypes open() as:

int open(const char *path, int oflag, ...);

描述的行为部分 O_CREAT 不说,如果你省略了必要的第三个参数,这意味着这种行为是未定义会发生什么 - 一切皆有可能

The section describing the behaviour of O_CREAT doesn't say what will happen if you omit the necessary third argument, which means the behaviour is undefined - anything is possible.

在实践中,使用的栈的一部分被打算​​堆叠帧或返回地址或类似的东西很可能 - 归一合理的近似,这可以被认为是随机的整数

In practice, the use of part of the stack that was intended to be stack frame or return address or something similar is quite likely - unto a reasonable approximation, that can be considered a random integer.

借助 2008年POSIX标准的open(),其中包括:


  • O_FDCLOEXEC 指定的close-on-EXEC在开。

  • O_DIRECTORY 来指定该文件必须是一个目录。

  • O_NOFOLLOW 来指定不追逐符号链接。

  • O_FDCLOEXEC to specify close-on-exec at open.
  • O_DIRECTORY to specify that the file must be a directory.
  • O_NOFOLLOW to specify not to chase symlinks.

这篇关于没有模式的Unix O_CREAT标志指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆