(root)/
glibc-2.38/
sysvipc/
test-sysvsem.c
       1  /* Basic tests for SYSV semaphore functions.
       2     Copyright (C) 2016-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 <intprops.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <errno.h>
      23  #include <string.h>
      24  #include <stdbool.h>
      25  #include <sys/types.h>
      26  #include <sys/ipc.h>
      27  #include <sys/sem.h>
      28  
      29  #include <test-sysvipc.h>
      30  
      31  #include <support/support.h>
      32  #include <support/check.h>
      33  #include <support/temp_file.h>
      34  #include <support/xtime.h>
      35  #include <support/xsignal.h>
      36  
      37  /* These are for the temporary file we generate.  */
      38  static char *name;
      39  static int semid;
      40  
      41  static void
      42  remove_sem (void)
      43  {
      44    /* Enforce message queue removal in case of early test failure.
      45       Ignore error since the sem may already have being removed.  */
      46    semctl (semid, 0, IPC_RMID, 0);
      47  }
      48  
      49  static void
      50  do_prepare (int argc, char *argv[])
      51  {
      52    int fd = create_temp_file ("tst-sysvsem.", &name);
      53    if (fd == -1)
      54      FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
      55  }
      56  
      57  #define PREPARE do_prepare
      58  
      59  /* It is not an extensive test, but rather a functional one aimed to check
      60     correct parameter passing on kernel.  */
      61  
      62  #define SEM_MODE 0644
      63  
      64  union semun
      65  {
      66    int val;
      67    struct semid_ds *buf;
      68    unsigned short  *array;
      69  };
      70  
      71  static int
      72  do_test (void)
      73  {
      74    atexit (remove_sem);
      75  
      76    key_t key = ftok (name, 'G');
      77    if (key == -1)
      78      FAIL_EXIT1 ("ftok failed");
      79  
      80    semid = semget(key, 1, IPC_CREAT | IPC_EXCL | SEM_MODE);
      81    if (semid == -1)
      82      {
      83        if (errno == ENOSYS)
      84  	FAIL_UNSUPPORTED ("msgget not supported");
      85        FAIL_EXIT1 ("semget failed (errno=%d)", errno);
      86      }
      87  
      88    TEST_COMPARE (semctl (semid, 0, first_sem_invalid_cmd (), NULL), -1);
      89    TEST_COMPARE (errno, EINVAL);
      90  
      91    /* Get semaphore kernel information and do some sanity checks.  */
      92    struct semid_ds seminfo;
      93    if (semctl (semid, 0, IPC_STAT, (union semun) { .buf = &seminfo }) == -1)
      94      FAIL_EXIT1 ("semctl with IPC_STAT failed (errno=%d)", errno);
      95  
      96    if (seminfo.sem_perm.__key != key)
      97      FAIL_EXIT1 ("semid_ds::sem_perm::key (%d) != %d",
      98  		(int) seminfo.sem_perm.__key, (int) key);
      99    if (seminfo.sem_perm.mode != SEM_MODE)
     100      FAIL_EXIT1 ("semid_ds::sem_perm::mode (%o) != %o",
     101  		seminfo.sem_perm.mode, SEM_MODE);
     102    if (seminfo.sem_nsems != 1)
     103      FAIL_EXIT1 ("semid_ds::sem_nsems (%lu) != 1",
     104  		(long unsigned) seminfo.sem_nsems);
     105  
     106    /* Some lock/unlock basic tests.  */
     107    struct sembuf sb1 = { 0, 1, 0 };
     108    if (semop (semid, &sb1, 1) == -1)
     109      FAIL_EXIT1 ("semop failed (errno=%i)", errno);
     110  
     111    struct sembuf sb2 = { 0, -1, 0 };
     112    if (semop (semid, &sb2, 1) == -1)
     113      FAIL_EXIT1 ("semop failed (errno=%i)", errno);
     114  
     115  #ifdef _GNU_SOURCE
     116    /* Set a time for half a second.  The semaphore operation should timeout
     117       with EAGAIN.  */
     118    {
     119      struct timespec ts = { 0 /* sec */, 500000000 /* nsec */ };
     120      if (semtimedop (semid, &sb2, 1, &ts) != -1
     121          || (errno != EAGAIN && errno != ENOSYS))
     122        FAIL_EXIT1 ("semtimedop succeed or returned errno != {EAGAIN,ENOSYS} "
     123  		  "(errno=%i)", errno);
     124    }
     125  
     126    {
     127      support_create_timer (0, 100000000, false, NULL);
     128      struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
     129      TEST_COMPARE (semtimedop (semid, &sb2, 1, &ts), -1);
     130      TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
     131    }
     132  #endif
     133  
     134    /* Finally free up the semnaphore resource.  */
     135    if (semctl (semid, 0, IPC_RMID, 0) == -1)
     136      FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
     137  
     138    return 0;
     139  }
     140  
     141  #include <support/test-driver.c>