Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change read/write permissions on a file descriptor

Tags:

c

linux

unlink

dup

I'm working on a linux C project and I'm having trouble working with file descriptors.

I have an orphan file descriptor (the file was open()'d then unlink()'d but the fd is still good) that has write-only permission. The original backing file had full permissions (created with S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), but alas the file was opened with O_WRONLY. Is it possible to duplicate the file descriptor and change the copy to O_RDWR?

psudo-code:


//open orphan file
int fd = open(fname, O_WRONLY, ...)
unlink(fname)
//fd is still good, but I can't read from it

//...

//I want to be able to read from orphan file
int fd2 = dup(fd)
//----change fd2 to read/write???----

Thanks in advance! -Andrew

like image 869
Andrew Klofas Avatar asked Jan 09 '11 03:01

Andrew Klofas


People also ask

What is the meaning of chmod 777?

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions.

What does chmod 644 mean?

Permissions of 644 mean that the owner of the file has read and write access, while the group members and other users on the system only have read access.

What is 755 chmod?

755 means read and execute access for everyone and also write access for the owner of the file. When you perform chmod 755 filename command you allow everyone to read and execute the file, the owner is allowed to write to the file as well.


1 Answers

No, there is no POSIX function to change the open mode. You will need to open it in read / write mode. Since you are created a temporary file, though, I strongly recommend that you use mkstemp. That function properly opens the file in read/write mode and unlinks it. Most importantly, it avoids a race condition in naming and creating the file, thereby avoiding a vulnerability in the creation of temporary files.

like image 112
Michael Aaron Safyan Avatar answered Oct 02 '22 20:10

Michael Aaron Safyan