1 /* Basic tests for SYSV semaphore functions.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <sys/types.h>
27 #include <support/support.h>
28 #include <support/check.h>
29 #include <support/temp_file.h>
31 /* These are for the temporary file we generate. */
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);
44 do_prepare (int argc
, char *argv
[])
46 int fd
= create_temp_file ("tst-sysvsem.", &name
);
48 FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno
);
51 #define PREPARE do_prepare
53 /* It is not an extensive test, but rather a functional one aimed to check
54 correct parameter passing on kernel. */
62 unsigned short *array
;
70 key_t key
= ftok (name
, 'G');
72 FAIL_EXIT1 ("ftok failed");
74 semid
= semget(key
, 1, IPC_CREAT
| IPC_EXCL
| SEM_MODE
);
78 FAIL_UNSUPPORTED ("msgget not supported");
79 FAIL_EXIT1 ("semget failed (errno=%d)", errno
);
82 /* Get semaphore kernel information and do some sanity checks. */
83 struct semid_ds seminfo
;
84 if (semctl (semid
, 0, IPC_STAT
, (union semun
) { .buf
= &seminfo
}) == -1)
85 FAIL_EXIT1 ("semctl with IPC_STAT failed (errno=%d)", errno
);
87 if (seminfo
.sem_perm
.__key
!= key
)
88 FAIL_EXIT1 ("semid_ds::sem_perm::key (%d) != %d",
89 (int) seminfo
.sem_perm
.__key
, (int) key
);
90 if (seminfo
.sem_perm
.mode
!= SEM_MODE
)
91 FAIL_EXIT1 ("semid_ds::sem_perm::mode (%o) != %o",
92 seminfo
.sem_perm
.mode
, SEM_MODE
);
93 if (seminfo
.sem_nsems
!= 1)
94 FAIL_EXIT1 ("semid_ds::sem_nsems (%lu) != 1",
95 (long unsigned) seminfo
.sem_nsems
);
97 /* Some lock/unlock basic tests. */
98 struct sembuf sb1
= { 0, 1, 0 };
99 if (semop (semid
, &sb1
, 1) == -1)
100 FAIL_EXIT1 ("semop failed (errno=%i)", errno
);
102 struct sembuf sb2
= { 0, -1, 0 };
103 if (semop (semid
, &sb2
, 1) == -1)
104 FAIL_EXIT1 ("semop failed (errno=%i)", errno
);
107 /* Set a time for half a second. The semaphore operation should timeout
109 struct timespec ts
= { 0 /* sec */, 500000000 /* nsec */ };
110 if (semtimedop (semid
, &sb2
, 1, &ts
) != -1
111 || (errno
!= EAGAIN
&& errno
!= ENOSYS
))
112 FAIL_EXIT1 ("semtimedop succeed or returned errno != {EAGAIN,ENOSYS} "
113 "(errno=%i)", errno
);
116 /* Finally free up the semnaphore resource. */
117 if (semctl (semid
, 0, IPC_RMID
, 0) == -1)
118 FAIL_EXIT1 ("semctl failed (errno=%d)", errno
);
123 #include <support/test-driver.c>