0

Redirecting output from a known file descriptors to a file is as simple as

./binary 1> ./file 2>&1

But, let's say I have a binary which might be using file descriptors other than 0, 1 and 2.

File descriptors 1 and 2 default to current tty if the commands are run interactively

I'm running the binary interactively, but I think the binary has other file descriptors pointing to current tty because I have already redirected 1 and 2 file descriptors to a file, but still some output is being written to terminal, not in the file.

It might be using n number of other file descriptors, but I don't know which ones it's using.

I just want to redirect all these file descriptors which are pointing to current tty to a file.

I don't care which file descriptor the binary is writing to, all streams should be redirected to this file

Something sort of like

./binary *> ./file
2
  • "I just want to redirect all file descriptors to a file." This won't work. Your binary needs to open some files, including, but not limited to, libraries; to see what I mean, run something trivial like strace echo foobar and count the open/openat calls. When you mess with these, things simply won't work. I think you really need to describe what it is that you're planning to do, and which problem you want to solve with that. Jul 19, 2021 at 9:04
  • @MarcusMüller Thanks for the response. Added a bit of info in the question Jul 19, 2021 at 9:13

1 Answer 1

1

File descriptors 1 and 2 default to current tty if the commands are run interactively

no, they don't point to current tty - they are stdout and stderr, which might be routed to the current terminal through how your shell and your terminal are set up. Kind of important distinction; your redirection splices the stdout and stderr to some specific file descriptor, but someone interacting with the terminal itself is not affected by it:

If your binary is still writing data to your terminal that doesn't get redirected, it's probably not interacting with stdout; it directly interacts with the pseudoterminal (man 7 pty for more info), e.g. through some library like ncurses.

While that is a character device and you could "snoop" what is written to it, that might not make sense in a "linearized" form. Nothing stops an application from writing words backwards ("move right 2 places, print o, move right 2 places, print l, move right 2 places, print l, move right 2 places, print e, move right 2 places, print h" instead of "hello"). Also, such programs tend to update significant parts of the screen at once!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .