1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Test that semop() basic functionality is correct 4 * 5 * Copyright (c) International Business Machines Corp., 2001 6 * 03/2001 - Written by Wayne Boyer 7 * 17/01/02 - Modified. Manoj Iyer, IBM Austin. TX. manjo@austin.ibm.com 8 */ 9 10#include <stdlib.h> 11#include "tst_test.h" 12#include "libnewipc.h" 13#include "lapi/sem.h" 14#include "semop.h" 15 16#define NSEMS 4 17 18static int sem_id = -1; 19static key_t semkey; 20 21static unsigned short int sarr[PSEMS]; 22static union semun get_arr = {.array = sarr}; 23static struct sembuf sops[PSEMS]; 24static struct tst_ts timeout; 25 26static struct test_case_t { 27 struct tst_ts *to; 28} tc[] = { 29 {NULL}, 30 {&timeout} 31}; 32 33static void run(unsigned int n) 34{ 35 struct time64_variants *tv = &variants[tst_variant]; 36 union semun arr = { .val = 0 }; 37 int fail = 0; 38 int i; 39 40 TEST(call_semop(tv, sem_id, sops, NSEMS, tst_ts_get(tc[n].to))); 41 if (TST_RET == -1) { 42 tst_res(TFAIL | TTERRNO, "semop() failed"); 43 return; 44 } 45 46 if (semctl(sem_id, 0, GETALL, get_arr) == -1) 47 tst_brk(TBROK | TERRNO, "semctl(%i, 0, GETALL, ...)", sem_id); 48 49 for (i = 0; i < NSEMS; i++) { 50 if (get_arr.array[i] != i * i) { 51 fail = 1; 52 } 53 } 54 55 if (fail) 56 tst_res(TFAIL, "semaphore values are wrong"); 57 else 58 tst_res(TPASS, "semaphore values are correct"); 59 60 for (i = 0; i < NSEMS; i++) { 61 if (semctl(sem_id, i, SETVAL, arr) == -1) 62 tst_brk(TBROK | TERRNO, "semctl(%i, %i, SETVAL, ...)", sem_id, i); 63 } 64} 65 66static void setup(void) 67{ 68 struct time64_variants *tv = &variants[tst_variant]; 69 int i; 70 71 tst_res(TINFO, "Testing variant: %s", tv->desc); 72 semop_supported_by_kernel(tv); 73 74 timeout.type = tv->ts_type; 75 tst_ts_set_sec(&timeout, 0); 76 tst_ts_set_nsec(&timeout, 10000); 77 78 semkey = GETIPCKEY(); 79 80 sem_id = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA); 81 if (sem_id == -1) 82 tst_brk(TBROK | TERRNO, "couldn't create semaphore in setup"); 83 84 for (i = 0; i < NSEMS; i++) { 85 sops[i].sem_num = i; 86 sops[i].sem_op = i * i; 87 sops[i].sem_flg = SEM_UNDO; 88 } 89} 90 91static void cleanup(void) 92{ 93 if (sem_id != -1) { 94 if (semctl(sem_id, 0, IPC_RMID) == -1) 95 tst_res(TWARN, "semaphore deletion failed."); 96 } 97} 98 99static struct tst_test test = { 100 .test = run, 101 .tcnt = ARRAY_SIZE(tc), 102 .test_variants = ARRAY_SIZE(variants), 103 .setup = setup, 104 .cleanup = cleanup, 105 .needs_tmpdir = 1, 106}; 107