blob: 3ebc14910cdb0ccfdc404fac82ee46f82f0a9646 [file] [log] [blame]
subrata_modak2e2b18c2009-05-29 12:26:40 +00001/******************************************************************************/
Garrett Cooper53740502010-12-16 00:04:01 -08002/* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd */
3/* Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>, */
subrata_modak2e2b18c2009-05-29 12:26:40 +00004/* Yumiko Sugita <yumiko.sugita.yf@hitachi.com>, */
5/* Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp> */
Garrett Cooper53740502010-12-16 00:04:01 -08006/* */
subrata_modak2e2b18c2009-05-29 12:26:40 +00007/* 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 Cooper53740502010-12-16 00:04:01 -08009/* 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 Gao4548c6c2012-10-19 18:03:36 +080019/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
Garrett Cooper53740502010-12-16 00:04:01 -080020/* */
subrata_modak2e2b18c2009-05-29 12:26:40 +000021/******************************************************************************/
22/******************************************************************************/
Garrett Cooper53740502010-12-16 00:04:01 -080023/* */
24/* File: mq_ulink01.c */
25/* */
26/* Description: This tests the mq_ulink() syscall */
subrata_modak2e2b18c2009-05-29 12:26:40 +000027/* */
28/* */
29/* */
30/* */
31/* */
Garrett Cooper53740502010-12-16 00:04:01 -080032/* */
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_modak2e2b18c2009-05-29 12:26:40 +000047/******************************************************************************/
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_modak2e2b18c2009-05-29 12:26:40 +000062#include "../utils/include_j_h.h"
63#include "../utils/common_j_h.c"
64
subrata_modak2e2b18c2009-05-29 12:26:40 +000065#include "test.h"
subrata_modak2e2b18c2009-05-29 12:26:40 +000066#include "linux_syscall_numbers.h"
67
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020068char *TCID = "mq_ulink01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080069int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020070int TST_TOTAL = 1;
subrata_modak2e2b18c2009-05-29 12:26:40 +000071
72/* Extern Global Functions */
73/******************************************************************************/
Garrett Cooper53740502010-12-16 00:04:01 -080074/* */
75/* Function: cleanup */
76/* */
subrata_modak2e2b18c2009-05-29 12:26:40 +000077/* Description: Performs all one time clean up for this test on successful */
Garrett Cooper53740502010-12-16 00:04:01 -080078/* 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_modak2e2b18c2009-05-29 12:26:40 +000086/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
Garrett Cooper53740502010-12-16 00:04:01 -080087/* On success - Exits calling tst_exit(). With '0' return code. */
88/* */
subrata_modak2e2b18c2009-05-29 12:26:40 +000089/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040090void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080091{
Garrett Cooper2c282152010-12-16 00:55:50 -080092
Garrett Cooper53740502010-12-16 00:04:01 -080093 tst_rmdir();
subrata_modak2e2b18c2009-05-29 12:26:40 +000094}
95
96/* Local Functions */
97/******************************************************************************/
Garrett Cooper53740502010-12-16 00:04:01 -080098/* */
99/* Function: setup */
100/* */
subrata_modak2e2b18c2009-05-29 12:26:40 +0000101/* Description: Performs all one time setup for this test. This function is */
Garrett Cooper53740502010-12-16 00:04:01 -0800102/* 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_modak2e2b18c2009-05-29 12:26:40 +0000113/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400114void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115{
Cyril Hrubis91bfa5b2014-02-12 17:28:17 +0100116 tst_require_root(NULL);
Garrett Cooper53740502010-12-16 00:04:01 -0800117 /* Capture signals if any */
118 /* Create temporary directories */
119 TEST_PAUSE;
120 tst_tmpdir();
subrata_modak2e2b18c2009-05-29 12:26:40 +0000121}
122
subrata_modak2e2b18c2009-05-29 12:26:40 +0000123/*
124 * Macros
125 */
126#define SYSCALL_NAME "mq_ulink"
127
subrata_modak2e2b18c2009-05-29 12:26:40 +0000128enum test_type {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800129 NORMAL,
subrata_modak2e2b18c2009-05-29 12:26:40 +0000130};
131
subrata_modak2e2b18c2009-05-29 12:26:40 +0000132/*
133 * Data Structure
134 */
135struct test_case {
136 char *user;
Garrett Cooper53740502010-12-16 00:04:01 -0800137 char *qname;
138 int ttype;
139 int ret;
140 int err;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000141};
142
subrata_modak2e2b18c2009-05-29 12:26:40 +0000143/* Test cases
144*
145* test status of errors on man page
146*
Garrett Cooper53740502010-12-16 00:04:01 -0800147* EACCES v (permission is denied)
subrata_modak2e2b18c2009-05-29 12:26:40 +0000148* ENAMETOOLONG v (too long name length)
Garrett Cooper53740502010-12-16 00:04:01 -0800149* ENOENT v (named message queue does not exist)
subrata_modak2e2b18c2009-05-29 12:26:40 +0000150*/
151
152static struct test_case tcase[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 { // 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_modak2e2b18c2009-05-29 12:26:40 +0000192};
193
194/*
195 * do_test()
196 *
197 * Input : TestCase Data
198 * Return : RESULT_OK(0), RESULT_NG(1)
199 *
200 */
201
202static int do_test(struct test_case *tc)
203{
Garrett Cooper53740502010-12-16 00:04:01 -0800204 int sys_ret;
205 int sys_errno;
206 int result = RESULT_OK;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000207 int rc, fd1 = -1, fd2 = -1;
Garrett Cooper53740502010-12-16 00:04:01 -0800208 uid_t old_uid = -1;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000209
Garrett Cooper53740502010-12-16 00:04:01 -0800210 /*
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_modak2e2b18c2009-05-29 12:26:40 +0000215
Garrett Cooper53740502010-12-16 00:04:01 -0800216 /*
217 * Open message queue
218 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800219 rc = mq_open(QUEUE_NAME, O_CREAT | O_EXCL | O_RDWR, S_IRWXU, NULL);
Garrett Cooper53740502010-12-16 00:04:01 -0800220 if (rc == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221 tst_resm(TFAIL | TTERRNO, "mq_open failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800222 result = 1;
223 goto EXIT;
224 }
225 fd1 = rc;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000226
Garrett Cooper53740502010-12-16 00:04:01 -0800227 /*
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_modak2e2b18c2009-05-29 12:26:40 +0000237
Garrett Cooper53740502010-12-16 00:04:01 -0800238 /*
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_modak2e2b18c2009-05-29 12:26:40 +0000246
Garrett Cooper53740502010-12-16 00:04:01 -0800247 /*
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_modak2e2b18c2009-05-29 12:26:40 +0000252
253EXIT:
Garrett Cooper53740502010-12-16 00:04:01 -0800254 if (tc->user != NULL && old_uid != -1)
255 cleanup_euid(old_uid);
subrata_modak2e2b18c2009-05-29 12:26:40 +0000256
Garrett Cooper53740502010-12-16 00:04:01 -0800257 if (fd1 >= 0)
258 close(fd1);
259 if (fd2 >= 0)
260 close(fd2);
261 mq_unlink(QUEUE_NAME);
Garrett Cooperc89ea2b2010-12-19 10:07:55 -0800262 return 0;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000263}
264
Wanlong Gao354ebb42012-12-07 10:10:04 +0800265int main(int ac, char **av)
266{
Garrett Cooper53740502010-12-16 00:04:01 -0800267 int i;
268 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200269 const char *msg;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000270
Garrett Cooper53740502010-12-16 00:04:01 -0800271 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper2384bd42010-11-22 14:53:11 -0800272 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak2e2b18c2009-05-29 12:26:40 +0000273
Garrett Cooper53740502010-12-16 00:04:01 -0800274 setup();
subrata_modak2e2b18c2009-05-29 12:26:40 +0000275
Garrett Cooper53740502010-12-16 00:04:01 -0800276 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800277 tst_count = 0;
Garrett Cooper53740502010-12-16 00:04:01 -0800278 for (testno = 0; testno < TST_TOTAL; ++testno) {
subrata_modak2e2b18c2009-05-29 12:26:40 +0000279
Garrett Cooper53740502010-12-16 00:04:01 -0800280 int ret;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000281
Garrett Cooper53740502010-12-16 00:04:01 -0800282 ret = 0;
subrata_modak2e2b18c2009-05-29 12:26:40 +0000283
Garrett Cooper53740502010-12-16 00:04:01 -0800284 for (i = 0; ret == 0 &&
Wanlong Gao354ebb42012-12-07 10:10:04 +0800285 i < (int)(sizeof(tcase) / sizeof(tcase[0])); i++) {
Garrett Cooper53740502010-12-16 00:04:01 -0800286 ret = do_test(&tcase[i]);
287 }
subrata_modak2e2b18c2009-05-29 12:26:40 +0000288
Garrett Cooper53740502010-12-16 00:04:01 -0800289 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800290 }
Garrett Cooper53740502010-12-16 00:04:01 -0800291 cleanup();
subrata_modak2e2b18c2009-05-29 12:26:40 +0000292 tst_exit();
Garrett Cooperc89ea2b2010-12-19 10:07:55 -0800293}