From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by sourceware.org (Postfix) with ESMTPS id 58654385DC19 for ; Fri, 11 Jun 2021 13:45:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 58654385DC19 Received: by mail-qk1-x72a.google.com with SMTP id d196so25815103qkg.12 for ; Fri, 11 Jun 2021 06:45:51 -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=ki8ml/jBY6mhiqcUNC53hxKGzeN0cWrFBliGbt59yKE=; b=ljFVoGDMRXUNes4+87reNL+9EKLTAPdVU5gMu6wd4lRk1SdE5Lh7TukeLB/AIUzcLV fz6y55GTxuzEVbEsgErnKI+0ZvTbbCdd3sj32NQw7u4r5lC9F5dwYy1ws0CV7i02Aq3o UJXFLu0IkUAdn7OPIPB06BUAKiShy63YA44bpyiyRuGXy4dNR2d/aDvB/KM/pxW2UDuu 8I88yQQ9vGpYbiPKgRCa7uvN4uMv2ZC6QjkfUznfH6nMnAmYeRxDKcs7ntskCzT6o8gi yurd5i4/7OC2JKGiAZV+XcuxRx1RCK1o0P8kz8IJXwLqrWQnXRSLJ6FpeGlWNc5fsml9 H2FA== X-Gm-Message-State: AOAM530Br6V8Ca3MrqkJuQKQ2RPoduzpYjxhxblKWhq9zDvTV5y1hLZu wFpT1GaJEIx2w+RrUuShs/ryUspG0dIGnw== X-Google-Smtp-Source: ABdhPJwRe7bARwfrMFM81nNz84gzVhUyY/wfyyBHJKlsAecob9/HkznS/7Q+/3PnbqEYBpX7rBWXcg== X-Received: by 2002:a37:7604:: with SMTP id r4mr3909711qkc.324.1623419150701; Fri, 11 Jun 2021 06:45:50 -0700 (PDT) Received: from [192.168.1.4] ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id a14sm4180127qtj.57.2021.06.11.06.45.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 11 Jun 2021 06:45:50 -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> <11145e53-3fbc-0f04-33f8-b2d9981f0ea8@linaro.org> <4429f842-e9c7-b907-3374-6b48c6fc089e@linaro.org> From: Adhemerval Zanella Message-ID: Date: Fri, 11 Jun 2021 10:45:47 -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.4 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: Fri, 11 Jun 2021 13:45:52 -0000 On 09/06/2021 10:12, Godmar Back wrote: > On Wed, Jun 9, 2021 at 8:00 AM Adhemerval Zanella < > adhemerval.zanella@linaro.org> wrote: > >> >> The libc interface should be as generic possible to cover most users cases, >> I really don't want to add a posix_spawnattr_tcsetpgrp_np that uses the >> groupid implicit from POSIX_SPAWN_SETPGROUP to someone ask for a >> posix_spawnattr_tcsetpgrp_np_ex so one can set the groupid. >> >> Also, requiring POSIX_SPAWN_SETPGROUP for posix_spawnattr_tcsetpgrp_np is >> not really a good API, it adds subtle semantics (should it use a default >> value set by posix_spawnattr_init or should we fail with EINVAL), and adds >> complexity in the error path (we will need to either pre validate the >> posix_spawnattr_t input before start the process creation or handle a >> possible invalid combination on the helper process itself). >> >> That's why I am more inclined to follow the tcsetpgrp on the posix_spawn >> extension and let the caller set the required groups. >> >> > I just checked and I don't think this will even work. The caller cannot > provide the process group id because the caller can't know it yet. > A typical sequence in the first child of a pipeline is "setpgid(0, 0); > tcsetpgrp(fd, getpgrp());" and AFAIK tcsetpgrp doesn't accept '0' to stand > for the > current process's process group. Indeed I think it wouldn't make sense to provide the input process group argument, in only does if the created process wants to become de process group leader (POSIX_SPAWN_SETPGROUP plus posix_spawnattr_setpgroup (0)). However I don't think adding a flag as QNX does (POSIX_SPAWN_TCSETPGROUP) is the best approach: it requires posix_spawn opening a new file descriptor to obtain the controlling terminal. It add extra error path and put pressure on the total file descriptor process has (assuming that caller expects no file operation is done by posix_spawn). So maybe this following: int posix_spawnattr_tcsetpgrp_np (posix_spawnattr_t *__attr, int fd); Similar to tcgetpgrp, it make the process group of the created process the foreground process group on the terminal associated to FD. It is similar to tcsetpgrp(), where FD must be controlling terminal of the calling process, and still be associated with its session. It can be used along with POSIX_SPAWN_SETPGROUP to make the created process a new group leader to be used as the process group. And then you can make a new process the foreground process group with: int fd = open (_PATH_TTY, O_RDWR | O_CLOEXEC); posix_spawnattr_t attr; posix_spawnattr_init (&attr); posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETPGROUP) posix_spawnattr_tcsetpgrp_np (&attr, fd); pid_t pid; char *args[] = { NULL }; posix_spawn (&pid, "/path/to/program", NULL, &attr, args, NULL);