1

I have to correct the return value of the open() syscall of a posix OS. I understood from the man-Pages that it has to return the file descriptor, and , in case of an error the system call will return -1 and set the errno value. The problem is that I do not know how to get the file descriptor for the opened nod. I checked all the files and didn't found a method that can assign a fd to processes.

Here is the method :

    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 is a struct that holds the data info for the system call. syscalls is the namesapace where all the system calls are. syscaller is used to send the call to the kernel, unsing the call_system method.

The call_system method:

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;
            }

        }
Niinle
  • 121
  • 9
  • 1
    What are `syscall_message`, `syscalls`, and `syscaller`? Are they part of some library or framework you're using? –  Nov 09 '14 at 19:15

1 Answers1

1

I don't know what you mean by "I can't get the file descriptor". As you have mentioned open() returns it. It's simply stored in an integer variable. If this variable is equal to -1, then something has gone wrong. For example if you have

int file = open(path, O_SYNC, O_DIRECT, O_RDONLY);

but you do not have the reading permissions for the file named path the variable file will get a value of -1. Additional manipulation on an opened file can be done via read() (if file was opened in read mode) and write() (if file was opened in write mode). I suggest you read the documentation on the open() function more carefully. If you need more control over the file descriptor I suggest you use fopen():

Community
  • 1
  • 1
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Yes, but I am referring to the way that `open()` system call woks. The method I posted is the `open()` system call for a Posix OS. The call sends the data need for opening a file to the kernel trough `call_system()`. After `call_system(msg)` does its magic and the file is or is not opened I have to return the `fd` of that file, or -1 in case of an error. Now, `msg` holds the data that the kernel uses to open a new nod. but after the node is opens it doesn't set a file descriptor for that nod. I will post the `call_system()` method. – Niinle Nov 09 '14 at 20:38