打开()系统调用文件描述符
Open() syscall filedesriptor
我必须更正posix操作系统的open()
系统调用的返回值。我从手册页中了解到,它必须返回文件描述符,如果出现错误,系统调用将返回-1并设置errno
值。问题是,我不知道如何为打开的nod获取文件描述符。我检查了所有的文件,没有找到一个可以为进程分配fd的方法。
方法如下:
int syscalls::open(const char *path, int oflags, mode_t mode){
syscall_message msg;
msg.call.type = syscalls::open_call;
msg.open_data.path_name = &path[0];
msg.open_data.flags = oflags;
msg.open_data.create_mode = mode;
syscaller::call_system(msg);
return msg.error.number;
}
syscall_message
是一个结构体,用于保存系统调用的数据信息。syscalls
是所有系统调用所在的namesapace
。syscaller
用于向内核发送调用,取消调用call_system
方法。
call_system
方法:
syscalls::open_call:
{
//get the file
i_fs_node_ptr file = i_fs::open_node( msg.open_data.path_name );
//add the file handle
if ( file )
{
cur_process->push_filehandle(
file,
msg.open_data.flags,
msg.open_data.create_mode );
}
else
{
msg.error.type = syscalls::e_no_such_entry;
}
}
我不知道你说的"我无法获得文件描述符"是什么意思。正如您所提到的,open()返回它。它只是存储在一个整数变量中。如果这个变量等于-1,那么说明出了问题。例如,如果你有
int file = open(path, O_SYNC, O_DIRECT, O_RDONLY);
但是您没有名为path的文件的读取权限,变量file将获得值-1。可以通过read()(如果文件以读取模式打开)和write()[/strong>(文件以写入模式打开)对打开的文件进行附加操作。我建议您仔细阅读关于open()函数的文档。如果您需要对文件描述符进行更多控制,我建议您使用fopen():
- 关于open()和fopen()区别的讨论
- fopen教程()
- fopen()上的文档
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- I2C 文件描述符上的 I2C 总线可写/可读标志
- 许多文件描述符在调用sys_clone时
- AMQP-CPP >处理程序中的错误文件描述符
- 如何使用 WINAPI 和 C++ 提取可执行文件的文件描述?
- 正在等待在非阻塞文件描述符上长时间运行ioctl
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 如何强制文件描述符缓冲我的输出
- 如何从 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 获取本机套接字文件描述器?
- 哪个更适合从C++写入敏感的日志文件,在文件描述符上写()或文件上的fprintf()?
- 将 select() 与非基于文件描述符的输入一起使用
- accept(..) 似乎正在修改我给它的文件描述符参数
- 使用 Select 多路复用未命名的管道和其他文件描述符
- 提升 ASIO 绑定:错误的文件描述符
- 使用文件描述符移动对象
- 无法从零MQ ZMQ_SERVER套接字中获取文件描述符
- read() 上的不同行为取决于写入不可写内存时表示文件、匿名管道或套接字的文件描述符
- 我是否需要在使用它的 R 包的描述文件中导入 RccpEigen,或者"LinkingTo"足够?
- GCC链接器描述文件强制符号位于特定地址