Is it safe to close a directory stream (DIR *) in both the parent and child process?












1















This page describes the guaranteed behavior of the functions readdir and readdir_r and contains the following sentence:




After a call to fork(), either the parent or child (but not both) may
continue processing the directory stream using readdir(), rewinddir()
or seekdir(). If both the parent and child processes use these
functions, the result is undefined.




I'm writing a fork-safe wrapper around the DIR*-related family of functions as an exercise and I'm wondering whether it's safe to closedir the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.










share|improve this question


















  • 1





    Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

    – 炸鱼薯条德里克
    3 hours ago













  • @炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

    – Gregory Nisbet
    3 hours ago













  • Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

    – 炸鱼薯条德里克
    3 hours ago











  • @炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

    – Gregory Nisbet
    3 hours ago


















1















This page describes the guaranteed behavior of the functions readdir and readdir_r and contains the following sentence:




After a call to fork(), either the parent or child (but not both) may
continue processing the directory stream using readdir(), rewinddir()
or seekdir(). If both the parent and child processes use these
functions, the result is undefined.




I'm writing a fork-safe wrapper around the DIR*-related family of functions as an exercise and I'm wondering whether it's safe to closedir the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.










share|improve this question


















  • 1





    Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

    – 炸鱼薯条德里克
    3 hours ago













  • @炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

    – Gregory Nisbet
    3 hours ago













  • Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

    – 炸鱼薯条德里克
    3 hours ago











  • @炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

    – Gregory Nisbet
    3 hours ago
















1












1








1


1






This page describes the guaranteed behavior of the functions readdir and readdir_r and contains the following sentence:




After a call to fork(), either the parent or child (but not both) may
continue processing the directory stream using readdir(), rewinddir()
or seekdir(). If both the parent and child processes use these
functions, the result is undefined.




I'm writing a fork-safe wrapper around the DIR*-related family of functions as an exercise and I'm wondering whether it's safe to closedir the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.










share|improve this question














This page describes the guaranteed behavior of the functions readdir and readdir_r and contains the following sentence:




After a call to fork(), either the parent or child (but not both) may
continue processing the directory stream using readdir(), rewinddir()
or seekdir(). If both the parent and child processes use these
functions, the result is undefined.




I'm writing a fork-safe wrapper around the DIR*-related family of functions as an exercise and I'm wondering whether it's safe to closedir the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.







posix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Gregory NisbetGregory Nisbet

1,4691020




1,4691020








  • 1





    Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

    – 炸鱼薯条德里克
    3 hours ago













  • @炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

    – Gregory Nisbet
    3 hours ago













  • Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

    – 炸鱼薯条德里克
    3 hours ago











  • @炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

    – Gregory Nisbet
    3 hours ago
















  • 1





    Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

    – 炸鱼薯条德里克
    3 hours ago













  • @炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

    – Gregory Nisbet
    3 hours ago













  • Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

    – 炸鱼薯条德里克
    3 hours ago











  • @炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

    – Gregory Nisbet
    3 hours ago










1




1





Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

– 炸鱼薯条德里克
3 hours ago







Executing in other process needs ptrace. Or you have setup IPC between parent and child so they can communicate and do the requested action.

– 炸鱼薯条德里克
3 hours ago















@炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

– Gregory Nisbet
3 hours ago







@炸鱼薯条德里克 I was going to have a static atomic flag and register a fork handler and force the user to pick when opendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.

– Gregory Nisbet
3 hours ago















Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

– 炸鱼薯条德里克
3 hours ago





Don't know what you're talking about. If you want disallow some process from accessing some file, then use mount namespace or simply revoke the file permissions for that user or use selinux or whatever. Then that process can only get an fd from another privileged process

– 炸鱼薯条德里克
3 hours ago













@炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

– Gregory Nisbet
3 hours ago







@炸鱼薯条德里克 I see. I'm not trying to force processes that I have no control over to "play by the POSIX rules" or prevent them for accessing particular files. I'm trying to understand what exactly the POSIX rules are regarding directory streams. My motivation for understanding what the rules are is to write a wrapper library around the POSIX API that makes it easier to write programs that only use the API in defined ways.

– Gregory Nisbet
3 hours ago












0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507845%2fis-it-safe-to-close-a-directory-stream-dir-in-both-the-parent-and-child-proc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507845%2fis-it-safe-to-close-a-directory-stream-dir-in-both-the-parent-and-child-proc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio