Mmaping shell-opened stdout [duplicate] - c

This question already has answers here:
Change read/write permissions on a file descriptor
(2 answers)
Reopen a file descriptor with another access?
(2 answers)
Closed 3 years ago.
Shells open redirected stdout with O_WRONLY. Such files cannot be mmaped (Write-only mapping a O_WRONLY opened file supposed to work?). Is there a work-around for this? Can I reopen the same filedescriptor (i.e., 1 == STDOUT_FILENO) but with O_RDWR without knowing a/the name of the file?

Related

read and write to the closed file descriptors [duplicate]

I have a question regarding file descriptors in Unix and C programming.
Let's say I use pipe(fd) to get file descriptor 3 and 4 for the pipe ends, 3 connects to the read end and 4 to the write end.
Now I use dup2(fd[write_end],1) to copy the descriptor of the write end (which was 4) to file descriptor 1 in my process. If I now do close(fd[write_end]) will it close descriptor 1 or descriptor 4?
After a successful call to dup2, both file descriptors are valid.
When you then call close(fd[write_end]), because fd[write_end] is set to 4 this is the same as close(4). So file descriptor 1 remains open and usable.

Read content of directory file [duplicate]

This question already has answers here:
Using `read` system call on a directory
(2 answers)
Linux C read a directory
(2 answers)
Closed 3 years ago.
It's possible to read a regular file or a symbolic link file ( not the file it's pointing to ).
Is there a way to read the content of a directory file in a file reading fashion (e.g. fread()) .

Do duplicated descriptor files get closed after closing the original file descriptor?

When duplicating a file descriptor fd calling dup, and closing the original file descriptors.
Do all the duplicated file descriptors get closed also?
No the duplicates won't be closed. Otherwise the main use-case (duplicating into STDOUT_FILENO and STDIN_FILENO) would be pretty useless.

Can you Hide files in c? [duplicate]

This question already has answers here:
Hide a file or directory using the Windows API from C
(4 answers)
Closed 8 years ago.
Does anybody know if it is possible to hide files or make them invisible to other users? Or, does creating file in "w" mode achieve invisibility?
Ex:
If I create a file like this:
FILE *fp = fopen("aFile","w");
Can other users on my system read it?
I guess I'm asking for the C way to add access modifiers to files in C, sort of like the chmod command does..
It is not possible to set access permission when using fopen.
Use CreateFile instead to open file and set access permissions.

how to check if the file is closed

I have file descriptor and inside my signal handler, i close the file. But due to other conditions, the file could have been closed earlier. Is there a way to check if the file descriptor points to an open file in c and linux?
UPDATE:
Is it possible to determine filename associated with a file descriptor? This way if the fd gets recycled, app can detect it.
Try to do any file operation like lseek on the file descriptor. If it returns -1. Then check errno, if its EBADF then the file descriptor is already closed.
Try to do lseek in an unaffected manner like below.
lseek(fd, 0, SEEK_CUR);
Usually while opening the first file in a program we always get the file descriptor as 3. After this file is closed, if we try to open some other file we will get the same file descriptor as 3. Because always we will get the lowest available number. If we are closing and reopening many files in a program, then we need to improve our code to track of file descriptors list to check whether its closed or not.
When you open a file, it always get the minimal available fd assigned. So if you close your fd, and then open another file somewhere in your code, you could easily have the same fd reassigned to this new file. So there is no reliable way to tell that the file descriptor is closed, because it can now point to another opened file.
After you closed the file descriptor fd assign -1 to it, so you later could test fd against -1 to see if you already closed it.
You could lookup the filename a (valid) file descriptor referrs to by calling readlink() on /proc/<pid>/fd/<file descriptor>.

Resources