subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 2 | /* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd */ |
| 3 | /* Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>, */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 4 | /* Yumiko Sugita <yumiko.sugita.yf@hitachi.com>, */ |
| 5 | /* Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp> */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 6 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 7 | /* This program is free software; you can redistribute it and/or modify */ |
| 8 | /* it under the terms of the GNU General Public License as published by */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 9 | /* the Free Software Foundation; either version 2 of the License, or */ |
| 10 | /* (at your option) any later version. */ |
| 11 | /* */ |
| 12 | /* This program is distributed in the hope that it will be useful, */ |
| 13 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ |
| 15 | /* the GNU General Public License for more details. */ |
| 16 | /* */ |
| 17 | /* You should have received a copy of the GNU General Public License */ |
| 18 | /* along with this program; if not, write to the Free Software */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 19 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 20 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 21 | /******************************************************************************/ |
| 22 | /******************************************************************************/ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 23 | /* */ |
| 24 | /* File: mq_ulink01.c */ |
| 25 | /* */ |
| 26 | /* Description: This tests the mq_ulink() syscall */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 27 | /* */ |
| 28 | /* */ |
| 29 | /* */ |
| 30 | /* */ |
| 31 | /* */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 32 | /* */ |
| 33 | /* Usage: <for command-line> */ |
| 34 | /* mq_ulink01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 35 | /* where, -c n : Run n copies concurrently. */ |
| 36 | /* -e : Turn on errno logging. */ |
| 37 | /* -i n : Execute test n times. */ |
| 38 | /* -I x : Execute test for x seconds. */ |
| 39 | /* -P x : Pause for x seconds between iterations. */ |
| 40 | /* -t : Turn on syscall timing. */ |
| 41 | /* */ |
| 42 | /* Total Tests: 1 */ |
| 43 | /* */ |
| 44 | /* Test Name: mq_ulink01 */ |
| 45 | /* History: Porting from Crackerjack to LTP is done by */ |
| 46 | /* Manas Kumar Nayak maknayak@in.ibm.com> */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 47 | /******************************************************************************/ |
| 48 | |
| 49 | #include <sys/syscall.h> |
| 50 | #include <sys/types.h> |
| 51 | #include <sys/stat.h> |
| 52 | #include <sys/uio.h> |
| 53 | #include <getopt.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <errno.h> |
| 56 | #include <stdio.h> |
| 57 | #include <unistd.h> |
| 58 | #include <string.h> |
| 59 | #include <mqueue.h> |
| 60 | #include <limits.h> |
| 61 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 62 | #include "../utils/include_j_h.h" |
| 63 | #include "../utils/common_j_h.c" |
| 64 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 65 | #include "test.h" |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 66 | #include "linux_syscall_numbers.h" |
| 67 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 68 | char *TCID = "mq_ulink01"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 70 | int TST_TOTAL = 1; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 71 | |
| 72 | /* Extern Global Functions */ |
| 73 | /******************************************************************************/ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 74 | /* */ |
| 75 | /* Function: cleanup */ |
| 76 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 77 | /* Description: Performs all one time clean up for this test on successful */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 78 | /* completion, premature exit or failure. Closes all temporary */ |
| 79 | /* files, removes all temporary directories exits the test with */ |
| 80 | /* appropriate return code by calling tst_exit() function. */ |
| 81 | /* */ |
| 82 | /* Input: None. */ |
| 83 | /* */ |
| 84 | /* Output: None. */ |
| 85 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 86 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 87 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 88 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 89 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 90 | void cleanup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 92 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 93 | tst_rmdir(); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Local Functions */ |
| 97 | /******************************************************************************/ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 98 | /* */ |
| 99 | /* Function: setup */ |
| 100 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 101 | /* Description: Performs all one time setup for this test. This function is */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 102 | /* typically used to capture signals, create temporary dirs */ |
| 103 | /* and temporary files that may be used in the course of this */ |
| 104 | /* test. */ |
| 105 | /* */ |
| 106 | /* Input: None. */ |
| 107 | /* */ |
| 108 | /* Output: None. */ |
| 109 | /* */ |
| 110 | /* Return: On failure - Exits by calling cleanup(). */ |
| 111 | /* On success - returns 0. */ |
| 112 | /* */ |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 113 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 114 | void setup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | { |
Cyril Hrubis | 91bfa5b | 2014-02-12 17:28:17 +0100 | [diff] [blame] | 116 | tst_require_root(NULL); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 117 | /* Capture signals if any */ |
| 118 | /* Create temporary directories */ |
| 119 | TEST_PAUSE; |
| 120 | tst_tmpdir(); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Macros |
| 125 | */ |
| 126 | #define SYSCALL_NAME "mq_ulink" |
| 127 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 128 | enum test_type { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 129 | NORMAL, |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 132 | /* |
| 133 | * Data Structure |
| 134 | */ |
| 135 | struct test_case { |
| 136 | char *user; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | char *qname; |
| 138 | int ttype; |
| 139 | int ret; |
| 140 | int err; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 143 | /* Test cases |
| 144 | * |
| 145 | * test status of errors on man page |
| 146 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 147 | * EACCES v (permission is denied) |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 148 | * ENAMETOOLONG v (too long name length) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 149 | * ENOENT v (named message queue does not exist) |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 150 | */ |
| 151 | |
| 152 | static struct test_case tcase[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | { // case00 |
| 154 | .ttype = NORMAL, |
| 155 | .qname = QUEUE_NAME, |
| 156 | .ret = 0, |
| 157 | .err = 0, |
| 158 | }, |
| 159 | { // case01 |
| 160 | .ttype = NORMAL, |
| 161 | .user = "nobody", |
| 162 | .qname = QUEUE_NAME, |
| 163 | .ret = -1, |
| 164 | .err = EACCES, |
| 165 | }, |
| 166 | { // case02 |
| 167 | .ttype = NORMAL, |
| 168 | // 0 1 2 3 |
| 169 | // 0123456789012345678901234567890123456789 |
| 170 | .qname = "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 171 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 172 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 173 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 174 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 175 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaa", |
| 176 | .ret = -1, |
| 177 | .err = ENOENT, |
| 178 | }, |
| 179 | { // case03 |
| 180 | .ttype = NORMAL, |
| 181 | // 0 1 2 3 |
| 182 | // 0123456789012345678901234567890123456789 |
| 183 | .qname = "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 184 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 185 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 186 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 187 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 188 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaa", |
| 189 | .ret = -1, |
| 190 | .err = ENAMETOOLONG, |
| 191 | }, |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | /* |
| 195 | * do_test() |
| 196 | * |
| 197 | * Input : TestCase Data |
| 198 | * Return : RESULT_OK(0), RESULT_NG(1) |
| 199 | * |
| 200 | */ |
| 201 | |
| 202 | static int do_test(struct test_case *tc) |
| 203 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 204 | int sys_ret; |
| 205 | int sys_errno; |
| 206 | int result = RESULT_OK; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 207 | int rc, fd1 = -1, fd2 = -1; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 208 | uid_t old_uid = -1; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 209 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 210 | /* |
| 211 | * When test ended with SIGTERM etc, mq discriptor is left remains. |
| 212 | * So we delete it first. |
| 213 | */ |
| 214 | TEST(mq_unlink(QUEUE_NAME)); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 215 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 216 | /* |
| 217 | * Open message queue |
| 218 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 219 | rc = mq_open(QUEUE_NAME, O_CREAT | O_EXCL | O_RDWR, S_IRWXU, NULL); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 220 | if (rc == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 221 | tst_resm(TFAIL | TTERRNO, "mq_open failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 222 | result = 1; |
| 223 | goto EXIT; |
| 224 | } |
| 225 | fd1 = rc; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 226 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 227 | /* |
| 228 | * Change effective user id |
| 229 | */ |
| 230 | if (tc->user != NULL) { |
| 231 | TEST(rc = setup_euid(tc->user, &old_uid)); |
| 232 | if (TEST_RETURN < 0) { |
| 233 | result = 1; |
| 234 | goto EXIT; |
| 235 | } |
| 236 | } |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 237 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 238 | /* |
| 239 | * Execute system call |
| 240 | */ |
| 241 | errno = 0; |
| 242 | TEST(sys_ret = mq_unlink(tc->qname)); |
| 243 | sys_errno = errno; |
| 244 | if (sys_ret >= 0) |
| 245 | fd2 = sys_ret; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 246 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 247 | /* |
| 248 | * Check results |
| 249 | */ |
| 250 | result |= (sys_errno != tc->err); |
| 251 | PRINT_RESULT(sys_ret >= 0, tc->ret, tc->err, sys_ret, sys_errno); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 252 | |
| 253 | EXIT: |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 254 | if (tc->user != NULL && old_uid != -1) |
| 255 | cleanup_euid(old_uid); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 256 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 257 | if (fd1 >= 0) |
| 258 | close(fd1); |
| 259 | if (fd2 >= 0) |
| 260 | close(fd2); |
| 261 | mq_unlink(QUEUE_NAME); |
Garrett Cooper | c89ea2b | 2010-12-19 10:07:55 -0800 | [diff] [blame] | 262 | return 0; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 265 | int main(int ac, char **av) |
| 266 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 267 | int i; |
| 268 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 269 | const char *msg; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 270 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 271 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 2384bd4 | 2010-11-22 14:53:11 -0800 | [diff] [blame] | 272 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 273 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 274 | setup(); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 275 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 276 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 277 | tst_count = 0; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 278 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 279 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 280 | int ret; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 281 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 282 | ret = 0; |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 283 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 284 | for (i = 0; ret == 0 && |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 285 | i < (int)(sizeof(tcase) / sizeof(tcase[0])); i++) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 286 | ret = do_test(&tcase[i]); |
| 287 | } |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 288 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 289 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 290 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 291 | cleanup(); |
subrata_modak | 2e2b18c | 2009-05-29 12:26:40 +0000 | [diff] [blame] | 292 | tst_exit(); |
Garrett Cooper | c89ea2b | 2010-12-19 10:07:55 -0800 | [diff] [blame] | 293 | } |