From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 0C20C3858D33 for ; Wed, 19 Apr 2023 13:56:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C20C3858D33 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-ot1-x329.google.com with SMTP id 46e09a7af769-6a5d9853d97so748026a34.2 for ; Wed, 19 Apr 2023 06:56:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681912582; x=1684504582; 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=cR7IN7rFVtETQTWWNC8Md9LZlwFqlkSyHRIEwJO6JdQ=; b=JguefHpT/UhVSD6Ry620f2len+r8nmAIw0gn+t0E3mHwPQjhFm0hYXVwtHafbabz96 5tzVD6+hw5oQ74qtIV8BNcerzESY9bECHtuJgIcf0xSc5Qu7u2yyg3Xe3s8saIF5A40w 8Szn6nxvzzDXXTThDfp6NXW0kpHMhPlWYRoG1IIljoEAV9U0wCaGrYsqzy45iTZ0IRH0 7YHBv4biVpbOv4CX4YvuEzJIEiq3NWzSm68VP7ZqUqxZqvSeuvpm6E7VDL/uqdniqd1s psrYLeyxjqD7ZuVKhfFS9ZhIwdEw6zn7hEpaFhFvZyihdeLupoQPQhE2f8iUx4+xa1Jp ENCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681912582; x=1684504582; 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=cR7IN7rFVtETQTWWNC8Md9LZlwFqlkSyHRIEwJO6JdQ=; b=VSVC+Y3nNCoJV/qJrR0TpA4xzhTKquSzpnoEcacwgsClPFtQdXCwkydSyAJvHvreuT HcRg5vO665ktxgx8/48abjL7b6bQz+xq78X4YsL3N91AyTvTJff63ogMO90aZVFFOhMD ZglJBynDLEwfqGA33IPyX5RLyXkhUzCeO8G8VceI8nFwJA4JeBWrtle39EK3GuYiqd4H q5JmBquO7kVI0Wmh75okUJjA5fuaBttfsA/XG4vnsZALWt3bjCGsEpSpR9ox8oLef3Is 3TQ7fZrqb7uG58Rt07+NiLlVisfqd9ZmUezxmEtqLRf7yyampdQFYlODDFMW8ruRv21v 2Ztg== X-Gm-Message-State: AAQBX9ckOJjCBYogOBlDSJs23AbPdOiEjlRV52PxQ5qe1yOGQ+LYM1QK Vlhp6pOJYyI76t7+iI6OYE2uBQ== X-Google-Smtp-Source: AKy350aWfVwJMEpQSISbRDChnp1pW2UQTYMR9VHEOCfMEjaIaJVgAhUsnet5XJM2Ec4fWEWRrXPoDQ== X-Received: by 2002:a05:6830:134f:b0:6a5:d450:1c30 with SMTP id r15-20020a056830134f00b006a5d4501c30mr3187401otq.33.1681912582293; Wed, 19 Apr 2023 06:56:22 -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 d16-20020a056830005000b0068bd922a244sm6772380otp.20.2023.04.19.06.56.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Apr 2023 06:56:21 -0700 (PDT) Message-ID: <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> Date: Wed, 19 Apr 2023 10:56:18 -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 References: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.0 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 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 CLOSURE should return false if the read should continue, or false >> + if the function should stop. > > Did you mean "true if the read should continue"? Indeed. > >> + TEST_COMPARE (pidfd_getpid (INT_MAX), -1); >> + { >> + pid_t querypid = pidfd_getpid (pidfd); >> + TEST_COMPARE (querypid, pid); >> + } > > I think it would be a good idea to add a negative test, eg for a > process that exited or so. Alright, I will add a more comprehensible test. > > (I am not subscribed to the list, so please CC directly if needed) >