xref: /freebsd/contrib/xz/src/common/tuklib_open_stdxxx.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       tuklib_open_stdxxx.c
6 /// \brief      Make sure that file descriptors 0, 1, and 2 are open
7 //
8 //  Author:     Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #include "tuklib_open_stdxxx.h"
13 
14 #ifndef TUKLIB_DOSLIKE
15 #	include <stdlib.h>
16 #	include <errno.h>
17 #	include <fcntl.h>
18 #	include <unistd.h>
19 #endif
20 
21 
22 extern void
23 tuklib_open_stdxxx(int err_status)
24 {
25 #ifdef TUKLIB_DOSLIKE
26 	// Do nothing, just silence warnings.
27 	(void)err_status;
28 
29 #else
30 	for (int i = 0; i <= 2; ++i) {
31 		// We use fcntl() to check if the file descriptor is open.
32 		if (fcntl(i, F_GETFD) == -1 && errno == EBADF) {
33 			// With stdin, we could use /dev/full so that
34 			// writing to stdin would fail. However, /dev/full
35 			// is Linux specific, and if the program tries to
36 			// write to stdin, there's already a problem anyway.
37 			const int fd = open("/dev/null", O_NOCTTY
38 					| (i == 0 ? O_WRONLY : O_RDONLY));
39 
40 			if (fd != i) {
41 				if (fd != -1)
42 					(void)close(fd);
43 
44 				// Something went wrong. Exit with the
45 				// exit status we were given. Don't try
46 				// to print an error message, since stderr
47 				// may very well be non-existent. This
48 				// error should be extremely rare.
49 				exit(err_status);
50 			}
51 		}
52 	}
53 #endif
54 
55 	return;
56 }
57