I have an application that needs to behave differently if run directly from the linux console. So if a user connects with SSH to run FooBar, or the user walks over to the console and logs in directly to run FooBar, I want it to do something different.
What C API do I need to call to tell the difference between these two scenarios? I was thinking I’d have to look at the “tty/pts” information (such as what I see when I run “ps axf”), but I’m not certain if that is the best solution, nor what API to call to get that information.
Hints appreciated. 🙂
Checking the return value of ttyname(3) against your stdin should give you the name of the terminal which is feeding the input of your process.
It will be /dev/console if the program is being run on the console (and doesn’t have it’s input redirected). You can also check stdout to see if it is connected to /dev/console – see which fits your usage scenario better.