From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by sourceware.org (Postfix) with ESMTPS id 6F5813838032 for ; Tue, 8 Jun 2021 16:42:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6F5813838032 Received: by mail-qk1-x72f.google.com with SMTP id k11so19175141qkk.1 for ; Tue, 08 Jun 2021 09:42:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=+hmZdHvKq2+dMKf+ETRENsv0qH1Oql+CYgZgYR8RYyg=; b=mYRlrQqzx6cJgfHrf1DRYMf1L7256YPCm1wwDjI1ZnB6IX/QQ5/TCEfVHqic8yLx2P hpvN7K9f+Z17k558S3EWJyXR4YG+ieewswWKJo6TWR8YaJe3mAA0VXTD3lkAJ9bgp4EU SE7epxpSm2SAfqnHYNzZOG/Ijlz310uO+ZNfnLZvfcTgUeXO3TgtdC8WIaaP0jAxv7RX BgHaERJaSjRsH7qJpqVPEujPdqe0X+JLJEhPkkcz79aMaM3PpST9f4lzTbEXM7BG1QbN rJROCnpz3u5bUCDVKh1icJm9bUgA07Q2f9k204Q/gA5EI8l6gONogS2ogwdFKZcwS4dy wZkw== X-Gm-Message-State: AOAM532Azx1Iw3mP7Rys8FaFOYf8j70P4Wa/63eBCZvjwi8GiKZR6QTy rNKWM58tKanKcKmU3Yij0Of/P+tQHkUTTQ== X-Google-Smtp-Source: ABdhPJzglqy0vJ1EmJA7thE+eOvlMourtpEuvvS+hu1RfMkGanMejE87j+pRzJWB0qlgh+bHri1Ylg== X-Received: by 2002:a05:620a:136a:: with SMTP id d10mr22088898qkl.422.1623170534885; Tue, 08 Jun 2021 09:42:14 -0700 (PDT) Received: from [192.168.1.4] ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id u123sm9030957qkh.83.2021.06.08.09.42.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 08 Jun 2021 09:42:14 -0700 (PDT) Subject: Re: supporting terminal ownership assignment (tcsetpgrp()) in posix_spawn To: Godmar Back Cc: Libc-help References: <7298cb72-becb-80bb-b2df-d97bdb201e95@linaro.org> From: Adhemerval Zanella Message-ID: <11145e53-3fbc-0f04-33f8-b2d9981f0ea8@linaro.org> Date: Tue, 8 Jun 2021 13:42:12 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.5 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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 16:42:17 -0000 On 08/06/2021 11:37, Godmar Back wrote: > 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). Indeed this won't accomplish anything in fact, one will need to issue a ioctl with TIOCSCTTY to recover the terminal (as login_tty does). I agree with you that the usual usercase for POSIX_SPAWN_SETSID is to have the created process to setup itself the required terminal. There is hacks to reparent a running program to a new terminal by using ptrace to inject syscalls on the target process, but I think it is really out of scope from the libc. > > >> >> 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. So the my question is whether providing the groupid as an argument is really required (I would say yes so it can be combined with posix_spawnattr_setpgroup). Another question is when to issue the tcsetpgrp related to POSIX_SPAWN_SETSID. I would say tcsetpgrp should be issued *before* setsid, so tcsetpgrp can return early if it fails. Otherwise tcsetpgrp will always fail if POSIX_SPAWN_SETSID is set (it would be a caller error, but I think from API viewpoint it should be better if we could minimize the possible error scenarios).