打开()系统调用文件描述符

Open() syscall filedesriptor

本文关键字:描述 文件 系统调用 打开      更新时间:2023-10-16

我必须更正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是所有系统调用所在的namesapacesyscaller用于向内核发送调用,取消调用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()上的文档