Signal sending problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moejoe22
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2008 10:31 pm

Signal sending problem

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Signal sending problem

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
moejoe22
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2008 10:31 pm

Re: Signal sending problem

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Signal sending problem

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
moejoe22
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2008 10:31 pm

Re: Signal sending problem

Post 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.
moejoe22
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2008 10:31 pm

Re: Signal sending problem

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Signal sending problem

Post 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"
There are 10 types of people in this world, those who understand binary and those who don't
moejoe22
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2008 10:31 pm

Re: Signal sending problem

Post 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`');
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Signal sending problem

Post 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.
Post Reply