Page 1 of 1

Signal sending problem

Posted: Sat Dec 06, 2008 10:34 pm
by moejoe22
I am trying to get php to send a signal to a concurrently running c/c++ program. I have tried several ways of attempting this but every time the php route fails.

I can from terminal send the appropriate signal and the c/c++ program(s) will acknowledge receiving the signal (I have custom handlers written for them) but when I try through php it does nothing.

Here's an example of some of the things I've tried:

$y = `./sendsig`;
system("./sendsig", $x);
system("./sikil", $z);
exec("./sendsig");

sendsig is a shell script I wrote that when run from terminal sends the right signal to the c/c++ prog and the program acknowledges the signal.

$y = `./siint`;
system("./siint", $x);
system("./siint", $z);
exec("./"siint);

siint is another c program that I wrote that sends the right signal to the c/c++ program and it acknowledges the signal being received (only when I run siint from terminal.. from php it blunders).

$pid = `pgrep ex`; // this line doesn't work atm so I have $pid set by hand to the process of the c/c++ program
$y = `kill -10 $pid`;
system("kill -10 $pid", $x);
exec("./sendsig");

Is the direct route I tried getting php to send the signal itself, and that didn't work at all.

Does anyone have any ideas? I'm running out of options to get php to send a signal to a program. It's possible that php refuses to send a signal to another program because of lack of permission.. I don't know.

Re: Signal sending problem

Posted: Sun Dec 07, 2008 2:45 pm
by VladSun
Try:

Code: Select all

echo exec('kill -10 `pgrep ex` 2>&1');
I'm pretty sure that Apache user process is not permitted to signal the C program process (supposing it's running under another user) and you will see it in the error message shown by the output of the PHP code above.
Also, SIG 10 is a strange signal to send... http://www.unix.com.ua/orelly/other/pui ... ect-2.html

Re: Signal sending problem

Posted: Sun Dec 07, 2008 11:22 pm
by moejoe22
So, how would I have the php message the c program? Or contact it in anyway? What if the c-program was initialized by apache? Signal 10 is a user specified signal, it seemed like a good one to catch.

Re: Signal sending problem

Posted: Mon Dec 08, 2008 5:17 am
by VladSun
What is the output from the code I gave you above?
If your C program runs under Apache user I think there should be no problems with sending signals to it from PHP.

Re: Signal sending problem

Posted: Mon Dec 08, 2008 7:11 pm
by moejoe22
sh: line 0: kill: ex: arguments must be process or job IDs
Is what my php page outputs after your line. I don't think I can have php run the c program.. because the c program is designed to run forever. All the php functions to run outside programs wait until the command returns.. and this one would never return. Also on that note, I wouldn't know how to obtain the pid for the c program if a php script ran the c program.

I appreciate your input, cause I've banged my head on the keyboard for several days on this problem.

Re: Signal sending problem

Posted: Mon Dec 08, 2008 7:12 pm
by moejoe22
moejoe22 wrote:
sh: line 0: kill: ex: arguments must be process or job IDs
Is what my php page outputs after your line. If I replace the pgrep segment with the pid that I manually input after finding it, the echo displays nothing.

I don't think I can have php run the c program.. because the c program is designed to run forever. All the php functions to run outside programs wait until the command returns.. and this one would never return. Also on that note, I wouldn't know how to obtain the pid for the c program if a php script ran the c program.

I appreciate your input, cause I've banged my head on the keyboard for several days on this problem.

Re: Signal sending problem

Posted: Mon Dec 08, 2008 7:36 pm
by VladSun
moejoe22 wrote:
sh: line 0: kill: ex: arguments must be process or job IDs
Is what my php page outputs after your line.
I really don't think it's the output of *my* code - it looks like you are trying to run:

Code: Select all

kill -10 ex
instead of

Code: Select all

kill -10 `pgrep ex`
;)

If you have permition problems you can either use sudo to execute the Bash script as root, or run the C program process under Apache user:

Code: Select all

su www-data -c "/bin/path/to/your/C_program arg1 arg2"

Re: Signal sending problem

Posted: Mon Dec 08, 2008 8:15 pm
by moejoe22
That output is from
echo exec('kill -10 `pgrep ex` 2>&1');
. Removing '2>&1' yields no output and the c program doesn't receive the signal.

I don't have access to be the super user on this server atm. I will try and use su to become my account and run the exce('kill -10 `pgrep ex`');

Re: Signal sending problem

Posted: Mon Dec 22, 2008 11:41 pm
by Weirdan
what about just sending the signal using posix_kill():

Code: Select all

 
$pid = trim(file_get_contents('/var/run/your.program.pid'));
if (!posix_kill($pid, SIGUSR1)) {
   echo htmlspecialchars(posix_strerror(posix_get_last_error()));
}
 
You will have to know the pid though, and I wouldn't recommend to obtain it by grepping the process list. It's much better to make your C program write the pid of its running process somewhere in the well known location on filesystem (like most unix daemons do, in /var/run/) and read it from there.