Akkua
Akkua

Reputation: 141

How can I start a WSL-based Linux executable from within PHP on Windows and capture its stdout?

I have a program that only works on Linux, and I have my development environment on Windows.


Cmd.exe:

C:\wsl echo "foo"
foo

PHP:

<?php
    $cmd = 'wsl echo "foo"';
    exec($cmd, $out, $code);
    dd($out, $code);
?>
// $out is []
// $code is -1073740791

Upvotes: 2

Views: 1427

Answers (2)

Juanjo Martinez
Juanjo Martinez

Reputation: 776

Summarizing Dai's answer;

You are probably running Apache as NT Authority\SYSTEM (aka Local System or LocalSystem), and WSL doesn't like to run as anything but your user (And its also a bad security practice)

A simple fix, at least for WSL's issues, is to change the service's properties to use your windows user, on services.msc, wampstackApache (or similar), log on.

echo shell_exec("whoami"); will then report your user correctly and WSL will run just fine.

Disclaimer; I dont know how bad of a security issue this is, but it sounds kinda bad

Upvotes: 0

Dai
Dai

Reputation: 154995

As discussed in the comments, the underlying issue was that your systemwide Apache web-server service (wampapache64) was configured to run as NT Authority\SYSTEM (aka Local System or LocalSystem).

...and running anything like a web-server as LocalSystem is a very, very bad idea for other reasons, especially when it's running PHP code - a simple mistake in uploaded-file handling or a failure to correctly sanitize user input could potentially hose your entire computer, especially when it's a publicly-exposed web-server that a malicious attacker can connect to.


Anyway:

Apparently when a process is running as SYSTEM it cannot use Win32's CreateProcess to invoke wsl to then indirectly invoke a Linux program.

So by changing your web-server to run as a real user account that meant that the CreateProcess... call invoked wsl within a supported environment.

Upvotes: 2

Related Questions