Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File description 0 not being closed #4

Open
Amtrix opened this issue Oct 14, 2020 · 1 comment
Open

File description 0 not being closed #4

Amtrix opened this issue Oct 14, 2020 · 1 comment

Comments

@Amtrix
Copy link

Amtrix commented Oct 14, 2020

Randomly stumbled upon this repo on how to daemonize a process. Super helpful! Found a small issue that caused me troubles. The fd = 0 is not getting closed at the line below.

for (fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--) {

@jirihnidek
Copy link
Owner

Yeah, it is not production code. 😄 The code is intended for learning/teaching purpose. Thus I didn't check for returned values in all cases to keep code readable. Maybe I will try to fix it. BTW: Could you estimate why it wasn't possible to close stdin? Something like this could give you some useful error message:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(void)
{
        int ret = 0;
        printf("closing stdin\n");
        ret = close(0);
        if (ret == 0){
                printf("stdin closed\n");
        } else {
                printf("closing stdin failed: %s\n", strerror(errno));
        }
        return EXIT_SUCCESS;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants