Permission denied opening '/dev/ttyS0' with PHP

Asked by ishunko

Hello,

I am truing to access a serial port in PHP using the following command:

fopen('/dev/ttyS0', 'w')

However, I get the error message:

open(/dev/ttyS0) [function.fopen]: failed to open stream: Permission denied in /var/www/...

How can I let it run or haw can a change permissions for PHP script to be able to access '/dev/ttyS0'?

Thank you!

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu php4 Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Ralph Janke (txwikinger) said :
#1

Thank you for your question.

There are several possibilities how to solve this depending on your circumstances.

The probably easiest way is allow write permission on the device file. This can be done with the command

sudo chmod a+w /dev/ttyS0

This allows everybody to write to this device.

You could also change your script of being suid (i.e. being root during execution even the calling user is not).

http://www.howtoforge.com/linux_setting_suid_sgid_bits

Finally you could run your script with in 'sudo' mode.

I hope this helps.

Revision history for this message
ishunko (ilya-shunko) said :
#2

Thank you for the quick reply.

I can run the script in 'sudo' mode in the terminal window without any problems. However, I'd like to run it in the browser.

Neither 'sudo chmod a+w /dev/ttyS0', nor changing the script to be suid solved my problem. I still get the same error message... Is there anything I am missing?

Thanks again.

Revision history for this message
Mariano Mara (marplatense) said :
#3

A shot in the dark here but, and correct me if I'm wrong, the permission problem seems to be in /var/www/... (possibly pointing to your file), not in /dev/ttySO (although it should be difficult to run a script that affects /dev). If you have other php files there please compare their access rights (with ls -l from terminal) just to be sure they're the same. Maybe if make it root:root they might work (pretty risky if you ask me): sudo chown root:root yourfile.php.

Revision history for this message
ishunko (ilya-shunko) said :
#4

I have compared the files -- they all seem to be the same. 'sudo chown root:root yourfile.php' din't seem to work as well... Looks like I need somehow give Apache permission to access the serial port', because I can run the file just fine in the terminal window.

Revision history for this message
Ralph Janke (txwikinger) said :
#5

I should have first looked at the permissions myself. It might be that the read permissions for non-owner and non-groups are missing too. You can fix this with

sudo chmod a+r /dev/ttyS0

However, you should be aware that you open this port for every user, so you should think about security concerns. If that is not an issue for you, it shouldn't matter.

I hope this helps.

Revision history for this message
McPeter (mcpeter) said :
#6

i try with suPHP and php5-cgi and it work :)

Can you help with this problem?

Provide an answer of your own, or ask ishunko for more information if necessary.

To post a message you must log in.