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