Blame docs/src/process.rst

Packit Service 7c31a4
Packit Service 7c31a4
.. _process:
Packit Service 7c31a4
Packit Service 7c31a4
:c:type:`uv_process_t` --- Process handle
Packit Service 7c31a4
=========================================
Packit Service 7c31a4
Packit Service 7c31a4
Process handles will spawn a new process and allow the user to control it and
Packit Service 7c31a4
establish communication channels with it using streams.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Data types
Packit Service 7c31a4
----------
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_process_t
Packit Service 7c31a4
Packit Service 7c31a4
    Process handle type.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_process_options_t
Packit Service 7c31a4
Packit Service 7c31a4
    Options for spawning the process (passed to :c:func:`uv_spawn`.
Packit Service 7c31a4
Packit Service 7c31a4
    ::
Packit Service 7c31a4
Packit Service 7c31a4
        typedef struct uv_process_options_s {
Packit Service 7c31a4
            uv_exit_cb exit_cb;
Packit Service 7c31a4
            const char* file;
Packit Service 7c31a4
            char** args;
Packit Service 7c31a4
            char** env;
Packit Service 7c31a4
            const char* cwd;
Packit Service 7c31a4
            unsigned int flags;
Packit Service 7c31a4
            int stdio_count;
Packit Service 7c31a4
            uv_stdio_container_t* stdio;
Packit Service 7c31a4
            uv_uid_t uid;
Packit Service 7c31a4
            uv_gid_t gid;
Packit Service 7c31a4
        } uv_process_options_t;
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal)
Packit Service 7c31a4
Packit Service 7c31a4
    Type definition for callback passed in :c:type:`uv_process_options_t` which
Packit Service 7c31a4
    will indicate the exit status and the signal that caused the process to
Packit Service 7c31a4
    terminate, if any.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_process_flags
Packit Service 7c31a4
Packit Service 7c31a4
    Flags to be set on the flags field of :c:type:`uv_process_options_t`.
Packit Service 7c31a4
Packit Service 7c31a4
    ::
Packit Service 7c31a4
Packit Service 7c31a4
        enum uv_process_flags {
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Set the child process' user id.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_SETUID = (1 << 0),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Set the child process' group id.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_SETGID = (1 << 1),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Do not wrap any arguments in quotes, or perform any other escaping, when
Packit Service 7c31a4
            * converting the argument list into a command line string. This option is
Packit Service 7c31a4
            * only meaningful on Windows systems. On Unix it is silently ignored.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Spawn the child process in a detached state - this will make it a process
Packit Service 7c31a4
            * group leader, and will effectively enable the child to keep running after
Packit Service 7c31a4
            * the parent exits. Note that the child process will still keep the
Packit Service 7c31a4
            * parent's event loop alive unless the parent process calls uv_unref() on
Packit Service 7c31a4
            * the child's process handle.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_DETACHED = (1 << 3),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Hide the subprocess window that would normally be created. This option is
Packit Service 7c31a4
            * only meaningful on Windows systems. On Unix it is silently ignored.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_WINDOWS_HIDE = (1 << 4),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Hide the subprocess console window that would normally be created. This 
Packit Service 7c31a4
            * option is only meaningful on Windows systems. On Unix it is silently
Packit Service 7c31a4
            * ignored.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_WINDOWS_HIDE_CONSOLE = (1 << 5),
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * Hide the subprocess GUI window that would normally be created. This 
Packit Service 7c31a4
            * option is only meaningful on Windows systems. On Unix it is silently
Packit Service 7c31a4
            * ignored.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_PROCESS_WINDOWS_HIDE_GUI = (1 << 6)
Packit Service 7c31a4
        };
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_stdio_container_t
Packit Service 7c31a4
Packit Service 7c31a4
    Container for each stdio handle or fd passed to a child process.
Packit Service 7c31a4
Packit Service 7c31a4
    ::
Packit Service 7c31a4
Packit Service 7c31a4
        typedef struct uv_stdio_container_s {
Packit Service 7c31a4
            uv_stdio_flags flags;
Packit Service 7c31a4
            union {
Packit Service 7c31a4
                uv_stream_t* stream;
Packit Service 7c31a4
                int fd;
Packit Service 7c31a4
            } data;
Packit Service 7c31a4
        } uv_stdio_container_t;
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_stdio_flags
Packit Service 7c31a4
Packit Service 7c31a4
    Flags specifying how a stdio should be transmitted to the child process.
Packit Service 7c31a4
Packit Service 7c31a4
    ::
Packit Service 7c31a4
Packit Service 7c31a4
        typedef enum {
Packit Service 7c31a4
            UV_IGNORE = 0x00,
Packit Service 7c31a4
            UV_CREATE_PIPE = 0x01,
Packit Service 7c31a4
            UV_INHERIT_FD = 0x02,
Packit Service 7c31a4
            UV_INHERIT_STREAM = 0x04,
Packit Service 7c31a4
            /*
Packit Service 7c31a4
            * When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
Packit Service 7c31a4
            * determine the direction of flow, from the child process' perspective. Both
Packit Service 7c31a4
            * flags may be specified to create a duplex data stream.
Packit Service 7c31a4
            */
Packit Service 7c31a4
            UV_READABLE_PIPE = 0x10,
Packit Service 7c31a4
            UV_WRITABLE_PIPE = 0x20
Packit Service 7c31a4
            /*
Packit Service 7c31a4
             * Open the child pipe handle in overlapped mode on Windows.
Packit Service 7c31a4
             * On Unix it is silently ignored.
Packit Service 7c31a4
             */
Packit Service 7c31a4
            UV_OVERLAPPED_PIPE = 0x40
Packit Service 7c31a4
        } uv_stdio_flags;
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Public members
Packit Service 7c31a4
^^^^^^^^^^^^^^
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_t.pid
Packit Service 7c31a4
Packit Service 7c31a4
    The PID of the spawned process. It's set after calling :c:func:`uv_spawn`.
Packit Service 7c31a4
Packit Service 7c31a4
.. note::
Packit Service 7c31a4
    The :c:type:`uv_handle_t` members also apply.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.exit_cb
Packit Service 7c31a4
Packit Service 7c31a4
    Callback called after the process exits.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.file
Packit Service 7c31a4
Packit Service 7c31a4
    Path pointing to the program to be executed.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.args
Packit Service 7c31a4
Packit Service 7c31a4
    Command line arguments. args[0] should be the path to the program. On
Packit Service 7c31a4
    Windows this uses `CreateProcess` which concatenates the arguments into a
Packit Service 7c31a4
    string this can cause some strange errors. See the
Packit Service 7c31a4
    ``UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS`` flag on :c:type:`uv_process_flags`.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.env
Packit Service 7c31a4
Packit Service 7c31a4
    Environment for the new process. If NULL the parents environment is used.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.cwd
Packit Service 7c31a4
Packit Service 7c31a4
    Current working directory for the subprocess.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.flags
Packit Service 7c31a4
Packit Service 7c31a4
    Various flags that control how :c:func:`uv_spawn` behaves. See
Packit Service 7c31a4
    :c:type:`uv_process_flags`.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.stdio_count
Packit Service 7c31a4
.. c:member:: uv_process_options_t.stdio
Packit Service 7c31a4
Packit Service 7c31a4
    The `stdio` field points to an array of :c:type:`uv_stdio_container_t`
Packit Service 7c31a4
    structs that describe the file descriptors that will be made available to
Packit Service 7c31a4
    the child process. The convention is that stdio[0] points to stdin,
Packit Service 7c31a4
    fd 1 is used for stdout, and fd 2 is stderr.
Packit Service 7c31a4
Packit Service 7c31a4
    .. note::
Packit Service 7c31a4
        On Windows file descriptors greater than 2 are available to the child process only if
Packit Service 7c31a4
        the child processes uses the MSVCRT runtime.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_process_options_t.uid
Packit Service 7c31a4
.. c:member:: uv_process_options_t.gid
Packit Service 7c31a4
Packit Service 7c31a4
    Libuv can change the child process' user/group id. This happens only when
Packit Service 7c31a4
    the appropriate bits are set in the flags fields.
Packit Service 7c31a4
Packit Service 7c31a4
    .. note::
Packit Service 7c31a4
        This is not supported on Windows, :c:func:`uv_spawn` will fail and set the error
Packit Service 7c31a4
        to ``UV_ENOTSUP``.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_stdio_container_t.flags
Packit Service 7c31a4
Packit Service 7c31a4
    Flags specifying how the stdio container should be passed to the child. See
Packit Service 7c31a4
    :c:type:`uv_stdio_flags`.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_stdio_container_t.data
Packit Service 7c31a4
Packit Service 7c31a4
    Union containing either the stream or fd to be passed on to the child
Packit Service 7c31a4
    process.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
API
Packit Service 7c31a4
---
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: void uv_disable_stdio_inheritance(void)
Packit Service 7c31a4
Packit Service 7c31a4
    Disables inheritance for file descriptors / handles that this process
Packit Service 7c31a4
    inherited from its parent. The effect is that child processes spawned by
Packit Service 7c31a4
    this process don't accidentally inherit these handles.
Packit Service 7c31a4
Packit Service 7c31a4
    It is recommended to call this function as early in your program as possible,
Packit Service 7c31a4
    before the inherited file descriptors can be closed or duplicated.
Packit Service 7c31a4
Packit Service 7c31a4
    .. note::
Packit Service 7c31a4
        This function works on a best-effort basis: there is no guarantee that libuv can discover
Packit Service 7c31a4
        all file descriptors that were inherited. In general it does a better job on Windows than
Packit Service 7c31a4
        it does on Unix.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_spawn(uv_loop_t* loop, uv_process_t* handle, const uv_process_options_t* options)
Packit Service 7c31a4
Packit Service 7c31a4
    Initializes the process handle and starts the process. If the process is
Packit Service 7c31a4
    successfully spawned, this function will return 0. Otherwise, the
Packit Service 7c31a4
    negative error code corresponding to the reason it couldn't spawn is
Packit Service 7c31a4
    returned.
Packit Service 7c31a4
Packit Service 7c31a4
    Possible reasons for failing to spawn would include (but not be limited to)
Packit Service 7c31a4
    the file to execute not existing, not having permissions to use the setuid or
Packit Service 7c31a4
    setgid specified, or not having enough memory to allocate for the new
Packit Service 7c31a4
    process.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionchanged:: 1.24.0 Added `UV_PROCESS_WINDOWS_HIDE_CONSOLE` and
Packit Service 7c31a4
                        `UV_PROCESS_WINDOWS_HIDE_GUI` flags.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_process_kill(uv_process_t* handle, int signum)
Packit Service 7c31a4
Packit Service 7c31a4
    Sends the specified signal to the given process handle. Check the documentation
Packit Service 7c31a4
    on :c:ref:`signal` for signal support, specially on Windows.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_kill(int pid, int signum)
Packit Service 7c31a4
Packit Service 7c31a4
    Sends the specified signal to the given PID. Check the documentation
Packit Service 7c31a4
    on :c:ref:`signal` for signal support, specially on Windows.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: uv_pid_t uv_process_get_pid(const uv_process_t* handle)
Packit Service 7c31a4
Packit Service 7c31a4
    Returns `handle->pid`.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionadded:: 1.19.0
Packit Service 7c31a4
Packit Service 7c31a4
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.