4

Why isatty never identifies that my program is using redirected stdin when using emacs? This is my code:

int val = isatty(STDIN_FILENO);
std::cout << val << std::endl;

If I launch my program from eshell (emacs): ./a.out it will return 1, and if I launch ls | ./a.out it will also return 1.

Why does it happen? Which conditions trigger this? Launching this from python (using POPEN(shell=True)) is safe?

Details: From the manpage for isatty(): isatty - test whether a file descriptor refers to a terminal. http://linux.die.net/man/3/isatty

rph
  • 901
  • 1
  • 10
  • 26
  • 1
    Doesn't the TTY belong to the whole process group, rather than to any one process? – Kerrek SB Jun 02 '16 at 14:26
  • From the manual: it actually checks if the file descriptor number belongs to the terminal. So, stdin would normally be terminal. But when using pipes, it would not be TTY. I'm following same solution described here http://stackoverflow.com/questions/2564503/how-do-i-check-if-my-program-has-data-piped-into-it. – rph Jun 02 '16 at 14:27
  • Hm. Try calling `setsid()` at the beginning of your program. – Kerrek SB Jun 02 '16 at 14:31
  • Including setsid() doesn't help. – rph Jun 02 '16 at 14:33
  • 1
    try running your program as a daemon process see if it return 0 in that case. (create a cron job) – Rob Jun 02 '16 at 14:38
  • Tricky! I was running my program from eshell (emacs). Running from a bare terminal (ssh) it works fine. Why is that? – rph Jun 02 '16 at 14:40
  • 1
    How are you running your program? Can you provide a [mcve]? The code you're providing should work assuming your process isn't running inside another process. – Barry Jun 02 '16 at 14:40
  • Is the `LD_PRELOAD` environment variable pointing to a custom `isatty`? I know I've done that before... – kirbyfan64sos Jun 02 '16 at 14:41
  • Under my eshell LD_PRELOAD is unset. – rph Jun 02 '16 at 14:42

0 Answers0