From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) by sourceware.org (Postfix) with ESMTPS id 14084394741B for ; Thu, 27 Jan 2022 11:39:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 14084394741B Received: by mail-oi1-x22b.google.com with SMTP id q186so5291366oih.8 for ; Thu, 27 Jan 2022 03:39:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=tGBW5f+uPGvovg/SHeJqOPs2YVRl8t3k2ulYwFch7XM=; b=A/Hgg+mFHtfvYoQSmWWRS5XK74tS2aFKJHOSC0gFa56LRKI7CbcyfshM6J2IC7wHkf E+ZwIMtnmlYdFpjJb8N0Lcouiq8ZGog+VzNjz/Ms9DY1faMz1P/ztrCDVZwerG68IQm6 g4BU5dxmlYffXXZ0Rl8Ax1YaVDErWxQtCEUGB2C0ESGuPJ+LyesD6yPi7OLtDveZMPt2 o90wt1f5ZxPjzPS0Px9AjLGCiAoIb/d2ZnArA20R1baxx1QeH9wvDX66iDFO59MB1UZZ SHm5bHbRXhiyp+TsA7scp4PnaYUCzh6eBNYzFhH1Qes3YW8VJ3+8TjWKsOBzoNYt22TJ 1s3Q== X-Gm-Message-State: AOAM533Ilg+yo5oor4+HoWL+iDMSXwbtKjFuQyrkGHIHWXEBgFtxpC8Z sZa+J1TqBhvqJoV8aq5gKzUzE+A6iegTUQ== X-Google-Smtp-Source: ABdhPJyTCYI3mN4WYRsh8tn80GEPGqnjRcoW4LPBKrYntcfBK4cl+p9KP+/FdHi/kikswaXxuydu/g== X-Received: by 2002:a05:6808:309d:: with SMTP id bl29mr1959329oib.300.1643283541421; Thu, 27 Jan 2022 03:39:01 -0800 (PST) Received: from ?IPV6:2804:431:c7cb:27f8:865d:8f66:952e:4ca2? ([2804:431:c7cb:27f8:865d:8f66:952e:4ca2]) by smtp.gmail.com with ESMTPSA id q4sm9570773otk.39.2022.01.27.03.39.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Jan 2022 03:39:01 -0800 (PST) Message-ID: Date: Thu, 27 Jan 2022 08:38:58 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.1 Subject: Re: [glibc] posix: Add terminal control setting support for posix_spawn Content-Language: en-US To: Florian Weimer , libc-alpha@sourceware.org References: <20220125180124.B88AC3857C70@sourceware.org> <87bkzxpi6q.fsf@oldenburg.str.redhat.com> From: Adhemerval Zanella In-Reply-To: <87bkzxpi6q.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 11:39:03 -0000 On 27/01/2022 07:49, Florian Weimer via Libc-alpha wrote: > * 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 >> >> 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); > > 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), “If the > process has a controlling terminal, the process can call tcsetpgrp to > set the foreground process group ID […]”. 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 > I did not considered the closefrom in conjunction with closefrom file action and making a file action does make sense indeed. Maybe a better approach would be to remove posix_spawnattr_tcsetpgrp_np and posix_spawnattr_tcsetpgrp_np and add: int posix_spawn_file_actions_tcsetgrp_np (posix_spawn_file_actions_t *, int fd); Similar to posix_spawn_file_actions_addfchdir_np. So to create a process and set the controlling terminal, one can use the following sequence: int tcfd = open (_PATH_TTY, O_RDONLY, 0600); posix_spawnattr_t attr; posix_spawnattr_init (&attr); posix_spawnattr_setflags (&attr, POSIX_SPAWN_TCSETPGROUP); posix_spawn_file_actions_t actions; posix_spawn_file_actions_init (&actions); posix_spawn_file_actions_tcsetgrp_np (&actions, tcfd); So users would be able to add a posix_spawn_file_actions_addclose or posix_spawn_file_actions_addclosefrom_np.