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 8444789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:29:19+00:00 2026-06-10T09:29:19+00:00

I have an open device descriptor, where I don’t know the device name and

  • 0

I have an open device descriptor, where I don’t know the device name and the options
passed to open(…).
I want to open a new device descriptor with the same options passed to open.

int newFd = copy(referenceFd);

Where copy would do the job. dup() is certainly the wrong choice as a further ioctl() on newFd would also alter the referenceFd, therefore I want to open a new descriptor.

Is there a system call which provides such functionality?
I have not been able to find something yet.

  • 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-10T09:29:21+00:00Added an answer on June 10, 2026 at 9:29 am

    First, get the descriptor flags of file descriptor fd using

        int flags = fcntl(fd, F_GETFL);
    

    Then, reopen the descriptor using the Linux-specific /proc/self/fd/fd entry:

        int  newfd;
        char buffer[32];
    
        if (snprintf(buffer, sizeof buffer, "/proc/self/fd/%d", fd) < sizeof buffer) {
            do {
                newfd = open(buffer, flags & ~(O_TRUNC | O_EXCL), 0666);
            } while (newfd == -1 && errno == EINTR);
            if (newfd == -1) {
                /* Error: Cannot reopen file. Error in errno. */
            }
        } else {
            /* Error: the path does not fit in the buffer. */
        }
    

    The reopened file descriptor is then in newfd.

    Note that you most likely want to make sure O_TRUNC (truncate the file) and O_EXCL (fail if exists) are not in the flags, to avoid the cases where reopening using the exact original flags would cause unwanted results.

    You do not want to use lstat(), because that opens up a race condition with respect to rename — a user renaming the target file between the lstat() and the open() in above code. The above avoids that completely.

    If you don’t want to be Linux-specific, add code that tries the same with /dev/fd/fd if the above fails. It is supported in some Unix systems (and most Linux distros too).

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

Sidebar

Related Questions

I have open pdf file through my application.when click on device back button it
I have a device driver module which does a register_chardev(). In the driver open()
I want to add some functionality to my Android device. However i don't want
I want to create a new activity on the orientation change of the device
I have open port url 192.168.1.9 and its username : test and password: test
I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have a open file dialog with three filters: QString fileName = QFileDialog::getOpenFileName( this,
I have an open-source app which uploads photos to Facebook. To save bandwidth, the
I have an open-source iPhone API that I downloaded, and that is a standalone
I have an open source'd website. Some content is private such as connection parameters,

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.