Change read/write permissions on a file descriptor - c

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

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.

int fd = open(fname, O_WRONLY, ...)
int fd_ro = open(fname, O_RDONLY, ...)
unlink(fname)
{ write to fd }
close (fd);
read or execute(!) fd_ro

Related

Get file descriptor from file name in C

Is is possible to get a file descriptor from a file path in C? And if not, what is the standard way of generating a file descriptor in C?
See below for what I'm trying to do with a file descriptor:
char file[] = "/tmp/run"
int file_descriptor = /* fill in code to get file descriptor */
fchmod(file_descriptor, S_IRUSR | S_IWUSR | S_IXUSR)
That's exactly what the POSIX open() function does: It gives you a file descriptor that refers to the path you passed in.
(Note that neither open nor file descriptors in general are part of standard C.)

C: cannot create or append to a file

I am trying to open a file for both read and write operations.
If the file is already there, it should append. (I want to be able to write to it, and maybe read from it later)
However, if the file is there, I cannot append to it (I get a permission denied: cannot create file)
int main()
{
int file;
file = open("redirect.txt", O_RDWR | O_APPEND | O_CREAT, 777);
if(!(file == -1)) //edited per comment
{
close(file);
}
else
perror("File could not be created\n");
return 0;
}
This only opens a new file if it does not exist, but does not append to an existing file if it does exist.
You're forgetting that the mode parameter to open() must be in octal. This will work:
file = open("redirect.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
As zwol also mentioned, it's generally a good idea to create files with 0666 (since they don't need to be executable).

How to tell if a text file is empty using open() instead of fopen()

If one can use fopen() then the solution is easy:
FILE *fp;
fp = fopen(path, "r");
fseek (fp, 0, SEEK_END);
size = ftell(fp);
if size is zero the file is empty.
However what if the requirement was that fopen() cannot be used. Instead, what I have to use is system calls like open():
int f = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
ftell() can only be used with FILE type streams. I've googled this and could not find any solutions.
It is not a C question (standard C does not know about open) but a Linux or POSIX one.
You don't necessarily need open(2), and you should realize that on Linux some other process could write into a file that you have opened.
Then you might get the size of a file using stat(2) (with the .st_size field) and you could get the size of an opened file descriptor using fstat
You might also use lseek(2) (it could be used both for setting and querying the current file offset of an opened file descriptor).

Cannot open the file created with O_CREAT

I created a file with the flag O_CREAT, but when I tried to open the created "out.txt" with Notepad. It said "cannot open this file" or something like "access denied".
fd = open("out.txt", O_CREAT);
You must close your file using close-call. Otherwise its contents won't be flushed and the file not written to disk. Besides you might be telling what you want to do on the file.
fd = open("out.txt", O_WRONLY | O_CREAT); //write to the file
//write to file
close(fd); //might check return value
See Wikipedia on this.

C system calls open / read / write / close and O_CREAT|O_EXCL

Given the following code (it's supposed to write "helloworld" in a "helloworld" file, and then read the text):
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FNAME "helloworld"
int main(){
int filedes, nbytes;
char buf[128];
/* Creates a file */
if((filedes=open(FNAME, O_CREAT | O_EXCL | O_WRONLY | O_APPEND,
S_IRUSR | S_IWUSR)) == -1){
write(2, "Error1\n", 7);
}
/* Writes hello world to file */
if(write(filedes, FNAME, 10) != 10)
write(2, "Error2\n", 7);
/* Close file */
close(filedes);
if((filedes = open(FNAME, O_RDONLY))==-1)
write(2, "Error3\n", 7);
/* Prints file contents on screen */
if((nbytes=read(filedes, buf, 128)) == -1)
write(2, "Error4\n", 7);
if(write(1, buf, nbytes) != nbytes)
write(2, "Error5\n", 7);
/* Close file after read */
close(filedes);
return (0);
}
The first time I run the program, the output is:
helloworld
After that every time I to run the program, the output is:
Error1
Error2
helloworld
I don't understand why the text isn't appended, as I've specified the O_APPEND file.
Is it because I've included O_CREAT?
It the file is already created, shouldn't O_CREAT be ignored?
O_EXCL forces the file to be created. If the file already exists, the call fails.
It is used to ensure that the file has to be created, with the given permissions passed in the third parameter. In short, you have these options:
O_CREAT: Create the file with the given permissions if the file doesn't already exist. If the file exists, it is opened and permissions are ignored.
O_CREAT | O_EXCL: Create the file with the given permissions if the file doesn't already exist. If the file exists, it fails. This is useful in order to create lockfiles and guarantee exclusive access to the file (as long as all programs which use that file follow the same protocol).
O_CREAT | O_TRUNC: Create the file with the given permissions if the file doesn't already exist. Otherwise, truncate the file to zero bytes. This has more of the effect we expect when we think "create a new blank file". Still, it keeps the permissions already present in the existing file.
More information from the manual page:
O_EXCL
When used with O_CREAT, if the file
already exists it is an error and
the open() will fail. In this context,
a symbolic link exists, regardless of
where it points to. O_EXCL is broken
on NFS file systems; programs which
rely on it for performing locking
tasks will contain a race condition.
The solution for performing atomic
file locking using a lockfile is to
create a unique file on the same file
system (e.g., incorporating hostname
and pid), use link(2) to make a link
to the lockfile. If link() returns 0,
the lock is successful. Otherwise, use
stat(2) on the unique file to check if
its link count has increased to 2, in
which case the lock is also
successful.

Resources