Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:50:09+00:00 2026-06-17T19:50:09+00:00

Assume the OS is linux. Suppose I opened a file for write and get

  • 0

Assume the OS is linux. Suppose I opened a file for write and get a file descriptor fdw. Is it possible to get another file descriptor fdr, with read-only access to the file without calling open again? The reason I don’t want to call open is the underlying file may have been moved or even unlinked in the file system by other processes, so re-use the same file name is not reliable against such actions. So my question is: is there anyway to open a file descriptor with different access right if given only a file descriptor? dup or dup2 doesn’t change the access right, I think.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:50:11+00:00Added an answer on June 17, 2026 at 7:50 pm

    Yes! The trick is to access the deleted file via /proc/self/fd/n. It’s a linux-only trick, as far as I know.

    Run this program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main() {
        FILE* out_file;
        FILE* in_file;
        char* dev_fd_path;
        char buffer[128];
    
        /* Write “hi!” to test.txt */
        out_file = fopen("test.txt", "w");
        fputs("hi!\n", out_file);
        fflush(out_file);
    
        /* Delete the file */
        unlink("test.txt");
    
        /* Verify that the file is gone */
        system("ls test.txt");
    
        /* Reopen the filehandle in read-mode from /proc */
        asprintf(&dev_fd_path, "/proc/self/fd/%d", fileno(out_file));
        in_file = fopen(dev_fd_path, "r");
        if (!in_file) {
            perror("in_file is NULL");
            exit(1);
        }
        printf("%s", fgets(buffer, sizeof(buffer), in_file));
    
        return 0;
    }
    

    It writes some text to a file, deletes it, but keeps the file descriptor open and and then reopens it via a different route. Files aren’t actually deleted until the last process holding the last file descriptor closes it, and until then, you can get at the file contents via /proc.


    Thanks to my old boss Anatoly for teaching me this trick when I deleted some important files that were fortunately still being appended to by another process!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I want to access this remote file in vim ftp://amr@steamboy//home/amr/bin/dr_snapshot.py and, when prompted,
Assume we have a very small embedded system consisting only of the linux kernel
Suppose that I compiled a dynamic library (Windows DLL and/or Linux shared object file,
Assume I use a C++ program to maintain a queue in Linux, and do
Assume I have an enumerable object enum and now I want to get the
Assume I've declared a function (or class, doesn't matter) in a header file, which
Assume a TCP socket on the local linux host is in a connected state
Is it safe to assume that the loopback network adapter on a Linux system
In my program I read from the serial device (Linux, 8N1) without any problem.
I'm embedding some driver into a Linux kernel when I get this error (I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.