I’m implementing a system call in a 2.6.22 kernel. In my system call I obtain a file descriptor like this:
fd = sys_open(filename, O_WRONLY|O_CREAT, 0544);
However, I get a negative number (-13) for fd when filename points to a read only file. The problem is that I need to write to filename, even if it’s read only or owned by another user.
So my question is this, how can I write to a read only file from the kernel?
And yes, I’ve read the post in linux journal that says writing to a file from the kernel is a bad idea.
I still need to do it.
The negative number isn’t a file descriptor, it’s an error code. Specifically it will be the negative version of one of the
errno.herror numbers.In this case as you have
-13you are looking at error 13 which isEACCESmeaning that you don’t have permission to write to the file.