]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysvipc/test-sysvsem.c
Fix math/test-nearbyint-except for no-exceptions configurations.
[thirdparty/glibc.git] / sysvipc / test-sysvsem.c
CommitLineData
1afc369f 1/* Basic tests for SYSV semaphore functions.
bfff8b1b 2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
1afc369f
AZ
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 <http://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <errno.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/ipc.h>
25#include <sys/sem.h>
26
27#include <support/support.h>
28#include <support/check.h>
29#include <support/temp_file.h>
30
31/* These are for the temporary file we generate. */
32static char *name;
33static int semid;
34
35static void
36remove_sem (void)
37{
38 /* Enforce message queue removal in case of early test failure.
39 Ignore error since the sem may already have being removed. */
40 semctl (semid, 0, IPC_RMID, 0);
41}
42
43static void
44do_prepare (int argc, char *argv[])
45{
46 int fd = create_temp_file ("tst-sysvsem.", &name);
47 if (fd == -1)
48 FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
49}
50
51#define PREPARE do_prepare
52
53/* It is not an extensive test, but rather a functional one aimed to check
54 correct parameter passing on kernel. */
55
56#define SEM_MODE 0644
57
58static int
59do_test (void)
60{
61 atexit (remove_sem);
62
63 key_t key = ftok (name, 'G');
64 if (key == -1)
65 FAIL_EXIT1 ("ftok failed");
66
67 semid = semget(key, 1, IPC_CREAT | IPC_EXCL | SEM_MODE);
68 if (semid == -1)
69 {
70 if (errno == ENOSYS)
71 FAIL_UNSUPPORTED ("msgget not supported");
72 FAIL_EXIT1 ("semget failed (errno=%d)", errno);
73 }
74
75 /* Get semaphore kernel information and do some sanity checks. */
76 struct semid_ds seminfo;
77 if (semctl (semid, 0, IPC_STAT, &seminfo) == -1)
78 FAIL_EXIT1 ("semctl with IPC_STAT failed (errno=%d)", errno);
79
80 if (seminfo.sem_perm.__key != key)
81 FAIL_EXIT1 ("semid_ds::sem_perm::key (%d) != %d",
82 (int) seminfo.sem_perm.__key, (int) key);
83 if (seminfo.sem_perm.mode != SEM_MODE)
84 FAIL_EXIT1 ("semid_ds::sem_perm::mode (%o) != %o",
85 seminfo.sem_perm.mode, SEM_MODE);
86 if (seminfo.sem_nsems != 1)
87 FAIL_EXIT1 ("semid_ds::sem_nsems (%lu) != 1",
88 (long unsigned) seminfo.sem_nsems);
89
90 /* Some lock/unlock basic tests. */
91 struct sembuf sb1 = { 0, 1, 0 };
92 if (semop (semid, &sb1, 1) == -1)
93 FAIL_EXIT1 ("semop failed (errno=%i)", errno);
94
95 struct sembuf sb2 = { 0, -1, 0 };
96 if (semop (semid, &sb2, 1) == -1)
97 FAIL_EXIT1 ("semop failed (errno=%i)", errno);
98
99#ifdef _GNU_SOURCE
100 /* Set a time for half a second. The semaphore operation should timeout
101 with EAGAIN. */
102 struct timespec ts = { 0 /* sec */, 500000000 /* nsec */ };
103 if (semtimedop (semid, &sb2, 1, &ts) != -1
104 || (errno != EAGAIN && errno != ENOSYS))
105 FAIL_EXIT1 ("semtimedop succeed or returned errno != {EAGAIN,ENOSYS} "
106 "(errno=%i)", errno);
107#endif
108
109 /* Finally free up the semnaphore resource. */
110 if (semctl (semid, 0, IPC_RMID, 0) == -1)
111 FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
112
113 return 0;
114}
115
116#include <support/test-driver.c>