(root)/
glibc-2.38/
sysdeps/
unix/
sysv/
linux/
tst-gettid.c
       1  /* Smoke test for the gettid system call.
       2     Copyright (C) 2019-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <support/check.h>
      20  #include <support/namespace.h>
      21  #include <support/xthread.h>
      22  #include <support/xunistd.h>
      23  
      24  /* TID of the initial (main) thread.  */
      25  static pid_t initial_tid;
      26  
      27  /* Check that PID and TID are the same in a subprocess.  */
      28  static void
      29  subprocess (void *closure)
      30  {
      31    TEST_COMPARE (getpid (), gettid ());
      32    TEST_VERIFY (gettid () != initial_tid);
      33  }
      34  
      35  /* Check that the TID changes in a new thread.  */
      36  static void *
      37  threadfunc (void *closure)
      38  {
      39    TEST_VERIFY (getpid () != gettid ());
      40    TEST_VERIFY (gettid () != initial_tid);
      41    return NULL;
      42  }
      43  
      44  /* Check for interactions with vfork.  */
      45  static void
      46  test_vfork (void)
      47  {
      48    pid_t proc = vfork ();
      49    if (proc == 0)
      50      {
      51        if (getpid () != gettid ())
      52          _exit (1);
      53        if (gettid () == initial_tid)
      54          _exit (2);
      55        _exit (0);
      56      }
      57    int status;
      58    xwaitpid (proc, &status, 0);
      59    TEST_COMPARE (status, 0);
      60  }
      61  
      62  static int
      63  do_test (void)
      64  {
      65    initial_tid = gettid ();
      66  
      67    /* The main thread has the same TID as the PID.  */
      68    TEST_COMPARE (getpid (), gettid ());
      69  
      70    test_vfork ();
      71  
      72    support_isolate_in_subprocess (subprocess, NULL);
      73  
      74    xpthread_join (xpthread_create (NULL, threadfunc, NULL));
      75  
      76    return 0;
      77  }
      78  
      79  #include <support/test-driver.c>