From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C23193858D35 for ; Mon, 22 May 2023 13:00:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C23193858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684760418; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9tChVIsXmgazYNP64FBhNqOlTInpclkBz5q0l+Y61Nk=; b=Q2ch6h9i19eue4eSkLaxFd07OiGwvo2fEGakJ6YWmz7u+1+RIuKKDDSgyHypFIn+TYz0S3 UvIQWoot0Z7kaeHGW+/2ig0iL+CrzFuRSZKqb9qyUbwyvyEjqLKcAhsIhi4GeixwZnctYV LjdNmfjQefYFIlVzOoYEu6Znt0Yt8Pg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-157-xEH1lE8VNI6X-y0DY-wTIg-1; Mon, 22 May 2023 09:00:17 -0400 X-MC-Unique: xEH1lE8VNI6X-y0DY-wTIg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 79CD9381151B; Mon, 22 May 2023 13:00:16 +0000 (UTC) Received: from oldenburg3.str.redhat.com (unknown [10.39.192.96]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8B5FC407DEC6; Mon, 22 May 2023 13:00:15 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella Cc: libc-alpha@sourceware.org, Luca Boccassi , Philip Withnall Subject: Re: [PATCH v4 3/3] linux: Add pidfd_getpid References: <20230517173516.1988283-1-adhemerval.zanella@linaro.org> <20230517173516.1988283-4-adhemerval.zanella@linaro.org> Date: Mon, 22 May 2023 15:00:13 +0200 In-Reply-To: <20230517173516.1988283-4-adhemerval.zanella@linaro.org> (Adhemerval Zanella's message of "Wed, 17 May 2023 14:35:16 -0300") Message-ID: <87h6s44j36.fsf@oldenburg3.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: * Adhemerval Zanella: > This interface allows to obtain the associated pid ID from the =E2=80=9Cprocess ID=E2=80=9D > process file descriptor. It is done by parsing the procps fdinfo > information. Its prototype is: > > pid_t pidfd_getpid (int fd) > > It returns the associated pid or -1 in case of an error and set the > errno accordingly. The possible errno values are the smae from Typos: =E2=80=9Cand set[s] [] errno accordingly.=E2=80=9D =E2=80=9Care [those] from=E2=80=9D > open, read, and close (used on procps parsing), along with: > > - EINVAL if the FP is negative (similar to fexecve). > > - EBADF if the FD does not have a PID associated of if the fdinfo =E2=80=9Cassociated o[r] if=E2=80=9D > fields contains a value larger than pid_t. > > - EREMOTE if the PID is in a separate namespace. > > - ESRCH if the process is already terminated. > diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/l= inux/pidfd_getpid.c > new file mode 100644 > index 0000000000..ae5fa100eb > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c > @@ -0,0 +1,122 @@ > +pid_t > +pidfd_getpid (int fd) > +{ > + if (__glibc_unlikely (fd < 0)) > + { > + __set_errno (EINVAL); > + return -1; > + } > + > + char fdinfoname[FDINFO_FILENAME_LEN]; > + > + char *p =3D mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX, > +=09=09 strlen (FDINFO_TO_FILENAME_PREFIX)); > + *_fitoa_word (fd, p, 10, 0) =3D '\0'; > + > + struct parse_fdinfo_t fdinfo =3D { .found =3D false, .pid =3D -1 }; > + if (procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo) =3D=3D -1) > + /* The fdinfo contains an invalid 'Pid:' value. */ > + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF); > + > + /* The FD does not have a 'Pid:' entry associated. */ > + if (!fdinfo.found) > + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF); > + > + /* The pidfd cannot be resolved because it is in a separate pid > + namespace. */ > + if (fdinfo.pid =3D=3D 0) > + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EREMOTE); > + > + /* A negative value means the process is terminated. */ > + if (fdinfo.pid < 0) > + return INLINE_SYSCALL_ERROR_RETURN_VALUE (ESRCH); > + > + return fdinfo.pid; We are making a ot of error codes here. How likely is it that they match what a future ioctl will do? > diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linu= x/tst-pidfd.c > index 64d8a2ef40..a03de4b290 100644 > --- a/sysdeps/unix/sysv/linux/tst-pidfd.c > +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c > + /* Check if pidfd_getpid returns ESRCH for exited subprocess. */ > + { > + int pidfd =3D pidfd_fork (0); > + if (pidfd =3D=3D 0) > + exit (EXIT_SUCCESS); Should be _exit, so that the atexit handlers do not run. I think the main remaining question is errno alignment with a future direct kernel interface for this. Thanks, Florian