From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 585A63858C74 for ; Thu, 27 Jan 2022 10:49:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 585A63858C74 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-321-HhUkBrCCMji8iSa48iHjFg-1; Thu, 27 Jan 2022 05:49:26 -0500 X-MC-Unique: HhUkBrCCMji8iSa48iHjFg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B562F1091DA4 for ; Thu, 27 Jan 2022 10:49:25 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 19D25110F93 for ; Thu, 27 Jan 2022 10:49:24 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: Re: [glibc] posix: Add terminal control setting support for posix_spawn References: <20220125180124.B88AC3857C70@sourceware.org> Date: Thu, 27 Jan 2022 11:49:17 +0100 In-Reply-To: <20220125180124.B88AC3857C70@sourceware.org> (Adhemerval Zanella via Glibc-cvs's message of "Tue, 25 Jan 2022 18:01:24 +0000 (GMT)") Message-ID: <87bkzxpi6q.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Jan 2022 10:49:29 -0000 * Adhemerval Zanella via Glibc-cvs: > commit 342cc934a3bf74ac618e2318d738f22ac93257ba > Author: Adhemerval Zanella > Date: Mon Jun 14 14:41:31 2021 -0300 > > posix: Add terminal control setting support for posix_spawn > =20 > Currently there is no proper way to set the controlling terminal thro= ugh > posix_spawn in race free manner [1]. This forces shell implementatio= ns > to keep using fork+exec when launching background process groups, > even when using posix_spawn yields better performance. > =20 > This patch adds a new GNU extension so the creating process can > configure the created process terminal group. This is done with a ne= w > 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 t= o > the controlling terminal. > =20 > 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). > =20 > To create a process and set the controlling terminal, one can use the > following sequence: > =20 > posix_spawnattr_t attr; > posix_spawnattr_init (&attr); > posix_spawnattr_setflags (&attr, POSIX_SPAWN_TCSETPGROUP); > posix_spawnattr_tcsetpgrp_np (&attr, tcfd); > =20 > If the idea is also to create a new process groups: > =20 > 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); Sorry, I did not look at this in detail so far. A file descriptor outside the file actions API makes me quite nervous. Advanced Programming in the UNIX Environment says (like POSIX), =E2=80=9CIf= the process has a controlling terminal, the process can call tcsetpgrp to set the foreground process group ID [=E2=80=A6]=E2=80=9D. This suggests to= me that a file action may be required to establish a controlling terminal. Callers of posix_spawn might restrict that to the spawned process. As far as I know, Linux follows SVR4, and opening the terminal device under /dev/pts/ in a session leader that has no controlling terminal establishes one. This translate to a posix_spawn file action. So the action established by posix_spawnattr_tcsetpgrp_np must come after the file actions. But the descriptor used by posix_spawnattr_tcsetpgrp_np must still be open. So it would have to come before a closefrom file action (or a regular close). I think this means that the action established by posix_spawnattr_tcsetpgrp_np needs to be sequenced among the file actions, and needs to be a file action itself. Thanks, Florian