Running adb from PHP

glarkou asked:

I moved from Ubuntu to CentOS and I now have a problem running adb from PHP.

I am trying to execute shell_exec("/path/to/adb devices");

and I get:

ADB server didn't ACK
failed to start daemon
error cannot connect to daemon
daemon not running. starting it now on port 5037

Of course adb is not able to start because it is already running and listening on port 5037 and If I try to run adb devices as root or as a regular user from the command line it returns all connected devices.

This configuration was previously working with Ubuntu.

What is the difference? Any suggestions?

My answer:

I’m going to say it’s a 90% chance it’s SELinux related. You can confirm it by looking for entries in /var/log/audit/audit.log.

CentOS has SELinux enabled by default.

When you run adb from your PHP script, it is most likely running under httpd‘s security context, which doesn’t permit outgoing network connections.

The quick fix is to allow httpd to make outgoing network connections:

setsebool -P httpd_can_network_connect on

Keep in mind that this will allow outgoing connections to anywhere.

The secure fix is to write a local policy confining adb and allowing it to connect to port 5037, access USB devices, etc., but this is a lot of work…

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.