robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesrefactor(os): libuv_proc_stdio #40506d303617132

master
6.3 KiB196 linesraw
1#include <assert.h>
2#include <locale.h>
3#include <stdint.h>
4#include <uv.h>
5
6#include "nvim/eval/typval.h"
7#include "nvim/event/defs.h"
8#include "nvim/event/libuv_proc.h"
9#include "nvim/event/loop.h"
10#include "nvim/event/proc.h"
11#include "nvim/log.h"
12#include "nvim/os/os.h"
13#include "nvim/os/os_defs.h"
14#include "nvim/path.h"
15#include "nvim/types_defs.h"
16#include "nvim/ui_client.h"
17
18#include "event/libuv_proc.c.generated.h"
19
20/// Configures a stdio slot (`idx`) before spawning a process: connects the parent end to
21/// `parent_pipe` and sets up the fd/pipe the child inherits. `child_readable` is true for the
22/// child's stdin (the child reads), false for stdout/stderr (the child writes).
23///
24/// On Windows, `win_create_pipe` requests a non-inherited pipe (UV_CREATE_PIPE).
25/// - stdout always needs this (IOCP);
26/// - channel jobs also set it for stdin/stderr.
27/// - NOTE(!): libuv sets CREATE_NO_WINDOW only when *all* stdio slots are non-UV_INHERIT_FD!
28///   A single UV_INHERIT_FD slot re-attaches child => leaks CON writes to TUI #40074.
29///   https://github.com/libuv/libuv/blob/601a1537bb5628398c2389efbc7eecd062e8aac2/src/win/process.c#L1032-L1041
30///
31/// Records any fd the parent must close after spawn in `to_close[idx]`.
32static void libuv_proc_stdio(LibuvProc *uvproc, int idx, uv_pipe_t *parent_pipe,
33                             bool child_readable, bool overlapped, bool win_create_pipe,
34                             int *to_close)
35{
36#ifdef MSWIN
37  if (win_create_pipe) {
38    uvproc->uvstdio[idx].flags = UV_CREATE_PIPE
39                                 | (child_readable ? UV_READABLE_PIPE : UV_WRITABLE_PIPE);
40    if (overlapped) {
41      // Pipe must also be readable for IOCP to work on Windows.
42      uvproc->uvstdio[idx].flags |= UV_OVERLAPPED_PIPE | UV_READABLE_PIPE;
43    }
44    uvproc->uvstdio[idx].data.stream = (uv_stream_t *)parent_pipe;
45    return;
46  }
47#endif
48
49  // Inherited-fd pipe: create a uv_pipe() pair, hand one end to child via UV_INHERIT_FD and keep
50  // other for parent.
51  //
52  // On non-Windows uv_pipe() is preferred over UV_CREATE_PIPE: as of libuv 1.51, UV_CREATE_PIPE
53  // uses socketpair() (behaves confusingly on Linux: breaks /proc/<pid>/fd/0, which the Linux
54  // socket maintainer disowned).
55  int child_flags = 0;
56#ifdef MSWIN
57  // Overlapped child stdin must be non-blocking.
58  child_flags = (child_readable && overlapped) ? UV_NONBLOCK_PIPE : 0;
59#endif
60  // pipe_pair[0] is the read end, pipe_pair[1] the write end; the parent end is non-blocking.
61  uv_file pipe_pair[2];
62  uv_pipe(pipe_pair,
63          child_readable ? child_flags : UV_NONBLOCK_PIPE,
64          child_readable ? UV_NONBLOCK_PIPE : child_flags);
65
66  // child_readable: child reads pipe_pair[0], parent writes pipe_pair[1].
67  // else:           child writes pipe_pair[1], parent reads pipe_pair[0].
68  int child_fd = child_readable ? pipe_pair[0] : pipe_pair[1];
69  int parent_fd = child_readable ? pipe_pair[1] : pipe_pair[0];
70  uvproc->uvstdio[idx].flags = UV_INHERIT_FD;
71  uvproc->uvstdio[idx].data.fd = child_fd;
72  to_close[idx] = child_fd;
73  uv_pipe_open(parent_pipe, parent_fd);
74}
75
76/// @returns zero on success, or negative error code
77int libuv_proc_spawn(LibuvProc *uvproc)
78  FUNC_ATTR_NONNULL_ALL
79{
80  Proc *proc = (Proc *)uvproc;
81  uvproc->uvopts.file = proc_get_exepath(proc);
82  uvproc->uvopts.args = proc->argv;
83  uvproc->uvopts.flags = UV_PROCESS_WINDOWS_HIDE;
84#ifdef MSWIN
85  // libuv collapses the argv to a CommandLineToArgvW()-style string. cmd.exe
86  // expects a different syntax (must be prepared by the caller before now).
87  if (os_shell_is_cmdexe(proc->argv[0])) {
88    uvproc->uvopts.flags |= UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
89    // cmd.exe compatibility: backslashes required for path.
90    TO_BACKSLASH(proc->argv[0]);
91  }
92  if (proc->detach) {
93    uvproc->uvopts.flags |= UV_PROCESS_DETACHED;
94  }
95#else
96  // Always setsid() on unix-likes. #8107
97  uvproc->uvopts.flags |= UV_PROCESS_DETACHED;
98#endif
99  uvproc->uvopts.exit_cb = exit_cb;
100  uvproc->uvopts.cwd = proc->cwd;
101
102  uvproc->uvopts.stdio = uvproc->uvstdio;
103  uvproc->uvopts.stdio_count = 3;
104  uvproc->uvstdio[0].flags = UV_IGNORE;
105  uvproc->uvstdio[1].flags = UV_IGNORE;
106  uvproc->uvstdio[2].flags = UV_IGNORE;
107
108  if (ui_client_forward_stdin) {
109    assert(UI_CLIENT_STDIN_FD == 3);
110    uvproc->uvopts.stdio_count = 4;
111    uvproc->uvstdio[3].data.fd = 0;
112    uvproc->uvstdio[3].flags = UV_INHERIT_FD;
113  }
114  uvproc->uv.data = proc;
115
116  if (proc->env) {
117    uvproc->uvopts.env = tv_dict_to_env(proc->env);
118  } else {
119    uvproc->uvopts.env = NULL;
120  }
121
122  int to_close[3] = { -1, -1, -1 };
123
124  if (!proc->in.closed) {
125    libuv_proc_stdio(uvproc, 0, &proc->in.uv.pipe, true, proc->overlapped, proc->stdio_noinherit,
126                     to_close);
127  }
128
129  if (!proc->out.s.closed) {
130    // Windows: stdout always uses a non-inherited pipe (IOCP).
131    libuv_proc_stdio(uvproc, 1, &proc->out.s.uv.pipe, false, proc->overlapped, true, to_close);
132  }
133
134  if (!proc->err.s.closed) {
135    libuv_proc_stdio(uvproc, 2, &proc->err.s.uv.pipe, false, proc->overlapped,
136                     proc->stdio_noinherit, to_close);
137  } else if (proc->fwd_err) {
138    uvproc->uvstdio[2].flags = UV_INHERIT_FD;
139    uvproc->uvstdio[2].data.fd = STDERR_FILENO;
140  }
141
142  int status;
143  if ((status = uv_spawn(&proc->loop->uv, &uvproc->uv, &uvproc->uvopts))) {
144    ILOG("uv_spawn(%s) failed: %s", uvproc->uvopts.file, uv_strerror(status));
145    if (uvproc->uvopts.env) {
146      os_free_fullenv(uvproc->uvopts.env);
147    }
148    goto exit;
149  }
150
151  proc->pid = uvproc->uv.pid;
152exit:
153  for (int i = 0; i < 3; i++) {
154    if (to_close[i] > -1) {
155      close(to_close[i]);
156    }
157  }
158  return status;
159}
160
161void libuv_proc_close(LibuvProc *uvproc)
162  FUNC_ATTR_NONNULL_ARG(1)
163{
164  uv_close((uv_handle_t *)&uvproc->uv, close_cb);
165}
166
167static void close_cb(uv_handle_t *handle)
168{
169  Proc *proc = handle->data;
170  if (proc->internal_close_cb) {
171    proc->internal_close_cb(proc);
172  }
173  LibuvProc *uvproc = (LibuvProc *)proc;
174  if (uvproc->uvopts.env) {
175    os_free_fullenv(uvproc->uvopts.env);
176  }
177}
178
179static void exit_cb(uv_process_t *handle, int64_t status, int term_signal)
180{
181  Proc *proc = handle->data;
182#ifdef MSWIN
183  // Use stored/expected signal.
184  term_signal = proc->exit_signal;
185#endif
186  proc->status = term_signal ? 128 + term_signal : (int)status;
187  proc->internal_exit_cb(proc);
188}
189
190LibuvProc libuv_proc_init(Loop *loop, void *data)
191{
192  LibuvProc rv = {
193    .proc = proc_init(loop, kProcTypeUv, data)
194  };
195  return rv;
196}