Esempio n. 1
0
        internal static PhpProcessHandle Validate(PhpResource resource)
        {
            PhpProcessHandle result = resource as PhpProcessHandle;

            if (result == null || !result.IsValid)
            {
                PhpException.Throw(PhpError.Warning, Resources.LibResources.invalid_process_resource);
                return(null);
            }

            return(result);
        }
Esempio n. 2
0
        //private sealed class ActivePipe
        //{
        //    private const int BufferSize = 1024;

        //    Stream stream;
        //    StreamAccessOptions access;
        //    PhpStream phpStream;
        //    public AsyncCallback callback;
        //    public byte[] buffer;

        //    public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no)
        //    {
        //        if (access == StreamAccessOptions.Read && !phpStream.CanWrite ||
        //          access == StreamAccessOptions.Write && !phpStream.CanRead)
        //        {
        //            PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_invalid_mode, desc_no.ToString());
        //            return false;
        //        }

        //        ActivePipe pipe = new ActivePipe();
        //        pipe.stream = stream;
        //        pipe.phpStream = phpStream;
        //        pipe.access = access;
        //        pipe.callback = new AsyncCallback(pipe.Callback);

        //        if (access == StreamAccessOptions.Read)
        //        {
        //            var buffer = new byte[BufferSize];
        //            stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null);
        //            pipe.buffer = buffer;
        //        }
        //        else
        //        {
        //            pipe.buffer = phpStream.ReadBytes(BufferSize);
        //            if (pipe.buffer != null)
        //                stream.BeginWrite(pipe.buffer, 0, pipe.buffer.Length, pipe.callback, null);
        //            else
        //                stream.Dispose();
        //        }

        //        return true;
        //    }

        //    private void Callback(IAsyncResult ar)
        //    {
        //        if (access == StreamAccessOptions.Read)
        //        {
        //            int count = stream.EndRead(ar);
        //            if (count > 0)
        //            {
        //                if (count != buffer.Length)
        //                {
        //                    // TODO: improve streams
        //                    var buf = new byte[count];
        //                    Buffer.BlockCopy(buffer, 0, buf, 0, count);
        //                    phpStream.WriteBytes(buf);
        //                }
        //                else
        //                {
        //                    phpStream.WriteBytes(buffer);
        //                }

        //                stream.BeginRead(buffer, 0, buffer.Length, callback, ar.AsyncState);
        //            }
        //            else
        //            {
        //                stream.Dispose();
        //            }
        //        }
        //        else
        //        {
        //            buffer = phpStream.ReadBytes(BufferSize);
        //            if (buffer != null)
        //            {
        //                stream.BeginWrite(buffer, 0, buffer.Length, callback, ar.AsyncState);
        //            }
        //            else
        //            {
        //                stream.EndWrite(ar);
        //                stream.Dispose();
        //            }
        //        }
        //    }
        //}

        #endregion

        #region proc_close, proc_get_status, proc_terminate

        public static int proc_close(PhpResource process)
        {
            PhpProcessHandle handle = PhpProcessHandle.Validate(process);

            if (handle == null)
            {
                return(-1);
            }

            var code = CloseProcess(handle.Process);

            handle.Dispose();
            return(code);
        }
Esempio n. 3
0
        public static int proc_terminate(PhpResource process, int signal = 255)
        {
            PhpProcessHandle handle = PhpProcessHandle.Validate(process);

            if (handle == null)
            {
                return(-1);
            }

            try
            {
                handle.Process.Kill();
            }
            catch (Exception e)
            {
                PhpException.Throw(PhpError.Warning,
                                   Resources.LibResources.error_terminating_process,
                                   handle.Process.ProcessName, handle.Process.Id.ToString(), e.Message);
                return(-1);
            }
            return(handle.Process.ExitCode);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="process"></param>
        /// <returns>
        /// <list type="bullet">
        /// <term>"command"</term><description>The command string that was passed to proc_open()</description>
        /// <term>"pid"</term><description>process id</description>
        /// <term>"running"</term><description>TRUE if the process is still running, FALSE if it has terminated</description>
        /// <term>"signaled"</term><description>TRUE if the child process has been terminated by an uncaught signal. Always set to FALSE on Windows.</description>
        /// <term>"stopped"</term><description>TRUE if the child process has been stopped by a signal. Always set to FALSE on Windows.</description>
        /// <term>"exitcode"</term><description>the exit code returned by the process (which is only meaningful if running is FALSE)</description>
        /// <term>"termsig"</term><description>the number of the signal that caused the child process to terminate its execution (only meaningful if signaled is TRUE)</description>
        /// <term>"stopsig"</term><description>the number of the signal that caused the child process to stop its execution (only meaningful if stopped is TRUE)</description>
        /// </list>
        /// </returns>
        public static PhpArray proc_get_status(PhpResource process)
        {
            PhpProcessHandle handle = PhpProcessHandle.Validate(process);

            if (handle == null)
            {
                return(null);
            }

            var result = new PhpArray(8)
            {
                { "command", handle.Command },
                { "pid", handle.Process.Id },
                { "running", !handle.Process.HasExited },
                { "signaled", false }, // UNIX
                { "stopped", false },  // UNIX
                { "exitcode", handle.Process.HasExited? handle.Process.ExitCode : -1 },
                { "termsig", 0 },      // UNIX
                { "stopsig", 0 },      // UNIX
            };

            return(result);
        }