From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 9E08B3851C21 for ; Wed, 19 Apr 2023 14:11:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E08B3851C21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-38e4c98e5ceso234025b6e.1 for ; Wed, 19 Apr 2023 07:11:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681913480; x=1684505480; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=gUDFvBNQU6CpRNWiGWEKbX7QfV2Ce6wtQLT+n5NrhIc=; b=uSJ+3DmfXddNxzt0mJ+2AYmdIAMtJ8y46NbGGSj51Q3N6gxWwetHM3JEWkm12V3XcE 6k/J91xP+kqomz9ywio5/cn7JVGe+nT1b3hi7kFaykOAZQzPzkHDhdvwGeScpCvfq7gA tw4TYRC9C/2m9uXGksOpAKurrDUHn8tiDJtK5Z3KGgZ+e7Gh3czWc0QKTlSsl4euvhn+ 03PdUgMP8W7UT63GdRpfDqC03OkfqW89XJKoZC2YXqnABYc3HTGIHne/wtdH7sN+Ka6h L8y1lHm/a1OEc1slpfQBoCaKEUDwSSeY7oF6aVRi7ZE1li5XF64Fee8uUGz1bcZYLA9d 4JRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681913480; x=1684505480; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gUDFvBNQU6CpRNWiGWEKbX7QfV2Ce6wtQLT+n5NrhIc=; b=deF1fsosl6K6309lgRmnTQRw43kEvpwvEKwOjrCT6HZawI1nxaAFH8KIjkKC3jn6H/ Z+2WGXmXiq4/fIBixmeQmZfsLd1HIDSeucFyj0wL4jO5KRFdA0ki9SsuttEFMexz1wh7 NwGPlEIiVGUfykfFj8lzBDWNH40qysNWxJLg4cc8WFiDiJByclFDkKAOftAfEkKd7Iyp UApL191987C6OCq+6YL9pWsy1UzhIUmfkhmHHeoDMYaO+N3m5fEaQvVzOErDHDuStAzo VUmiX4ff6eA7yR6nCxf68KHb7rBsupSFsmXIW7BbaXnqdH0JFmBBKy5ervJkb+1bTtFt mJ0g== X-Gm-Message-State: AAQBX9czRiKz5qlNU9Tnf4PWBXl9pILjee1S7rxcfz2yAbNtgX0i+y32 4yRnmv5n58nFjvFfl/7eLuiMYL2YCHzCd5X/qptKkg== X-Google-Smtp-Source: AKy350bHd2VAto3fmHHKexahczylJQQKXO78l0k8npRhU/G4yhedQufRWK0Q+Mxl6g72mViySeQVTw== X-Received: by 2002:a05:6808:1901:b0:38e:192d:3012 with SMTP id bf1-20020a056808190100b0038e192d3012mr3454589oib.41.1681913479920; Wed, 19 Apr 2023 07:11:19 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c3:333:688f:9c89:b48e:ce61? ([2804:1b3:a7c3:333:688f:9c89:b48e:ce61]) by smtp.gmail.com with ESMTPSA id j21-20020a056808057500b0038e086c764dsm3412190oig.43.2023.04.19.07.11.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Apr 2023 07:11:19 -0700 (PDT) Message-ID: <017622ea-f4ae-94de-2abd-7d87531ecf1c@linaro.org> Date: Wed, 19 Apr 2023 11:11:16 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH 4/4] linux: Add pidfd_getpid Content-Language: en-US To: Luca Boccassi Cc: libc-alpha@sourceware.org, Lennart Poettering References: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.7 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 19/04/23 11:01, Luca Boccassi wrote: > On Wed, 19 Apr 2023 at 14:56, Adhemerval Zanella Netto > wrote: >> On 19/04/23 07:29, Luca Boccassi wrote: >>>> +parse_fdinfo (const char *l, void *arg) >>>> +{ >>>> + enum { fieldlen = sizeof ("Pid:") - 1 }; >>>> + if (strncmp (l, "Pid:", fieldlen) != 0) >>>> + return true; >>>> + >>>> + l += fieldlen; >>>> + >>>> + char *endp; >>>> + unsigned long int n = strtoul (l, &endp, 10); >>>> + if (l == endp || n > INT_MAX) >>>> + return true; >>>> + >>>> + *(pid_t *)arg = n; >>>> + return false; >>>> +} >>>> + >>>> +pid_t >>>> +pidfd_getpid (int fd) >>>> +{ >>>> + if (__glibc_unlikely (fd < 0)) >>>> + { >>>> + __set_errno (EINVAL); >>>> + return -1; >>>> + } >>>> + >>>> + char fdinfoname[FDINFO_FILENAME_LEN]; >>>> + >>>> + char *p = mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX, >>>> + strlen (FDINFO_TO_FILENAME_PREFIX)); >>>> + *_fitoa_word (fd, p, 10, 0) = '\0'; >>>> + >>>> + pid_t pid; >>>> + if (procutils_read_file (fdinfoname, parse_fdinfo, &pid) == -1) >>>> + return -1; >>>> + >>>> + return pid; >>> >>> Having implemented this parsing by hand across 3 projects, it is great >>> to see a glibc helper coming. >>> >>> However, please handle the case of Pid being 0 and -1 explicitly, and >>> return a recognizable errno. fdinfo listing 0 means the pidfd cannot be >>> resolved because it's in a separate pid namespace, so something EREMOTE >>> would suffice. -1 means the process exited, so ESRCH seems like the >>> right error. The distiction between these cases and other errors is >>> important to userspace where we do process tracking, like >>> systemd/dbus/polkit. >> >> Ok, it should a matter to handle the 'Pid:' as signed. However, I am not >> sure if it is really worth to set the errno in cases, since the information >> will already available from the function return itself. > > The function returns the pid, so the same information wouldn't be > available. Eg. returning -1 because you can't read proc because of > selinux permissions is different from returning -1 because the process > was reaped and fdinfo contained -1 so it was just passed through. We > need to know this distinction in userspace to handle it properly. > Setting the errno accordingly seems the best way to do that to me. I am kind worried where such errors might clash with other open/read/close used in parsing, but at least it seems ESRCH should not be generated in such cases. I am still not sure about EREMOTE, maybe we should just map the pid equal 0 to -1/EREMOTE.