1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*\
7 * [Description]
8 *
9 * This case checks that semget() correclty creates a semaphore set.
10 */
11
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/ipc.h>
15 #include "lapi/sem.h"
16 #include "tst_test.h"
17 #include "libnewipc.h"
18 #include "tst_safe_sysv_ipc.h"
19
20 static int sem_id = -1, sem_key = -1;
21
check_functionality(void)22 static void check_functionality(void)
23 {
24 struct semid_ds semary;
25 union semun un_arg;
26
27 un_arg.buf = &semary;
28 SAFE_SEMCTL(sem_id, 0, IPC_STAT, un_arg);
29 TST_EXP_EQ_LI(un_arg.buf->sem_nsems, PSEMS);
30 TST_EXP_EQ_LI(un_arg.buf->sem_perm.cuid, geteuid());
31
32 tst_res(TPASS, "basic semaphore values are okay");
33 }
34
verify_semget(void)35 static void verify_semget(void)
36 {
37 TEST(semget(sem_key, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA));
38 if (TST_RET == -1) {
39 tst_res(TFAIL | TTERRNO, "semget() failed");
40 return;
41 }
42
43 sem_id = TST_RET;
44 check_functionality();
45
46 SAFE_SEMCTL(sem_id, PSEMS, IPC_RMID);
47 }
48
setup(void)49 static void setup(void)
50 {
51 sem_key = GETIPCKEY();
52 }
53
cleanup(void)54 static void cleanup(void)
55 {
56 if (sem_id != -1)
57 SAFE_SEMCTL(sem_id, PSEMS, IPC_RMID);
58 }
59
60 static struct tst_test test = {
61 .needs_tmpdir = 1,
62 .setup = setup,
63 .cleanup = cleanup,
64 .test_all = verify_semget,
65 };
66