From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 420273838036 for ; Tue, 8 Jun 2021 14:37:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 420273838036 Received: by mail-lj1-x229.google.com with SMTP id 131so27340600ljj.3 for ; Tue, 08 Jun 2021 07:37:19 -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=xed4Sw2JAbHfEPYJCrFm4hQz9BB/AXtkTmOFmfuZFVo=; b=EWlpdKfB1drqkKmLTH4SEU16QYbXDiLvF7hEV3fvDYTvS//p99WYd/a9mz7UNZ27wA +SYbFrQ0tCN+ePCXwJJp1KbFIQ8cDKaV8P6yD4pPd0Sde888c4VJd6aaK37KDlF1SdGH ReIEcbSw+RcLdYr1IYHZo6yajp26dDoxRvLltLrSfPNEsYWcHY+SOtkRj75mUZaMCXIt LnuFCn6oifnEoW6tKohOokFvh1SyyDq41a/B4L/DL/Mw8uM5DQQU7Sd1c1RJ7AHD2bsu LJXvqTAUWRP7xoBQKyc+ARC64prbAj4ZvGEum/1qMQd0J8108yX6c1qPC0bym3SXL5VO KCAA== X-Gm-Message-State: AOAM533IRbB9I/uJrZ9cKRBiN+KV/i6JGFtR5V0o4lHBBnd71ne7HYZa iUouqVbqxzQBp7TnoWcDTcjKO1iGJ+6U3j6MKJU= X-Google-Smtp-Source: ABdhPJw0gTYfvPArfCIvwAwjASX0/hMEzUCY4riKqdYDF6wz+aWiBjGmb72ijGkczrgoYg0ryzcxQck6v+JTfU8AhhM= X-Received: by 2002:a05:651c:201e:: with SMTP id s30mr11283739ljo.71.1623163038073; Tue, 08 Jun 2021 07:37:18 -0700 (PDT) MIME-Version: 1.0 References: <7298cb72-becb-80bb-b2df-d97bdb201e95@linaro.org> In-Reply-To: From: Godmar Back Date: Tue, 8 Jun 2021 10:37:06 -0400 Message-ID: Subject: Re: supporting terminal ownership assignment (tcsetpgrp()) in posix_spawn To: Adhemerval Zanella Cc: Libc-help X-Spam-Status: No, score=-0.6 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: Tue, 08 Jun 2021 14:37:21 -0000 On Tue, Jun 8, 2021 at 9:50 AM Adhemerval Zanella < adhemerval.zanella@linaro.org> wrote: > > So one options might be to add something as the one I suggested before, > but as generic extension instead of a file extension (which does not make > sense in fact): > > int posix_spawnattr_tcsetpgrp_np (posix_spawnattr_t *__attr, int fd, > pid_t pgrp); > > Similar to tcgetpgrp, it make the created process group with process > group > instructed by the PGRP argument the foreground process group on the > terminal > associated to FD. If PGRP is 0, the current group obtained with > getpgrp() > will be used, otherwise PGRP will be used. > > This is done after just after setting the process group ID > (POSIX_SPAWN_SETPGROUP) and right before setting the effective user and > group id (POSIX_SPAWN_RESETIDS). > > So if the called want to create a new session id, it can issue: > > posix_spawnattr_t attr; > posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETSID); > posix_spawnattr_tcsetpgrp_np (&attr, fd, 0); > > And the created process will issue the following in the order: > > setsid (); > tcsetpgrp (fd, getpgid (0)); > > After setsid(), the caller is a new session leader and a new process group leader, but it doesn't have a controlling terminal. (setsid(2) says: "The calling process will be the only process in the new process group and in the new session. Initially, the new session has no controlling terminal.") The man page of tcsetpgrp however states that the fd passed: "must be the controlling terminal of the calling process." So if you implemented it like that in a library, it should fail based on the description in the man pages. I'm not sure if POSIX_SPAWN_SETSID needs to be able to be combined with setting the terminal's foreground process group. Those are different use cases. In the case of setsid, the spawned process must be prepared to become a new session leader (open a new controlling terminal, etc.) so it would not generally assume to already be a member of the terminal's fg process group (since it doesn't have a controlling terminal yet). > > If the caller already has group it want to use, it can issue instead: > > posix_spawnattr_t attr; > posix_spawnattr_setpgroup (&attr, groupid); > posix_spawnattr_tcsetpgrp_np (&attr, fd, groupid); > > Which in turn will make the created process to issue: > > setpgid (0, groupid); > tcsetpgrp (fd, groupid); > For this use case, as long as it supports groupid == 0, this should work as it is what shells currently do. - Godmar