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

  • Home
  • SEARCH
  • 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 7489803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:22:35+00:00 2026-05-29T15:22:35+00:00

I have a large C++ application on Linux with many first party and third

  • 0

I have a large C++ application on Linux with many first party and third party libraries built and linked against.

There are certain parts of the application that should execute without accessing the filesystem or network (particularly to load networked files). Periodically we find that this operation does indeed load files usually due to programmer error.

How can I enforce this with in the code? For example something like:

try {
  lockFileSystem();
  Application->DoImportantOperation();
  unlockFileSystem();
} catch ( InvalidFileSystemAccess )
{ 
 // bad programmer, no pizza
}

Or alternatively is there some sort of lower level callback that the app can hook when a file is opened?

Note I’m aware of the awesomeness of strace, but its gotten to the point where this needs to be enforced as part of the application execution, not as post-hoc manual test.

  • 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-05-29T15:22:36+00:00Added an answer on May 29, 2026 at 3:22 pm

    It depends what exactly the code is legitimately trying to do, but you could possibly do this with setrlimit()‘s RLIMIT_NOFILE.

    Something like this should work:

    #include <sys/resource.h>
    
    struct scoped_fd_blocker {
        rlim_t prev;
        scoped_fd_blocker() {
            rlimit lim;
            getrlimit(RLIMIT_NOFILE, &lim); // get the current limit
            prev = lim.rlim_cur; // save old limit
            lim.rlim_cur = 0; // set the soft limit to 0
            setrlimit(RLIMIT_NOFILE, &lim); // do the set
        }
    
        ~scoped_fd_blocker() {
            rlimit lim;
            getrlimit(RLIMIT_NOFILE, &lim); // get the current limit
            lim.rlim_cur = prev; // reset the soft limit to the previous value
            setrlimit(RLIMIT_NOFILE, &lim); // do the set
        }
    };
    
    
    // Example Usage:
    void do_stuff() {
        scoped_fd_blocker blocker;
        Application->DoImportantOperation();
    }
    

    Basically this tells the OS not to let your process open any file descriptor, even if an existing one is closed, by zeroing the soft open file descriptor limit of the process. Note that this is more than just files and could have some unintended consequences. This would include files, sockets, event objects, directories, shared resources, pipes, and would also prevent C-libraries from opening files. (Some C libraries do use file locks and stuff to manage concurrency.) Think about all of the things that open files (like dlopen for example).

    Any attempt to open a file descriptor will fail (return -1) and errno will be set to EMFILE which translates to “Error 24: Too many open files”.

    I’ve put the whole thing in a struct so that it’s strongly exception safe.

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

Sidebar

Related Questions

I have a large framework consisting of many C++ classes. Is there a way
I have tried using electric fence to debug large C application on linux. It
I have a large application (~50 modules) using a structure similar to the following:
I have a large application that uses EJB 2.x entity beans (BMP). This is
We have fairly large C++ application which is composed of about 60 projects in
We have a large enterprise application where projects are scoped designed and finally coded
I have a rather large application that has literally a hundred DDLs with Yes
I have a fairly large application that uses WPF for its user interface. I
I have a large PHP application. After I changed some settings I get a
Here at work we have a very large application with multiple sub applications. (500

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.