1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file open_stdxxx.h
4 /// \brief Make sure that file descriptors 0, 1, and 2 are open
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
22 open_stdxxx(int status)
24 for (int i = 0; i <= 2; ++i) {
25 // We use fcntl() to check if the file descriptor is open.
26 if (fcntl(i, F_GETFD) == -1 && errno == EBADF) {
27 // With stdin, we could use /dev/full so that
28 // writing to stdin would fail. However, /dev/full
29 // is Linux specific, and if the program tries to
30 // write to stdin, there's already a problem anyway.
31 const int fd = open("/dev/null", O_NOCTTY
32 | (i == 0 ? O_WRONLY : O_RDONLY));
35 // Something went wrong. Exit with the
36 // exit status we were given. Don't try
37 // to print an error message, since stderr
38 // may very well be non-existent. This
39 // error should be extremely rare.