Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop launching a binary from command line

Tags:

c

linux

How to make an application to prevent itself from launching from the command line?

I have a binary which should be launched by a daemon; but when somebody tries to launch the binary from command line, I should error out stating "cannot be launched from command line". Tried googling but in vain.

PS. http://www.daniweb.com/software-development/c/threads/449682/stop-launching-from-command-line I wanted to check if there are better ways than those mentioned in the link..

like image 411
boomerang Avatar asked Apr 10 '26 09:04

boomerang


1 Answers

I am not sure there is a bullet proof answer (to how to prevent a program to be started from command line). You could consider

  • testing with isatty(3) that STDIN_FILENO (i.e. 0) is not a tty
  • try to open /dev/tty (it should fail) see tty(4)
  • testing with getsid(2) that your are not in the same session than your parent, or starting a new session with setsid(2)
  • calling yourself daemon(3)

And I am not sure that you always want to reject being started from a terminal. For debugging, you surely want to be able to be started from a terminal. I actually would just warn, not quit, if started from a terminal.

And you probably want to install your program outside of standard paths, maybe in some libexec/ or sbin/ directory.

See also capabilities(7), pty(7), termios(3)

like image 58
Basile Starynkevitch Avatar answered Apr 12 '26 22:04

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!