From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 61512382E832 for ; Mon, 7 Jun 2021 21:23:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 61512382E832 Received: by mail-lj1-x234.google.com with SMTP id e2so24158346ljk.4 for ; Mon, 07 Jun 2021 14:23:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iV4IKki8WEkPz+zS7qfWJaY4c7szP5p7kf5yRe7VBW0=; b=bt5N48O7B8bKBFP3LIgAm/HMhqMgrTBHBFDmDO2IAhVeFdO2qNg1vOBE+k84rfBrwA HXtF7l/rvNmdWwBy47ol6FrO/T9PqQRsuYaI8Q90uDzWmkGU/UEWGaEcGnfQR4uHjwWg 2ufst8QfELT2H9bC9w02M0/x2quC8ESVRfztb8ZxsCGM7SRwz0XrhgMrID72ewTchoCO GCQvz0uRFq48pWq/UT0xBwP4/w58IrAngo9OMzc6LZ8qc3T1BVM8KII9U6v0VHZ59FbD XqssLQDeQC7zOJ+M1yWXYqVZWTToUs+WHd4R84szXUkrEi4yOZpZQINNRRjZZ7wzrpOq f3SA== X-Gm-Message-State: AOAM532hIvDeuq9mftxMnHcj8/cDFQddKDQNyjjS3iMLVDx3fmSiO7DD Gxlgqdi2YkgmcWzzSJac1hYsXl8WpM4nPZ0E7tUneFRAYMyVKw== X-Google-Smtp-Source: ABdhPJyqWCqf9FX4k2KqIn5ARuc+55AEOdLLP1M40H6x146WcsCP6JXDAoZp7Ke4VJ/pEfjoG1A5a0HwmglvFoN7W/Q= X-Received: by 2002:a2e:8e99:: with SMTP id z25mr16584688ljk.146.1623100998952; Mon, 07 Jun 2021 14:23:18 -0700 (PDT) MIME-Version: 1.0 References: <7298cb72-becb-80bb-b2df-d97bdb201e95@linaro.org> In-Reply-To: <7298cb72-becb-80bb-b2df-d97bdb201e95@linaro.org> From: Godmar Back Date: Mon, 7 Jun 2021 17:23:07 -0400 Message-ID: Subject: Re: supporting terminal ownership assignment (tcsetpgrp()) in posix_spawn To: Adhemerval Zanella Cc: Libc-help X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jun 2021 21:23:23 -0000 On Mon, Jun 7, 2021 at 5:04 PM Adhemerval Zanella < adhemerval.zanella@linaro.org> wrote: > > > On 03/06/2021 10:58, Godmar Back via Libc-help wrote: > > I've recently been playing with posix_spawn() and noticed that it lacks > > support for assigning terminal ownership (as would be done via > > tcsetpgrp()). As a consequence, it cannot be used for job control shells > > when starting jobs in the foreground [1]. > > > > IBM's implementation of spawn [2] for zOS supports SPAWN_SETTCPGRP for > this > > reason. > > > > Even though the POSIX spec states "Future Directions: None" I came across > > this Austin group issue [3] which proposes to add a way to set the child > > process's session id, and which appears to have been accepted in 2016. > This > > is an example of evolution of posix_spawn. > > > > Is anyone aware of efforts to add something similar to POSIX, and more > > specifically, to Linux? > > > > If the current implementation of posix_spawn is library based (I'm > guessing > > it is) then there would be nothing to prevent Linux from adding > additional > > flags, just like QNX or Blackberry have already done ([3]). > > Other OS might provide a syscall for posix_spawn (I know MacOSX does and > I think maybe Solaris as well), but for Linux is done on top of clone > syscall. > > And glibc alread supports POSIX_SPAWN_SETSID since 2.26, so why can't you > use it along with with tcsetpgrp() called from the parent? SETSID sets a new session, which is a different concept than the foreground process group of a controlling terminal. tcsetpgrp() informs the OS which process group shall be the foreground process group of the terminal to which it is applied. However, this can't be called by the parent since the parent doesn't know the process group id of the process that will be spawned by the call to posix_spawn(). This process id (and the corresponding process group id) is known to the parent only after the call to posix_spawn() was made (and has returned). By that point, it's too late to set the controlling terminal's foreground process group. Specifically, by the time posix_spawn returns, the child process may have already acted under the assumption that it has ownership of the terminal. Since the child process is in a separate process group (because POSIX_SPAWN_SETPGROUP was given to posix_spawn()), this may lead to the suspension of the child process via SIGTTIN/SIGTTOU. Thus, calling tcsetpgrp() from the parent after the fact cannot be done in a race-free fashion, one needs to ensure that it's done after placing the child in a new process group but before exec'ing the child's executable. In Linux's library-based implementation of posix_spawn(), support for POSIX_SPAWN_TCSETPGROUP could be easily added - my question is why hasn't it? - Godmar