From patchwork Tue Apr 18 21:35:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 67962 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3461C3858D28 for ; Tue, 18 Apr 2023 21:35:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3461C3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681853738; bh=x/4pywq5lDgRUqzqD6i+oM2SG75XMgFVbRZL9w3+OiQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=AciZ4TfMhHwVkqYlMwcoO1OYLkATX7QmrWgPR74J8Z9VI97YU7u4pDIqnwIgtae+m OnaHKiFU2ZKr6DGs+l3gs+fqBqRUeSKYZLXLj/5NHEhEDxNLno4fTrHEnqUgrL4wt9 xHuRwz0arKCSmfrIXkL4wArcEFV9k8NLdeFcldX8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id D73733858D28 for ; Tue, 18 Apr 2023 21:35:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D73733858D28 Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6a5f1503886so472636a34.2 for ; Tue, 18 Apr 2023 14:35:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681853711; x=1684445711; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x/4pywq5lDgRUqzqD6i+oM2SG75XMgFVbRZL9w3+OiQ=; b=jz54mb0IJU7HyPF3aMhv4B1L87fEO43HBflcFx0VPuTVpLcxGdCVSCuBDL1mvX2/Rn Ts7cxjInkIEj4fE6k1YB3m3hBAIFWhelDoU4OlwPJEnvC6GrkjQvs7I4lV3sV4r9IOGm VI2kk/aGl0HnLDgH01hK7pQqWc4G4JWyF/19p2QLwC4ZXyDqNJZXoonZ103PBy1b5Acy jzE9ZeTJxWjmXhQfBnxe053TI9IqYrgmkkpC+sS/m6ftI4cV8ZCHwVpQSmAJP6Cb9AZl qFXoyUSMp53qCMUj1K2cRT6cP1k32kN4XeeMzKvamtburqmdajjQO8OCdNHE9ToUjvfP XR4g== X-Gm-Message-State: AAQBX9dLJrv2W3XmSU9N3GwxtqGM2Wfte7jkEBC7xU/bB7zm/Rvw1K5f kGQXK39Aa4QsaIMy0x9cyLjOAs/iZwnTkm3uQPX3rg== X-Google-Smtp-Source: AKy350YrOIbSHIRsxL6+6YGe1x9ue4d231B7DyGqzGHjWP2knPlOk2/Y6NAfmxS3MPCjsjk21qjvEQ== X-Received: by 2002:a9d:644c:0:b0:6a5:edcd:8c8a with SMTP id m12-20020a9d644c000000b006a5edcd8c8amr1413568otl.1.1681853711210; Tue, 18 Apr 2023 14:35:11 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:333:3aff:2c50:5de7:ce94]) by smtp.gmail.com with ESMTPSA id e26-20020a05683013da00b006a6080ba7desm167581otq.58.2023.04.18.14.35.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 14:35:10 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/4] posix: Re-flow and sort multiline definitions Date: Tue, 18 Apr 2023 18:35:02 -0300 Message-Id: <20230418213505.3834934-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> References: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" --- posix/Makefile | 551 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 425 insertions(+), 126 deletions(-) diff --git a/posix/Makefile b/posix/Makefile index 7e4f252a40..3e7ca8e18c 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -22,145 +22,435 @@ subdir := posix include ../Makeconfig -headers := sys/utsname.h sys/times.h sys/wait.h sys/types.h unistd.h \ - glob.h regex.h wordexp.h fnmatch.h \ - getopt.h bits/getopt_core.h bits/getopt_ext.h bits/getopt_posix.h \ - bits/types.h bits/typesizes.h bits/pthreadtypes.h \ - bits/pthreadtypes-arch.h bits/thread-shared-types.h \ - bits/posix1_lim.h bits/posix2_lim.h bits/posix_opt.h \ - bits/local_lim.h tar.h bits/utsname.h bits/confname.h \ - bits/waitflags.h bits/waitstatus.h sys/unistd.h sched.h \ - bits/sched.h bits/cpu-set.h re_comp.h wait.h bits/environments.h \ - cpio.h spawn.h bits/unistd.h bits/types/struct_sched_param.h \ - bits/unistd_ext.h bits/types/idtype_t.h bits/mman_ext.h - -routines := \ - uname \ - times \ - wait waitpid wait3 wait4 waitid \ - alarm sleep pause nanosleep \ - fork _Fork vfork _exit register-atfork \ - execve fexecve execv execle execl execvp execlp execvpe \ - getpid getppid \ - getuid geteuid getgid getegid getgroups setuid setgid group_member \ - getpgid setpgid getpgrp bsd-getpgrp setpgrp getsid setsid \ - getresuid getresgid setresuid setresgid \ - pathconf sysconf fpathconf \ - glob glob64 globfree globfree64 glob_pattern_p fnmatch regex \ - glob-lstat-compat glob64-lstat-compat \ - confstr \ - getopt getopt1 \ - sched_setp sched_getp sched_sets sched_gets sched_yield sched_primax \ - sched_primin sched_rr_gi sched_getaffinity sched_setaffinity \ - getaddrinfo gai_strerror wordexp \ - pread pwrite pread64 pwrite64 \ - spawn_faction_init spawn_faction_destroy spawn_faction_addclose \ - spawn_faction_addopen spawn_faction_adddup2 spawn_valid_fd \ - spawn_faction_addchdir spawn_faction_addfchdir \ - spawn_faction_addclosefrom \ - spawn_faction_addtcsetpgrp_np \ - spawnattr_init spawnattr_destroy \ - spawnattr_getdefault spawnattr_setdefault \ - spawnattr_getflags spawnattr_setflags \ - spawnattr_getpgroup spawnattr_setpgroup spawn spawnp spawni \ - spawnattr_getsigmask spawnattr_getschedpolicy spawnattr_getschedparam \ - spawnattr_setsigmask spawnattr_setschedpolicy spawnattr_setschedparam \ - posix_madvise \ - get_child_max sched_cpucount sched_cpualloc sched_cpufree \ - streams-compat \ - shm-directory \ - execveat \ - glob64-time64 \ - globfree64-time64 - -aux := init-posix environ -tests := test-errno tstgetopt testfnm runtests runptests \ - tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \ - tst-mmap tst-mmap-offset tst-getaddrinfo tst-truncate \ - tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \ - tst-chmod bug-regex1 bug-regex2 bug-regex3 bug-regex4 \ - tst-gnuglob tst-gnuglob64 tst-regex bug-regex6 bug-regex7 \ - bug-regex8 bug-regex9 bug-regex10 bug-regex11 bug-regex12 \ - bug-regex13 bug-regex14 bug-regex15 bug-regex16 \ - bug-regex17 bug-regex18 bug-regex19 \ - bug-regex21 bug-regex22 bug-regex23 bug-regex24 \ - bug-regex25 bug-regex26 bug-regex27 bug-regex28 \ - bug-regex29 bug-regex30 bug-regex31 bug-regex32 \ - tst-nice tst-nanosleep tst-regex2 \ - transbug tst-rxspencer tst-pcre tst-boost \ - bug-ga1 tst-vfork1 tst-vfork2 \ - tst-waitid tst-wait4 tst-wait3 \ - tst-getaddrinfo2 bug-glob2 bug-glob3 tst-sysconf \ - tst-execvp1 tst-execvp2 tst-execlp1 tst-execlp2 \ - tst-execv1 tst-execv2 tst-execl1 tst-execl2 \ - tst-execve1 tst-execve2 tst-execle1 tst-execle2 \ - tst-execvp3 tst-execvp4 \ - tst-execvpe1 tst-execvpe2 tst-execvpe3 tst-execvpe4 \ - tst-execvpe5 tst-execvpe6 \ - tst-getaddrinfo3 tst-fnmatch2 tst-cpucount tst-cpuset \ - bug-getopt1 bug-getopt2 bug-getopt3 bug-getopt4 \ - bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \ - tst-pathconf tst-rxspencer-no-utf8 \ - tst-fnmatch3 bug-regex36 \ - tst-fnmatch4 tst-fnmatch5 tst-fnmatch6 tst-fnmatch7 \ - tst-posix_spawn-fd tst-posix_spawn-setsid \ - tst-posix_fadvise tst-posix_fadvise64 \ - tst-sysconf-empty-chroot tst-glob_symlinks tst-fexecve \ - tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \ - bug-regex38 tst-regcomp-truncated tst-spawn-chdir \ - tst-wordexp-nocmd tst-execveat tst-spawn5 \ - tst-sched_getaffinity tst-spawn6 tst-spawn7 +headers := \ + bits/confname.h \ + bits/cpu-set.h \ + bits/environments.h \ + bits/getopt_core.h \ + bits/getopt_ext.h \ + bits/getopt_posix.h \ + bits/local_lim.h \ + bits/mman_ext.h \ + bits/posix1_lim.h \ + bits/posix2_lim.h \ + bits/posix_opt.h \ + bits/pthreadtypes-arch.h \ + bits/pthreadtypes.h \ + bits/sched.h \ + bits/thread-shared-types.h \ + bits/types.h \ + bits/types/idtype_t.h \ + bits/types/struct_sched_param.h \ + bits/typesizes.h \ + bits/unistd.h \ + bits/unistd_ext.h \ + bits/utsname.h \ + bits/waitflags.h \ + bits/waitstatus.h \ + cpio.h \ + fnmatch.h \ + getopt.h \ + glob.h \ + re_comp.h \ + regex.h \ + sched.h \ + spawn.h \ + sys/times.h \ + sys/types.h \ + sys/unistd.h \ + sys/utsname.h \ + sys/wait.h \ + tar.h \ + unistd.h \ + wait.h \ + wordexp.h \ + # headers + +routines := \ + _Fork \ + _exit \ + alarm \ + bsd-getpgrp \ + confstr \ + execl \ + execle \ + execlp \ + execv \ + execve \ + execveat \ + execvp \ + execvpe \ + fexecve \ + fnmatch \ + fork \ + fpathconf \ + gai_strerror \ + get_child_max \ + getaddrinfo \ + getegid \ + geteuid \ + getgid \ + getgroups \ + getopt \ + getopt1 \ + getpgid \ + getpgrp \ + getpid \ + getppid \ + getresgid \ + getresuid \ + getsid \ + getuid \ + glob \ + glob-lstat-compat \ + glob64 \ + glob64-lstat-compat \ + glob64-time64 \ + glob_pattern_p \ + globfree \ + globfree64 \ + globfree64-time64 \ + group_member \ + nanosleep \ + pathconf \ + pause \ + posix_madvise \ + pread \ + pread64 \ + pwrite \ + pwrite64 \ + regex \ + register-atfork \ + sched_cpualloc \ + sched_cpucount \ + sched_cpufree \ + sched_getaffinity \ + sched_getp \ + sched_gets \ + sched_primax \ + sched_primin \ + sched_rr_gi \ + sched_setaffinity \ + sched_setp \ + sched_sets \ + sched_yield \ + setgid \ + setpgid \ + setpgrp \ + setresgid \ + setresuid \ + setsid \ + setuid \ + shm-directory \ + sleep \ + spawn \ + spawn_faction_addchdir \ + spawn_faction_addclose \ + spawn_faction_addclosefrom \ + spawn_faction_adddup2 \ + spawn_faction_addfchdir \ + spawn_faction_addopen \ + spawn_faction_addtcsetpgrp_np \ + spawn_faction_destroy \ + spawn_faction_init \ + spawn_valid_fd \ + spawnattr_destroy \ + spawnattr_getdefault \ + spawnattr_getflags \ + spawnattr_getpgroup \ + spawnattr_getschedparam \ + spawnattr_getschedpolicy \ + spawnattr_getsigmask \ + spawnattr_init \ + spawnattr_setdefault \ + spawnattr_setflags \ + spawnattr_setpgroup \ + spawnattr_setschedparam \ + spawnattr_setschedpolicy \ + spawnattr_setsigmask \ + spawni \ + spawnp \ + streams-compat \ + sysconf \ + times \ + uname \ + vfork \ + wait \ + wait3 \ + wait4 \ + waitid \ + waitpid \ + wordexp \ + # routines + +aux := \ + environ \ + init-posix \ + #aux + +tests := \ + bug-ga1 \ + bug-getopt1 \ + bug-getopt2 \ + bug-getopt3 \ + bug-getopt4 \ + bug-getopt5 \ + bug-glob2 \ + bug-glob3 \ + bug-regex1 \ + bug-regex10 \ + bug-regex11 \ + bug-regex12 \ + bug-regex13 \ + bug-regex14 \ + bug-regex15 \ + bug-regex16 \ + bug-regex17 \ + bug-regex18 \ + bug-regex19 \ + bug-regex2 \ + bug-regex21 \ + bug-regex22 \ + bug-regex23 \ + bug-regex24 \ + bug-regex25 \ + bug-regex26 \ + bug-regex27 \ + bug-regex28 \ + bug-regex29 \ + bug-regex3 \ + bug-regex30 \ + bug-regex31 \ + bug-regex32 \ + bug-regex34 \ + bug-regex35 \ + bug-regex36 \ + bug-regex37 \ + bug-regex38 \ + bug-regex4 \ + bug-regex6 \ + bug-regex7 \ + bug-regex8 \ + bug-regex9 \ + regexbug1 \ + runptests \ + runtests \ + test-errno \ + test-ssize-max \ + test-vfork \ + testfnm \ + transbug \ + tst-boost \ + tst-chmod \ + tst-cpucount \ + tst-cpuset \ + tst-dir \ + tst-execl1 \ + tst-execl2 \ + tst-execle1 \ + tst-execle2 \ + tst-execlp1 \ + tst-execlp2 \ + tst-execv1 \ + tst-execv2 \ + tst-execve1 \ + tst-execve2 \ + tst-execveat \ + tst-execvp1 \ + tst-execvp2 \ + tst-execvp3 \ + tst-execvp4 \ + tst-execvpe1 \ + tst-execvpe2 \ + tst-execvpe3 \ + tst-execvpe4 \ + tst-execvpe5 \ + tst-execvpe6 \ + tst-fexecve \ + tst-fnmatch \ + tst-fnmatch2 \ + tst-fnmatch3 \ + tst-fnmatch4 \ + tst-fnmatch5 \ + tst-fnmatch6 \ + tst-fnmatch7 \ + tst-fork \ + tst-getaddrinfo \ + tst-getaddrinfo2 \ + tst-getaddrinfo3 \ + tst-getopt_long1 \ + tst-glob-tilde \ + tst-glob_symlinks \ + tst-gnuglob \ + tst-gnuglob64 \ + tst-mmap \ + tst-mmap-offset \ + tst-nanosleep \ + tst-nice \ + tst-pathconf \ + tst-pcre \ + tst-posix_fadvise \ + tst-posix_fadvise64 \ + tst-posix_spawn-fd \ + tst-posix_spawn-setsid \ + tst-preadwrite \ + tst-preadwrite64 \ + tst-regcomp-truncated \ + tst-regex \ + tst-regex2 \ + tst-regexloc \ + tst-rxspencer \ + tst-rxspencer-no-utf8 \ + tst-sched_getaffinity \ + tst-spawn-chdir \ + tst-spawn4 \ + tst-spawn5 \ + tst-spawn6 \ + tst-spawn7 \ + tst-sysconf \ + tst-sysconf-empty-chroot \ + tst-truncate \ + tst-truncate64 \ + tst-vfork1 \ + tst-vfork2 \ + tst-wait3 \ + tst-wait4 \ + tst-waitid \ + tst-wordexp-nocmd \ + tstgetopt \ + # tests # Test for the glob symbol version that was replaced in glibc 2.27. ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes) -tests += tst-glob_lstat_compat +tests += \ + tst-glob_lstat_compat + # tests endif # Test for the posix_spawn, posix_spawnp symbol versions replaced in # glibc 2.15. ifeq ($(have-GLIBC_2.14)$(build-shared),yesyes) -tests += tst-spawn4-compat +tests += \ + tst-spawn4-compat \ + # tests endif -tests-internal := bug-regex5 bug-regex20 bug-regex33 \ - tst-rfc3484 tst-rfc3484-2 tst-rfc3484-3 -tests-container := bug-ga2 tst-vfork3 -tests-time64 := tst-wait4-time64 tst-wait3-time64 tst-gnuglob64-time64 -xtests := tst-getaddrinfo4 tst-getaddrinfo5 tst-sched_rr_get_interval -xtests-time64 := tst-sched_rr_get_interval-time64 +tests-internal := \ + bug-regex20 \ + bug-regex33 \ + bug-regex5 \ + tst-rfc3484 \ + tst-rfc3484-2 \ + tst-rfc3484-3 \ + # tests-internal + +tests-container := \ + bug-ga2 \ + tst-vfork3 \ + # tests-container + +tests-time64 := \ + tst-gnuglob64-time64 \ + tst-wait3-time64 \ + tst-wait4-time64 \ + # tests-time64 + +xtests := \ + tst-getaddrinfo4 \ + tst-getaddrinfo5 \ + tst-sched_rr_get_interval \ + # xtests + +xtests-time64 := \ + tst-sched_rr_get_interval-time64 \ + # xtests-time64 + ifeq (yes,$(build-shared)) -test-srcs := globtest -tests += wordexp-test tst-exec tst-spawn tst-spawn2 tst-spawn3 +test-srcs := \ + globtest + # tests-src +tests += \ + tst-exec \ + tst-spawn \ + tst-spawn2 \ + tst-spawn3 \ + wordexp-test \ + # tests endif + ifeq (yesyes,$(build-shared)$(have-thread-library)) -tests += tst-getopt-cancel tst-_Fork +tests += \ + tst-_Fork \ + tst-getopt-cancel \ + # tests endif -tests-static = tst-exec-static tst-spawn-static -tests += $(tests-static) -others := getconf -install-bin := getconf -install-others-programs := $(inst_libexecdir)/getconf -before-compile += $(objpfx)posix-conf-vars-def.h +tests-static = \ + tst-exec-static \ + tst-spawn-static \ + # tests-static + +tests += \ + $(tests-static) + # tests + +others := \ + getconf \ + # others + +install-bin := \ + getconf \ + # getconf + +install-others-programs := \ + $(inst_libexecdir)/getconf \ + # install-others-programs + +before-compile += \ + $(objpfx)posix-conf-vars-def.h \ + # before-compile # So they get cleaned up. -generated += $(addprefix wordexp-test-result, 1 2 3 4 5 6 7 8 9 10) \ - annexc annexc.out wordexp-tst.out bug-regex2-mem.out \ - bug-regex2.mtrace bug-regex14-mem.out bug-regex14.mtrace \ - bug-regex21-mem.out bug-regex21.mtrace \ - bug-regex31-mem.out bug-regex31.mtrace \ - tst-rxspencer-no-utf8-mem.out tst-rxspencer-no-utf8.mtrace \ - tst-getconf.out \ - tst-pcre-mem.out tst-pcre.mtrace tst-boost-mem.out \ - tst-boost.mtrace bug-ga2.mtrace bug-ga2-mem.out \ - bug-glob2.mtrace bug-glob2-mem.out tst-vfork3-mem.out \ - tst-vfork3.mtrace getconf.speclist tst-fnmatch-mem.out \ - tst-fnmatch.mtrace bug-regex36.mtrace \ - testcases.h ptestcases.h +generated += \ + $(addprefix wordexp-test-result, 1 2 3 4 5 6 7 8 9 10) \ + annexc \ + annexc.out \ + bug-ga2-mem.out \ + bug-ga2.mtrace \ + bug-glob2-mem.out \ + bug-glob2.mtrace \ + bug-regex14-mem.out \ + bug-regex14.mtrace \ + bug-regex2-mem.out \ + bug-regex2.mtrace \ + bug-regex21-mem.out \ + bug-regex21.mtrace \ + bug-regex31-mem.out \ + bug-regex31.mtrace \ + bug-regex36.mtrace \ + getconf.speclist \ + ptestcases.h \ + testcases.h \ + tst-boost-mem.out \ + tst-boost.mtrace \ + tst-fnmatch-mem.out \ + tst-fnmatch.mtrace \ + tst-getconf.out \ + tst-pcre-mem.out \ + tst-pcre.mtrace \ + tst-rxspencer-no-utf8-mem.out \ + tst-rxspencer-no-utf8.mtrace \ + tst-vfork3-mem.out \ + tst-vfork3.mtrace \ + wordexp-tst.out \ + # generated ifeq ($(run-built-tests),yes) ifeq (yes,$(build-shared)) -tests-special += $(objpfx)globtest.out $(objpfx)wordexp-tst.out +tests-special += \ + $(objpfx)globtest.out \ + $(objpfx)wordexp-tst.out \ + # tests-special endif endif @@ -168,13 +458,22 @@ endif # XXX Please note that for now we ignore the result of this test. tests-special += $(objpfx)annexc.out ifeq ($(run-built-tests),yes) -tests-special += $(objpfx)bug-regex2-mem.out $(objpfx)bug-regex14-mem.out \ - $(objpfx)bug-regex21-mem.out $(objpfx)bug-regex31-mem.out \ - $(objpfx)tst-rxspencer-no-utf8-mem.out $(objpfx)tst-pcre-mem.out \ - $(objpfx)tst-boost-mem.out $(objpfx)tst-getconf.out \ - $(objpfx)bug-glob2-mem.out $(objpfx)tst-vfork3-mem.out \ - $(objpfx)tst-fnmatch-mem.out $(objpfx)bug-regex36-mem.out \ - $(objpfx)tst-glob-tilde-mem.out $(objpfx)bug-ga2-mem.out +tests-special += \ + $(objpfx)bug-ga2-mem.out \ + $(objpfx)bug-glob2-mem.out \ + $(objpfx)bug-regex14-mem.out \ + $(objpfx)bug-regex2-mem.out \ + $(objpfx)bug-regex21-mem.out \ + $(objpfx)bug-regex31-mem.out \ + $(objpfx)bug-regex36-mem.out \ + $(objpfx)tst-boost-mem.out \ + $(objpfx)tst-fnmatch-mem.out \ + $(objpfx)tst-getconf.out \ + $(objpfx)tst-glob-tilde-mem.out \ + $(objpfx)tst-pcre-mem.out \ + $(objpfx)tst-rxspencer-no-utf8-mem.out \ + $(objpfx)tst-vfork3-mem.out \ + # tests-special endif include ../Rules From patchwork Tue Apr 18 21:35:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 67963 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6D0443858C50 for ; Tue, 18 Apr 2023 21:35:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D0443858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681853750; bh=Aczz+w7s4chq6SMyLJSkqLGgd2fPK2d6cmBWxG29DZE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=BMz6vBMxcfwmCe75elK3bRoMPOPvzoJbnVikKCU/hEdlN30/ivyw+0vI5yl26dtwL g6RZty/njO9ud3TrCTBs/ZTvyAMl+oGylJKtGCvc5Ng2Un66e6jlrQ9S9vennHGlR6 KnV7bfbzjyeR6XWEVLpeROOuTNdmF6vdCgiKLzZw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 6AF603857835 for ; Tue, 18 Apr 2023 21:35:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6AF603857835 Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-6a5ef766282so588041a34.0 for ; Tue, 18 Apr 2023 14:35:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681853715; x=1684445715; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Aczz+w7s4chq6SMyLJSkqLGgd2fPK2d6cmBWxG29DZE=; b=JoCVLCBOnPIvTH+vUKiuURdNRQp9VtuFoXJI5oB373MhdRrwSkhcM1+NL98LTRbv1k jzFTjscwocpCFPhDk2cAbQxQeOeFJTHLIJBWPypwI8bji90s767QVNfUm/jAhKES4Fkg ocSgt3PQVqQGW08bINxcC6OWajfGklrtbABvbF5RZx5G0+ov7jN2xGGYZvWRWOFdIEvi EatEIrXT18jdrzITMkVcbbvOYFFQes9nbEk1DY1GcU1KBzJAeX0AJYUCWScy1Aa4/e91 mANiPCVCRwKFNbF2l4NkCXBtjcJF3HcQjdsuF2WSLdR2P7+eheWHqEDz0oN+laiS/+BL wZog== X-Gm-Message-State: AAQBX9d1L1zUJLvuvLfsTRJJXu2Vw2aL0T6HUHvgxSUiH4wD9a1F+15z zi9zSrf7GMYgniVikj6kUrVRcKSLio/twU+MwKx+Zw== X-Google-Smtp-Source: AKy350bSrAknMPe9EnqrX5nqlgX1MiIdTuoNrp2Tj4pIGIggX3I4+LAyJwv+GZPp1mzHuW2mg/tQKQ== X-Received: by 2002:a9d:7c9a:0:b0:694:7e8f:2547 with SMTP id q26-20020a9d7c9a000000b006947e8f2547mr1619601otn.22.1681853713530; Tue, 18 Apr 2023 14:35:13 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:333:3aff:2c50:5de7:ce94]) by smtp.gmail.com with ESMTPSA id e26-20020a05683013da00b006a6080ba7desm167581otq.58.2023.04.18.14.35.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 14:35:12 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/4] posix: Add pidfd_spawn and pidfd_spawnp (BZ# 30349) Date: Tue, 18 Apr 2023 18:35:03 -0300 Message-Id: <20230418213505.3834934-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> References: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Returning a pidfd allows a process to keep a race-free handle to a child process, otherwise the caller will need to either use pidfd_open (which still might be subject to TOCTOU) or keep using the old racy interface. The implementation makes sure that kernel must support the complete pidfd interface, meaning that waitid (P_PIDFD) should be supported. It ensure that non racy workaround is required (such as reading procfs fdinfo pid to use along with old wait interfaces). If kernel does not have the required support the interface returns ENOSYS. These interfaces are similar to the posix_spawn and posix_spawnp, with the only different being the return being explicit an integer where the process file descriptor will be returned. Their prototype are: int pidfd_spawn (int *restrict pidfd, const char *restrict file, const posix_spawn_file_actions_t *restrict facts, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]) int pidfd_spawnp (int *restrict pidfd, const char *restrict path, const posix_spawn_file_actions_t *restrict facts, const posix_spawnattr_t *restrict attrp, char *const argv[restrict_arr], char *const envp[restrict_arr]); A new symbol is used instead of a posix_spawn extension to avoid possible issue with language bindings that might track the argument lifetime. Although for Linux pid_t and int are interchangeable, POSIX only state that pid_t should be a signed interger. Both symbols reuse the posix_spawn posix_spawn_file_actions_t and posix_spawnattr_t, to either to rehash posix_spawn API or add a new one. It also mean that both interfaces support the same attribute and file actions, and a new flag or file actions on posix_spawn is also added automatically for pidfd_spawn. Also, using posix_spawn plumbering allows to reuse most of the current testing with some changes: - waitid is used instead of waitpid, since it is a more generic interface. - tst-posix_spawn-setsid.c is adapted to take in consideration that caller can check for session id directly. The test now spawn itself and write the session id a file instead. - tst-spawn3.c need to know where pidfd_spawn is used so it keep an extra file description ununsed. Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid support), Linux 5.15 (only clone support), and Linux 5.19 (full support including clone3). --- NEWS | 7 + bits/spawn_ext.h | 21 +++ include/clone_internal.h | 4 + manual/process.texi | 14 +- posix/Makefile | 2 + posix/spawn.h | 2 + posix/spawn_int.h | 3 +- posix/tst-posix_spawn-setsid.c | 168 +++++++++++++----- posix/tst-spawn-chdir.c | 15 +- posix/tst-spawn.c | 24 +-- posix/tst-spawn.h | 36 ++++ posix/tst-spawn2.c | 17 +- posix/tst-spawn3.c | 100 ++++++----- posix/tst-spawn4.c | 7 +- posix/tst-spawn5.c | 14 +- posix/tst-spawn6.c | 15 +- posix/tst-spawn7.c | 13 +- sysdeps/unix/sysv/linux/Makefile | 31 +++- sysdeps/unix/sysv/linux/Versions | 4 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 2 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 2 + sysdeps/unix/sysv/linux/arc/libc.abilist | 2 + sysdeps/unix/sysv/linux/arm/be/libc.abilist | 2 + sysdeps/unix/sysv/linux/arm/le/libc.abilist | 2 + sysdeps/unix/sysv/linux/bits/spawn_ext.h | 45 +++++ sysdeps/unix/sysv/linux/clone-pidfd-support.c | 58 ++++++ sysdeps/unix/sysv/linux/csky/libc.abilist | 2 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 2 + sysdeps/unix/sysv/linux/i386/libc.abilist | 2 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 2 + .../sysv/linux/loongarch/lp64/libc.abilist | 2 + .../sysv/linux/m68k/coldfire/libc.abilist | 2 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 2 + .../sysv/linux/microblaze/be/libc.abilist | 2 + .../sysv/linux/microblaze/le/libc.abilist | 2 + .../sysv/linux/mips/mips32/fpu/libc.abilist | 2 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 2 + .../sysv/linux/mips/mips64/n32/libc.abilist | 2 + .../sysv/linux/mips/mips64/n64/libc.abilist | 2 + sysdeps/unix/sysv/linux/nios2/libc.abilist | 2 + sysdeps/unix/sysv/linux/or1k/libc.abilist | 2 + sysdeps/unix/sysv/linux/pidfd_spawn.c | 30 ++++ sysdeps/unix/sysv/linux/pidfd_spawnp.c | 30 ++++ .../linux/powerpc/powerpc32/fpu/libc.abilist | 2 + .../powerpc/powerpc32/nofpu/libc.abilist | 2 + .../linux/powerpc/powerpc64/be/libc.abilist | 2 + .../linux/powerpc/powerpc64/le/libc.abilist | 2 + .../unix/sysv/linux/riscv/rv32/libc.abilist | 2 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 2 + .../unix/sysv/linux/s390/s390-32/libc.abilist | 2 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 2 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 2 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 2 + .../sysv/linux/sparc/sparc32/libc.abilist | 2 + .../sysv/linux/sparc/sparc64/libc.abilist | 2 + sysdeps/unix/sysv/linux/spawni.c | 20 ++- .../sysv/linux/tst-posix_spawn-setsid-pidfd.c | 20 +++ .../unix/sysv/linux/tst-spawn-chdir-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn-pidfd.h | 63 +++++++ sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c | 20 +++ sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c | 20 +++ .../unix/sysv/linux/x86_64/64/libc.abilist | 2 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 2 + 68 files changed, 838 insertions(+), 153 deletions(-) create mode 100644 bits/spawn_ext.h create mode 100644 posix/tst-spawn.h create mode 100644 sysdeps/unix/sysv/linux/bits/spawn_ext.h create mode 100644 sysdeps/unix/sysv/linux/clone-pidfd-support.c create mode 100644 sysdeps/unix/sysv/linux/pidfd_spawn.c create mode 100644 sysdeps/unix/sysv/linux/pidfd_spawnp.c create mode 100644 sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn-pidfd.h create mode 100644 sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c create mode 100644 sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c diff --git a/NEWS b/NEWS index 83d082afad..081d0180f4 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,13 @@ Major new features: * PRIb* and PRIB* macros from C2X have been added to . +* On Linux, the pidfd_spawn and pidfd_spawp function have been added. + They have similar prototype and semantic as posix_spawn, but instead of + returning a process ID, they return a file descriptor that can be used + along other pidfd function (like pidfd_send_signal, poll, or waitid). + The pidfd functionality avoid the issue of PID reuse with traditional + posix_spawn interface. + Deprecated and removed features, and other changes affecting compatibility: * In the Linux kernel for the hppa/parisc architecture some of the diff --git a/bits/spawn_ext.h b/bits/spawn_ext.h new file mode 100644 index 0000000000..75b504a768 --- /dev/null +++ b/bits/spawn_ext.h @@ -0,0 +1,21 @@ +/* POSIX spawn extensions. Generic version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SPAWN_H +# error "Never include directly; use instead." +#endif diff --git a/include/clone_internal.h b/include/clone_internal.h index dd380f119e..54df6c8917 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -35,6 +35,10 @@ extern int __clone_internal_fallback (struct clone_args *__cl_args, void *__arg) attribute_hidden; +/* Return whether the kernel supports pid file descriptor, including clone + with CLONE_PIDFD and waitid with P_PIDFD. */ +extern bool __clone_pidfd_supported (void) attribute_hidden; + #ifndef _ISOMAC libc_hidden_proto (__clone3) libc_hidden_proto (__clone_internal) diff --git a/manual/process.texi b/manual/process.texi index 9307379194..927aebbe3d 100644 --- a/manual/process.texi +++ b/manual/process.texi @@ -136,13 +136,13 @@ creating a process and making it run another program. @cindex parent process @cindex subprocess A new processes is created when one of the functions -@code{posix_spawn}, @code{fork}, @code{_Fork} or @code{vfork} is called. -(The @code{system} and @code{popen} also create new processes internally.) -Due to the name of the @code{fork} function, the act of creating a new -process is sometimes called @dfn{forking} a process. Each new process -(the @dfn{child process} or @dfn{subprocess}) is allocated a process -ID, distinct from the process ID of the parent process. @xref{Process -Identification}. +@code{posix_spawn}, @code{fork}, @code{_Fork}, @code{vfork}, or +@code{pidfd_spawn} is called. (The @code{system} and @code{popen} also +create new processes internally.) Due to the name of the @code{fork} +function, the act of creating a new process is sometimes called +@dfn{forking} a process. Each new process (the @dfn{child process} or +@dfn{subprocess}) is allocated a process ID, distinct from the process +ID of the parent process. @xref{Process Identification}. After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a diff --git a/posix/Makefile b/posix/Makefile index 3e7ca8e18c..7ace82057e 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -37,6 +37,7 @@ headers := \ bits/pthreadtypes-arch.h \ bits/pthreadtypes.h \ bits/sched.h \ + bits/spawn_ext.h \ bits/thread-shared-types.h \ bits/types.h \ bits/types/idtype_t.h \ @@ -591,6 +592,7 @@ tst-spawn-static-ARGS = $(tst-spawn-ARGS) tst-spawn5-ARGS = -- $(host-test-program-cmd) tst-spawn6-ARGS = -- $(host-test-program-cmd) tst-spawn7-ARGS = -- $(host-test-program-cmd) +tst-posix_spawn-setsid-ARGS = -- $(host-test-program-cmd) tst-dir-ARGS = `pwd` `cd $(common-objdir)/$(subdir); pwd` `cd $(common-objdir); pwd` $(objpfx)tst-dir tst-chmod-ARGS = $(objdir) tst-vfork3-ARGS = --test-dir=$(objpfx) diff --git a/posix/spawn.h b/posix/spawn.h index 0a6a070523..2e20991627 100644 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -229,6 +229,8 @@ posix_spawn_file_actions_addtcsetpgrp_np (posix_spawn_file_actions_t *, #endif /* __USE_MISC */ +#include + __END_DECLS #endif /* spawn.h */ diff --git a/posix/spawn_int.h b/posix/spawn_int.h index aeb066c44f..64ee03e62d 100644 --- a/posix/spawn_int.h +++ b/posix/spawn_int.h @@ -76,12 +76,13 @@ struct __spawn_action #define SPAWN_XFLAGS_USE_PATH 0x1 #define SPAWN_XFLAGS_TRY_SHELL 0x2 +#define SPAWN_XFLAGS_RET_PIDFD 0x4 extern int __posix_spawn_file_actions_realloc (posix_spawn_file_actions_t * file_actions) attribute_hidden; -extern int __spawni (pid_t *pid, const char *path, +extern int __spawni (int *pid, const char *path, const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *attrp, char *const argv[], char *const envp[], int xflags) attribute_hidden; diff --git a/posix/tst-posix_spawn-setsid.c b/posix/tst-posix_spawn-setsid.c index 124d878ce2..751674165c 100644 --- a/posix/tst-posix_spawn-setsid.c +++ b/posix/tst-posix_spawn-setsid.c @@ -18,78 +18,158 @@ #include #include +#include +#include +#include #include #include #include +#include #include +#include #include #include +#include +#include +#include + +/* Nonzero if the program gets called via `exec'. */ +static int restart; + +/* Hold the four initial argument used to respawn the process, plus + the extra '--direct' and '--restart', and a final NULL. */ +static char *initial_argv[7]; +static int initial_argv_count; + +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +static char *pidfile; + +static pid_t +read_child_sid (void) +{ + int pidfd = xopen (pidfile, O_RDONLY, 0); + + char buf[INT_STRLEN_BOUND (pid_t)]; + ssize_t n = read (pidfd, buf, sizeof (buf)); + TEST_VERIFY (n < sizeof buf && n >= 0); + buf[n] = '\0'; + + /* We only expect to read the PID. */ + char *endp; + long int rpid = strtol (buf, &endp, 10); + TEST_VERIFY (endp != buf); + + xclose (pidfd); + + return rpid; +} + +/* Called on process re-execution, write down the session id on PIDFILE. */ +_Noreturn static void +handle_restart (const char *pidfile) +{ + int pidfd = xopen (pidfile, O_WRONLY, 0); + + char buf[INT_STRLEN_BOUND (pid_t)]; + int s = snprintf (buf, sizeof buf, "%d", getsid (0)); + size_t n = write (pidfd, buf, s); + TEST_VERIFY (n == s); + + xclose (pidfd); + + exit (EXIT_SUCCESS); +} static void do_test_setsid (bool test_setsid) { - pid_t sid, child_sid; - int res; - /* Current session ID. */ - sid = getsid(0); - if (sid == (pid_t) -1) - FAIL_EXIT1 ("getsid (0): %m"); + pid_t sid = getsid (0); + TEST_VERIFY (sid != (pid_t) -1); posix_spawnattr_t attrp; - /* posix_spawnattr_init should not fail (it basically memset the - attribute). */ - posix_spawnattr_init (&attrp); + TEST_COMPARE (posix_spawnattr_init (&attrp), 0); if (test_setsid) - { - res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID); - if (res != 0) - { - errno = res; - FAIL_EXIT1 ("posix_spawnattr_setflags: %m"); - } - } - - /* Program to run. */ - char *args[2] = { (char *) "true", NULL }; - pid_t child; - - res = posix_spawnp (&child, "true", NULL, &attrp, args, environ); - /* posix_spawnattr_destroy is noop. */ - posix_spawnattr_destroy (&attrp); - - if (res != 0) - { - errno = res; - FAIL_EXIT1 ("posix_spawnp: %m"); - } + TEST_COMPARE (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID), 0); + + /* 1 or 4 elements from initial_argv: + + path to ld.so optional + + --library-path optional + + the library path optional + + application name + + --direct + + --restart + + pidfile */ + int argv_size = initial_argv_count + 2; + char *args[argv_size]; + int argc = 0; + + for (char **arg = initial_argv; *arg != NULL; arg++) + args[argc++] = *arg; + args[argc++] = pidfile; + args[argc] = NULL; + TEST_VERIFY (argc < argv_size); + + PID_T_TYPE pid; + TEST_COMPARE (POSIX_SPAWN (&pid, args[0], NULL, &attrp, args, environ), 0); + TEST_COMPARE (posix_spawnattr_destroy (&attrp), 0); + + siginfo_t sinfo; + TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + + pid_t child_sid = read_child_sid (); /* Child should have a different session ID than parent. */ - child_sid = getsid (child); - - if (child_sid == (pid_t) -1) - FAIL_EXIT1 ("getsid (%i): %m", child); + TEST_VERIFY (child_sid != (pid_t) -1); if (test_setsid) - { - if (child_sid == sid) - FAIL_EXIT1 ("child session ID matched parent one"); - } + TEST_VERIFY (child_sid != sid); else - { - if (child_sid != sid) - FAIL_EXIT1 ("child session ID did not match parent one"); - } + TEST_VERIFY (child_sid == sid); } static int -do_test (void) +do_test (int argc, char *argv[]) { + /* We must have either: + + - one or four parameters if called initially: + + argv[1]: path for ld.so optional + + argv[2]: "--library-path" optional + + argv[3]: the library path optional + + argv[4]: the application name + + - six parameters left if called through re-execution: + + argv[5/1]: the application name + + argv[6/2]: the pidfile + + * When built with --enable-hardcoded-path-in-tests or issued without + using the loader directly. */ + + if (restart) + handle_restart (argv[1]); + + TEST_VERIFY_EXIT (argc == 2 || argc == 5); + + int i; + for (i = 0; i < argc - 1; i++) + initial_argv[i] = argv[i + 1]; + initial_argv[i++] = (char *) "--direct"; + initial_argv[i++] = (char *) "--restart"; + initial_argv_count = i; + + create_temp_file ("tst-posix_spawn-setsid-", &pidfile); + do_test_setsid (false); do_test_setsid (true); return 0; } +#define TEST_FUNCTION_ARGV do_test #include diff --git a/posix/tst-spawn-chdir.c b/posix/tst-spawn-chdir.c index b335092d7f..c01ca6692d 100644 --- a/posix/tst-spawn-chdir.c +++ b/posix/tst-spawn-chdir.c @@ -29,7 +29,9 @@ #include #include #include +#include #include +#include /* Reads the file at PATH, which must consist of exactly one line. Removes the line terminator at the end of the file. */ @@ -169,17 +171,18 @@ do_test (void) char *const argv[] = { (char *) "pwd", NULL }; char *const envp[] = { NULL } ; - pid_t pid; + PID_T_TYPE pid; if (do_spawnp) - TEST_COMPARE (posix_spawnp (&pid, "pwd", &actions, + TEST_COMPARE (POSIX_SPAWNP (&pid, "pwd", &actions, NULL, argv, envp), 0); else - TEST_COMPARE (posix_spawn (&pid, "subdir/pwd-symlink", &actions, + TEST_COMPARE (POSIX_SPAWN (&pid, "subdir/pwd-symlink", &actions, NULL, argv, envp), 0); TEST_VERIFY (pid > 0); - int status; - xwaitpid (pid, &status, 0); - TEST_COMPARE (status, 0); + siginfo_t sinfo; + TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); /* Check that the current directory did not change. */ { diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c index 6782a322fc..c44d90756a 100644 --- a/posix/tst-spawn.c +++ b/posix/tst-spawn.c @@ -25,11 +25,13 @@ #include #include #include +#include #include #include #include #include +#include /* Nonzero if the program gets called via `exec'. */ @@ -143,9 +145,9 @@ handle_restart (const char *fd1s, const char *fd2s, const char *fd3s, static int do_test (int argc, char *argv[]) { - pid_t pid; + PID_T_TYPE pid; int fd4; - int status; + siginfo_t sinfo; posix_spawn_file_actions_t actions; char fd1name[18]; char fd2name[18]; @@ -233,17 +235,16 @@ do_test (int argc, char *argv[]) spargv[i++] = fd5name; spargv[i] = NULL; - TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ), + TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], &actions, NULL, spargv, environ), 0); /* Wait for the children. */ - TEST_COMPARE (xwaitpid (pid, &status, 0), pid); - TEST_VERIFY (WIFEXITED (status)); - TEST_VERIFY (!WIFSIGNALED (status)); - TEST_COMPARE (WEXITSTATUS (status), 0); + TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); /* Same test but with a NULL pid argument. */ - TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ), + TEST_COMPARE (POSIX_SPAWN (NULL, argv[1], &actions, NULL, spargv, environ), 0); /* Cleanup. */ @@ -251,10 +252,9 @@ do_test (int argc, char *argv[]) free (name3_copy); /* Wait for the children. */ - xwaitpid (-1, &status, 0); - TEST_VERIFY (WIFEXITED (status)); - TEST_VERIFY (!WIFSIGNALED (status)); - TEST_COMPARE (WEXITSTATUS (status), 0); + TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); return 0; } diff --git a/posix/tst-spawn.h b/posix/tst-spawn.h new file mode 100644 index 0000000000..a6f2dc8680 --- /dev/null +++ b/posix/tst-spawn.h @@ -0,0 +1,36 @@ +/* Generic definitions for posix_spawn tests. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef PID_T_TYPE +# define PID_T_TYPE pid_t +#endif + +#ifndef POSIX_SPAWN +# define POSIX_SPAWN(__child, __path, __actions, __attr, __argv, __envp) \ + posix_spawn (__child, __path, __actions, __attr, __argv, __envp) +#endif + +#ifndef POSIX_SPAWNP +# define POSIX_SPAWNP(__child, __path, __actions, __attr, __argv, __envp) \ + posix_spawnp (__child, __path, __actions, __attr, __argv, __envp) +#endif + +#ifndef WAITID +# define WAITID(__idtype, __id, __info, __opts) \ + waitid (__idtype, __id, __info, __opts) +#endif diff --git a/posix/tst-spawn2.c b/posix/tst-spawn2.c index 40dc692488..f5c1f13039 100644 --- a/posix/tst-spawn2.c +++ b/posix/tst-spawn2.c @@ -26,6 +26,7 @@ #include #include +#include int do_test (void) @@ -35,9 +36,9 @@ do_test (void) const char *program = "/path/to/invalid/binary"; char * const args[] = { 0 }; - pid_t pid = -1; + PID_T_TYPE pid = -1; - int ret = posix_spawn (&pid, program, 0, 0, args, environ); + int ret = POSIX_SPAWN (&pid, program, 0, 0, args, environ); if (ret != ENOENT) { errno = ret; @@ -51,14 +52,13 @@ do_test (void) FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid); /* Check if no child is actually created. */ - ret = waitpid (-1, NULL, 0); - if (ret != -1 || errno != ECHILD) - FAIL_EXIT1 ("waitpid: %m)"); + TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1); + TEST_COMPARE (errno, ECHILD); /* Same as before, but with posix_spawnp. */ char *args2[] = { (char*) program, 0 }; - ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ); + ret = POSIX_SPAWNP (&pid, args2[0], 0, 0, args2, environ); if (ret != ENOENT) { errno = ret; @@ -68,9 +68,8 @@ do_test (void) if (pid != -1) FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid); - ret = waitpid (-1, NULL, 0); - if (ret != -1 || errno != ECHILD) - FAIL_EXIT1 ("waitpid: %m)"); + TEST_COMPARE (WAITID (P_ALL, 0, NULL, WEXITED), -1); + TEST_COMPARE (errno, ECHILD); return 0; } diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c index e7ce0fb386..bd21ac6c4b 100644 --- a/posix/tst-spawn3.c +++ b/posix/tst-spawn3.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -27,9 +28,12 @@ #include #include #include +#include #include #include +#include +#include static int do_test (void) @@ -48,7 +52,6 @@ do_test (void) struct rlimit rl; int max_fd = 24; - int ret; /* Set maximum number of file descriptor to a low value to avoid open too many files in environments where RLIMIT_NOFILE is large and to @@ -66,7 +69,7 @@ do_test (void) /* Exhauste the file descriptor limit with temporary files. */ int files[max_fd]; int nfiles = 0; - for (;;) + for (; nfiles < max_fd; nfiles++) { int fd = create_temp_file ("tst-spawn3.", NULL); if (fd == -1) @@ -75,75 +78,82 @@ do_test (void) FAIL_EXIT1 ("create_temp_file: %m"); break; } - files[nfiles++] = fd; + files[nfiles] = fd; } + assert (nfiles != 0); posix_spawn_file_actions_t a; - if (posix_spawn_file_actions_init (&a) != 0) - FAIL_EXIT1 ("posix_spawn_file_actions_init"); + TEST_COMPARE (posix_spawn_file_actions_init (&a), 0); /* Executes a /bin/sh echo $$ 2>&1 > ${objpfx}tst-spawn3.pid . */ const char pidfile[] = OBJPFX "tst-spawn3.pid"; - if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY - | O_CREAT | O_TRUNC, 0644) != 0) - FAIL_EXIT1 ("posix_spawn_file_actions_addopen"); + TEST_COMPARE (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, + O_WRONLY| O_CREAT | O_TRUNC, + 0644), + 0); - if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0) - FAIL_EXIT1 ("posix_spawn_file_actions_adddup2"); + TEST_COMPARE (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, + STDERR_FILENO), + 0); /* Since execve (called by posix_spawn) might require to open files to actually execute the shell script, setup to close the temporary file descriptors. */ - for (int i=0; i= 0); - unlink (pidfile); + xunlink (pidfile); /* We only expect to read the PID. */ char *endp; long int rpid = strtol (buf, &endp, 10); - if (*endp != '\n') - FAIL_EXIT1 ("*endp != \'n\'"); - if (endp == buf) - FAIL_EXIT1 ("read empty line"); + TEST_VERIFY (*endp == '\n' && endp != buf); - if (rpid != pid) - FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid); + TEST_COMPARE (rpid, sinfo.si_pid); return 0; } diff --git a/posix/tst-spawn4.c b/posix/tst-spawn4.c index 327f04ea6c..8bf8bd52df 100644 --- a/posix/tst-spawn4.c +++ b/posix/tst-spawn4.c @@ -24,6 +24,7 @@ #include #include #include +#include static int do_test (void) @@ -38,15 +39,15 @@ do_test (void) TEST_VERIFY_EXIT (chmod (scriptname, 0x775) == 0); - pid_t pid; + PID_T_TYPE pid; int status; /* Check if scripts without shebang are correctly not executed. */ - status = posix_spawn (&pid, scriptname, NULL, NULL, (char *[]) { 0 }, + status = POSIX_SPAWN (&pid, scriptname, NULL, NULL, (char *[]) { 0 }, (char *[]) { 0 }); TEST_VERIFY_EXIT (status == ENOEXEC); - status = posix_spawnp (&pid, scriptname, NULL, NULL, (char *[]) { 0 }, + status = POSIX_SPAWNP (&pid, scriptname, NULL, NULL, (char *[]) { 0 }, (char *[]) { 0 }); TEST_VERIFY_EXIT (status == ENOEXEC); diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c index 1bd1afabcc..e42f0eb25c 100644 --- a/posix/tst-spawn5.c +++ b/posix/tst-spawn5.c @@ -33,6 +33,7 @@ #include #include +#include /* Nonzero if the program gets called via `exec'. */ static int restart; @@ -161,14 +162,13 @@ spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd, args[argc] = NULL; TEST_VERIFY (argc < argv_size); - pid_t pid; - int status; + PID_T_TYPE pid; + siginfo_t sinfo; - TEST_COMPARE (posix_spawn (&pid, args[0], fa, NULL, args, environ), 0); - TEST_COMPARE (xwaitpid (pid, &status, 0), pid); - TEST_VERIFY (WIFEXITED (status)); - TEST_VERIFY (!WIFSIGNALED (status)); - TEST_COMPARE (WEXITSTATUS (status), 0); + TEST_COMPARE (POSIX_SPAWN (&pid, args[0], fa, NULL, args, environ), 0); + TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); } static void diff --git a/posix/tst-spawn6.c b/posix/tst-spawn6.c index 4e29d78168..ff36351cd6 100644 --- a/posix/tst-spawn6.c +++ b/posix/tst-spawn6.c @@ -32,6 +32,7 @@ #include #include #include +#include #ifndef PATH_MAX # define PATH_MAX 1024 @@ -108,17 +109,15 @@ run_subprogram (int argc, char *argv[], const posix_spawnattr_t *attr, spargv[i] = NULL; pid_t pid; - TEST_COMPARE (posix_spawn (&pid, argv[1], actions, attr, spargv, environ), + TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], actions, attr, spargv, environ), exp_err); if (exp_err != 0) return; - int status; - TEST_COMPARE (xwaitpid (pid, &status, WUNTRACED), pid); - TEST_VERIFY (WIFEXITED (status)); - TEST_VERIFY (!WIFSTOPPED (status)); - TEST_VERIFY (!WIFSIGNALED (status)); - TEST_COMPARE (WEXITSTATUS (status), 0); + siginfo_t sinfo; + TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); } static int @@ -202,7 +201,7 @@ do_test (int argc, char *argv[]) if (restart) return handle_restart (argv[1], argv[2]); - pid_t pid = xfork (); + PID_T_TYPE pid = xfork (); if (pid == 0) { /* Create a pseudo-terminal to avoid interfering with the one using by diff --git a/posix/tst-spawn7.c b/posix/tst-spawn7.c index fb06915cb7..cc4498830b 100644 --- a/posix/tst-spawn7.c +++ b/posix/tst-spawn7.c @@ -24,7 +24,9 @@ #include #include #include +#include #include +#include /* Nonzero if the program gets called via `exec'. */ #define CMDLINE_OPTIONS \ @@ -81,14 +83,13 @@ spawn_signal_test (const char *type, const posix_spawnattr_t *attr) { spargs[check_type_argc] = (char*) type; - pid_t pid; - int status; + PID_T_TYPE pid; + siginfo_t sinfo; TEST_COMPARE (posix_spawn (&pid, spargs[0], NULL, attr, spargs, environ), 0); - TEST_COMPARE (xwaitpid (pid, &status, 0), pid); - TEST_VERIFY (WIFEXITED (status)); - TEST_VERIFY (!WIFSIGNALED (status)); - TEST_COMPARE (WEXITSTATUS (status), 0); + TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); } static void diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index b4323b2604..15e0d69a4b 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -435,9 +435,28 @@ endif ifeq ($(subdir),posix) sysdep_headers += bits/initspin.h -sysdep_routines += sched_getcpu oldglob getcpu +sysdep_routines += \ + clone-pidfd-support \ + getcpu \ + oldglob \ + sched_getcpu \ + pidfd_spawn \ + pidfd_spawnp \ + # sysdep_routines -tests += tst-affinity tst-affinity-pid +tests += \ + tst-affinity \ + tst-affinity-pid \ + tst-spawn-pidfd \ + tst-spawn2-pidfd \ + tst-spawn3-pidfd \ + tst-spawn4-pidfd \ + tst-spawn5-pidfd \ + tst-spawn6-pidfd \ + tst-spawn7-pidfd \ + tst-spawn-chdir-pidfd \ + tst-posix_spawn-setsid-pidfd \ + # tests tests-static := tst-affinity-static tests += $(tests-static) @@ -445,6 +464,14 @@ tests += $(tests-static) CFLAGS-fork.c = $(libio-mtsafe) CFLAGS-getpid.o = -fomit-frame-pointer CFLAGS-getpid.os = -fomit-frame-pointer + +CFLAGS-tst-spawn3-pidfd.c += -DOBJPFX=\"$(objpfx)\" + +tst-spawn-pidfd-ARGS = -- $(host-test-program-cmd) +tst-spawn5-pidfd-ARGS = -- $(host-test-program-cmd) +tst-spawn6-pidfd-ARGS = -- $(host-test-program-cmd) +tst-spawn7-pidfd-ARGS = -- $(host-test-program-cmd) +tst-posix_spawn-setsid-pidfd-ARGS = -- $(host-test-program-cmd) endif ifeq ($(subdir),inet) diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions index bc59bce42f..28825473ae 100644 --- a/sysdeps/unix/sysv/linux/Versions +++ b/sysdeps/unix/sysv/linux/Versions @@ -321,6 +321,10 @@ libc { __ppoll64_chk; %endif } + GLIBC_2.38 { + pidfd_spawn; + pidfd_spawnp; + } GLIBC_PRIVATE { # functions used in other libraries __syscall_rt_sigqueueinfo; diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index 0e2d9c3045..bac74ae474 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2665,3 +2665,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index f1bec1978d..dadd302f8d 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2774,6 +2774,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist index aa874b88d0..0f4f07b6bb 100644 --- a/sysdeps/unix/sysv/linux/arc/libc.abilist +++ b/sysdeps/unix/sysv/linux/arc/libc.abilist @@ -2426,3 +2426,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index afbd57da6f..38e5dcb81a 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -546,6 +546,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0xa0 GLIBC_2.4 _IO_2_1_stdin_ D 0xa0 diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index e7364cd3fe..423d20cbb5 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -543,6 +543,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0xa0 GLIBC_2.4 _IO_2_1_stdin_ D 0xa0 diff --git a/sysdeps/unix/sysv/linux/bits/spawn_ext.h b/sysdeps/unix/sysv/linux/bits/spawn_ext.h new file mode 100644 index 0000000000..ec56923d88 --- /dev/null +++ b/sysdeps/unix/sysv/linux/bits/spawn_ext.h @@ -0,0 +1,45 @@ +/* POSIX spawn extensions. Linux version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SPAWN_H +# error "Never include directly; use instead." +#endif + +__BEGIN_DECLS + +#ifdef __USE_GNU + +extern int pidfd_spawn (int *__restrict __pidfd, + const char *__restrict __file, + const posix_spawn_file_actions_t *__restrict __facts, + const posix_spawnattr_t *__restrict __attrp, + char *const __argv[__restrict_arr], + char *const __envp[__restrict_arr]) + __nonnull ((2, 5)); + +extern int pidfd_spawnp (int *__restrict __pidfd, + const char *__restrict __path, + const posix_spawn_file_actions_t *__restrict __facts, + const posix_spawnattr_t *__restrict __attrp, + char *const __argv[__restrict_arr], + char *const __envp[__restrict_arr]) + __nonnull ((2, 5)); + +#endif /* __USE_GNU */ + +__END_DECLS diff --git a/sysdeps/unix/sysv/linux/clone-pidfd-support.c b/sysdeps/unix/sysv/linux/clone-pidfd-support.c new file mode 100644 index 0000000000..4bf2317c17 --- /dev/null +++ b/sysdeps/unix/sysv/linux/clone-pidfd-support.c @@ -0,0 +1,58 @@ +/* Check if kernel supports PID file descriptors. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +/* The PID file descriptors was added during multiple releases: + - Linux 5.2 added CLONE_PIDFD support for clone and __clone_pidfd_supported + syscall. + - Linux 5.3 added support for poll and CLONE_PIDFD for clone3. + - Linux 5.4 added P_PIDFD support on waitid. + + For internal usage on spawn and fork, it only make sense to return a file + descriptor if caller can actually waitid on it. */ +bool +__clone_pidfd_supported (void) +{ + static int supported = 0; + int state = atomic_load_relaxed (&supported); + if (state == 0) + { + /* Linux define the maximum allocated file descriptor value as + 0x7fffffc0 (from fs/file.c): + + #define __const_min(x, y) ((x) < (y) ? (x) : (y)) + unsigned int sysctl_nr_open_max = + __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG; + + So it can detect whther kernel support all pidd support by using + using a valid but never allocated file descriptor: if is not + supported waitid will return EINVAL, otherwise EBADF. + + Also tThe waitid is a cancellation entrypoint, so issue the syscall + directly. */ + int r = INTERNAL_SYSCALL_CALL (waitid, P_PIDFD, INT_MAX, NULL, + WEXITED | WNOHANG, NULL); + state = r == -EBADF ? 1 : -1; + atomic_store_relaxed (&supported, state); + } + + return state == 1; +} diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index 913fa59215..488418d14e 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2702,3 +2702,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index 43af3a9811..75d495e1c7 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2651,6 +2651,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index af72f8fab0..e53877c52a 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2835,6 +2835,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index 48cbb0fa50..5ed2b1ebf4 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2600,6 +2600,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist index c15884bb0b..d050fbf6d0 100644 --- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist +++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist @@ -2186,3 +2186,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 3738db81df..6465af266c 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -547,6 +547,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F GLIBC_2.4 _IO_2_1_stderr_ D 0x98 GLIBC_2.4 _IO_2_1_stdin_ D 0x98 diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index ed13627752..00013cc564 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2778,6 +2778,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index 8357738621..34085d81ab 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2751,3 +2751,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index 58c5da583d..b26b116f1f 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2748,3 +2748,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index d3741945cd..c86418d156 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2743,6 +2743,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 5319fdc204..3c6f8f4d60 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2741,6 +2741,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index 1743ea6eb9..8dfa065638 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2749,6 +2749,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 9b1f53c6ac..0ca815f2cd 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2651,6 +2651,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index ae1c6ca1b5..a1e0f772c6 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2790,3 +2790,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist index a7c572c947..91cd6075b9 100644 --- a/sysdeps/unix/sysv/linux/or1k/libc.abilist +++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist @@ -2172,3 +2172,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/pidfd_spawn.c b/sysdeps/unix/sysv/linux/pidfd_spawn.c new file mode 100644 index 0000000000..9f4a5780e6 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pidfd_spawn.c @@ -0,0 +1,30 @@ +/* pidfd_spawn - Spawn a process and return a pid file descriptor. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include "spawn_int.h" + +int +pidfd_spawn (int *pidfd, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, char *const argv[], + char *const envp[]) +{ + return __spawni (pidfd, path, file_actions, attrp, argv, envp, + SPAWN_XFLAGS_RET_PIDFD); +} diff --git a/sysdeps/unix/sysv/linux/pidfd_spawnp.c b/sysdeps/unix/sysv/linux/pidfd_spawnp.c new file mode 100644 index 0000000000..c8260fcd01 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pidfd_spawnp.c @@ -0,0 +1,30 @@ +/* pidfd_spawnp - Spawn a process and return a pid file descriptor. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include "spawn_int.h" + +int +pidfd_spawnp (int *pidfd, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, char *const argv[], + char *const envp[]) +{ + return __spawni (pidfd, path, file_actions, attrp, argv, envp, + SPAWN_XFLAGS_USE_PATH | SPAWN_XFLAGS_RET_PIDFD); +} diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index 074fa031a7..21244ece27 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2817,6 +2817,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index dfcb4bd2d5..238d00998b 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2850,6 +2850,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index 63bbccf3f9..36fb9b57ac 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2571,6 +2571,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index ab85fd61ef..0c25995661 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2885,3 +2885,5 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index b716f5c763..cda030f623 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2428,3 +2428,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index 774e777b65..bd20c202fb 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2628,3 +2628,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index 8625135c48..a1e9247f72 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2815,6 +2815,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index d00c7eb262..b80a131bde 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2608,6 +2608,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index b63037241d..496a0185b6 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2658,6 +2658,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index d80055617d..3e0d858d08 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2655,6 +2655,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index 5be55c11d2..84dcda8db2 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2810,6 +2810,8 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F GLIBC_2.4 _IO_printf F GLIBC_2.4 _IO_sprintf F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index 475fdaae15..1688a0a7e9 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2623,6 +2623,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index bc321d4c58..2d2aed7e94 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -68,6 +68,7 @@ struct posix_spawn_args int xflags; bool use_clone3; int err; + int pidfd; }; /* Older version requires that shell script without shebang definition @@ -309,7 +310,7 @@ fail: /* Spawn a new process executing PATH with the attributes describes in *ATTRP. Before running the process perform the actions described in FILE-ACTIONS. */ static int -__spawnix (pid_t * pid, const char *file, +__spawnix (int *pid, const char *file, const posix_spawn_file_actions_t * file_actions, const posix_spawnattr_t * attrp, char *const argv[], char *const envp[], int xflags, @@ -319,6 +320,15 @@ __spawnix (pid_t * pid, const char *file, struct posix_spawn_args args; int ec; + bool use_pidfd = xflags & SPAWN_XFLAGS_RET_PIDFD; + + /* For CLONE_PIDFD, older kernel might not fail with unsupported flags or + some version might not support waitid (P_PIDFD). So to avoid the need + to handle the error on the helper process, check for full pidfd + support. */ + if (use_pidfd && !__clone_pidfd_supported ()) + return ENOSYS; + /* To avoid imposing hard limits on posix_spawn{p} the total number of arguments is first calculated to allocate a mmap to hold all possible values. */ @@ -368,6 +378,7 @@ __spawnix (pid_t * pid, const char *file, args.argv = argv; args.argc = argc; args.envp = envp; + args.pidfd = 0; args.xflags = xflags; internal_signal_block_all (&args.oldmask); @@ -384,10 +395,13 @@ __spawnix (pid_t * pid, const char *file, { /* Unsupported flags like CLONE_CLEAR_SIGHAND will be cleared up by __clone_internal_fallback. */ - .flags = CLONE_CLEAR_SIGHAND | CLONE_VM | CLONE_VFORK, + .flags = CLONE_CLEAR_SIGHAND | CLONE_VM | CLONE_VFORK + | (use_pidfd ? CLONE_PIDFD : 0), .exit_signal = SIGCHLD, .stack = (uintptr_t) stack, .stack_size = stack_size, + .pidfd = use_pidfd ? (uintptr_t) &args.pidfd : 0, + .parent_tid = use_pidfd ? (uintptr_t) &args.pidfd : 0, }; #ifdef HAVE_CLONE3_WRAPPER args.use_clone3 = true; @@ -429,7 +443,7 @@ __spawnix (pid_t * pid, const char *file, __munmap (stack, stack_size); if ((ec == 0) && (pid != NULL)) - *pid = new_pid; + *pid = use_pidfd ? args.pidfd : new_pid; internal_signal_restore_set (&args.oldmask); diff --git a/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c b/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c new file mode 100644 index 0000000000..4372833f07 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-posix_spawn-setsid-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c new file mode 100644 index 0000000000..019527b31b --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn-chdir-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c new file mode 100644 index 0000000000..c430995af8 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h new file mode 100644 index 0000000000..ea51c22447 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn-pidfd.h @@ -0,0 +1,63 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +#define PID_T_TYPE int + +/* Call posix_spawn with POSIX_SPAWN_PIDFD set. */ +static inline int +pidfd_spawn_check (int *pidfd, const char *path, + const posix_spawn_file_actions_t *fa, + const posix_spawnattr_t *attr, char *const argv[], + char *const envp[]) +{ + int r = pidfd_spawn (pidfd, path, fa, attr, argv, envp); + if (r == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag"); + return r; +} + +#define POSIX_SPAWN(__pidfd, __path, __actions, __attr, __argv, __envp) \ + pidfd_spawn_check (__pidfd, __path, __actions, __attr, __argv, __envp) + +static inline int +pidfd_spawnp_check (int *pidfd, const char *file, + const posix_spawn_file_actions_t *fa, + const posix_spawnattr_t *attr, + char *const argv[], char *const envp[]) +{ + int r = pidfd_spawnp (pidfd, file, fa, attr, argv, envp); + if (r == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag"); + return r; +} + +#define POSIX_SPAWNP(__child, __path, __actions, __attr, __argv, __envp) \ + pidfd_spawnp_check (__child, __path, __actions, __attr, __argv, __envp) + +#define WAITID(__idtype, __id, __info, __opts) \ + ({ \ + __typeof (__idtype) __new_idtype = __idtype == P_PID \ + ? P_PIDFD : __idtype; \ + waitid (__new_idtype, __id, __info, __opts); \ + }) + +#define TST_SPAWN_PIDFD 1 diff --git a/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c new file mode 100644 index 0000000000..03ba7a3d15 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn2-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c new file mode 100644 index 0000000000..8ad9a16854 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn3-pidfd.c @@ -0,0 +1,20 @@ +/* Check posix_spawn add file actions. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c new file mode 100644 index 0000000000..83922da7d1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn4-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c new file mode 100644 index 0000000000..149c352bf8 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn5-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c new file mode 100644 index 0000000000..d3f5859457 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn6-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c b/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c new file mode 100644 index 0000000000..3aec86bec2 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-spawn7-pidfd.c @@ -0,0 +1,20 @@ +/* Tests for spawn pidfd extension. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index 6cfb928bc8..b7422d5747 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2574,6 +2574,8 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index c735097172..90680bc1d7 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2680,3 +2680,5 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_spawn F +GLIBC_2.38 pidfd_spawnp F From patchwork Tue Apr 18 21:35:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 67964 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C3813853836 for ; Tue, 18 Apr 2023 21:36:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C3813853836 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681853782; bh=gFSjBsQEMGmNIVnEuLbsEN4QRktDOpaVTAGBcRwluWQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=J+Srk5PfgXoEt5gk/4NFWOfPy4HN0wCZ+pG59LM4H58K91EvtOHVtz7lNIZVp79Bo m+V9le2GknMazcFic9BXdtaQ1NRjzN3StaeXHLMbJL20VyUaDDzesG7TV8s+KdZaDH /z+0eydO+nQ14+3n0Lp7Lw2Ke9N8+XqyAIx7rLnc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 84FAE3857341 for ; Tue, 18 Apr 2023 21:35:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 84FAE3857341 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6a5f852002dso738622a34.3 for ; Tue, 18 Apr 2023 14:35:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681853716; x=1684445716; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gFSjBsQEMGmNIVnEuLbsEN4QRktDOpaVTAGBcRwluWQ=; b=exac/51iVcCBV0AAfY9UL+11gIYnGHbNJ8H6BJDm1XYTf05DlCPB1s+WNRKn2ltlw+ POEM/lR/xzWk+Yd+qgiXOo32a72YWqVR4Ez8qLnszPRwJ5rC4e62qaDeCSBaTUkDaByp 9hPIIfqvlRsaAbJqR3KNbCMQG3cu3sgpBgGCFemOuir3P8j8tdLG7Opo5StwpoLnxx3E PV1MX1DNqHQAoRpZcWAY+b0NySlbxzeb92ul00GfktNocZtidLn54JYSEfckmAJB2HU5 bIicind8beSVgrsI6iPwUw9pQ1XhWQaQdT0QSL1mhIBbOag586rBelTDvHPZ/+m/rHDb HvgA== X-Gm-Message-State: AAQBX9cRJDXOozSgl6E6lzl05kbOULmX2wHys0fBtDnw41rKmE70D76e 3NHeXKcQlT6+JiVIvSY6blLUQEuxOg3lhrzZi4kodg== X-Google-Smtp-Source: AKy350a15ynfHZ6V+nl0nq/cMvu2EUMMK0aspoc/r19NiSQjWluLl+cbDovb8VU2vbm5pLXgNvOMEg== X-Received: by 2002:a9d:6e95:0:b0:6a5:e5e2:1019 with SMTP id a21-20020a9d6e95000000b006a5e5e21019mr1670069otr.12.1681853715432; Tue, 18 Apr 2023 14:35:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:333:3aff:2c50:5de7:ce94]) by smtp.gmail.com with ESMTPSA id e26-20020a05683013da00b006a6080ba7desm167581otq.58.2023.04.18.14.35.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 14:35:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 3/4] posix: Add pidfd_fork Date: Tue, 18 Apr 2023 18:35:04 -0300 Message-Id: <20230418213505.3834934-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> References: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Returning a pidfd allows a process to keep a race-free handle to a child process, otherwise the caller will need to either use pidfd_open (which still might be subject to TOCTOU) or keep using the old racy interface. The implementation makes sure that kernel must support the complete pidfd interface, meaning that waitid (P_PIDFD) should be supported. It ensure that non racy workaround is required (such as reading procfs fdinfo pid to use along with old wait interfaces). If kernel does not have the required support the interface returns -1 and set errno to ENOSYS. The interface has semantic similar to fork and _Fork, its prototype is: int pidfd_fork (unsigned int flags) The kernel already sets O_CLOEXEC as default and it follow fork/_Fork convention on returning a positive or negative value to the parent (with negative indicating an error) and zero to the child. Different than fork, pidfd_fork does not run the pthread_atfork handlers (similar to _Fork). It can be change by using PIDFD_FORK_RUNATFORK with flags. Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid support), Linux 5.15 (only clone support), and Linux 5.19 (full support including clone3). --- NEWS | 5 + manual/process.texi | 20 +-- posix/Makefile | 3 +- posix/fork-internal.c | 125 +++++++++++++++ posix/fork-internal.h | 29 ++++ posix/fork.c | 98 +----------- sysdeps/nptl/_Fork.c | 2 +- sysdeps/unix/sysv/linux/Makefile | 2 + sysdeps/unix/sysv/linux/Versions | 1 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 + sysdeps/unix/sysv/linux/arc/libc.abilist | 1 + sysdeps/unix/sysv/linux/arch-fork.h | 16 +- sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 + sysdeps/unix/sysv/linux/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 1 + .../sysv/linux/loongarch/lp64/libc.abilist | 1 + .../sysv/linux/m68k/coldfire/libc.abilist | 1 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 + .../sysv/linux/microblaze/be/libc.abilist | 1 + .../sysv/linux/microblaze/le/libc.abilist | 1 + .../sysv/linux/mips/mips32/fpu/libc.abilist | 1 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 + .../sysv/linux/mips/mips64/n32/libc.abilist | 1 + .../sysv/linux/mips/mips64/n64/libc.abilist | 1 + sysdeps/unix/sysv/linux/nios2/libc.abilist | 1 + sysdeps/unix/sysv/linux/or1k/libc.abilist | 1 + sysdeps/unix/sysv/linux/pidfd_fork.c | 76 +++++++++ .../linux/powerpc/powerpc32/fpu/libc.abilist | 1 + .../powerpc/powerpc32/nofpu/libc.abilist | 1 + .../linux/powerpc/powerpc64/be/libc.abilist | 1 + .../linux/powerpc/powerpc64/le/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv32/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-32/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 + .../sysv/linux/sparc/sparc32/libc.abilist | 1 + .../sysv/linux/sparc/sparc64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sys/pidfd.h | 13 ++ sysdeps/unix/sysv/linux/tst-pidfd_fork.c | 150 ++++++++++++++++++ .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 47 files changed, 460 insertions(+), 114 deletions(-) create mode 100644 posix/fork-internal.c create mode 100644 posix/fork-internal.h create mode 100644 sysdeps/unix/sysv/linux/pidfd_fork.c create mode 100644 sysdeps/unix/sysv/linux/tst-pidfd_fork.c diff --git a/NEWS b/NEWS index 081d0180f4..2ac69a8a2c 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,11 @@ Major new features: The pidfd functionality avoid the issue of PID reuse with traditional posix_spawn interface. +* On Linux, the pidfd_fork has been added. It has a similar semantic + as fork or _Fork, where it clone the calling process. However instead of + return a process ID, it returns a file descriptor that can be used along + other pidfd functions. + Deprecated and removed features, and other changes affecting compatibility: * In the Linux kernel for the hppa/parisc architecture some of the diff --git a/manual/process.texi b/manual/process.texi index 927aebbe3d..410e91202c 100644 --- a/manual/process.texi +++ b/manual/process.texi @@ -137,12 +137,12 @@ creating a process and making it run another program. @cindex subprocess A new processes is created when one of the functions @code{posix_spawn}, @code{fork}, @code{_Fork}, @code{vfork}, or -@code{pidfd_spawn} is called. (The @code{system} and @code{popen} also -create new processes internally.) Due to the name of the @code{fork} -function, the act of creating a new process is sometimes called -@dfn{forking} a process. Each new process (the @dfn{child process} or -@dfn{subprocess}) is allocated a process ID, distinct from the process -ID of the parent process. @xref{Process Identification}. +@code{pidfd_spawn}, or @code{pidfd_fork} is called. (The @code{system} +and @code{popen} also create new processes internally.) Due to the name +of the @code{fork} function, the act of creating a new process is +sometimes called @dfn{forking} a process. Each new process (the +@dfn{child process} or @dfn{subprocess}) is allocated a process ID, +distinct from the process ID of the parent process. @xref{Process Identification}. After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a @@ -153,10 +153,10 @@ limited information about why the child terminated---for example, its exit status code. A newly forked child process continues to execute the same program as -its parent process, at the point where the @code{fork} or @code{_Fork} -call returns. You can use the return value from @code{fork} or -@code{_Fork} to tell whether the program is running in the parent process -or the child. +its parent process, at the point where the @code{fork}, @code{_Fork}, +or @code{pidfd_fork} call returns. You can use the return value from +@code{fork}, @code{_Fork}, or @code{pidfd_fork} to tell whether the +program is running in the parent process or the child. @cindex process image Having several processes run the same program is only occasionally diff --git a/posix/Makefile b/posix/Makefile index 7ace82057e..0c5ce4eddb 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -84,6 +84,7 @@ routines := \ fexecve \ fnmatch \ fork \ + fork-internal \ fpathconf \ gai_strerror \ get_child_max \ @@ -579,7 +580,7 @@ CFLAGS-execl.os = -fomit-frame-pointer CFLAGS-execvp.os = -fomit-frame-pointer CFLAGS-execlp.os = -fomit-frame-pointer CFLAGS-nanosleep.c += -fexceptions -fasynchronous-unwind-tables -CFLAGS-fork.c = $(libio-mtsafe) $(config-cflags-wno-ignored-attributes) +CFLAGS-fork-internal.c = $(libio-mtsafe) $(config-cflags-wno-ignored-attributes) tstgetopt-ARGS = -a -b -cfoobar --required foobar --optional=bazbug \ --none random --col --color --colour diff --git a/posix/fork-internal.c b/posix/fork-internal.c new file mode 100644 index 0000000000..be0d3219af --- /dev/null +++ b/posix/fork-internal.c @@ -0,0 +1,125 @@ +/* Internal fork definitions. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static void +fresetlockfiles (void) +{ + _IO_ITER i; + + for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i)) + if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0) + _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock)); +} + +uint64_t +__fork_pre (bool multiple_threads, struct nss_database_data *nss_database_data) +{ + uint64_t lastrun = __run_prefork_handlers (multiple_threads); + + /* If we are not running multiple threads, we do not have to + preserve lock state. If fork runs from a signal handler, only + async-signal-safe functions can be used in the child. These data + structures are only used by unsafe functions, so their state does + not matter if fork was called from a signal handler. */ + if (multiple_threads) + { + call_function_static_weak (__nss_database_fork_prepare_parent, + nss_database_data); + + _IO_list_lock (); + + /* Acquire malloc locks. This needs to come last because fork + handlers may use malloc, and the libio list lock has an + indirect malloc dependency as well (via the getdelim + function). */ + call_function_static_weak (__malloc_fork_lock_parent); + } + + return lastrun; +} + +void +__fork_pos (int state, uint64_t lastrun, bool multiple_threads, + struct nss_database_data *nss_database_data) +{ + if (state == 0) + { + fork_system_setup (); + + /* Reset the lock state in the multi-threaded case. */ + if (multiple_threads) + { + __libc_unwind_link_after_fork (); + + fork_system_setup_after_fork (); + + /* Release malloc locks. */ + call_function_static_weak (__malloc_fork_unlock_child); + + /* Reset the file list. These are recursive mutexes. */ + fresetlockfiles (); + + /* Reset locks in the I/O code. */ + _IO_list_resetlock (); + + call_function_static_weak (__nss_database_fork_subprocess, + nss_database_data); + } + + /* Reset the lock the dynamic loader uses to protect its data. */ + __rtld_lock_initialize (GL(dl_load_lock)); + + /* Reset the lock protecting dynamic TLS related data. */ + __rtld_lock_initialize (GL(dl_load_tls_lock)); + + reclaim_stacks (); + + /* Run the handlers registered for the child. */ + __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun); + } + else + { + /* If _Fork failed, preserve its errno value. */ + int save_errno = errno; + + /* Release acquired locks in the multi-threaded case. */ + if (multiple_threads) + { + /* Release malloc locks, parent process variant. */ + call_function_static_weak (__malloc_fork_unlock_parent); + + /* We execute this even if the 'fork' call failed. */ + _IO_list_unlock (); + } + + /* Run the handlers registered for the parent. */ + __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun); + + if (state < 0) + __set_errno (save_errno); + } +} diff --git a/posix/fork-internal.h b/posix/fork-internal.h new file mode 100644 index 0000000000..3814d851c4 --- /dev/null +++ b/posix/fork-internal.h @@ -0,0 +1,29 @@ +/* Internal fork definitions. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _FORK_INTERNAL_H +#define _FORK_INTERNAL_H + +#include +#include + +uint64_t __fork_pre (bool, struct nss_database_data *) attribute_hidden; +void __fork_pos (int, uint64_t, bool, struct nss_database_data *) + attribute_hidden; + +#endif diff --git a/posix/fork.c b/posix/fork.c index b4aaa9fa6d..4423cc5792 100644 --- a/posix/fork.c +++ b/posix/fork.c @@ -16,25 +16,10 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include +#include #include #include - -static void -fresetlockfiles (void) -{ - _IO_ITER i; - - for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i)) - if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0) - _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock)); -} +#include pid_t __libc_fork (void) @@ -46,89 +31,14 @@ __libc_fork (void) best effort to make is async-signal-safe at least for single-thread case. */ bool multiple_threads = !SINGLE_THREAD_P; - uint64_t lastrun; - - lastrun = __run_prefork_handlers (multiple_threads); struct nss_database_data nss_database_data; - /* If we are not running multiple threads, we do not have to - preserve lock state. If fork runs from a signal handler, only - async-signal-safe functions can be used in the child. These data - structures are only used by unsafe functions, so their state does - not matter if fork was called from a signal handler. */ - if (multiple_threads) - { - call_function_static_weak (__nss_database_fork_prepare_parent, - &nss_database_data); - - _IO_list_lock (); - - /* Acquire malloc locks. This needs to come last because fork - handlers may use malloc, and the libio list lock has an - indirect malloc dependency as well (via the getdelim - function). */ - call_function_static_weak (__malloc_fork_lock_parent); - } + uint64_t lastrun = __fork_pre (multiple_threads, &nss_database_data); pid_t pid = _Fork (); - if (pid == 0) - { - fork_system_setup (); - - /* Reset the lock state in the multi-threaded case. */ - if (multiple_threads) - { - __libc_unwind_link_after_fork (); - - fork_system_setup_after_fork (); - - /* Release malloc locks. */ - call_function_static_weak (__malloc_fork_unlock_child); - - /* Reset the file list. These are recursive mutexes. */ - fresetlockfiles (); - - /* Reset locks in the I/O code. */ - _IO_list_resetlock (); - - call_function_static_weak (__nss_database_fork_subprocess, - &nss_database_data); - } - - /* Reset the lock the dynamic loader uses to protect its data. */ - __rtld_lock_initialize (GL(dl_load_lock)); - - /* Reset the lock protecting dynamic TLS related data. */ - __rtld_lock_initialize (GL(dl_load_tls_lock)); - - reclaim_stacks (); - - /* Run the handlers registered for the child. */ - __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun); - } - else - { - /* If _Fork failed, preserve its errno value. */ - int save_errno = errno; - - /* Release acquired locks in the multi-threaded case. */ - if (multiple_threads) - { - /* Release malloc locks, parent process variant. */ - call_function_static_weak (__malloc_fork_unlock_parent); - - /* We execute this even if the 'fork' call failed. */ - _IO_list_unlock (); - } - - /* Run the handlers registered for the parent. */ - __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun); - - if (pid < 0) - __set_errno (save_errno); - } + __fork_pos (pid, lastrun, multiple_threads, &nss_database_data); return pid; } diff --git a/sysdeps/nptl/_Fork.c b/sysdeps/nptl/_Fork.c index f8322ae557..aa99e05b5b 100644 --- a/sysdeps/nptl/_Fork.c +++ b/sysdeps/nptl/_Fork.c @@ -22,7 +22,7 @@ pid_t _Fork (void) { - pid_t pid = arch_fork (&THREAD_SELF->tid); + pid_t pid = arch_fork (0, NULL, &THREAD_SELF->tid); if (pid == 0) { struct pthread *self = THREAD_SELF; diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 15e0d69a4b..7f0233449f 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -440,6 +440,7 @@ sysdep_routines += \ getcpu \ oldglob \ sched_getcpu \ + pidfd_fork \ pidfd_spawn \ pidfd_spawnp \ # sysdep_routines @@ -456,6 +457,7 @@ tests += \ tst-spawn7-pidfd \ tst-spawn-chdir-pidfd \ tst-posix_spawn-setsid-pidfd \ + tst-pidfd_fork \ # tests tests-static := tst-affinity-static diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions index 28825473ae..7e4109e9c5 100644 --- a/sysdeps/unix/sysv/linux/Versions +++ b/sysdeps/unix/sysv/linux/Versions @@ -322,6 +322,7 @@ libc { %endif } GLIBC_2.38 { + pidfd_fork; pidfd_spawn; pidfd_spawnp; } diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index bac74ae474..dbf7c44319 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2665,5 +2665,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index dadd302f8d..a9d6ffce14 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2774,6 +2774,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist index 0f4f07b6bb..c08d548556 100644 --- a/sysdeps/unix/sysv/linux/arc/libc.abilist +++ b/sysdeps/unix/sysv/linux/arc/libc.abilist @@ -2426,5 +2426,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/arch-fork.h b/sysdeps/unix/sysv/linux/arch-fork.h index 84e5ef5a7f..ce08fdb99e 100644 --- a/sysdeps/unix/sysv/linux/arch-fork.h +++ b/sysdeps/unix/sysv/linux/arch-fork.h @@ -32,24 +32,24 @@ override it with one of the supported calling convention (check generic kernel-features.h for the clone abi variants). */ static inline pid_t -arch_fork (void *ctid) +arch_fork (int flags, void *ptid, void *ctid) { - const int flags = CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD; long int ret; + flags |= CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD; #ifdef __ASSUME_CLONE_BACKWARDS # ifdef INLINE_CLONE_SYSCALL - ret = INLINE_CLONE_SYSCALL (flags, 0, NULL, 0, ctid); + ret = INLINE_CLONE_SYSCALL (flags, 0, ptid, 0, ctid); # else - ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, 0, ctid); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, ptid, 0, ctid); # endif #elif defined(__ASSUME_CLONE_BACKWARDS2) - ret = INLINE_SYSCALL_CALL (clone, 0, flags, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, 0, flags, ptid, ctid, 0); #elif defined(__ASSUME_CLONE_BACKWARDS3) - ret = INLINE_SYSCALL_CALL (clone, flags, 0, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, 0, ptid, ctid, 0); #elif defined(__ASSUME_CLONE2) - ret = INLINE_SYSCALL_CALL (clone2, flags, 0, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone2, flags, 0, 0, ptid, ctid, 0); #elif defined(__ASSUME_CLONE_DEFAULT) - ret = INLINE_SYSCALL_CALL (clone, flags, 0, NULL, ctid, 0); + ret = INLINE_SYSCALL_CALL (clone, flags, 0, ptid, ctid, 0); #else # error "Undefined clone variant" #endif diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index 38e5dcb81a..f348336ee7 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -546,6 +546,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index 423d20cbb5..5aed593521 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -543,6 +543,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index 488418d14e..8a7022aaa8 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2702,5 +2702,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index 75d495e1c7..e2ac6949df 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2651,6 +2651,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index e53877c52a..5e8d49fc1f 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2835,6 +2835,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index 5ed2b1ebf4..3cd3c80334 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2600,6 +2600,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist index d050fbf6d0..0965db2ff0 100644 --- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist +++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist @@ -2186,5 +2186,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 6465af266c..1ec24886a3 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -547,6 +547,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index 00013cc564..239b1d70be 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2778,6 +2778,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index 34085d81ab..dd41d63394 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2751,5 +2751,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index b26b116f1f..55c3dcd471 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2748,5 +2748,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index c86418d156..295e4d8608 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2743,6 +2743,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 3c6f8f4d60..19fb1aebe7 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2741,6 +2741,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index 8dfa065638..2dd6ef5416 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2749,6 +2749,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 0ca815f2cd..6445d27220 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2651,6 +2651,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index a1e0f772c6..c1cc5134c9 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2790,5 +2790,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist index 91cd6075b9..293643346a 100644 --- a/sysdeps/unix/sysv/linux/or1k/libc.abilist +++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist @@ -2172,5 +2172,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/pidfd_fork.c b/sysdeps/unix/sysv/linux/pidfd_fork.c new file mode 100644 index 0000000000..f7f8e0bbe4 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pidfd_fork.c @@ -0,0 +1,76 @@ +/* pidfd_fork - Duplicated calling process and return a process file + descriptor. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static int +forkfd (void) +{ + /* The kernel should not change the pidfd for the child, and the returning + value on the parent is always larger than 0 (since a new file descriptor + is always opened with CLONE_PIDFD is the kernel supports it. */ + int pidfd = 0; + int ret = arch_fork (CLONE_PIDFD, &pidfd, &THREAD_SELF->tid); + if (ret < 0) + return -1; + else if (ret == 0) + { + struct pthread *self = THREAD_SELF; + + /* Initialize the robust mutex, check _Fork implementation for a full + description why this is required. */ +#if __PTHREAD_MUTEX_HAVE_PREV + self->robust_prev = &self->robust_head; +#endif + self->robust_head.list = &self->robust_head; + INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, + sizeof (struct robust_list_head)); + } + return pidfd; +} + +int +pidfd_fork (unsigned int flags) +{ + if (!__clone_pidfd_supported ()) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (ENOSYS); + + if (flags & ~(PIDFD_FORK_RUNATFORK)) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + + int pidfd; + if (flags & PIDFD_FORK_RUNATFORK) + { + bool multiple_threads = !SINGLE_THREAD_P; + struct nss_database_data nss_database_data; + + uint64_t lastrun = __fork_pre (multiple_threads, &nss_database_data); + pidfd = forkfd (); + /* It follow the usual fork semantic, where a positive or negative + value is returned to parent, and 0 for the child. */ + __fork_pos (pidfd, lastrun, multiple_threads, &nss_database_data); + } + else + pidfd = forkfd (); + + return pidfd; +} diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index 21244ece27..03a35e860d 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2817,6 +2817,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index 238d00998b..a91af4245b 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2850,6 +2850,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index 36fb9b57ac..c15137b9ad 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2571,6 +2571,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index 0c25995661..12c0f4cfba 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2885,5 +2885,6 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index cda030f623..9537dac42d 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2428,5 +2428,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index bd20c202fb..ea72e6b648 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2628,5 +2628,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index a1e9247f72..d6c895b487 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2815,6 +2815,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index b80a131bde..b375f5a7ab 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2608,6 +2608,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index 496a0185b6..7b2266ecb3 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2658,6 +2658,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index 3e0d858d08..2f9f732044 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2655,6 +2655,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index 84dcda8db2..144d54ebf2 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2810,6 +2810,7 @@ GLIBC_2.38 __nldbl___isoc23_vsscanf F GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index 1688a0a7e9..bc8bd8ff54 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2623,6 +2623,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sys/pidfd.h b/sysdeps/unix/sysv/linux/sys/pidfd.h index 342e593288..ed1d3b0ab4 100644 --- a/sysdeps/unix/sysv/linux/sys/pidfd.h +++ b/sysdeps/unix/sysv/linux/sys/pidfd.h @@ -46,4 +46,17 @@ extern int pidfd_getfd (int __pidfd, int __targetfd, extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info, unsigned int __flags) __THROW; + +/* Issue the pthread_atfork on pidfd_fork. */ +#define PIDFD_FORK_RUNATFORK (1U << 1) + +/* Clone the calling process, creating an exact copy. + + The __FLAGS can be used to specify whether the pthread_atfork handlers will + be issued. The default is to not run it, similar to _Fork. + + Return -1 for errors, 0 to the new process, and the process file descriptor + of the new process to the parent process. */ +extern int pidfd_fork (unsigned int __flags) __THROW; + #endif /* _PIDFD_H */ diff --git a/sysdeps/unix/sysv/linux/tst-pidfd_fork.c b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c new file mode 100644 index 0000000000..aecb470c8d --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c @@ -0,0 +1,150 @@ +/* Basic tests for pidfd_fork. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SIG_PID_EXIT_CODE 20 + +static bool atfork_prepare_var; +static bool atfork_parent_var; +static bool atfork_child_var; + +static void +atfork_prepare (void) +{ + atfork_prepare_var = true; +} + +static void +atfork_parent (void) +{ + atfork_parent_var = true; +} + +static void +atfork_child (void) +{ + atfork_child_var = true; +} + +static int +singlethread_test (unsigned int flags) +{ + const char testdata1[] = "abcdefghijklmnopqrtuvwxz"; + enum { testdatalen1 = array_length (testdata1) }; + const char testdata2[] = "01234567890"; + enum { testdatalen2 = array_length (testdata2) }; + + pid_t ppid = getpid (); + + int tempfd = create_temp_file ("tst-pidfd_fork", NULL); + + /* Check if the opened file is shared between process by read and write + some data on parent and child processes. */ + xwrite (tempfd, testdata1, testdatalen1); + off_t off = xlseek (tempfd, 0, SEEK_CUR); + TEST_COMPARE (off, testdatalen1); + + int pidfd = pidfd_fork (flags); + if (pidfd == -1 && errno == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support CLONE_PIDFD clone flag"); + TEST_VERIFY_EXIT (pidfd != -1); + + if (pidfd == 0) + { + if (flags & PIDFD_FORK_RUNATFORK) + TEST_VERIFY (atfork_child_var); + else + TEST_VERIFY (!atfork_child_var); + + TEST_VERIFY_EXIT (getpid () != ppid); + TEST_COMPARE (getppid(), ppid); + + TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen1); + + xlseek (tempfd, 0, SEEK_SET); + char buf[testdatalen1]; + TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen1); + TEST_COMPARE_BLOB (buf, testdatalen1, testdata1, testdatalen1); + + xlseek (tempfd, 0, SEEK_SET); + xwrite (tempfd, testdata2, testdatalen2); + + xclose (tempfd); + + _exit (EXIT_SUCCESS); + } + else + TEST_VERIFY_EXIT (pidfd != -1); + + { + siginfo_t sinfo; + TEST_COMPARE (waitid (P_PIDFD, pidfd, &sinfo, WEXITED), 0); + TEST_COMPARE (sinfo.si_code, CLD_EXITED); + TEST_COMPARE (sinfo.si_status, 0); + } + + TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen2); + + xlseek (tempfd, 0, SEEK_SET); + char buf[testdatalen2]; + TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen2); + + TEST_COMPARE_BLOB (buf, testdatalen2, testdata2, testdatalen2); + + return 0; +} + +static int +do_test (void) +{ + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + + /* With default flags, pidfd_fork acts as _Fork and do not run the + pthread_atfork handlers. */ + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + singlethread_test (0); + TEST_VERIFY (!atfork_prepare_var); + TEST_VERIFY (!atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + /* With PIDFD_FORK_RUNATFORK, pidfd_fork acts as fork. */ + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + singlethread_test (PIDFD_FORK_RUNATFORK); + TEST_VERIFY (atfork_prepare_var); + TEST_VERIFY (atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index b7422d5747..045205de0c 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2574,6 +2574,7 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index 90680bc1d7..5a67604948 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2680,5 +2680,6 @@ GLIBC_2.38 __isoc23_wcstoull F GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F +GLIBC_2.38 pidfd_fork F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F From patchwork Tue Apr 18 21:35:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 67965 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD21C3856956 for ; Tue, 18 Apr 2023 21:36:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD21C3856956 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681853787; bh=yfJbrGyNUmPpFw2LdC4em41BFhkHCmiFkTyRtjNPY/I=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=N/OG4Zui6TTKGJEBiC5L8IFX0iNET6QfIJh5VIKa2FhNrLIrEeeWgXiYnX4eMR9A1 YcDeb7xBVL5ORYmBPgyk66Iwu2gHACSCnlnxogHa80ouIAesjuG9xNmt1+6jzhlhiD 8AcfYOI6Ey+DJlBf6ooI1b53+JueFtUuom8gc1Tw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 53EB33857017 for ; Tue, 18 Apr 2023 21:35:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53EB33857017 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6a5f03551fdso893220a34.0 for ; Tue, 18 Apr 2023 14:35:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681853718; x=1684445718; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yfJbrGyNUmPpFw2LdC4em41BFhkHCmiFkTyRtjNPY/I=; b=kTEobv41OrUzCpLgaASMZEZu9GHWkSDuLQMYNjbT1NeW5xNWoQGD4jz7Fdxlmvb7Gx RExoFazBJmDIUnPnpVXQXbzHCaZkqzPTM51XhBv0TesJZ56lgYVuQr7MjFIJV0upAqRA 5JVFmjavmp0e013opjS9S1nugyooPQRIcnoDpFFymEAtjzsmnkAOSVGaIG10ZFPF35yf aQhq6VHbe3LI0OXgkRbbyGiEL2ihRrXdu88RHt6SAt3Dm8txV4SDOP+eeaF/e8t65ddE VqN0RTpNOqYFv1Z1K3P0Iqj2lB7WZX+B7KOEKKMYHTBR0hW81G7XRDN8fREKhuk1FXxq 9YZg== X-Gm-Message-State: AAQBX9c6KqmE+MtifPgAij/94/8xv5HsKGfiTGbHieRkL47PJD6ItZl0 gmuuHFlHbHZttl/YsfhBI4L6OU9YD83O0asc3N76Uw== X-Google-Smtp-Source: AKy350YpPT8bemZP+P+XKGbE7TxBlsbo0lT7BHZH1xS6YRhY5mBuN9f3/YRXxPPfd7ZRVruqhRdJDg== X-Received: by 2002:a05:6830:1c2:b0:6a5:e61d:aa8c with SMTP id r2-20020a05683001c200b006a5e61daa8cmr1870247ota.3.1681853717589; Tue, 18 Apr 2023 14:35:17 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:333:3aff:2c50:5de7:ce94]) by smtp.gmail.com with ESMTPSA id e26-20020a05683013da00b006a6080ba7desm167581otq.58.2023.04.18.14.35.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 14:35:16 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 4/4] linux: Add pidfd_getpid Date: Tue, 18 Apr 2023 18:35:05 -0300 Message-Id: <20230418213505.3834934-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> References: <20230418213505.3834934-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This interface allows to obtain the associated pid ID from the process file descriptor. It is done by parsing the procps fdinfo information. Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid support), Linux 5.15 (only clone support), and Linux 5.19 (full support including clone3). --- NEWS | 4 + sysdeps/unix/sysv/linux/Makefile | 2 + sysdeps/unix/sysv/linux/Versions | 1 + sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 + sysdeps/unix/sysv/linux/arc/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 + sysdeps/unix/sysv/linux/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 1 + .../sysv/linux/loongarch/lp64/libc.abilist | 1 + .../sysv/linux/m68k/coldfire/libc.abilist | 1 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 + .../sysv/linux/microblaze/be/libc.abilist | 1 + .../sysv/linux/microblaze/le/libc.abilist | 1 + .../sysv/linux/mips/mips32/fpu/libc.abilist | 1 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 + .../sysv/linux/mips/mips64/n32/libc.abilist | 1 + .../sysv/linux/mips/mips64/n64/libc.abilist | 1 + sysdeps/unix/sysv/linux/nios2/libc.abilist | 1 + sysdeps/unix/sysv/linux/or1k/libc.abilist | 1 + sysdeps/unix/sysv/linux/pidfd_getpid.c | 70 +++++++++++++ .../linux/powerpc/powerpc32/fpu/libc.abilist | 1 + .../powerpc/powerpc32/nofpu/libc.abilist | 1 + .../linux/powerpc/powerpc64/be/libc.abilist | 1 + .../linux/powerpc/powerpc64/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/procutils.c | 99 +++++++++++++++++++ sysdeps/unix/sysv/linux/procutils.h | 37 +++++++ .../unix/sysv/linux/riscv/rv32/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-32/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 + .../sysv/linux/sparc/sparc32/libc.abilist | 1 + .../sysv/linux/sparc/sparc64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sys/pidfd.h | 4 + sysdeps/unix/sysv/linux/tst-pidfd.c | 7 ++ .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 42 files changed, 258 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/pidfd_getpid.c create mode 100644 sysdeps/unix/sysv/linux/procutils.c create mode 100644 sysdeps/unix/sysv/linux/procutils.h diff --git a/NEWS b/NEWS index 2ac69a8a2c..39b5f8beb0 100644 --- a/NEWS +++ b/NEWS @@ -33,6 +33,10 @@ Major new features: return a process ID, it returns a file descriptor that can be used along other pidfd functions. +* On Linux, the pidfd_getpid has been added. It allows to retrieve the + process ID associated with process file descriptor created with + pid_spawn or pidfd_fork. + Deprecated and removed features, and other changes affecting compatibility: * In the Linux kernel for the hppa/parisc architecture some of the diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 7f0233449f..ab309b13a9 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -441,8 +441,10 @@ sysdep_routines += \ oldglob \ sched_getcpu \ pidfd_fork \ + pidfd_getpid \ pidfd_spawn \ pidfd_spawnp \ + procutils \ # sysdep_routines tests += \ diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions index 7e4109e9c5..047bc0c92a 100644 --- a/sysdeps/unix/sysv/linux/Versions +++ b/sysdeps/unix/sysv/linux/Versions @@ -323,6 +323,7 @@ libc { } GLIBC_2.38 { pidfd_fork; + pidfd_getpid; pidfd_spawn; pidfd_spawnp; } diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist index dbf7c44319..febfc78f29 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist @@ -2666,5 +2666,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist index a9d6ffce14..87f93beeb8 100644 --- a/sysdeps/unix/sysv/linux/alpha/libc.abilist +++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist @@ -2775,6 +2775,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist index c08d548556..dd33567a75 100644 --- a/sysdeps/unix/sysv/linux/arc/libc.abilist +++ b/sysdeps/unix/sysv/linux/arc/libc.abilist @@ -2427,5 +2427,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist index f348336ee7..2344b8ae48 100644 --- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist @@ -547,6 +547,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist index 5aed593521..b34ec3e9cc 100644 --- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist @@ -544,6 +544,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist index 8a7022aaa8..f59d84fdb0 100644 --- a/sysdeps/unix/sysv/linux/csky/libc.abilist +++ b/sysdeps/unix/sysv/linux/csky/libc.abilist @@ -2703,5 +2703,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist index e2ac6949df..f6527bb9db 100644 --- a/sysdeps/unix/sysv/linux/hppa/libc.abilist +++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist @@ -2652,6 +2652,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index 5e8d49fc1f..28ccb54206 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2836,6 +2836,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/ia64/libc.abilist b/sysdeps/unix/sysv/linux/ia64/libc.abilist index 3cd3c80334..aabf217819 100644 --- a/sysdeps/unix/sysv/linux/ia64/libc.abilist +++ b/sysdeps/unix/sysv/linux/ia64/libc.abilist @@ -2601,6 +2601,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist index 0965db2ff0..8b53d82e8c 100644 --- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist +++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist @@ -2187,5 +2187,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist index 1ec24886a3..66bf4532ab 100644 --- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist @@ -548,6 +548,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _Exit F diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist index 239b1d70be..0ee9fef825 100644 --- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist @@ -2779,6 +2779,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist index dd41d63394..8861ebb4ca 100644 --- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist @@ -2752,5 +2752,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist index 55c3dcd471..008dda3746 100644 --- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist @@ -2749,5 +2749,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist index 295e4d8608..2d0af493ed 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist @@ -2744,6 +2744,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist index 19fb1aebe7..ae82d18edb 100644 --- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist @@ -2742,6 +2742,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist index 2dd6ef5416..87aee7f86d 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist @@ -2750,6 +2750,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist index 6445d27220..c0d1fd370a 100644 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist @@ -2652,6 +2652,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/nios2/libc.abilist b/sysdeps/unix/sysv/linux/nios2/libc.abilist index c1cc5134c9..06092621b9 100644 --- a/sysdeps/unix/sysv/linux/nios2/libc.abilist +++ b/sysdeps/unix/sysv/linux/nios2/libc.abilist @@ -2791,5 +2791,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist index 293643346a..88dcdd8b98 100644 --- a/sysdeps/unix/sysv/linux/or1k/libc.abilist +++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist @@ -2173,5 +2173,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c new file mode 100644 index 0000000000..09cd81f863 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c @@ -0,0 +1,70 @@ +/* pidfd_getpid - Get the associated pid from the pid file descriptor. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include <_itoa.h> +#include +#include +#include +#include +#include +#include + +#define FDINFO_TO_FILENAME_PREFIX "/proc/self/fdinfo/" + +#define FDINFO_FILENAME_LEN \ + (sizeof (FDINFO_TO_FILENAME_PREFIX) + INT_STRLEN_BOUND (int)) + +static bool +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; +} diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist index 03a35e860d..ce885f9136 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist @@ -2818,6 +2818,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist index a91af4245b..8063a0606e 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist @@ -2851,6 +2851,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist index c15137b9ad..21fb95a352 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist @@ -2572,6 +2572,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist index 12c0f4cfba..14bda5b5b8 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist @@ -2886,5 +2886,6 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c new file mode 100644 index 0000000000..4909afeae1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/procutils.c @@ -0,0 +1,99 @@ +/* Utilities functions to read/parse Linux procfs and sysfs. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static char * +next_line (int fd, char *const buffer, char **cp, char **re, + char *const buffer_end) +{ + char *res = *cp; + char *nl = memchr (*cp, '\n', *re - *cp); + if (nl == NULL) + { + if (*cp != buffer) + { + if (*re == buffer_end) + { + memmove (buffer, *cp, *re - *cp); + *re = buffer + (*re - *cp); + *cp = buffer; + + ssize_t n = __read_nocancel (fd, *re, buffer_end - *re); + if (n < 0) + return NULL; + + *re += n; + + nl = memchr (*cp, '\n', *re - *cp); + while (nl == NULL && *re == buffer_end) + { + /* Truncate too long lines. */ + *re = buffer + 3 * (buffer_end - buffer) / 4; + n = __read_nocancel (fd, *re, buffer_end - *re); + if (n < 0) + return NULL; + + nl = memchr (*re, '\n', n); + **re = '\0'; + *re += n; + } + } + else + nl = memchr (*cp, '\n', *re - *cp); + + res = *cp; + } + + if (nl == NULL) + nl = *re - 1; + } + + *nl = '\0'; + *cp = nl + 1; + assert (*cp <= *re); + + return res == *re ? NULL : res; +} + +int +procutils_read_file (const char *filename, procutils_closure_t closure, + void *arg) +{ + enum { buffer_size = 1024 }; + char buffer[buffer_size]; + char *buffer_end = buffer + buffer_size; + char *cp = buffer_end; + char *re = buffer_end; + + int fd = __open64_nocancel (filename, O_RDONLY | O_CLOEXEC); + if (fd == -1) + return -1; + + char *l; + while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL) + if (!closure (l, arg)) + break; + + __close_nocancel_nostatus (fd); + + return 0; +} diff --git a/sysdeps/unix/sysv/linux/procutils.h b/sysdeps/unix/sysv/linux/procutils.h new file mode 100644 index 0000000000..afb2d43bed --- /dev/null +++ b/sysdeps/unix/sysv/linux/procutils.h @@ -0,0 +1,37 @@ +/* Utilities functions to read/parse Linux procfs and sysfs. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _PROCUTILS_H +#define _PROCUTILS_H + +#include + +typedef bool (*procutils_closure_t)(const char *line, void *arg); + +/* Open and read the path FILENAME, line per line, and call CLOSURE with + argument ARG on each line. The read is done with a static buffer, + with non-cancellable calls, and the line is null terminated. + + The CLOSURE should return false if the read should continue, or false + if the function should stop. + + It returns 0 in case of success, or -1 otherwise. */ +int procutils_read_file (const char *filename, procutils_closure_t closure, + void *arg) attribute_hidden; + +#endif diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index 9537dac42d..b294ec1021 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2429,5 +2429,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index ea72e6b648..61921a02d4 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2629,5 +2629,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist index d6c895b487..fbc2d5826f 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist @@ -2816,6 +2816,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist index b375f5a7ab..45ab26d7a7 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +++ b/sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist @@ -2609,6 +2609,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist index 7b2266ecb3..8de69e0057 100644 --- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist @@ -2659,6 +2659,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist index 2f9f732044..3821ce3676 100644 --- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist +++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist @@ -2656,6 +2656,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist index 144d54ebf2..7be5b0c125 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist @@ -2811,6 +2811,7 @@ GLIBC_2.38 __nldbl___isoc23_vswscanf F GLIBC_2.38 __nldbl___isoc23_vwscanf F GLIBC_2.38 __nldbl___isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 _IO_fprintf F diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist index bc8bd8ff54..a09b2efc90 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist @@ -2624,6 +2624,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/sys/pidfd.h b/sysdeps/unix/sysv/linux/sys/pidfd.h index ed1d3b0ab4..f111a7d8d6 100644 --- a/sysdeps/unix/sysv/linux/sys/pidfd.h +++ b/sysdeps/unix/sysv/linux/sys/pidfd.h @@ -59,4 +59,8 @@ extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info, of the new process to the parent process. */ extern int pidfd_fork (unsigned int __flags) __THROW; +/* Query the process ID (PID) from process descriptor __FD. Return the PID + or -1 in case of an error. */ +extern pid_t pidfd_getpid (int __fd) __THROW; + #endif /* _PIDFD_H */ diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c index 64d8a2ef40..39d86fb668 100644 --- a/sysdeps/unix/sysv/linux/tst-pidfd.c +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -118,6 +119,12 @@ do_test (void) int pidfd = pidfd_open (pid, 0); TEST_VERIFY (pidfd != -1); + TEST_COMPARE (pidfd_getpid (INT_MAX), -1); + { + pid_t querypid = pidfd_getpid (pidfd); + TEST_COMPARE (querypid, pid); + } + /* Wait for first sigtimedwait. */ support_process_state_wait (pid, support_process_state_sleeping); TEST_COMPARE (pidfd_send_signal (pidfd, SIGUSR1, NULL, 0), 0); diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index 045205de0c..0726b6e8d0 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2575,6 +2575,7 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F GLIBC_2.4 __confstr_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index 5a67604948..9c1f6830ba 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2681,5 +2681,6 @@ GLIBC_2.38 __isoc23_wcstoull_l F GLIBC_2.38 __isoc23_wcstoumax F GLIBC_2.38 __isoc23_wscanf F GLIBC_2.38 pidfd_fork F +GLIBC_2.38 pidfd_getpid F GLIBC_2.38 pidfd_spawn F GLIBC_2.38 pidfd_spawnp F