From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 08C2E3857811; Thu, 19 May 2022 10:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 08C2E3857811 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: make sure exec'ed process exists early in process list X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/cygwin-3_3-branch X-Git-Oldrev: ea243efe235e28f4782fb6f5cf3e99048f82fc05 X-Git-Newrev: a8629d6eaf4695d353b73f9d88aa123fedc2827a Message-Id: <20220519100633.08C2E3857811@sourceware.org> Date: Thu, 19 May 2022 10:06:33 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 10:06:33 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Da8629d6eaf4= 695d353b73f9d88aa123fedc2827a commit a8629d6eaf4695d353b73f9d88aa123fedc2827a Author: Corinna Vinschen Date: Thu May 19 10:46:33 2022 +0200 Cygwin: make sure exec'ed process exists early in process list =20 killpg(pgid, 0) (or kill_pgrp(pgid, si_signo=3D0), in signal.cc) fails (returns -1) even when there is a process in the process group pgid, if the process is in the middle of spawnve(), see =20 https://cygwin.com/pipermail/cygwin/2022-May/251479.html =20 When exec'ing a process the assumption is that the exec'ed process crea= tes its own symlink (in pinfo::thisproc() in pinfo.cc). If the exec'ing process calls NtClose on it's own winpid symlink, but the exec'ed process didn't progress enough into initialization, there's a slim chance that neither the exec'ing process, nor the exec'ed process has a winpid symlink attached. =20 Always create the winpid symlink in spawn.cc, even for exec'ed Cygwin processes. Make sure to dup the handle into the new process, and stop creating the winpid symlink in exec'ed processes. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/pinfo.cc | 6 ++++-- winsup/cygwin/release/3.3.6 | 4 ++++ winsup/cygwin/spawn.cc | 15 ++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 5e04ea3da..a16e2198a 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -55,10 +55,11 @@ void pinfo::thisproc (HANDLE h) { procinfo =3D NULL; + bool execed =3D !!h; =20 DWORD flags =3D PID_IN_USE | PID_ACTIVE; /* Forked process or process started from non-Cygwin parent needs a pid.= */ - if (!h) + if (!execed) { cygheap->pid =3D create_cygwin_pid (); flags |=3D PID_NEW; @@ -72,7 +73,8 @@ pinfo::thisproc (HANDLE h) procinfo->dwProcessId =3D myself_initial.dwProcessId; procinfo->sendsig =3D myself_initial.sendsig; wcscpy (procinfo->progname, myself_initial.progname); - create_winpid_symlink (); + if (!execed) + create_winpid_symlink (); procinfo->exec_sendsig =3D NULL; procinfo->exec_dwProcessId =3D 0; debug_printf ("myself dwProcessId %u", procinfo->dwProcessId); diff --git a/winsup/cygwin/release/3.3.6 b/winsup/cygwin/release/3.3.6 index 6d1f0bc8b..6d722433f 100644 --- a/winsup/cygwin/release/3.3.6 +++ b/winsup/cygwin/release/3.3.6 @@ -3,3 +3,7 @@ Bug Fixes =20 - Fix an issue that command "cmd /c script -c cmd" crashes if it is issued in console of Windows 7. + +- Fix killpg failing because the exec'ing as well as the exec'ed + process are not in the pidlist for a brief moment. + Addresses: https://cygwin.com/pipermail/cygwin/2022-May/251479.html diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc index c9e1fb6d2..e0f1247e1 100644 --- a/winsup/cygwin/spawn.cc +++ b/winsup/cygwin/spawn.cc @@ -862,13 +862,14 @@ child_info_spawn::worker (const char *prog_arg, const= char *const *argv, strace.execing =3D 1; myself.hProcess =3D hExeced =3D pi.hProcess; HANDLE old_winpid_hdl =3D myself.shared_winpid_handle (); - if (!real_path.iscygexec ()) - { - /* If the child process is not a Cygwin process, we have to - create a new winpid symlink on behalf of the child process - not being able to do this by itself. */ - myself.create_winpid_symlink (); - } + /* We have to create a new winpid symlink on behalf of the child + process. For Cygwin processes we also have to create a reference + in the child. */ + myself.create_winpid_symlink (); + if (real_path.iscygexec ()) + DuplicateHandle (GetCurrentProcess (), + myself.shared_winpid_handle (), + pi.hProcess, NULL, 0, 0, DUPLICATE_SAME_ACCESS); NtClose (old_winpid_hdl); real_path.get_wide_win32_path (myself->progname); // FIXME: race? sigproc_printf ("new process name %W", myself->progname);