1
+ // SPDX-License-Identifier: GPL-2.0-or-later
1
2
/*
3
+ * Test that semop() basic functionality is correct
2
4
*
3
- * Copyright (c) International Business Machines Corp., 2001
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License as published by
7
- * the Free Software Foundation; either version 2 of the License, or
8
- * (at your option) any later version.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13
- * the GNU General Public License for more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program; if not, write to the Free Software
17
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
- */
19
-
20
- /*
21
- * NAME
22
- * semop01.c
23
- *
24
- * DESCRIPTION
25
- * semop01 - test that semop() basic functionality is correct
26
- *
27
- * ALGORITHM
28
- * create a semaphore set and initialize some values
29
- * loop if that option was specified
30
- * call semop() to set values for the primitive semaphores
31
- * check the return code
32
- * if failure, issue a FAIL message.
33
- * otherwise,
34
- * if doing functionality testing
35
- * get the semaphore values and compare with expected values
36
- * if correct,
37
- * issue a PASS message
38
- * otherwise
39
- * issue a FAIL message
40
- * else issue a PASS message
41
- * call cleanup
42
- *
43
- * HISTORY
44
- * 03/2001 - Written by Wayne Boyer
5
+ * Copyright (c) International Business Machines Corp., 2001
6
+ * 03/2001 - Written by Wayne Boyer
45
7
* 17/01/02 - Modified. Manoj Iyer, IBM Austin. TX. [email protected]
46
- * 4th argument to semctl() system call was modified according
47
- * to man pages.
48
- * In my opinion The test should not even have compiled but
49
- * it was working due to some mysterious reason.
50
- *
51
- * RESTRICTIONS
52
- * none
53
8
*/
54
9
55
- #include "ipcsem.h"
56
-
57
- #define NSEMS 4 /* the number of primitive semaphores to test */
10
+ #include <stdlib.h>
11
+ #include <sys/sem.h>
12
+ #include "tst_test.h"
13
+ #include "libnewipc.h"
14
+ #include "lapi/semun.h"
58
15
59
- char * TCID = "semop01" ;
60
- int TST_TOTAL = 1 ;
16
+ #define NSEMS 4
61
17
62
- int sem_id_1 = -1 ; /* a semaphore set with read & alter permissions */
18
+ static int sem_id = -1 ;
19
+ static key_t semkey ;
63
20
64
- union semun get_arr ;
65
- struct sembuf sops [PSEMS ];
21
+ static unsigned short int sarr [PSEMS ];
22
+ static union semun get_arr = {.array = sarr };
23
+ static struct sembuf sops [PSEMS ];
66
24
67
- int main ( int ac , char * * av )
25
+ static void run ( void )
68
26
{
69
- int lc ;
70
- int i ;
27
+ union semun arr = { .val = 0 };
71
28
int fail = 0 ;
29
+ int i ;
72
30
73
- tst_parse_opts (ac , av , NULL , NULL );
74
-
75
- setup ();
76
-
77
- for (lc = 0 ; TEST_LOOPING (lc ); lc ++ ) {
78
- tst_count = 0 ;
79
-
80
- TEST (semop (sem_id_1 , sops , NSEMS ));
81
-
82
- if (TEST_RETURN == -1 ) {
83
- tst_resm (TFAIL , "%s call failed - errno = %d : %s" ,
84
- TCID , TEST_ERRNO , strerror (TEST_ERRNO ));
85
- } else {
86
- /* get the values and make sure they */
87
- /* are the same as what was set */
88
- if (semctl (sem_id_1 , 0 , GETALL , get_arr ) == -1 ) {
89
- tst_brkm (TBROK , cleanup ,
90
- "semctl() failed in functional test" );
91
- }
92
-
93
- for (i = 0 ; i < NSEMS ; i ++ ) {
94
- if (get_arr .array [i ] != i * i ) {
95
- fail = 1 ;
96
- }
97
- }
98
- if (fail )
99
- tst_resm (TFAIL ,
100
- "semaphore values are wrong" );
101
- else
102
- tst_resm (TPASS ,
103
- "semaphore values are correct" );
104
- }
31
+ TEST (semop (sem_id , sops , NSEMS ));
32
+ if (TST_RET == -1 ) {
33
+ tst_res (TFAIL | TTERRNO , "semop() failed" );
34
+ return ;
35
+ }
36
+
37
+ if (semctl (sem_id , 0 , GETALL , get_arr ) == -1 )
38
+ tst_brk (TBROK | TERRNO , "semctl(%i, 0, GETALL, ...)" , sem_id );
105
39
106
- /*
107
- * clean up things in case we are looping
108
- */
109
- union semun set_arr ;
110
- set_arr .val = 0 ;
111
- for (i = 0 ; i < NSEMS ; i ++ ) {
112
- if (semctl (sem_id_1 , i , SETVAL , set_arr ) == -1 ) {
113
- tst_brkm (TBROK , cleanup , "semctl failed" );
114
- }
40
+ for (i = 0 ; i < NSEMS ; i ++ ) {
41
+ if (get_arr .array [i ] != i * i ) {
42
+ fail = 1 ;
115
43
}
116
44
}
117
45
118
- cleanup ();
119
- tst_exit ();
46
+ if (fail )
47
+ tst_res (TFAIL , "semaphore values are wrong" );
48
+ else
49
+ tst_res (TPASS , "semaphore values are correct" );
50
+
51
+ for (i = 0 ; i < NSEMS ; i ++ ) {
52
+ if (semctl (sem_id , i , SETVAL , arr ) == -1 )
53
+ tst_brk (TBROK | TERRNO , "semctl(%i, %i, SETVAL, ...)" , sem_id , i );
54
+ }
120
55
}
121
56
122
- void setup (void )
57
+ static void setup (void )
123
58
{
124
59
int i ;
125
60
126
- tst_sig (NOFORK , DEF_HANDLER , cleanup );
127
-
128
- TEST_PAUSE ;
61
+ semkey = GETIPCKEY ();
129
62
130
- tst_tmpdir ();
131
-
132
- get_arr .array = malloc (sizeof (unsigned short int ) * PSEMS );
133
- if (get_arr .array == NULL )
134
- tst_brkm (TBROK , cleanup , "malloc failed" );
135
-
136
- semkey = getipckey ();
137
-
138
- sem_id_1 = semget (semkey , PSEMS , IPC_CREAT | IPC_EXCL | SEM_RA );
139
- if (sem_id_1 == -1 )
140
- tst_brkm (TBROK , cleanup , "couldn't create semaphore in setup" );
63
+ sem_id = semget (semkey , PSEMS , IPC_CREAT | IPC_EXCL | SEM_RA );
64
+ if (sem_id == -1 )
65
+ tst_brk (TBROK | TERRNO , "couldn't create semaphore in setup" );
141
66
142
67
for (i = 0 ; i < NSEMS ; i ++ ) {
143
68
sops [i ].sem_num = i ;
@@ -146,11 +71,17 @@ void setup(void)
146
71
}
147
72
}
148
73
149
- void cleanup (void )
74
+ static void cleanup (void )
150
75
{
151
- rm_sema (sem_id_1 );
152
-
153
- free (get_arr .array );
154
-
155
- tst_rmdir ();
76
+ if (sem_id != -1 ) {
77
+ if (semctl (sem_id , 0 , IPC_RMID ) == -1 )
78
+ tst_res (TWARN , "semaphore deletion failed." );
79
+ }
156
80
}
81
+
82
+ static struct tst_test test = {
83
+ .test_all = run ,
84
+ .setup = setup ,
85
+ .cleanup = cleanup ,
86
+ .needs_tmpdir = 1 ,
87
+ };
0 commit comments