1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * 03/2001 - Written by Wayne Boyer
6 */
7 /*\
8 * [Description]
9 *
10 * Test for semctl() EACCES error.
11 */
12
13 #include <pwd.h>
14 #include "tst_safe_sysv_ipc.h"
15 #include "tst_test.h"
16 #include "lapi/sem.h"
17 #include "libnewipc.h"
18
19 static int sem_id = -1;
20
verify_semctl(void)21 static void verify_semctl(void)
22 {
23 struct semid_ds sem_ds;
24 union semun un_arg;
25
26 un_arg.buf = &sem_ds;
27
28 TST_EXP_FAIL(semctl(sem_id, 0, IPC_STAT, un_arg), EACCES,
29 "semctl(IPC_STAT) with nobody user");
30 }
31
setup(void)32 static void setup(void)
33 {
34 static key_t semkey;
35 struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
36
37 SAFE_SETUID(ltpuser->pw_uid);
38
39 semkey = GETIPCKEY();
40
41 sem_id = SAFE_SEMGET(semkey, PSEMS, IPC_CREAT | IPC_EXCL);
42 }
43
cleanup(void)44 void cleanup(void)
45 {
46 if (sem_id != -1)
47 SAFE_SEMCTL(sem_id, 0, IPC_RMID);
48 }
49
50 static struct tst_test test = {
51 .setup = setup,
52 .cleanup = cleanup,
53 .needs_root = 1,
54 .test_all = verify_semctl,
55 };
56