Linux: Add gettid system call wrapper [BZ #6399]
Commit Message
This commit adds gettid to <unistd.h> on Linux, and not to the
kernel-independent GNU API.
2018-12-05 Florian Weimer <fweimer@redhat.com>
[BZ #6399]
Linux: Add gettid system call wrapper.
* posix/Makefile (headers): Add bits/unistd_ext.h.
* posix/bits/unistd_ext.h: New file.
* posix/unistd.h: Include it.
* sysdeps/unix/sysv/linux/Makefile [subdir == misc] (tests): Add
tst-gettid.
(tst-gettid): Link with $(shared-thread-library).
* sysdeps/unix/sysv/linux/Version (GLIBC_2.29): Export gettid.
* sysdeps/unix/sysv/linux/bits/unistd_ext.h: New file.
* sysdeps/unix/sysv/linux/bits/syscalls.list (gettid): Add.
* sysdeps/unix/sysv/linux/bits/tst-gettid.c: New file.
* sysdeps/unix/sysv/linux/aarch64/libc.abilist (GLIBC_2.29):
Add gettid.
* sysdeps/unix/sysv/linux/alpha/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/arm/libc.abilist (GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/hppa/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/i386/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/ia64/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/microblaze/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/nios2/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
(GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/sh/libc.abilist (GLIBC_2.29): Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/x86_64/64/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist (GLIBC_2.29):
Likewise.
* sysdeps/unix/sysv/linux/tst-setgetname.c (gettid): Remove.
Comments
On Wed, 5 Dec 2018, Florian Weimer wrote:
> This commit adds gettid to <unistd.h> on Linux, and not to the
> kernel-independent GNU API.
This is missing a NEWS entry, and documentation (which seems particularly
important in this case, to discuss what a tid is and is not and what
interfaces it can be used with).
* Joseph Myers:
> On Wed, 5 Dec 2018, Florian Weimer wrote:
>
>> This commit adds gettid to <unistd.h> on Linux, and not to the
>> kernel-independent GNU API.
>
> This is missing a NEWS entry,
I plan to add this:
+* On Linux, the gettid function has been added.
> and documentation (which seems particularly
> important in this case, to discuss what a tid is and is not and what
> interfaces it can be used with).
I can put more details into the comment:
/* Return the kernel thread ID (TID) of the current thread. The
returned value is not subject to caching. Most Linux system calls
accept a TID in place of a PID. Using the TID to change properties
of a thread that has been created using pthread_create can lead to
undefined behavior (comparable to manipulating file descriptors
directly that have not been created explicitly). Note that a TID
uniquely identifies a thread only while this thread is running; a
TID can be reused once a thread has exited, even if the thread is
not detached and has not been joined. */
I do not want to contribute to the manual at this point.
I would rather improve the manpages project documentation and clarify
there when a PID is actually a TID and deals with what are essentially
task properties.
Should we have a separate conversation about the future of the manual?
Thanks,
Florian
On Wed, 5 Dec 2018, Florian Weimer wrote:
> I do not want to contribute to the manual at this point.
I think additions to the manual should generally be required for adding
new functions to glibc.
I'm also wondering about adding a testcase that fails if any undocumented
function is declared in a header unless the lack of documentation for that
function is XFAILed with reference to an open bug, to make it more obvious
when documentation is missing from a patch submission.
* Florian Weimer:
> This commit adds gettid to <unistd.h> on Linux, and not to the
> kernel-independent GNU API.
>
> 2018-12-05 Florian Weimer <fweimer@redhat.com>
>
> [BZ #6399]
> Linux: Add gettid system call wrapper.
> * posix/Makefile (headers): Add bits/unistd_ext.h.
> * posix/bits/unistd_ext.h: New file.
> * posix/unistd.h: Include it.
> * sysdeps/unix/sysv/linux/Makefile [subdir == misc] (tests): Add
> tst-gettid.
> (tst-gettid): Link with $(shared-thread-library).
> * sysdeps/unix/sysv/linux/Version (GLIBC_2.29): Export gettid.
> * sysdeps/unix/sysv/linux/bits/unistd_ext.h: New file.
Any comments on the technical approach chosen here? Is the declaration
in <unistd.h> reasonable? Is the mechanism to make <unistd.h>
extensible okay?
Or should the prototype go into <sched.h>?
Thanks,
Florian
On 12/5/18 1:30 PM, Joseph Myers wrote:
> On Wed, 5 Dec 2018, Florian Weimer wrote:
>
>> I do not want to contribute to the manual at this point.
>
> I think additions to the manual should generally be required for adding
> new functions to glibc.
I agree, even if the documentation is minimal, and you can always ask for
help in writing the manual pages.
> I'm also wondering about adding a testcase that fails if any undocumented
> function is declared in a header unless the lack of documentation for that
> function is XFAILed with reference to an open bug, to make it more obvious
> when documentation is missing from a patch submission.
Agreed.
* Joseph Myers:
> On Wed, 5 Dec 2018, Florian Weimer wrote:
>
>> I do not want to contribute to the manual at this point.
>
> I think additions to the manual should generally be required for adding
> new functions to glibc.
I posted some precursor patches to document thread IDs:
<https://sourceware.org/ml/libc-alpha/2018-12/msg00236.html>
<https://sourceware.org/ml/libc-alpha/2018-12/msg00237.html>
Once at least the first one is in, I can add documentation for gettid as
well.
Thanks,
Florian
On December 10, 2018 11:46:21 AM GMT+01:00, Florian Weimer <fweimer@redhat.com> wrote:
>* Joseph Myers:
>
>> On Wed, 5 Dec 2018, Florian Weimer wrote:
>>
>>> I do not want to contribute to the manual at this point.
>>
>> I think additions to the manual should generally be required for
>adding
>> new functions to glibc.
>
>I posted some precursor patches to document thread IDs:
>
> <https://sourceware.org/ml/libc-alpha/2018-12/msg00236.html>
Florian, if this is not just about POSIX but also about the actual implementation
could you maybe add a section to the manual what happens when:
- the thread-group leader exits
- the thread-group leader becomes a zombie
- whether a thread-group can be reparented
?
Obviously all three are closely related.
Christian
> <https://sourceware.org/ml/libc-alpha/2018-12/msg00237.html>
>
>Once at least the first one is in, I can add documentation for gettid
>as
>well.
>
>Thanks,
>Florian
* Christian Brauner:
> Florian, if this is not just about POSIX but also about the actual
> implementation could you maybe add a section to the manual what
> happens when:
> - the thread-group leader exits
> - the thread-group leader becomes a zombie
> - whether a thread-group can be reparented
> ?
> Obviously all three are closely related.
I lack the technical expertise in this area to do this, but input is
welcome. I can probably come up with wording. Some of this should
perhaps go to the man-pages project because I think the glibc manual
should document only cases which are possible to observe with the glibc
implementation (with NPTL using CLONE_THREAD etc.).
I think we really need to document the different effects of returning
from main (or calling exit directly) on the one hand, and calling
pthread_exit from the main thread on the other. Some of the thread
group behavior could be included in that.
Thanks,
Florian
On December 10, 2018 1:02:46 PM GMT+01:00, Florian Weimer <fweimer@redhat.com> wrote:
>* Christian Brauner:
>
>> Florian, if this is not just about POSIX but also about the actual
>> implementation could you maybe add a section to the manual what
>> happens when:
>
>> - the thread-group leader exits
>> - the thread-group leader becomes a zombie
>> - whether a thread-group can be reparented
>> ?
>> Obviously all three are closely related.
>
>I lack the technical expertise in this area to do this, but input is
>welcome. I can probably come up with*
*If* [sic!] pidfd_send_signal() gets in I have
follow up patches that do allow sending signals to process groups and threads.
I've been looking into the exact behavior
of thread-groups and process groups
so I can spend some time documenting it.
I will have to write this down for myself anyway. (If I haven't made it clear: that's a big *if* on whether signaling via fds lands).
> wording. Some of this should
>perhaps go to the man-pages project because I think the glibc manual
>should document only cases which are possible to observe with the glibc
>implementation (with NPTL using CLONE_THREAD etc.).
>
>I think we really need to document the different effects of returning
>from main (or calling exit directly) on the one hand, and calling
>pthread_exit from the main thread on the other. Some of the thread
>group behavior could be included in that.
Very much agreed!
Thanks for all the work!
Christian
>
>Thanks,
>Florian
On 12/10/18 5:46 AM, Florian Weimer wrote:
> * Joseph Myers:
>
>> On Wed, 5 Dec 2018, Florian Weimer wrote:
>>
>>> I do not want to contribute to the manual at this point.
>>
>> I think additions to the manual should generally be required for adding
>> new functions to glibc.
>
> I posted some precursor patches to document thread IDs:
>
> <https://sourceware.org/ml/libc-alpha/2018-12/msg00236.html>
> <https://sourceware.org/ml/libc-alpha/2018-12/msg00237.html>
>
> Once at least the first one is in, I can add documentation for gettid as
> well.
I have reviewed both. The first I think is OK now, the second needs some
iteration.
On 12/5/18 5:53 AM, Florian Weimer wrote:
> This commit adds gettid to <unistd.h> on Linux, and not to the
> kernel-independent GNU API.
NEWS:
+* On Linux, the gettid function has been added.
OK.
Overall this looks good to me, regarding unistd_ext.h, that also looks
OK to me and is an internal implementation detail we can refine if we
find commonality among other headers and a different structure appears
that we like better.
This patch needs a manual entry for gettid. Post a v2 with that and I
can go ahead and ACK this, conditional on the other 2 manual patches
going in, then we'll have:
* Documentation for a tid.
* Documentation for sched_* issues.
* Wrapper and documentation for gettid.
That would be all we need and we'd have a new wrapper for gettid().
> 2018-12-05 Florian Weimer <fweimer@redhat.com>
>
> [BZ #6399]
> Linux: Add gettid system call wrapper.
> * posix/Makefile (headers): Add bits/unistd_ext.h.
> * posix/bits/unistd_ext.h: New file.
> * posix/unistd.h: Include it.
> * sysdeps/unix/sysv/linux/Makefile [subdir == misc] (tests): Add
> tst-gettid.
> (tst-gettid): Link with $(shared-thread-library).
> * sysdeps/unix/sysv/linux/Version (GLIBC_2.29): Export gettid.
> * sysdeps/unix/sysv/linux/bits/unistd_ext.h: New file.
> * sysdeps/unix/sysv/linux/bits/syscalls.list (gettid): Add.
> * sysdeps/unix/sysv/linux/bits/tst-gettid.c: New file.
> * sysdeps/unix/sysv/linux/aarch64/libc.abilist (GLIBC_2.29):
> Add gettid.
[snip abilist]
> * sysdeps/unix/sysv/linux/tst-setgetname.c (gettid): Remove.
>
> diff --git a/posix/Makefile b/posix/Makefile
> index a8fb1e1839..49d7a91b77 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -31,7 +31,8 @@ headers := sys/utsname.h sys/times.h sys/wait.h sys/types.h unistd.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
> + cpio.h spawn.h bits/unistd.h bits/types/struct_sched_param.h \
> + bits/unistd_ext.h
OK.
>
> routines := \
> uname \
> diff --git a/posix/bits/unistd_ext.h b/posix/bits/unistd_ext.h
> new file mode 100644
> index 0000000000..8019cfa758
> --- /dev/null
> +++ b/posix/bits/unistd_ext.h
> @@ -0,0 +1,21 @@
> +/* System-specific extensions of <unistd.h>, generic version.
> + Copyright (C) 2018 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
> + <http://www.gnu.org/licenses/>. */
> +
> +#ifndef _UNISTD_H
> +# error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
> +#endif
OK.
> diff --git a/posix/unistd.h b/posix/unistd.h
> index a8cf28b5e7..127ed494ce 100644
> --- a/posix/unistd.h
> +++ b/posix/unistd.h
> @@ -1166,6 +1166,9 @@ int getentropy (void *__buffer, size_t __length) __wur;
> # include <bits/unistd.h>
> #endif
>
> +/* System-specific extensions. */
> +#include <bits/unistd_ext.h>
OK. I think this idea in general is OK. We have used types/foo.h for type split out
from headers, but have not generalized splitting out extensions like this.
> +
> __END_DECLS
>
> #endif /* unistd.h */
> diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> index 362cf3b950..99884799e6 100644
> --- a/sysdeps/unix/sysv/linux/Makefile
> +++ b/sysdeps/unix/sysv/linux/Makefile
> @@ -48,7 +48,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
> tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
> tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
> test-errno-linux tst-memfd_create tst-mlock2 tst-pkey \
> - tst-rlimit-infinity tst-ofdlocks
> + tst-rlimit-infinity tst-ofdlocks tst-gettid
OK.
> tests-internal += tst-ofdlocks-compat
>
>
> @@ -100,6 +100,8 @@ $(objpfx)tst-sysconf-iov_max: $(objpfx)tst-sysconf-iov_max-uapi.o
>
> $(objpfx)tst-pkey: $(shared-thread-library)
>
> +$(objpfx)tst-gettid: $(shared-thread-library)
OK.
> +
> endif # $(subdir) == misc
>
> ifeq ($(subdir),time)
> diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
> index 336c13b57d..6b5cd99b51 100644
> --- a/sysdeps/unix/sysv/linux/Versions
> +++ b/sysdeps/unix/sysv/linux/Versions
> @@ -171,6 +171,9 @@ libc {
> mlock2;
> pkey_alloc; pkey_free; pkey_set; pkey_get; pkey_mprotect;
> }
> + GLIBC_2.29 {
> + gettid;
> + }
OK.
> GLIBC_PRIVATE {
> # functions used in other libraries
> __syscall_rt_sigqueueinfo;
[snip ablist diffs]
> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> new file mode 100644
> index 0000000000..5f964bca94
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> @@ -0,0 +1,32 @@
> +/* System-specific extensions of <unistd.h>, Linux version.
> + Copyright (C) 2018 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
> + <http://www.gnu.org/licenses/>. */
> +
> +#ifndef _UNISTD_H
> +# error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
> +#endif
> +
> +#ifdef __USE_GNU
OK, makse sense to be under USE_GNU.
> +
> +/* Return the kernel thread ID (TID) of the current thread. The
> + returned value is not subject to caching. Note that a TID uniquely
> + identifies a thread only while this thread is running; a TID can be
> + reused once a thread has exited, even if the thread is not detached
> + and has not been joined. */
> +extern __pid_t gettid (void) __THROW;
OK.
> +
> +#endif
[snip abilist diffs]
> diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
> index e24ea29e35..544ab5d54a 100644
> --- a/sysdeps/unix/sysv/linux/syscalls.list
> +++ b/sysdeps/unix/sysv/linux/syscalls.list
> @@ -112,3 +112,4 @@ process_vm_writev EXTRA process_vm_writev i:ipipii process_vm_writev
> memfd_create EXTRA memfd_create i:si memfd_create
> pkey_alloc EXTRA pkey_alloc i:ii pkey_alloc
> pkey_free EXTRA pkey_free i:i pkey_free
> +gettid EXTRA gettid Ei: __gettid gettid
OK.
> diff --git a/sysdeps/unix/sysv/linux/tst-gettid.c b/sysdeps/unix/sysv/linux/tst-gettid.c
> new file mode 100644
> index 0000000000..78bbaee4f3
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/tst-gettid.c
> @@ -0,0 +1,79 @@
> +/* Smoke test for the gettid system call.
> + Copyright (C) 2018 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
> + <http://www.gnu.org/licenses/>. */
> +
> +#include <support/check.h>
> +#include <support/namespace.h>
> +#include <support/xthread.h>
> +#include <support/xunistd.h>
> +
> +/* TID of the initial (main) thread. */
> +static pid_t initial_tid;
> +
> +/* Check that PID and TID are the same in a subprocess. */
> +static void
> +subprocess (void *closure)
> +{
> + TEST_COMPARE (getpid (), gettid ());
> + TEST_VERIFY (gettid () != initial_tid);
OK.
> +}
> +
> +/* Check that the TID changes in a new thread. */
> +static void *
> +threadfunc (void *closure)
> +{
> + TEST_VERIFY (getpid () != gettid ());
> + TEST_VERIFY (gettid () != initial_tid);
OK.
> + return NULL;
> +}
> +
> +/* Check for interactions with vfork. */
> +static void
> +test_vfork (void)
> +{
> + pid_t proc = vfork ();
> + if (proc == 0)
> + {
> + if (getpid () != gettid ())
> + _exit (1);
> + if (gettid () == initial_tid)
> + _exit (2);
OK.
> + _exit (0);
> + }
> + int status;
> + xwaitpid (proc, &status, 0);
> + TEST_COMPARE (status, 0);
> +}
> +
> +static int
> +do_test (void)
> +{
> + initial_tid = gettid ();
> +
> + /* The main thread has the same TID as the PID. */
> + TEST_COMPARE (getpid (), gettid ());
> +
> + test_vfork ();
> +
> + support_isolate_in_subprocess (subprocess, NULL);
> +
> + xpthread_join (xpthread_create (NULL, threadfunc, NULL));
OK.
> +
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
> diff --git a/sysdeps/unix/sysv/linux/tst-setgetname.c b/sysdeps/unix/sysv/linux/tst-setgetname.c
> index b80ff59074..95d8bb9ba4 100644
> --- a/sysdeps/unix/sysv/linux/tst-setgetname.c
> +++ b/sysdeps/unix/sysv/linux/tst-setgetname.c
> @@ -43,12 +43,6 @@
> terminator. */
> #define TASK_COMM_LEN 16
>
> -long
> -gettid (void)
> -{
> - return syscall(__NR_gettid);
> -}
OK.
> -
> /* On Linux we can read this task's name from /proc. */
> int
> get_self_comm (long tid, char *buf, size_t len)
[snip abilist diffs]
@@ -31,7 +31,8 @@ headers := sys/utsname.h sys/times.h sys/wait.h sys/types.h unistd.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
+ cpio.h spawn.h bits/unistd.h bits/types/struct_sched_param.h \
+ bits/unistd_ext.h
routines := \
uname \
new file mode 100644
@@ -0,0 +1,21 @@
+/* System-specific extensions of <unistd.h>, generic version.
+ Copyright (C) 2018 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
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _UNISTD_H
+# error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
+#endif
@@ -1166,6 +1166,9 @@ int getentropy (void *__buffer, size_t __length) __wur;
# include <bits/unistd.h>
#endif
+/* System-specific extensions. */
+#include <bits/unistd_ext.h>
+
__END_DECLS
#endif /* unistd.h */
@@ -48,7 +48,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
test-errno-linux tst-memfd_create tst-mlock2 tst-pkey \
- tst-rlimit-infinity tst-ofdlocks
+ tst-rlimit-infinity tst-ofdlocks tst-gettid
tests-internal += tst-ofdlocks-compat
@@ -100,6 +100,8 @@ $(objpfx)tst-sysconf-iov_max: $(objpfx)tst-sysconf-iov_max-uapi.o
$(objpfx)tst-pkey: $(shared-thread-library)
+$(objpfx)tst-gettid: $(shared-thread-library)
+
endif # $(subdir) == misc
ifeq ($(subdir),time)
@@ -171,6 +171,9 @@ libc {
mlock2;
pkey_alloc; pkey_free; pkey_set; pkey_get; pkey_mprotect;
}
+ GLIBC_2.29 {
+ gettid;
+ }
GLIBC_PRIVATE {
# functions used in other libraries
__syscall_rt_sigqueueinfo;
@@ -2138,4 +2138,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
@@ -2033,6 +2033,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -123,6 +123,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
new file mode 100644
@@ -0,0 +1,32 @@
+/* System-specific extensions of <unistd.h>, Linux version.
+ Copyright (C) 2018 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
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _UNISTD_H
+# error "Never include <bits/unistd_ext.h> directly; use <unistd.h> instead."
+#endif
+
+#ifdef __USE_GNU
+
+/* Return the kernel thread ID (TID) of the current thread. The
+ returned value is not subject to caching. Note that a TID uniquely
+ identifies a thread only while this thread is running; a TID can be
+ reused once a thread has exited, even if the thread is not detached
+ and has not been joined. */
+extern __pid_t gettid (void) __THROW;
+
+#endif
@@ -1880,6 +1880,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -2045,6 +2045,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1914,6 +1914,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -124,6 +124,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0x98
@@ -1989,6 +1989,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -2130,4 +2130,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
@@ -1967,6 +1967,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1965,6 +1965,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1973,6 +1973,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1968,6 +1968,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -2171,4 +2171,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
@@ -1993,6 +1993,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1997,6 +1997,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -2228,4 +2228,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
@@ -123,6 +123,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 _Exit F
GLIBC_2.3 _IO_2_1_stderr_ D 0xe0
@@ -2100,4 +2100,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
@@ -2002,6 +2002,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1908,6 +1908,7 @@ GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
GLIBC_2.29 __fentry__ F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1884,6 +1884,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1996,6 +1996,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -1937,6 +1937,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -112,3 +112,4 @@ process_vm_writev EXTRA process_vm_writev i:ipipii process_vm_writev
memfd_create EXTRA memfd_create i:si memfd_create
pkey_alloc EXTRA pkey_alloc i:ii pkey_alloc
pkey_free EXTRA pkey_free i:i pkey_free
+gettid EXTRA gettid Ei: __gettid gettid
new file mode 100644
@@ -0,0 +1,79 @@
+/* Smoke test for the gettid system call.
+ Copyright (C) 2018 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
+ <http://www.gnu.org/licenses/>. */
+
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/xthread.h>
+#include <support/xunistd.h>
+
+/* TID of the initial (main) thread. */
+static pid_t initial_tid;
+
+/* Check that PID and TID are the same in a subprocess. */
+static void
+subprocess (void *closure)
+{
+ TEST_COMPARE (getpid (), gettid ());
+ TEST_VERIFY (gettid () != initial_tid);
+}
+
+/* Check that the TID changes in a new thread. */
+static void *
+threadfunc (void *closure)
+{
+ TEST_VERIFY (getpid () != gettid ());
+ TEST_VERIFY (gettid () != initial_tid);
+ return NULL;
+}
+
+/* Check for interactions with vfork. */
+static void
+test_vfork (void)
+{
+ pid_t proc = vfork ();
+ if (proc == 0)
+ {
+ if (getpid () != gettid ())
+ _exit (1);
+ if (gettid () == initial_tid)
+ _exit (2);
+ _exit (0);
+ }
+ int status;
+ xwaitpid (proc, &status, 0);
+ TEST_COMPARE (status, 0);
+}
+
+static int
+do_test (void)
+{
+ initial_tid = gettid ();
+
+ /* The main thread has the same TID as the PID. */
+ TEST_COMPARE (getpid (), gettid ());
+
+ test_vfork ();
+
+ support_isolate_in_subprocess (subprocess, NULL);
+
+ xpthread_join (xpthread_create (NULL, threadfunc, NULL));
+
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -43,12 +43,6 @@
terminator. */
#define TASK_COMM_LEN 16
-long
-gettid (void)
-{
- return syscall(__NR_gettid);
-}
-
/* On Linux we can read this task's name from /proc. */
int
get_self_comm (long tid, char *buf, size_t len)
@@ -1895,6 +1895,7 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.3 __ctype_b_loc F
GLIBC_2.3 __ctype_tolower_loc F
@@ -2146,4 +2146,5 @@ GLIBC_2.28 thrd_current F
GLIBC_2.28 thrd_equal F
GLIBC_2.28 thrd_sleep F
GLIBC_2.28 thrd_yield F
+GLIBC_2.29 gettid F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F