proc - PHP Script hangs when passing a param to process -
i have downloaded open-source terminal emulator github , modified little. class running process looks this:
<?php class cprocess { const proc_ok = true; const proc_failed = false; private static $instance = null; public static function factory() { if (!isset(self::$instance)) { self::$instance = new cprocess(); } return self::$instance; } private $pipes; private $process; private $stdinref; private $stdoutref; private $stderref; private function __construct() { } public function __destruct() { return $this->close(); } public function open($command) { $spec = array( array("pty"), // stdin array("pty"), // stdout array("pty") // stderr ); if (!$this->process = proc_open($command, $spec, $this->pipes)) { return self::proc_failed; } $this->stdinref = &$this->pipes[0]; $this->stdoutref = &$this->pipes[1]; $this->stderref = &$this->pipes[2]; $this->setblocking(0); return self::proc_ok; } public function isresource() { return is_resource($this->process); } public function setblocking($blocking = 1) { return stream_set_blocking($this->stdoutref, $blocking); } public function getstatus() { return proc_get_status($this->process); } public function get() { $out = stream_get_contents($this->stdoutref); return $out; } public function put($data) { fwrite($this->stdinref, $data . "\n"); sleep(1); fflush($this->stdinref); } public function close() { if (is_resource($this->process)) { fclose($this->stdinref); fclose($this->stdoutref); fclose($this->stderref); return proc_close($this->process); } } public function metadata() { return stream_get_meta_data($this->stdoutref); } }
now want authenticate login/password pair using following method:
public static function autenticate($login, $password) { if (true === debug_mode) { error_log("authenticate(): login={$login} password={$password}",0); } error_log("test"); self::$username = $login; $process = cprocess::factory(); $cmd = "su " . escapeshellarg($login); if (true === debug_mode) { error_log("authenticate(): cmd={$cmd}", 0); } $rc = $process->open($cmd); usleep(100000); // 500000 if ($rc === cprocess::proc_failed) { if (true === debug_mode) { error_log("authenticate(): process_open() returns {$rc}", 0); } return false; } if (true === debug_mode) { error_log("authenticate(): invoking process->put() param={$password}"); } $process->put($password); if (true === debug_mode) { error_log("authenticate(): process->put() returned value"); } usleep(100000); return (bool) !$process->close(); }
the problem script hangs when getting $process->put($password);
. browser shows 'waiting localhost' , lasts forever. in put();
method of class cprocess
tried php_eol
instead of \n
result same. what's wrong code?
the pipes using should more like, read/write type options...
$spec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", "/tmp/error-output.log", "a") );
Comments
Post a Comment