1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * HISTORY
6 * 06/30/2001 Port to Linux nsharoff@us.ibm.com
7 * 10/30/2002 Port to LTP dbarrera@us.ibm.com
8 * 10/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
9 * - Fix concurrency issue. A statically defined key was used. Leading
10 * to conflict with other instances of the same test.
11 */
12 /*\
13 * [Description]
14 *
15 * Basic tests for semctl().
16 *
17 * - semctl() with IPC_STAT where we check the semid_ds buf content
18 * - semctl() with SETVAL and GETVAL
19 * - semctl() with GETPID
20 * - semctl() with GETNCNT and GETZCNT
21 */
22
23 #include "tst_test.h"
24 #include "tst_safe_sysv_ipc.h"
25 #include "libnewipc.h"
26 #include "lapi/sem.h"
27
28 static int semid = -1;
29 static unsigned long nsems;
30
verify_semctl(void)31 static void verify_semctl(void)
32 {
33 int status;
34 struct semid_ds buf_ds;
35 union semun arg;
36
37 arg.buf = &buf_ds;
38 TST_EXP_PASS(semctl(semid, 0, IPC_STAT, arg));
39
40 if (arg.buf->sem_nsems != nsems) {
41 tst_res(TFAIL, "sem_nsems = %lu, expected %lu",
42 arg.buf->sem_nsems, nsems);
43 } else {
44 tst_res(TPASS, "sem_nsems = %lu", arg.buf->sem_nsems);
45 }
46
47 if (arg.buf->sem_perm.uid != getuid()) {
48 tst_res(TFAIL, "sem_perm.uid = %d, expected %d",
49 arg.buf->sem_perm.uid, getuid());
50 } else {
51 tst_res(TPASS, "sem_perm.uid = %d", arg.buf->sem_perm.uid);
52 }
53
54 if (arg.buf->sem_perm.gid != getgid()) {
55 tst_res(TFAIL, "sem_perm.gid = %d, expected %d",
56 arg.buf->sem_perm.gid, getgid());
57 } else {
58 tst_res(TPASS, "sem_perm.gid = %d", arg.buf->sem_perm.gid);
59 }
60
61 if (arg.buf->sem_perm.cuid != getuid()) {
62 tst_res(TFAIL, "sem_perm.cuid = %d, expected %d",
63 arg.buf->sem_perm.cuid, getuid());
64 } else {
65 tst_res(TPASS, "sem_perm.cuid = %d", arg.buf->sem_perm.cuid);
66 }
67
68 if (arg.buf->sem_perm.cgid != getgid()) {
69 tst_res(TFAIL, "sem_perm.cgid = %d, expected %d",
70 arg.buf->sem_perm.cgid, getgid());
71 } else {
72 tst_res(TPASS, "sem_perm.cgid = %d", arg.buf->sem_perm.cgid);
73 }
74
75 if ((status = semctl(semid, 0, GETVAL)) < 0)
76 tst_res(TFAIL | TERRNO, "semctl(GETVAL)");
77 else
78 tst_res(TPASS, "semctl(GETVAL) succeeded");
79
80 arg.val = 1;
81
82 if ((status = semctl(semid, 0, SETVAL, arg)) < 0)
83 tst_res(TFAIL | TERRNO, "SEMCTL(SETVAL)");
84 else
85 tst_res(TPASS, "semctl(SETVAL) succeeded");
86
87 if ((status = semctl(semid, 0, GETVAL)) < 0)
88 tst_res(TFAIL | TERRNO, "semctl(GETVAL)");
89 else
90 tst_res(TPASS, "semctl(GETVAL) succeeded");
91
92 if (status != arg.val) {
93 tst_res(TFAIL, "semctl(GETVAL) returned %d expected %d",
94 status, arg.val);
95 } else {
96 tst_res(TPASS, "semctl(GETVAL) returned %d", status);
97 }
98
99 if ((status = semctl(semid, 0, GETPID)) < 0)
100 tst_res(TFAIL | TERRNO, "semctl(GETPID)");
101 else
102 tst_res(TPASS, "semctl(GETPID) succeeded");
103
104 if (status != getpid()) {
105 tst_res(TFAIL, "semctl(GETPID) returned %d expected %d",
106 status, getpid());
107 } else {
108 tst_res(TPASS, "semctl(GETPID) returned %d", status);
109 }
110
111 if ((status = semctl(semid, 0, GETNCNT)) < 0)
112 tst_res(TFAIL | TERRNO, "semctl(GETNCNT)");
113 else
114 tst_res(TPASS, "semctl(GETNCNT) succeeded");
115
116 if (status != 0)
117 tst_res(TFAIL, "semctl(GETNCNT) returned %d expected 0",
118 status);
119 else
120 tst_res(TPASS, "semctl(GETNCNT) returned 0");
121
122 if ((status = semctl(semid, 0, GETZCNT)) < 0)
123 tst_res(TFAIL | TERRNO, "semctl(GETZCNT)");
124 else
125 tst_res(TPASS, "semctl(GETZCNT) succeeded");
126
127 if (status != 0)
128 tst_res(TFAIL, "error: unexpected semzcnt %d", status);
129 else
130 tst_res(TPASS, "semctl(GETZCNT) succeeded 0");
131 }
132
setup(void)133 static void setup(void)
134 {
135 key_t key = GETIPCKEY();
136 nsems = 1;
137
138 semid = SAFE_SEMGET(key, nsems, SEM_RA | IPC_CREAT);
139 }
140
cleanup(void)141 static void cleanup(void)
142 {
143 if (semid != -1)
144 SAFE_SEMCTL(semid, 0, IPC_RMID);
145 }
146
147 static struct tst_test test = {
148 .setup = setup,
149 .cleanup = cleanup,
150 .test_all = verify_semctl,
151 };
152