Rizin
unix-like reverse engineering framework and cli tools
tuklib_open_stdxxx.c File Reference

Make sure that file descriptors 0, 1, and 2 are open. More...

#include "tuklib_open_stdxxx.h"
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

Go to the source code of this file.

Functions

void tuklib_open_stdxxx (int err_status)
 

Detailed Description

Make sure that file descriptors 0, 1, and 2 are open.

Definition in file tuklib_open_stdxxx.c.

Function Documentation

◆ tuklib_open_stdxxx()

void tuklib_open_stdxxx ( int  err_status)

Definition at line 24 of file tuklib_open_stdxxx.c.

25 {
26 #ifdef TUKLIB_DOSLIKE
27  // Do nothing, just silence warnings.
28  (void)err_status;
29 
30 #else
31  for (int i = 0; i <= 2; ++i) {
32  // We use fcntl() to check if the file descriptor is open.
33  if (fcntl(i, F_GETFD) == -1 && errno == EBADF) {
34  // With stdin, we could use /dev/full so that
35  // writing to stdin would fail. However, /dev/full
36  // is Linux specific, and if the program tries to
37  // write to stdin, there's already a problem anyway.
38  const int fd = open("/dev/null", O_NOCTTY
39  | (i == 0 ? O_WRONLY : O_RDONLY));
40 
41  if (fd != i) {
42  if (fd != -1)
43  (void)close(fd);
44 
45  // Something went wrong. Exit with the
46  // exit status we were given. Don't try
47  // to print an error message, since stderr
48  // may very well be non-existent. This
49  // error should be extremely rare.
50  exit(err_status);
51  }
52  }
53  }
54 #endif
55 
56  return;
57 }
lzma_index ** i
Definition: index.h:629
static static fork const void static count close
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags fcntl
Definition: sflib.h:79
#define O_WRONLY
Definition: sftypes.h:487
#define O_NOCTTY
Definition: sftypes.h:491
#define O_RDONLY
Definition: sftypes.h:486
#define F_GETFD
Definition: sftypes.h:504
#define EBADF
Definition: sftypes.h:119
static const z80_opcode fd[]
Definition: z80_tab.h:997

References close, EBADF, test-lz4-list::exit, F_GETFD, fcntl, fd, i, O_NOCTTY, O_RDONLY, and O_WRONLY.

Referenced by io_init().