Is it possible that linux file descriptor 0 1 2 is not for stdin, stdout and stderr?

When the program starts, are file descriptors 0, 1, and 2 required for stdin, stdout, and stderr by default ?. And API calls like open (...), socket (...) do not return 0, 1, and 2, since are these values ​​already accepted ?. Is there a case where open (...) or socket (...) will return 0, 1 or 2. And 0, 1 and 2 are not related to stdin, stdout and stderr.

+4
source share
2 answers

At the level, the file descriptor stdin is defined as file descriptor 0 , stdout is defined as file descriptor 1 ; and stderr is defined as a file descriptor 2 . Cm. .

Even if your program or shell changes (for example, redirection with dup2 (2) ), which is a file descriptor of 0, it always remains stdin (since by definition it STDIN_FILENOis 0).

So, of course, stdin can be a pipe or socket or file (and not a terminal). You can test with isatty (3) if it's tty, and / or use fstat (2) to get status information.

Syscalls, (2) pipe (2) socket (2) , , STDIN_FILENO (.. 0), (, (2) -d ). , - .

, stdio (3), FILE stdin . fclose (3), freopen (3), fdopen (3)...

, ​​ stdin, stdout stderr , /sbin/init .

+6
  • , 0 1 2 stdin, stdout stderr .

    , .

    @EJP:

    FD 0 , inetd - , .

  • API, open (...), socket (...) 0 1 2, .

    .

  • , open (...) socket (...) 0 1 2.

    , -

    close(0);
    close(1);
    close(2);
    open(...); /* this open will return 0 if success */
    
+3

Source: https://habr.com/ru/post/1531471/


All Articles