robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d303617132
master
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 , 33bool child_readable ,bool overlapped ,bool win_create_pipe , 34int * to_close ) 35{ 36#ifdef MSWIN 37if (win_create_pipe ) { 38uvproc -> uvstdio [idx ].flags = UV_CREATE_PIPE 39 | (child_readable ?UV_READABLE_PIPE :UV_WRITABLE_PIPE ); 40if (overlapped ) { 41// Pipe must also be readable for IOCP to work on Windows. 42uvproc -> uvstdio [idx ].flags |=UV_OVERLAPPED_PIPE |UV_READABLE_PIPE ; 43 } 44uvproc -> uvstdio [idx ].data .stream = (uv_stream_t * )parent_pipe ; 45return ; 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). 55int child_flags = 0 ; 56#ifdef MSWIN 57// Overlapped child stdin must be non-blocking. 58child_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. 61uv_file pipe_pair [2 ]; 62uv_pipe (pipe_pair , 63child_readable ?child_flags :UV_NONBLOCK_PIPE , 64child_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]. 68int child_fd = child_readable ?pipe_pair [0 ] :pipe_pair [1 ]; 69int parent_fd = child_readable ?pipe_pair [1 ] :pipe_pair [0 ]; 70uvproc -> uvstdio [idx ].flags = UV_INHERIT_FD ; 71uvproc -> uvstdio [idx ].data .fd = child_fd ; 72to_close [idx ]= child_fd ; 73uv_pipe_open (parent_pipe ,parent_fd ); 74} 75 76/// @returns zero on success, or negative error code 77int libuv_proc_spawn (LibuvProc * uvproc ) 78FUNC_ATTR_NONNULL_ALL 79{ 80Proc * proc = (Proc * )uvproc ; 81uvproc -> uvopts .file = proc_get_exepath (proc ); 82uvproc -> uvopts .args = proc -> argv ; 83uvproc -> 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). 87if (os_shell_is_cmdexe (proc -> argv [0 ])) { 88uvproc -> uvopts .flags |=UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS ; 89// cmd.exe compatibility: backslashes required for path. 90TO_BACKSLASH (proc -> argv [0 ]); 91 } 92if (proc -> detach ) { 93uvproc -> uvopts .flags |=UV_PROCESS_DETACHED ; 94 } 95#else 96// Always setsid() on unix-likes. #8107 97uvproc -> uvopts .flags |=UV_PROCESS_DETACHED ; 98#endif 99uvproc -> uvopts .exit_cb = exit_cb ; 100uvproc -> uvopts .cwd = proc -> cwd ; 101 102uvproc -> uvopts .stdio = uvproc -> uvstdio ; 103uvproc -> uvopts .stdio_count = 3 ; 104uvproc -> uvstdio [0 ].flags = UV_IGNORE ; 105uvproc -> uvstdio [1 ].flags = UV_IGNORE ; 106uvproc -> uvstdio [2 ].flags = UV_IGNORE ; 107 108if (ui_client_forward_stdin ) { 109assert (UI_CLIENT_STDIN_FD == 3 ); 110uvproc -> uvopts .stdio_count = 4 ; 111uvproc -> uvstdio [3 ].data .fd = 0 ; 112uvproc -> uvstdio [3 ].flags = UV_INHERIT_FD ; 113 } 114uvproc -> uv .data = proc ; 115 116if (proc -> env ) { 117uvproc -> uvopts .env = tv_dict_to_env (proc -> env ); 118 }else { 119uvproc -> uvopts .env = NULL ; 120 } 121 122int to_close [3 ]= {-1 ,-1 ,-1 }; 123 124if (!proc -> in .closed ) { 125libuv_proc_stdio (uvproc ,0 ,& proc -> in .uv .pipe , true,proc -> overlapped ,proc -> stdio_noinherit , 126to_close ); 127 } 128 129if (!proc -> out .s .closed ) { 130// Windows: stdout always uses a non-inherited pipe (IOCP). 131libuv_proc_stdio (uvproc ,1 ,& proc -> out .s .uv .pipe , false,proc -> overlapped , true,to_close ); 132 } 133 134if (!proc -> err .s .closed ) { 135libuv_proc_stdio (uvproc ,2 ,& proc -> err .s .uv .pipe , false,proc -> overlapped , 136proc -> stdio_noinherit ,to_close ); 137 }else if (proc -> fwd_err ) { 138uvproc -> uvstdio [2 ].flags = UV_INHERIT_FD ; 139uvproc -> uvstdio [2 ].data .fd = STDERR_FILENO ; 140 } 141 142int status ; 143if ((status = uv_spawn (& proc -> loop -> uv ,& uvproc -> uv ,& uvproc -> uvopts ))) { 144ILOG ("uv_spawn(%s) failed: %s" ,uvproc -> uvopts .file ,uv_strerror (status )); 145if (uvproc -> uvopts .env ) { 146os_free_fullenv (uvproc -> uvopts .env ); 147 } 148 gotoexit ; 149 } 150 151proc -> pid = uvproc -> uv .pid ; 152exit : 153for (int i = 0 ;i < 3 ;i ++ ) { 154if (to_close [i ]> -1 ) { 155close (to_close [i ]); 156 } 157 } 158return status ; 159} 160 161void libuv_proc_close (LibuvProc * uvproc ) 162FUNC_ATTR_NONNULL_ARG (1 ) 163{ 164uv_close ((uv_handle_t * )& uvproc -> uv ,close_cb ); 165} 166 167static void close_cb (uv_handle_t * handle ) 168{ 169Proc * proc = handle -> data ; 170if (proc -> internal_close_cb ) { 171proc -> internal_close_cb (proc ); 172 } 173LibuvProc * uvproc = (LibuvProc * )proc ; 174if (uvproc -> uvopts .env ) { 175os_free_fullenv (uvproc -> uvopts .env ); 176 } 177} 178 179static void exit_cb (uv_process_t * handle ,int64_t status ,int term_signal ) 180{ 181Proc * proc = handle -> data ; 182#ifdef MSWIN 183// Use stored/expected signal. 184term_signal = proc -> exit_signal ; 185#endif 186proc -> status = term_signal ?128 + term_signal : (int )status ; 187proc -> internal_exit_cb (proc ); 188} 189 190LibuvProc libuv_proc_init (Loop * loop ,void * data ) 191{ 192LibuvProc rv = { 193 .proc = proc_init (loop ,kProcTypeUv ,data ) 194 }; 195return rv ; 196}