plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * NAME |
| 21 | * semctl01.c |
| 22 | * |
| 23 | * DESCRIPTION |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 24 | * semctl01 - test the 13 possible semctl() commands |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * |
| 26 | * ALGORITHM |
| 27 | * create a semaphore set with read and alter permissions |
| 28 | * loop if that option was specified |
| 29 | * loop through the test cases |
| 30 | * do any setup required for the test case |
| 31 | * make the semctl() call using the TEST() macro |
| 32 | * check the return code |
| 33 | * if failure, issue a FAIL message. |
| 34 | * otherwise, |
| 35 | * if doing functionality testing |
| 36 | * call the appropriate test function |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 37 | * if correct, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * issue a PASS message |
| 39 | * otherwise |
| 40 | * issue a FAIL message |
| 41 | * call cleanup |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | */ |
| 43 | |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 44 | |
| 45 | #ifndef _GNU_SOURCE |
| 46 | #define _GNU_SOURCE |
| 47 | #endif |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 48 | #include <sys/wait.h> |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 49 | #include "ipcsem.h" |
Cyril Hrubis | 49da8ad | 2017-10-03 13:45:04 +0200 | [diff] [blame] | 50 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | char *TCID = "semctl01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 54 | static int sem_id_1 = -1; |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 55 | static int sem_index; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * These are the various setup and check functions for the 10 different |
| 59 | * commands that are available for the semctl() call. |
| 60 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 61 | static void func_stat(void); |
| 62 | static void set_setup(void), func_set(void); |
| 63 | static void func_gall(void); |
| 64 | static void cnt_setup(int), func_cnt(int); |
| 65 | static void pid_setup(void), func_pid(int); |
| 66 | static void func_gval(int); |
| 67 | static void sall_setup(void), func_sall(void); |
| 68 | static void func_sval(void); |
| 69 | static void func_rmid(void); |
| 70 | static void child_cnt(void); |
| 71 | static void child_pid(void); |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 72 | static void func_iinfo(int); |
| 73 | static void func_sinfo(void); |
| 74 | static void func_sstat(int); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 76 | static struct semid_ds buf; |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 77 | static struct seminfo ipc_buf; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 78 | static unsigned short array[PSEMS]; |
| 79 | static struct sembuf sops; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 81 | #define INCVAL 2 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | #define NEWMODE 066 |
| 83 | #define NCHILD 5 |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 84 | #define SEM2 2 |
| 85 | #define SEM4 4 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | #define ONE 1 |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 87 | #ifdef _XLC_COMPILER |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 88 | #define SEMUN_CAST |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 89 | #else |
| 90 | #define SEMUN_CAST (union semun) |
| 91 | #endif |
| 92 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 93 | static int pid_arr[NCHILD]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 95 | #ifdef UCLINUX |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 96 | #define PIPE_NAME "semctl01" |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 97 | static char *argv0; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 98 | static int sem_op; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 101 | static struct test_case_t { |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 102 | int *semid; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 103 | int semnum; |
| 104 | int cmd; |
| 105 | void (*func_test) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | union semun arg; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 107 | void (*func_setup) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } TC[] = { |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 109 | {&sem_id_1, 0, IPC_STAT, func_stat, SEMUN_CAST & buf, NULL}, |
| 110 | {&sem_id_1, 0, IPC_SET, func_set, SEMUN_CAST & buf, set_setup}, |
| 111 | {&sem_id_1, 0, GETALL, func_gall, SEMUN_CAST array, NULL}, |
| 112 | {&sem_id_1, SEM4, GETNCNT, func_cnt, SEMUN_CAST & buf, cnt_setup}, |
| 113 | {&sem_id_1, SEM2, GETPID, func_pid, SEMUN_CAST & buf, pid_setup}, |
| 114 | {&sem_id_1, SEM2, GETVAL, func_gval, SEMUN_CAST & buf, NULL}, |
| 115 | {&sem_id_1, SEM4, GETZCNT, func_cnt, SEMUN_CAST & buf, cnt_setup}, |
| 116 | {&sem_id_1, 0, SETALL, func_sall, SEMUN_CAST array, sall_setup}, |
| 117 | {&sem_id_1, SEM4, SETVAL, func_sval, SEMUN_CAST INCVAL, NULL}, |
| 118 | {&sem_id_1, 0, IPC_INFO, func_iinfo, SEMUN_CAST & ipc_buf, NULL}, |
| 119 | {&sem_id_1, 0, SEM_INFO, func_sinfo, SEMUN_CAST & ipc_buf, NULL}, |
| 120 | {&sem_index, 0, SEM_STAT, func_sstat, SEMUN_CAST & buf, NULL}, |
| 121 | {&sem_id_1, 0, IPC_RMID, func_rmid, SEMUN_CAST & buf, NULL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 124 | int TST_TOTAL = ARRAY_SIZE(TC); |
| 125 | |
| 126 | static void kill_all_children(void) |
| 127 | { |
| 128 | int j, status; |
| 129 | |
| 130 | for (j = 0; j < NCHILD; j++) { |
Cyril Hrubis | d84a7ec | 2017-10-03 13:48:07 +0200 | [diff] [blame] | 131 | SAFE_KILL(cleanup, pid_arr[j], SIGKILL); |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
| 135 | * make sure children finished before we proceed with next testcase |
| 136 | */ |
| 137 | for (j = 0; j < NCHILD; j++) { |
Cyril Hrubis | 49da8ad | 2017-10-03 13:45:04 +0200 | [diff] [blame] | 138 | SAFE_WAIT(cleanup, &status); |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 142 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 144 | int lc; |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 145 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 147 | tst_parse_opts(argc, argv, NULL, NULL); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 148 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 149 | #ifdef UCLINUX |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 150 | argv0 = argv[0]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 151 | maybe_run_child(&child_pid, "nd", 1, &sem_id_1); |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 152 | maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sem_op); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 155 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
| 157 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 158 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * Set up any conditions if needed |
| 164 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | if (TC[i].func_setup != NULL) { |
| 166 | /* call the setup function */ |
| 167 | switch (TC[i].cmd) { |
| 168 | case GETNCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | (*TC[i].func_setup) (-ONE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | break; |
| 171 | case GETZCNT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | (*TC[i].func_setup) (0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | break; |
| 174 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | (*TC[i].func_setup) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 180 | TEST(semctl(*(TC[i].semid), TC[i].semnum, TC[i].cmd, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | TC[i].arg)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 182 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | if (TEST_RETURN == -1) { |
| 184 | tst_resm(TFAIL, "%s call failed - errno = %d " |
| 185 | ": %s", TCID, TEST_ERRNO, |
| 186 | strerror(TEST_ERRNO)); |
| 187 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 188 | /* |
| 189 | * call the appropriate test function |
| 190 | * and pass the return value where it |
| 191 | * is needed to perform certain tests. |
| 192 | */ |
| 193 | switch (TC[i].cmd) { |
| 194 | case GETNCNT: |
| 195 | case GETZCNT: |
| 196 | case GETPID: |
| 197 | case GETVAL: |
| 198 | case IPC_INFO: |
| 199 | case SEM_STAT: |
| 200 | (*TC[i].func_test) (TEST_RETURN); |
| 201 | break; |
| 202 | default: |
| 203 | (*TC[i].func_test) (); |
| 204 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * If testing GETNCNT or GETZCNT, clean up the children. |
| 210 | */ |
| 211 | switch (TC[i].cmd) { |
| 212 | case GETNCNT: |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 213 | case GETZCNT: |
Jan Stancek | 7018c13 | 2015-11-13 14:05:28 +0100 | [diff] [blame] | 214 | kill_all_children(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | } |
| 218 | /* |
| 219 | * recreate the semaphore resource if looping |
| 220 | */ |
| 221 | if (TEST_LOOPING(lc)) { |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 222 | sem_id_1 = semget(semkey, PSEMS, |
| 223 | IPC_CREAT | IPC_EXCL | SEM_RA); |
| 224 | if (sem_id_1 == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | tst_brkm(TBROK, cleanup, |
| 226 | "couldn't recreate " "semaphore"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | cleanup(); |
| 231 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 232 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * func_stat() - check the functionality of the IPC_STAT command with semctl() |
| 237 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 238 | static void func_stat(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | { |
| 240 | /* check the number of semaphores and the ipc_perm.mode value */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 241 | if (buf.sem_nsems == PSEMS && buf.sem_perm.mode == (SEM_RA)) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 242 | tst_resm(TPASS, "buf.sem_nsems and buf.sem_perm.mode" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 243 | " are correct"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 244 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | tst_resm(TFAIL, "semaphore STAT info is incorrect"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
| 249 | * set_setup() - set up for the IPC_SET command with semctl() |
| 250 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 251 | static void set_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | { |
| 253 | /* set up a new mode for the semaphore set */ |
| 254 | buf.sem_perm.mode = SEM_RA | NEWMODE; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * func_set() - check the functionality of the IPC_SET command with semctl() |
| 259 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 260 | static void func_set(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 261 | { |
| 262 | /* first stat the semaphore to get the new data */ |
| 263 | if (semctl(sem_id_1, 0, IPC_STAT, (union semun)&buf) == -1) { |
| 264 | tst_resm(TBROK, "stat failed in func_set()"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | /* check that the new mode is what we set */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 269 | if (buf.sem_perm.mode == (SEM_RA | NEWMODE)) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | tst_resm(TPASS, "buf.sem_perm.mode is correct"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 271 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 272 | tst_resm(TFAIL, "semaphore mode info is incorrect"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * func_gall() - check the functionality of the GETALL command with semctl() |
| 277 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 278 | static void func_gall(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 279 | { |
| 280 | int i; |
| 281 | |
| 282 | /* the initial value of the primitive semaphores should be zero */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | if (array[i] != 0) { |
| 285 | tst_resm(TFAIL, "semaphore %d has unexpected value", i); |
| 286 | return; |
| 287 | } |
| 288 | } |
| 289 | tst_resm(TPASS, "semaphores have expected values"); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl() |
| 294 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 295 | static void cnt_setup(int opval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 296 | { |
| 297 | int pid, i; |
| 298 | |
| 299 | sops.sem_num = SEM4; |
| 300 | sops.sem_flg = 0; |
| 301 | |
| 302 | /* |
| 303 | * if seting up for GETZCNT, the semaphore value needs to be positive |
| 304 | */ |
| 305 | if (opval == 0) { |
| 306 | /* initialize the semaphore value to ONE */ |
| 307 | sops.sem_op = ONE; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 308 | if (semop(sem_id_1, &sops, 1) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 309 | tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 310 | } |
| 311 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 312 | /* set the correct operation */ |
| 313 | sops.sem_op = opval; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 314 | for (i = 0; i < NCHILD; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 315 | /* fork five children to wait */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 316 | pid = FORK_OR_VFORK(); |
| 317 | if (pid == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 318 | tst_brkm(TBROK, cleanup, "fork failed in cnt_setup"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 319 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 320 | if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 321 | #ifdef UCLINUX |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 322 | sem_op = sops.sem_op; |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 323 | if (self_exec(argv0, "ndd", 2, sem_id_1, sem_op) < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 324 | tst_brkm(TBROK, cleanup, "self_exec failed " |
| 325 | "in cnt_setup"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 326 | #else |
| 327 | child_cnt(); |
| 328 | #endif |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 329 | } else { |
Cyril Hrubis | f9791ef | 2015-03-05 16:09:16 +0100 | [diff] [blame] | 330 | TST_PROCESS_STATE_WAIT(cleanup, pid, 'S'); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 331 | /* save the pid so we can kill it later */ |
| 332 | pid_arr[i] = pid; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 337 | static void child_cnt(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 338 | { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 339 | #ifdef UCLINUX |
| 340 | sops.sem_op = (short int)sem_op; |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 341 | #endif |
| 342 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 343 | sops.sem_num = SEM4; |
| 344 | sops.sem_flg = 0; |
| 345 | |
| 346 | /* |
| 347 | * Do a semop that will cause the child to sleep. |
| 348 | * The child process will be killed in the func_ncnt |
| 349 | * routine which should cause an error to be return |
| 350 | * by the semop() call. |
| 351 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 352 | if (semop(sem_id_1, &sops, 1) != -1) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 353 | tst_resm(TBROK, "semop succeeded - cnt_setup"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 354 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 355 | exit(0); |
| 356 | } |
| 357 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 358 | /* |
| 359 | * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands |
| 360 | * with semctl() |
| 361 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 362 | static void func_cnt(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 363 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 364 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 365 | if (rval == NCHILD) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 366 | tst_resm(TPASS, "number of sleeping processes is correct"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 367 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 368 | tst_resm(TFAIL, "number of sleeping processes is not correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
| 372 | * pid_setup() - set up for the GETPID command with semctl() |
| 373 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 374 | static void pid_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 375 | { |
| 376 | int pid; |
subrata_modak | 41399ed | 2008-03-14 19:16:23 +0000 | [diff] [blame] | 377 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 378 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 379 | * Fork a child to do a semop that will pass. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 380 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 381 | pid = FORK_OR_VFORK(); |
| 382 | if (pid == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 383 | tst_brkm(TBROK, cleanup, "fork failed in pid_setup()"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 384 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 385 | if (pid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 386 | #ifdef UCLINUX |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 387 | if (self_exec(argv0, "nd", 1, sem_id_1) < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 388 | tst_brkm(TBROK, cleanup, "self_exec failed " |
| 389 | "in pid_setup()"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 390 | #else |
| 391 | child_pid(); |
| 392 | #endif |
Cyril Hrubis | f9791ef | 2015-03-05 16:09:16 +0100 | [diff] [blame] | 393 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 394 | pid_arr[SEM2] = pid; |
Cyril Hrubis | f9791ef | 2015-03-05 16:09:16 +0100 | [diff] [blame] | 395 | TST_PROCESS_STATE_WAIT(cleanup, pid, 'Z'); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 399 | static void child_pid(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 400 | { |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 401 | sops.sem_num = SEM2; |
| 402 | sops.sem_op = ONE; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 403 | sops.sem_flg = 0; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 404 | /* |
| 405 | * Do a semop that will increment the semaphore. |
| 406 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 407 | if (semop(sem_id_1, &sops, 1) == -1) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 408 | tst_resm(TBROK, "semop failed - pid_setup"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 409 | exit(0); |
| 410 | } |
| 411 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 412 | /* |
| 413 | * func_pid() - check the functionality of the GETPID command with semctl() |
| 414 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 415 | static void func_pid(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 416 | { |
| 417 | /* compare the rval (pid) to the saved pid from the setup */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 418 | if (rval == pid_arr[SEM2]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 419 | tst_resm(TPASS, "last pid value is correct"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 420 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 421 | tst_resm(TFAIL, "last pid value is not correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
| 425 | * func_gval() - check the functionality of the GETVAL command with semctl() |
| 426 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 427 | static void func_gval(int rval) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 428 | { |
| 429 | /* |
| 430 | * This is a simple test. The semaphore value should be equal |
| 431 | * to ONE as it was set in the last test (GETPID). |
| 432 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 433 | if (rval == 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 434 | tst_resm(TPASS, "semaphore value is correct"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 435 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 436 | tst_resm(TFAIL, "semaphore value is not correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | * all_setup() - set up for the SETALL command with semctl() |
| 441 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 442 | static void sall_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 443 | { |
| 444 | int i; |
| 445 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 446 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 447 | /* initialize the array values to 3 */ |
| 448 | array[i] = 3; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * func_sall() - check the functionality of the SETALL command with semctl() |
| 454 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 455 | static void func_sall(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 456 | { |
| 457 | int i; |
| 458 | unsigned short rarray[PSEMS]; |
| 459 | |
| 460 | /* |
| 461 | * do a GETALL and compare the values to those set above |
| 462 | */ |
| 463 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 464 | if (semctl(sem_id_1, 0, GETALL, (union semun)rarray) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 465 | tst_brkm(TBROK, cleanup, "semctl failed in func_sall"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 466 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 467 | for (i = 0; i < PSEMS; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 468 | if (array[i] != rarray[i]) { |
| 469 | tst_resm(TFAIL, "semaphore values are not correct"); |
| 470 | return; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | tst_resm(TPASS, "semaphore values are correct"); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * func_sval() - check the functionality of the SETVAL command with semctl() |
| 479 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 480 | static void func_sval(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 481 | { |
| 482 | int semv; |
iyermanoj | e2cb9cd | 2002-01-21 18:48:13 +0000 | [diff] [blame] | 483 | union semun arr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * do a GETVAL and compare it to the value set above |
| 487 | */ |
| 488 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 489 | semv = semctl(sem_id_1, SEM4, GETVAL, arr); |
| 490 | if (semv == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 491 | tst_brkm(TBROK, cleanup, "semctl failed in func_sval"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 492 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 493 | if (semv != INCVAL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 494 | tst_resm(TFAIL, "semaphore value is not what was set"); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 495 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 496 | tst_resm(TPASS, "semaphore value is correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
| 500 | * func_rmid() - check the functionality of the IPC_RMID command with semctl() |
| 501 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 502 | static void func_rmid(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 503 | { |
| 504 | |
| 505 | /* |
| 506 | * do a semop() - we should get EINVAL |
| 507 | */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 508 | if (semop(sem_id_1, &sops, 1) != -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 509 | tst_resm(TFAIL, "semop succeeded on expected fail"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 510 | |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 511 | if (errno != EINVAL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 512 | tst_resm(TFAIL, "returned errno - %d - is not expected", errno); |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 513 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 514 | tst_resm(TPASS, "semaphore appears to be removed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 515 | |
| 516 | sem_id_1 = -1; |
| 517 | } |
| 518 | |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 519 | static void func_iinfo(int hidx) |
| 520 | { |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 521 | if (hidx >= 0) { |
| 522 | sem_index = hidx; |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 523 | tst_resm(TPASS, "the highest index is correct"); |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 524 | } else { |
| 525 | sem_index = 0; |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 526 | tst_resm(TFAIL, "the highest index is incorrect"); |
DAN LI | f74f0a9 | 2013-05-22 09:52:09 +0800 | [diff] [blame] | 527 | } |
DAN LI | a63f933 | 2013-05-13 10:24:33 +0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void func_sinfo(void) |
| 531 | { |
| 532 | if (ipc_buf.semusz < 1) |
| 533 | tst_resm(TFAIL, "number of semaphore sets is incorrect"); |
| 534 | else |
| 535 | tst_resm(TPASS, "number of semaphore sets is correct"); |
| 536 | } |
| 537 | |
| 538 | static void func_sstat(int semidx) |
| 539 | { |
| 540 | if (semidx >= 0) |
| 541 | tst_resm(TPASS, "id of the semaphore set is correct"); |
| 542 | else |
| 543 | tst_resm(TFAIL, "id of the semaphore set is incorrect"); |
| 544 | } |
| 545 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 546 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 547 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 548 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 549 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 550 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 551 | TEST_PAUSE; |
| 552 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 553 | tst_tmpdir(); |
| 554 | |
Cyril Hrubis | f9791ef | 2015-03-05 16:09:16 +0100 | [diff] [blame] | 555 | TST_CHECKPOINT_INIT(tst_rmdir); |
| 556 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 557 | /* get an IPC resource key */ |
| 558 | semkey = getipckey(); |
| 559 | |
| 560 | /* create a semaphore set with read and alter permissions */ |
DAN LI | bffa886 | 2013-05-13 10:21:43 +0800 | [diff] [blame] | 561 | sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA); |
| 562 | if (sem_id_1 == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 563 | tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 564 | } |
| 565 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 566 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 567 | { |
| 568 | /* if it exists, remove the semaphore resource */ |
| 569 | rm_sema(sem_id_1); |
| 570 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 571 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 572 | } |