LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2006, 01:12 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
How to check calling program/script? (C/C++)


I have a C++ application that I wrote. Due to the special nature of this program, it is not intended to be invoked directly by the user, but instead there is a Perl script which does some important pre-processing for calling the C++ application.


Is there a way that I can have the C++ application check that it was called by this Perl script? (I googled and didn't find an answer). Currently I do a cheap trick where the Perl script sends an undocumented command line argument to the application, and the application will refuse to run unless it has been given that argument (so a user can technically still invoke the application directly). Thanks in advance.
 
Old 03-29-2006, 02:05 PM   #2
burntfuse
Member
 
Registered: Sep 2005
Location: Laurel, MD, USA
Distribution: Slackware 10.1, FC5
Posts: 164

Rep: Reputation: 30
Hmmm, that's a tough problem...if Perl spawns new programs as child processes, maybe you could have the program find its parent process with the entries in /proc, then make sure that the command line of the parent process (in /proc/[process_num]/stat) has 'perl' (or whatever the name of the executable is) in it? Sort of a nasty hack, but I guess it's more secure than relying on the command line option remaining undocumented. It won't work, however, if processes spawned by Perl have only init as a parent.
 
Old 03-29-2006, 02:14 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Why not use a semaphore? Have your Perl app "touch" some file and your app check for that file's existence. You could even check the the "creation time" for the file was reasonably close to that of the start of the C app.

Last edited by PTrenholme; 03-29-2006 at 02:16 PM.
 
Old 03-29-2006, 02:34 PM   #4
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by burntfuse
Hmmm, that's a tough problem...if Perl spawns new programs as child processes, maybe you could have the program find its parent process with the entries in /proc, then make sure that the command line of the parent process (in /proc/[process_num]/stat) has 'perl' (or whatever the name of the executable is) in it? Sort of a nasty hack, but I guess it's more secure than relying on the command line option remaining undocumented. It won't work, however, if processes spawned by Perl have only init as a parent.
It doesn't spawn it as a new child process (the invocations of the application also need to run on a distributed computing cluster). I wanted to avoid a Linux-specific solution to the problem, because the app is designed to be portable.

Quote:
Originally Posted by PTrenholme
Why not use a semaphore? Have your Perl app "touch" some file and your app check for that file's existence. You could even check the the "creation time" for the file was reasonably close to that of the start of the C app.
This Perl script may execute the application multiple times in one run through the script, and it also does some amount of computation before the program is run (parses configuration files).




Thanks for the suggestions. I just wanted to make sure there wasn't some easy method for doing this that I wasn't aware of. Since it doesn't sound like there is an easy solution, I think my curent method will do the job.
 
Old 03-29-2006, 02:51 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by R00ts
[snip]This Perl script may execute the application multiple times in one run through the script, and it also does some amount of computation before the program is run (parses configuration files).
[snip]
Well, you could still use a file to pass a "token" to the C program (generated by some cryptic process) which the C program needs to "match" before it would run. Something as simple as an XOR with a key might work to discourage casual use of the code. Yes, "touch" is *NIX specific, but files are not.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
newbie/program for calling BBS's Ltkpr49 Linux - Software 1 06-29-2005 09:45 AM
calling a system function directly from C program appas Programming 8 08-03-2004 01:50 AM
calling another script to be run. sonesay Programming 2 05-23-2004 06:08 AM
Calling All Script Guru's Shinobi Linux - General 5 04-02-2003 03:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration