plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * semctl01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * semctl01 - test the 10 possible semctl() commands |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * create a semaphore set with read and alter permissions |
| 29 | * loop if that option was specified |
| 30 | * loop through the test cases |
| 31 | * do any setup required for the test case |
| 32 | * make the semctl() call using the TEST() macro |
| 33 | * check the return code |
| 34 | * if failure, issue a FAIL message. |
| 35 | * otherwise, |
| 36 | * if doing functionality testing |
| 37 | * call the appropriate test function |
| 38 | * if correct, |
| 39 | * issue a PASS message |
| 40 | * otherwise |
| 41 | * issue a FAIL message |
| 42 | * call cleanup |
| 43 | * |
| 44 | * USAGE: <for command-line> |
| 45 | * semctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 46 | * where, -c n : Run n copies concurrently. |
| 47 | * -f : Turn off functionality Testing. |
| 48 | * -i n : Execute test n times. |
| 49 | * -I x : Execute test for x seconds. |
| 50 | * -P x : Pause for x seconds between iterations. |
| 51 | * -t : Turn on syscall timing. |
| 52 | * |
| 53 | * HISTORY |
| 54 | * 03/2001 - Written by Wayne Boyer |
| 55 | * |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 56 | * 11/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com) |
| 57 | * - Fix concurrency issue. Due to the use of usleep function to |
| 58 | * synchronize processes, synchronization issues can occur on a loaded |
| 59 | * system. Fix this by using pipes to synchronize processes. |
| 60 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | * RESTRICTIONS |
| 62 | * none |
| 63 | */ |
| 64 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 65 | #include "ipcsem.h" |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 66 | #include "libtestsuite.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char *TCID = "semctl01"; |
| 69 | int TST_TOTAL = 10; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int sem_id_1 = -1; /* a semaphore set with read and alter permissions */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 73 | int sync_pipes[2]; |
| 74 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | /* |
| 76 | * These are the various setup and check functions for the 10 different |
| 77 | * commands that are available for the semctl() call. |
| 78 | */ |
| 79 | void func_stat(void); |
| 80 | void set_setup(void), func_set(void); |
| 81 | void func_gall(void); |
| 82 | void cnt_setup(int), func_cnt(int); |
| 83 | void pid_setup(void), func_pid(int); |
| 84 | void gval_setup(void), func_gval(int); |
| 85 | void sall_setup(void), func_sall(void); |
| 86 | void func_sval(void); |
| 87 | void func_rmid(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 88 | void child_cnt(void); |
| 89 | void child_pid(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | struct semid_ds buf; |
| 92 | unsigned short array[PSEMS]; |
| 93 | struct sembuf sops; |
| 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | #define INCVAL 2 /* a semaphore increment value */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | #define NEWMODE 066 |
| 97 | #define NCHILD 5 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | #define SEM2 2 /* semaphore to use for GETPID and GETVAL */ |
| 99 | #define SEM4 4 /* semaphore to use for GETNCNT and GETZCNT */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | #define ONE 1 |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 101 | #ifdef _XLC_COMPILER |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 102 | #define SEMUN_CAST |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 103 | #else |
| 104 | #define SEMUN_CAST (union semun) |
| 105 | #endif |
| 106 | |
| 107 | #ifdef _XLC_COMPILER |
| 108 | #define SEMUN_CAST |
| 109 | #else |
| 110 | #define SEMUN_CAST (union semun) |
| 111 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
| 113 | int pid_arr[NCHILD]; |
| 114 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 115 | #ifdef UCLINUX |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 116 | #define PIPE_NAME "semctl01" |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 117 | static char *argv0; |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 118 | int sem_op; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | struct test_case_t { |
| 122 | int semnum; /* the primitive semaphore to use */ |
| 123 | int cmd; /* the command to test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | void (*func_test) (); /* the test function */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | union semun arg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | void (*func_setup) (); /* the setup function if necessary */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | } TC[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | { |
| 129 | 0, IPC_STAT, func_stat, SEMUN_CAST & buf, NULL}, { |
| 130 | 0, IPC_SET, func_set, SEMUN_CAST & buf, set_setup}, { |
| 131 | 0, GETALL, func_gall, SEMUN_CAST array, NULL}, { |
| 132 | SEM4, GETNCNT, func_cnt, SEMUN_CAST & buf, cnt_setup}, { |
| 133 | SEM2, GETPID, func_pid, SEMUN_CAST & buf, pid_setup}, { |
| 134 | SEM2, GETVAL, func_gval, SEMUN_CAST & buf, NULL}, { |
| 135 | SEM4, GETZCNT, func_cnt, SEMUN_CAST & buf, cnt_setup}, { |
| 136 | 0, SETALL, func_sall, SEMUN_CAST array, sall_setup}, { |
| 137 | SEM4, SETVAL, func_sval, SEMUN_CAST INCVAL, NULL}, { |
| 138 | 0, IPC_RMID, func_rmid, SEMUN_CAST & buf, NULL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 141 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | int lc; /* loop counter */ |
| 144 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | int i, j; |
| 146 | |
| 147 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 148 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 149 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 151 | #ifdef UCLINUX |
| 152 | argv0 = av[0]; |
| 153 | maybe_run_child(&child_pid, "nd", 1, &sem_id_1); |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 154 | maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sem_op); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* The following loop checks looping state if -i option given */ |
| 160 | |
| 161 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 162 | /* reset Tst_count in case we are looping */ |
| 163 | Tst_count = 0; |
| 164 | |
| 165 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Set up any conditions if needed |
| 170 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | if (TC[i].func_setup != NULL) { |
| 172 | /* call the setup function */ |
| 173 | switch (TC[i].cmd) { |
| 174 | case GETNCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | (*TC[i].func_setup) (-ONE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | break; |
| 177 | case GETZCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | (*TC[i].func_setup) (0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | break; |
| 180 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | (*TC[i].func_setup) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Use TEST macro to make the call |
| 188 | */ |
| 189 | |
| 190 | TEST(semctl(sem_id_1, TC[i].semnum, TC[i].cmd, |
| 191 | TC[i].arg)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 192 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | if (TEST_RETURN == -1) { |
| 194 | tst_resm(TFAIL, "%s call failed - errno = %d " |
| 195 | ": %s", TCID, TEST_ERRNO, |
| 196 | strerror(TEST_ERRNO)); |
| 197 | } else { |
| 198 | if (STD_FUNCTIONAL_TEST) { |
| 199 | /* |
| 200 | * call the appropriate test function |
| 201 | * and pass the return value where it |
| 202 | * is needed to perform certain tests. |
| 203 | */ |
| 204 | switch (TC[i].cmd) { |
| 205 | case GETNCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | /*FALLTHROUGH*/ case GETZCNT: |
| 207 | /*FALLTHROUGH*/ case GETPID: |
| 208 | /*FALLTHROUGH*/ case GETVAL: |
| 209 | (*TC[i]. |
| 210 | func_test) (TEST_RETURN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | break; |
| 212 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | (*TC[i].func_test) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } else { |
| 217 | tst_resm(TPASS, "call succeeded"); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * If testing GETNCNT or GETZCNT, clean up the children. |
| 223 | */ |
| 224 | switch (TC[i].cmd) { |
| 225 | case GETNCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | /*FALLTHROUGH*/ case GETZCNT: |
| 227 | for (j = 0; j < NCHILD; j++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | if (kill(pid_arr[j], SIGKILL) == -1) { |
| 229 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | "child kill failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | /* |
| 237 | * recreate the semaphore resource if looping |
| 238 | */ |
| 239 | if (TEST_LOOPING(lc)) { |
| 240 | if ((sem_id_1 = semget(semkey, PSEMS, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 241 | IPC_CREAT | IPC_EXCL | SEM_RA)) |
| 242 | == -1) { |
| 243 | tst_brkm(TBROK, cleanup, |
| 244 | "couldn't recreate " "semaphore"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | cleanup(); |
| 250 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 251 | tst_exit(); |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 252 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | * func_stat() - check the functionality of the IPC_STAT command with semctl() |
| 257 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 258 | void func_stat() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 259 | { |
| 260 | /* check the number of semaphores and the ipc_perm.mode value */ |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 261 | if (buf.sem_nsems == PSEMS && buf.sem_perm.mode == (SEM_RA)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 262 | tst_resm(TPASS, "buf.sem_nsems and buf.sem_perm.mode" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 263 | " are correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 264 | } else { |
| 265 | tst_resm(TFAIL, "semaphore STAT info is incorrect"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * set_setup() - set up for the IPC_SET command with semctl() |
| 271 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 272 | void set_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 273 | { |
| 274 | /* set up a new mode for the semaphore set */ |
| 275 | buf.sem_perm.mode = SEM_RA | NEWMODE; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * func_set() - check the functionality of the IPC_SET command with semctl() |
| 280 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | void func_set() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 282 | { |
| 283 | /* first stat the semaphore to get the new data */ |
| 284 | if (semctl(sem_id_1, 0, IPC_STAT, (union semun)&buf) == -1) { |
| 285 | tst_resm(TBROK, "stat failed in func_set()"); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | /* check that the new mode is what we set */ |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 290 | if (buf.sem_perm.mode == (SEM_RA | NEWMODE)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 291 | tst_resm(TPASS, "buf.sem_perm.mode is correct"); |
| 292 | } else { |
| 293 | tst_resm(TFAIL, "semaphore mode info is incorrect"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * func_gall() - check the functionality of the GETALL command with semctl() |
| 299 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 300 | void func_gall() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 301 | { |
| 302 | int i; |
| 303 | |
| 304 | /* the initial value of the primitive semaphores should be zero */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 305 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 306 | if (array[i] != 0) { |
| 307 | tst_resm(TFAIL, "semaphore %d has unexpected value", i); |
| 308 | return; |
| 309 | } |
| 310 | } |
| 311 | tst_resm(TPASS, "semaphores have expected values"); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl() |
| 316 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 317 | void cnt_setup(int opval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 318 | { |
| 319 | int pid, i; |
| 320 | |
| 321 | sops.sem_num = SEM4; |
| 322 | sops.sem_flg = 0; |
| 323 | |
| 324 | /* |
| 325 | * if seting up for GETZCNT, the semaphore value needs to be positive |
| 326 | */ |
| 327 | if (opval == 0) { |
| 328 | /* initialize the semaphore value to ONE */ |
| 329 | sops.sem_op = ONE; |
| 330 | if (semop(sem_id_1, &sops, 1) == -1) { |
| 331 | tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup"); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | sops.sem_op = opval; /* set the correct operation */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 336 | for (i = 0; i < NCHILD; i++) { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 337 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 338 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 339 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 340 | /* fork five children to wait */ |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 341 | if ((pid = FORK_OR_VFORK()) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 342 | tst_brkm(TBROK, cleanup, "fork failed in cnt_setup"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 343 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 344 | if (pid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 345 | #ifdef UCLINUX |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 346 | sem_op = sops.sem_op; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 347 | if (self_exec(argv0, "ndd", 2, sem_id_1, sem_op) < 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 348 | tst_brkm(TBROK, cleanup, "self_exec failed " |
| 349 | "in cnt_setup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 350 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 351 | #else |
| 352 | child_cnt(); |
| 353 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 354 | } else { /* parent */ |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 355 | if (sync_pipe_wait(sync_pipes) == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 356 | tst_brkm(TBROK, cleanup, |
| 357 | "sync_pipe_wait failed"); |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 358 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 359 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 360 | tst_brkm(TBROK, cleanup, |
| 361 | "sync_pipe_close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 362 | |
| 363 | /* save the pid so we can kill it later */ |
| 364 | pid_arr[i] = pid; |
| 365 | } |
| 366 | } |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 367 | /* After last son has been created, give it a chance to execute the |
| 368 | * semop command before we continue. Without this sleep, on SMP machine |
| 369 | * the father semctl could be executed before the son semop. |
| 370 | */ |
| 371 | sleep(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 372 | } |
| 373 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 374 | void child_cnt() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 375 | { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 376 | #ifdef UCLINUX |
| 377 | sops.sem_op = (short int)sem_op; |
| 378 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) |
| 379 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
| 380 | #endif |
| 381 | |
| 382 | if (sync_pipe_notify(sync_pipes) == -1) |
| 383 | tst_brkm(TBROK, cleanup, "sync_pipe_notify failed"); |
| 384 | |
Subrata Modak | 9e365e8 | 2010-07-03 23:13:41 +0530 | [diff] [blame] | 385 | #ifdef UCLINUX |
| 386 | if (sync_pipe_close(sync_pipes, NULL) == -1) |
| 387 | #else |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 388 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
Subrata Modak | 9e365e8 | 2010-07-03 23:13:41 +0530 | [diff] [blame] | 389 | #endif |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 390 | tst_brkm(TBROK, cleanup, "sync_pipe_close failed"); |
| 391 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 392 | sops.sem_num = SEM4; |
| 393 | sops.sem_flg = 0; |
| 394 | |
| 395 | /* |
| 396 | * Do a semop that will cause the child to sleep. |
| 397 | * The child process will be killed in the func_ncnt |
| 398 | * routine which should cause an error to be return |
| 399 | * by the semop() call. |
| 400 | */ |
| 401 | if (semop(sem_id_1, &sops, 1) != -1) { |
| 402 | tst_resm(TBROK, "semop succeeded - cnt_setup"); |
| 403 | } |
| 404 | exit(0); |
| 405 | } |
| 406 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 407 | /* |
| 408 | * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands |
| 409 | * with semctl() |
| 410 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 411 | void func_cnt(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 412 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 413 | |
| 414 | if (rval == NCHILD) { |
| 415 | tst_resm(TPASS, "number of sleeping processes is correct"); |
| 416 | } else { |
| 417 | tst_resm(TFAIL, "number of sleeping processes is not correct"); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * pid_setup() - set up for the GETPID command with semctl() |
| 423 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 424 | void pid_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 425 | { |
| 426 | int pid; |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 427 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 428 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) { |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 429 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 430 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 431 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 432 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 433 | * Fork a child to do a semop that will pass. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 434 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 435 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 436 | tst_brkm(TBROK, cleanup, "fork failed in pid_setup()"); |
| 437 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 438 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 439 | if (pid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 440 | #ifdef UCLINUX |
| 441 | if (self_exec(argv0, "nd", 1, sem_id_1) < 0) { |
| 442 | tst_brkm(TBROK, cleanup, "self_exec failed " |
| 443 | "in pid_setup()"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 444 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 445 | #else |
| 446 | child_pid(); |
| 447 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 448 | } else { /* parent */ |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 449 | if (sync_pipe_wait(sync_pipes) == -1) |
| 450 | tst_brkm(TBROK, cleanup, "sync_pipe_wait failed"); |
| 451 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 452 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 453 | tst_brkm(TBROK, cleanup, "sync_pipe_close failed"); |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 454 | sleep(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 455 | pid_arr[SEM2] = pid; |
| 456 | } |
| 457 | } |
| 458 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 459 | void child_pid() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 460 | { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 461 | #ifdef UCLINUX |
| 462 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) |
| 463 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
| 464 | #endif |
| 465 | |
| 466 | if (sync_pipe_notify(sync_pipes) == -1) |
| 467 | tst_brkm(TBROK, cleanup, "sync_pipe_notify failed"); |
| 468 | |
| 469 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
| 470 | tst_brkm(TBROK, cleanup, "sync_pipe_close failed"); |
| 471 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 472 | sops.sem_num = SEM2; /* semaphore to change */ |
| 473 | sops.sem_op = ONE; /* operation is to increment semaphore */ |
| 474 | sops.sem_flg = 0; |
| 475 | |
| 476 | /* |
| 477 | * Do a semop that will increment the semaphore. |
| 478 | */ |
| 479 | if (semop(sem_id_1, &sops, 1) == -1) { |
| 480 | tst_resm(TBROK, "semop failed - pid_setup"); |
| 481 | } |
| 482 | exit(0); |
| 483 | } |
| 484 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 485 | /* |
| 486 | * func_pid() - check the functionality of the GETPID command with semctl() |
| 487 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 488 | void func_pid(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 489 | { |
| 490 | /* compare the rval (pid) to the saved pid from the setup */ |
| 491 | if (rval == pid_arr[SEM2]) { |
| 492 | tst_resm(TPASS, "last pid value is correct"); |
| 493 | } else { |
| 494 | tst_resm(TFAIL, "last pid value is not correct"); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * func_gval() - check the functionality of the GETVAL command with semctl() |
| 500 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 501 | void func_gval(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 502 | { |
| 503 | /* |
| 504 | * This is a simple test. The semaphore value should be equal |
| 505 | * to ONE as it was set in the last test (GETPID). |
| 506 | */ |
| 507 | if (rval == 1) { |
| 508 | tst_resm(TPASS, "semaphore value is correct"); |
| 509 | } else { |
| 510 | tst_resm(TFAIL, "semaphore value is not correct"); |
| 511 | } |
| 512 | |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * all_setup() - set up for the SETALL command with semctl() |
| 517 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 518 | void sall_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 519 | { |
| 520 | int i; |
| 521 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 522 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 523 | /* initialize the array values to 3 */ |
| 524 | array[i] = 3; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * func_sall() - check the functionality of the SETALL command with semctl() |
| 530 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 531 | void func_sall() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 532 | { |
| 533 | int i; |
| 534 | unsigned short rarray[PSEMS]; |
| 535 | |
| 536 | /* |
| 537 | * do a GETALL and compare the values to those set above |
| 538 | */ |
| 539 | |
| 540 | if (semctl(sem_id_1, 0, GETALL, (union semun)rarray) == -1) { |
| 541 | tst_brkm(TBROK, cleanup, "semctl failed in func_sall"); |
| 542 | } |
| 543 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 544 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 545 | if (array[i] != rarray[i]) { |
| 546 | tst_resm(TFAIL, "semaphore values are not correct"); |
| 547 | return; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | tst_resm(TPASS, "semaphore values are correct"); |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * func_sval() - check the functionality of the SETVAL command with semctl() |
| 556 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 557 | void func_sval() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 558 | { |
| 559 | int semv; |
iyermanoj | e2cb9cd | 2002-01-21 18:48:13 +0000 | [diff] [blame] | 560 | union semun arr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * do a GETVAL and compare it to the value set above |
| 564 | */ |
| 565 | |
iyermanoj | e2cb9cd | 2002-01-21 18:48:13 +0000 | [diff] [blame] | 566 | if ((semv = semctl(sem_id_1, SEM4, GETVAL, arr)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 567 | tst_brkm(TBROK, cleanup, "semctl failed in func_sval"); |
| 568 | } |
| 569 | |
| 570 | if (semv != INCVAL) { |
| 571 | tst_resm(TFAIL, "semaphore value is not what was set"); |
| 572 | } else { |
| 573 | tst_resm(TPASS, "semaphore value is correct"); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * func_rmid() - check the functionality of the IPC_RMID command with semctl() |
| 579 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 580 | void func_rmid() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 581 | { |
| 582 | |
| 583 | /* |
| 584 | * do a semop() - we should get EINVAL |
| 585 | */ |
| 586 | if (semop(sem_id_1, &sops, 1) != -1) { |
| 587 | tst_resm(TFAIL, "semop succeeded on expected fail"); |
| 588 | } |
| 589 | |
| 590 | if (errno != EINVAL) { |
| 591 | tst_resm(TFAIL, "returned errno - %d - is not expected", errno); |
| 592 | } else { |
| 593 | tst_resm(TPASS, "semaphore appears to be removed"); |
| 594 | } |
| 595 | |
| 596 | sem_id_1 = -1; |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * setup() - performs all the ONE TIME setup for this test. |
| 601 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 602 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 603 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 604 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 605 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 606 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 607 | TEST_PAUSE; |
| 608 | |
| 609 | /* |
| 610 | * Create a temporary directory and cd into it. |
| 611 | * This helps to ensure that a unique msgkey is created. |
| 612 | * See ../lib/libipc.c for more information. |
| 613 | */ |
| 614 | tst_tmpdir(); |
| 615 | |
| 616 | /* get an IPC resource key */ |
| 617 | semkey = getipckey(); |
| 618 | |
| 619 | /* create a semaphore set with read and alter permissions */ |
| 620 | if ((sem_id_1 = |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 621 | semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 622 | tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup"); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 628 | * or premature exit. |
| 629 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 630 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 631 | { |
| 632 | /* if it exists, remove the semaphore resource */ |
| 633 | rm_sema(sem_id_1); |
| 634 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 635 | tst_rmdir(); |
| 636 | |
| 637 | /* |
| 638 | * print timing stats if that option was specified. |
| 639 | * print errno log if that option was specified. |
| 640 | */ |
| 641 | TEST_CLEANUP; |
| 642 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 643 | } |