I am having a compiled C program say test in /usr/bin and a python program say pgm.py is in /opt/python/ . In pgm.py , I am calling the C program like os.system("test arg1 arg2") . Is it possible for the C program to know that it is being called by /opt/python/pgm.py ?
Asked
Active
Viewed 164 times
0
-
2Take the parents process ID and see what information you can get about the process. You might be able to get the commandline of the parent-process which would look like `python /opt/python/pgm.py` or something similar. – tkausl Jun 26 '16 at 14:06
-
1@tkausl, wouldn't the parent process be shell since `os.system()` executes as `sh -c test arg1 arg2`? – gaganso Jun 26 '16 at 14:38
-
@tkausl: Disagreed, as per *SilentMonk*'s comment. – alk Jun 26 '16 at 14:42
-
I am not sure how python `os.system()` works but if thats the case then we would take the parents parent. – tkausl Jun 26 '16 at 14:42
-
@tkausl: Theoretically yes, but how would you practically achieve this? – alk Jun 26 '16 at 14:43
-
1Just make the Python script pass its PID, real-path, whatever along with the other parameters when calling `test`. – alk Jun 26 '16 at 14:44
-
http://stackoverflow.com/questions/1525605/linux-programmatically-get-parent-pid-of-another-process – tkausl Jun 26 '16 at 14:45
-
@tkausl: Those are non-standard, not-portable solutions. And even if they were around, they aren't reliable, as introducing a race, when reading `/proc`. – alk Jun 26 '16 at 14:47
-
There are several platform-specific solutions. But would it not be simpler to add a parameter to your `test` program akin to `--i-am-pgm-py`, and specify that parameter when calling it from your `pgm.py` script? – Aya Jun 26 '16 at 14:58
-
SilentMonk is correct.I have checked. – BusyTraveller Jun 26 '16 at 15:02
-
@alk, actually my requirement is that the test program should respond only if the program name, path and mainly the hash of the python program match.If the python program is giving its PID and the path, then any junk program can give the correct path and execute the c program.Since the junk program gives the correct path, the hash always matches.Any other option is there to full fill my requirement ? My requirement is that , only /op/python/pgm.py should be able to run test. – BusyTraveller Jun 26 '16 at 15:16
-
@tkausl ps -p
-o ppid= is working. But you had told that it is not reliable. Any reason for that ? Does ps program fully rely on /proc file system – BusyTraveller Jun 26 '16 at 15:33 -
1Are you sure that your problem is who is my parent and not who should have access to run me? If it's access problem you should probably use system specific access restrictions that will guarantee your process is not run by not authorize apps or users. – Logman Jun 26 '16 at 17:14
-
@Logman yes access is the problem. But I cannot use system specific access restrictions, because in my set up , I am not supposed to trust the owner of the PC where my application is running. – BusyTraveller Jun 27 '16 at 08:53
2 Answers
0
Misc operating system interfaces will have the information you want. One way would be to get the python program to write the information to a temp file, and then pass the file as a c-line arg into the C program.

izaak_pyzaak
- 930
- 8
- 23
-
Thanks for the info . But my requirement is that , only /op/python/pgm.py should be able to run test.c.So the checking should happen in test.c regardless of the parameters given by pgm.py – BusyTraveller Jun 26 '16 at 15:43
-
Ok, that is difficult. But how "only" is only? You could have a hardcoded string in test.c to check that pgm.py is the caller. I.e the first thing in `main()`, in test.c is to check which program called it. If it is not pgm.py it `exit()`s. Otherwise, (it it is pgm.py) then the remaining body of `main()` i.e the business code, is allowed to run. – izaak_pyzaak Jun 26 '16 at 15:45
-
This info can be passed as a c-line arg in the temp file I talk about in the answer. – izaak_pyzaak Jun 26 '16 at 15:47
-
Any junk program can also pass the same info and the C program(test.c) will run thinking that it is called by the genuine program. So , instead of relying on the arguments passed , test.c should find them itself. But I think it is too difficult. – BusyTraveller Jun 26 '16 at 15:54
0
Assuming you're using something akin to Linux, you could use a platform-specific solution. For simplicity, I'm using a Python script test.py
in place of a binary.
pgm.py
#!/usr/bin/env python
import os
os.system('python test.py')
test.py
#!/usr/bin/env python
import os, errno
pid = os.getpid()
while 1:
try:
pid = int(open('/proc/%d/stat' % pid).read().split()[3])
cmd = os.readlink('/proc/%d/exe' % pid)
args = open('/proc/%d/cmdline' % pid).read().split('\0')
except OSError as e:
if e.errno == errno.EACCES:
print 'Permission denied for PID=%d' % pid
break
raise
print pid, cmd, args
if pid == 1:
break
When running pgm.py
, I get the output...
341 /bin/dash ['sh', '-c', 'python test.py', '']
340 /usr/bin/python2.7 ['python', './pgm.py', '']
13888 /bin/bash ['-bash', '']
Permission denied for PID=13887
So you could test use a simple comparison in test
which does something similar.

Aya
- 39,884
- 6
- 55
- 55