From 342cc934a3bf74ac618e2318d738f22ac93257ba Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Mon, 14 Jun 2021 14:41:31 -0300 Subject: posix: Add terminal control setting support for posix_spawn Currently there is no proper way to set the controlling terminal through posix_spawn in race free manner [1]. This forces shell implementations to keep using fork+exec when launching background process groups, even when using posix_spawn yields better performance. This patch adds a new GNU extension so the creating process can configure the created process terminal group. This is done with a new flag, POSIX_SPAWN_TCSETPGROUP, along with two new attribute functions: posix_spawnattr_tcsetpgrp_np, and posix_spawnattr_tcgetpgrp_np. The function sets a new attribute, spawn-tcgroupfd, that references to the controlling terminal. The controlling terminal is set after the spawn-pgroup attribute, and uses the spawn-tcgroupfd along with current creating process group (so it is composable with POSIX_SPAWN_SETPGROUP). To create a process and set the controlling terminal, one can use the following sequence: posix_spawnattr_t attr; posix_spawnattr_init (&attr); posix_spawnattr_setflags (&attr, POSIX_SPAWN_TCSETPGROUP); posix_spawnattr_tcsetpgrp_np (&attr, tcfd); If the idea is also to create a new process groups: posix_spawnattr_t attr; posix_spawnattr_init (&attr); posix_spawnattr_setflags (&attr, POSIX_SPAWN_TCSETPGROUP | POSIX_SPAWN_SETPGROUP); posix_spawnattr_tcsetpgrp_np (&attr, tcfd); posix_spawnattr_setpgroup (&attr, 0); The controlling terminal file descriptor is ignored if the new flag is not set. This interface is slight different than the one provided by QNX [2], which only provides the POSIX_SPAWN_TCSETPGROUP flag. The QNX documentation does not specify how the controlling terminal is obtained nor how it iteracts with POSIX_SPAWN_SETPGROUP. Since a glibc implementation is library based, it is more straightforward and avoid requires additional file descriptor operations to request the caller to setup the controlling terminal file descriptor (and it also allows a bit less error handling by posix_spawn). Checked on x86_64-linux-gnu and i686-linux-gnu. [1] https://github.com/ksh93/ksh/issues/79 [2] https://www.qnx.com/developers/docs/7.0.0/index.html#com.qnx.doc.neutrino.lib_ref/topic/p/posix_spawn.html Reviewed-by: Carlos O'Donell Tested-by: Carlos O'Donell --- termios/tcsetpgrp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'termios') diff --git a/termios/tcsetpgrp.c b/termios/tcsetpgrp.c index 581328b12a..1e4ff0987d 100644 --- a/termios/tcsetpgrp.c +++ b/termios/tcsetpgrp.c @@ -21,7 +21,7 @@ /* Set the foreground process group ID of FD set PGRP_ID. */ int -tcsetpgrp (int fd, pid_t pgrp_id) +__tcsetpgrp (int fd, pid_t pgrp_id) { if (fd < 0) { @@ -32,6 +32,7 @@ tcsetpgrp (int fd, pid_t pgrp_id) __set_errno (ENOSYS); return -1; } - +weak_alias (__tcsetpgrp, tcsetpgrp); +libc_hidden_def (__tcsetpgrp) stub_warning (tcsetpgrp) -- cgit 1.4.1