Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all I am writing a program in c++. I want to restrict its use so that it can be called from within a script only (any shell script). If ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    How to find whether my program is called from a script or from a terminal

    Hi all
    I am writing a program in c++. I want to restrict its use so that it can be called from within a script only (any shell script). If it is directly typed at the command prompt, I want the program to exit. How can I differentiate between these two types of invocations? Please suggest a foolproof solution. Also please not that it should be applicable for different platforms like gnu/linux, solaris, hu-us, aix etc. ie no platform specific syscalls. I am okay with library calls.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, this is a tricky one. I know how you can check whether a stream is tied to the console or not, but this a new one.

    I suppose you could possibly use the getppid function (Process Identification - The GNU C Library) and then somehow identify if that process is a terminal. Of course, that wouldn't eliminate, say, a "Run Program" dialog.

    Any method I can think of relies on you using a database to compare the invoker's name against, and this certainly is not foolproof, since there are tons of varying systems out there. I'm also relying on proc a bit, and I believe proc is Linux only.

    Why do you want it to only run from a script?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Use the getppid function to get the ppid.
    With the ppid you can read the first line of the status file in the /proc/ppid directory to get the name of the parent proces.

    Regards

    Edit: this only works on systems with a /proc filesystem and as Cabhan mentioned, this is Linux only.
    Last edited by Franklin52; 05-26-2007 at 08:36 PM. Reason: Need new glasses...

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    Post

    hi Cabhan & Franklin52,
    Thanks for the replies. @Cabhan: my program is for connecting to oracle. The user name and password I will pipe to sqlplus, so that it won't be shown when you give ps command. But I wan't the program to start only when shell scripts are run with sql commands in "here document".
    eg:
    connectdb sqlplus <<EOF
    select tname from tab;
    EOF
    where connectdb is my program

    As you said, using "isatty" we can find whether it is tied to terminal or not. But "here document" can be given at command prompt as well as in shell scripts. This is what I want to differentiate. Maximum I could think of was to use "ps -eaf" and export the value to some temp variable and read inside. But one problem I face is, if I am not giving #!/bin/bash at the beginning of the script, "ps -eaf" is not showing the full command as
    /bin/bash ./myscript.sh. Instead it just shows /bin/bash.
    The intention is to provide some basic security where the user has complete access to the system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...