glibc/sysvipc/test-sysvsem.c

142 lines
4.0 KiB
C
Raw Normal View History

/* Basic tests for SYSV semaphore functions.
Copyright (C) 2016-2022 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
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 07:40:42 +02:00
<https://www.gnu.org/licenses/>. */
#include <intprops.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <test-sysvipc.h>
#include <support/support.h>
#include <support/check.h>
#include <support/temp_file.h>
#include <support/xtime.h>
#include <support/xsignal.h>
/* These are for the temporary file we generate. */
static char *name;
static int semid;
static void
remove_sem (void)
{
/* Enforce message queue removal in case of early test failure.
Ignore error since the sem may already have being removed. */
semctl (semid, 0, IPC_RMID, 0);
}
static void
do_prepare (int argc, char *argv[])
{
int fd = create_temp_file ("tst-sysvsem.", &name);
if (fd == -1)
FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
}
#define PREPARE do_prepare
/* It is not an extensive test, but rather a functional one aimed to check
correct parameter passing on kernel. */
#define SEM_MODE 0644
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
static int
do_test (void)
{
atexit (remove_sem);
key_t key = ftok (name, 'G');
if (key == -1)
FAIL_EXIT1 ("ftok failed");
semid = semget(key, 1, IPC_CREAT | IPC_EXCL | SEM_MODE);
if (semid == -1)
{
if (errno == ENOSYS)
FAIL_UNSUPPORTED ("msgget not supported");
FAIL_EXIT1 ("semget failed (errno=%d)", errno);
}
TEST_COMPARE (semctl (semid, 0, first_sem_invalid_cmd (), NULL), -1);
TEST_COMPARE (errno, EINVAL);
/* Get semaphore kernel information and do some sanity checks. */
struct semid_ds seminfo;
if (semctl (semid, 0, IPC_STAT, (union semun) { .buf = &seminfo }) == -1)
FAIL_EXIT1 ("semctl with IPC_STAT failed (errno=%d)", errno);
if (seminfo.sem_perm.__key != key)
FAIL_EXIT1 ("semid_ds::sem_perm::key (%d) != %d",
(int) seminfo.sem_perm.__key, (int) key);
if (seminfo.sem_perm.mode != SEM_MODE)
FAIL_EXIT1 ("semid_ds::sem_perm::mode (%o) != %o",
seminfo.sem_perm.mode, SEM_MODE);
if (seminfo.sem_nsems != 1)
FAIL_EXIT1 ("semid_ds::sem_nsems (%lu) != 1",
(long unsigned) seminfo.sem_nsems);
/* Some lock/unlock basic tests. */
struct sembuf sb1 = { 0, 1, 0 };
if (semop (semid, &sb1, 1) == -1)
FAIL_EXIT1 ("semop failed (errno=%i)", errno);
struct sembuf sb2 = { 0, -1, 0 };
if (semop (semid, &sb2, 1) == -1)
FAIL_EXIT1 ("semop failed (errno=%i)", errno);
#ifdef _GNU_SOURCE
/* Set a time for half a second. The semaphore operation should timeout
with EAGAIN. */
{
struct timespec ts = { 0 /* sec */, 500000000 /* nsec */ };
if (semtimedop (semid, &sb2, 1, &ts) != -1
|| (errno != EAGAIN && errno != ENOSYS))
FAIL_EXIT1 ("semtimedop succeed or returned errno != {EAGAIN,ENOSYS} "
"(errno=%i)", errno);
}
{
support_create_timer (0, 100000000, false, NULL);
struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
TEST_COMPARE (semtimedop (semid, &sb2, 1, &ts), -1);
TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
}
#endif
/* Finally free up the semnaphore resource. */
if (semctl (semid, 0, IPC_RMID, 0) == -1)
FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
return 0;
}
#include <support/test-driver.c>