janpihlgren
Posts: 57
Joined: Thu Jul 26, 2012 4:00 am

Howto run a c-program within html?

Mon Mar 30, 2015 6:46 am

I have a homepage on RPi 2 B (/var/www/piface/) where I whish to run a c-progam (piface-blink.c) that have LEDs blink at a PiFace board.
I have tried the following php-code:

Code: Select all

<?php
 system("./piface-blink\n"); 
?>
I also tried the following:

Code: Select all

<?php
 exec("./piface-blink\n"); 
?>
I don't get any error.
The following will succeed:

Code: Select all

<?php
echo exec("ls -l piface-blink\n");
?>
So why will my try to execute a cprogram succeed?

User avatar
rpdom
Posts: 14984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Howto run a c-program within html?

Mon Mar 30, 2015 7:04 am

Have you checked the webserver logs to see if there are any errors?
If you are using Apache, the log will be /var/log/apache2/error.log. You may see a "permission denied" or other message in there.
The webserver usually runs as user "www-data". Does the program have suitable permissions to allow it to be run by that user or group?
As it is using GPIOs, does it need privileged access (sudo)?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Howto run a c-program within html?

Mon Mar 30, 2015 7:21 am

janpihlgren wrote:I don't get any error.
that's because you are not checking the output of the command executed. System() and Exec() have more parameters that what you used, and those parameters give you the output of the program..
Btw, i'm quite sure the error will be an access error (i suppose the www-data user is not authorized to execute programs)

janpihlgren
Posts: 57
Joined: Thu Jul 26, 2012 4:00 am

Re: Howto run a c-program within html?

Mon Mar 30, 2015 7:46 am

rpdom wrote:Have you checked the webserver logs to see if there are any errors?
If you are using Apache, the log will be /var/log/apache2/error.log. You may see a "permission denied" or other message in there.
The webserver usually runs as user "www-data". Does the program have suitable permissions to allow it to be run by that user or group?
As it is using GPIOs, does it need privileged access (sudo)?
Thanks for hint.
The program has the following privileges:
-rwxr-xr-x 1 pi pi 6259 mar 30 07:38 piface-blink

I now checked the log and find:
Unable to open SPI device: Permission denied
I changed the command to:

Code: Select all

exec('sudo ./piface-blink');
Now I get the error:
sudo: no tty present and no askpass program specified
What's askpass program?
And I don't need a tty(?) as I wish to run the program in the backgrund.

User avatar
rpdom
Posts: 14984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Howto run a c-program within html?

Mon Mar 30, 2015 7:59 am

Now I get the error:
sudo: no tty present and no askpass program specified
What's askpass program?
And I don't need a tty(?) as I wish to run the program in the backgrund.
The www-data user doesn't have permissions to use "sudo" (most users don't - it can be a big security issue if they can all do administrator things). So it is trying to ask for a password to be entered to verify that www-data can run stuff. It can't find a tty for someone to type in a password, and it can't find any other method (askpass), so it gives up.

In this situation I am very wary about having www-data accessing things as root, but there aren't may ways around that for accessing the hardware.

One way would be to set up an explicit entry in a sudoers file for www-data to be allowed to run that program only.

Code: Select all

#/etc/sudoers.d/www-data
#
# sudoers file for the www-data user - very limited permissions only

www-data    ALL=NOPASSWD:   /var/www/piface-blink
Create this file using "sudo nano /etc/sudoers.d/www-data" and you should be ready to go.

janpihlgren
Posts: 57
Joined: Thu Jul 26, 2012 4:00 am

Solved-Re: Howto run a c-program within html?

Mon Mar 30, 2015 8:22 am

Hi :D
That's do it!!! :D
Thanks rpdom

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Howto run a c-program within html?

Mon Mar 30, 2015 11:57 am

janpihlgren wrote:
I now checked the log and find:
Unable to open SPI device: Permission denied
You shouldn't need to add www-data to the sudoers file.

sudo adduser www-data spi
sudo service apache2 restart # or NGINX or lighthttpd if you use those.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 14984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Howto run a c-program within html?

Mon Mar 30, 2015 7:32 pm

DougieLawson wrote:You shouldn't need to add www-data to the sudoers file.

sudo adduser www-data spi
That's an even better solution. I didn't know the spi interface had a group.

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Howto run a c-program within html?

Mon Mar 30, 2015 8:11 pm

rpdom wrote:
DougieLawson wrote:You shouldn't need to add www-data to the sudoers file.

sudo adduser www-data spi
That's an even better solution. I didn't know the spi interface had a group.
It got added with a firmware/kernel update. I2C also has a i2c group.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

Re: Howto run a c-program within html?

Mon May 04, 2015 6:06 am

when I try to do

Code: Select all

sudo adduser www-data spi
it says the group doesn't exist :/

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Howto run a c-program within html?

Mon May 04, 2015 12:09 pm

sudo apt-get update && sudo apt-get upgrade
Then reboot.
That should get the stuff that adds the spi and i2c groups and the kernel that supports that.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

teo.dorel
Posts: 1
Joined: Fri Nov 03, 2017 6:21 am

Re: Howto run a c-program within html?

Fri Nov 03, 2017 6:31 am

Hi,
I finally got the last piece of the puzzle.
Thanks for sharing. It was driving me nuts.

I made a simple php script that runs through shell_exec a c program that uses gpio pins. The c program is not in the same directorul as the web page, the wiringpi also in a different place, closeby.
After setting correct execution permission on all directories and files this cherry "sudo adduser www-data gpio" and restarting the apache service did the job

Thank you again for sharing.
Chears


Return to “C/C++”