jerome1232
August 22nd, 2018, 08:59 PM
Alright, so I'm making a little robot car on a raspberry pi with a web interface. I'm a beginner programming so a lot of my coding I'm learning as I'm making it.
My biggest hurdle I hit was figuring out how on earth was I going to get keystrokes in a web browser communicated to a python script. I found my answer in unix domain sockets.
So I've successfully gotten two python scripts communicated via unix domain sockets, but I can't get PHP to work. Specifically I'm running into permissions issues.
This is the php portion of my test script.
<?php
echo php_uname();
echo '<br>';
echo shell_exec('uptime -p');
echo '<br>';
// This is just showing me the current user running the php script
echo 'Current Script owner: '.get_current_user();
echo '<br>';
// Creating a unix domain socket
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($sock === false) {
echo "socket_create() failed: reason: "
.socket_strerror(socket_last_error($sock))."<br>";
}
// Setting up my path relative to the virtual servers root
$path = $_SERVER['DOCUMENT_ROOT'].'/tmp/test';
// Checking that my path is what I think it's supposed to be
echo "This is the path to the socket: ".$path."<br>";
// Binding to the socket
if(socket_bind($sock, $path) === false) {
echo "socket_bind() failed: reason:"
.socket_strerror(socket_last_error($sock))."<br>";
}
// checking for errors after having bound to the socket
echo '<br>';
// listening on the bound socket
$isListen = socket_listen($sock);
while ($isListen) {
$conn = socket_accept($sock);
if ($conn == False) {
usleep(100);
} else if ($conn > 0) {
echo socket_read($sock, 1);
socket_close($sock);
}
}
socket_close($sock) ?>
This is what gets outputted to the webpage when that runs.
Linux raspberrypi 4.14.62-v7+ #1134 SMP Tue Aug 14 17:10:10 BST 2018 armv7l
up 1 day, 19 hours, 3 minutes
Current Script owner: pi
This is the path to the socket: /home/pi/Documents/robot_car/webpage/tmp/test
socket_bind() failed: reason: Permission denied
I don't understand why php doesn't have permission to create a unix domain socket, even going the other way I was able to create a unix domain socket with python, but php would get denied when trying to connect to it.
My biggest hurdle I hit was figuring out how on earth was I going to get keystrokes in a web browser communicated to a python script. I found my answer in unix domain sockets.
So I've successfully gotten two python scripts communicated via unix domain sockets, but I can't get PHP to work. Specifically I'm running into permissions issues.
This is the php portion of my test script.
<?php
echo php_uname();
echo '<br>';
echo shell_exec('uptime -p');
echo '<br>';
// This is just showing me the current user running the php script
echo 'Current Script owner: '.get_current_user();
echo '<br>';
// Creating a unix domain socket
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($sock === false) {
echo "socket_create() failed: reason: "
.socket_strerror(socket_last_error($sock))."<br>";
}
// Setting up my path relative to the virtual servers root
$path = $_SERVER['DOCUMENT_ROOT'].'/tmp/test';
// Checking that my path is what I think it's supposed to be
echo "This is the path to the socket: ".$path."<br>";
// Binding to the socket
if(socket_bind($sock, $path) === false) {
echo "socket_bind() failed: reason:"
.socket_strerror(socket_last_error($sock))."<br>";
}
// checking for errors after having bound to the socket
echo '<br>';
// listening on the bound socket
$isListen = socket_listen($sock);
while ($isListen) {
$conn = socket_accept($sock);
if ($conn == False) {
usleep(100);
} else if ($conn > 0) {
echo socket_read($sock, 1);
socket_close($sock);
}
}
socket_close($sock) ?>
This is what gets outputted to the webpage when that runs.
Linux raspberrypi 4.14.62-v7+ #1134 SMP Tue Aug 14 17:10:10 BST 2018 armv7l
up 1 day, 19 hours, 3 minutes
Current Script owner: pi
This is the path to the socket: /home/pi/Documents/robot_car/webpage/tmp/test
socket_bind() failed: reason: Permission denied
I don't understand why php doesn't have permission to create a unix domain socket, even going the other way I was able to create a unix domain socket with python, but php would get denied when trying to connect to it.