blob: aaaba61bd82eff77d329e8bcb8ead84fdf144c3d [file] [log] [blame]
Garrett Cooper60fa8012010-11-22 13:50:58 -08001/********************************************************************************/
2/* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd */
3/* Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>, */
4/* Yumiko Sugita <yumiko.sugita.yf@hitachi.com>, */
5/* Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp> */
6/* */
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 */
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 Gao4548c6c2012-10-19 18:03:36 +080019/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
Garrett Cooper60fa8012010-11-22 13:50:58 -080020/* USA */
21/********************************************************************************/
22/************************************************************************/
23/* */
24/* File: mq_timedsend01.c */
25/* */
26/* Description: This tests the mq_timedsend() syscall */
27/* */
28/* */
29/* */
30/* */
31/* */
32/* */
33/* Usage: <for command-line> */
34/* mq_timedsend01 [-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_timedsend01 */
45/* History: Porting from Crackerjack to LTP is done by */
46/* Manas Kumar Nayak maknayak@in.ibm.com> */
47/************************************************************************/
subrata_modakfc114ed2009-05-29 12:29:55 +000048#include <sys/syscall.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <sys/wait.h>
52#include <getopt.h>
53#include <stdlib.h>
54#include <errno.h>
55#include <stdio.h>
56#include <unistd.h>
57#include <string.h>
58#include <mqueue.h>
59#include <time.h>
60#include <signal.h>
61#include <limits.h>
62
subrata_modakfc114ed2009-05-29 12:29:55 +000063#include "../utils/include_j_h.h"
64#include "../utils/common_j_h.c"
65
subrata_modakfc114ed2009-05-29 12:29:55 +000066#include "test.h"
subrata_modakfc114ed2009-05-29 12:29:55 +000067#include "linux_syscall_numbers.h"
68
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020069char *TCID = "mq_timedsend01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080070int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020071int TST_TOTAL = 1;
subrata_modak266ec972009-10-18 17:56:53 +000072struct sigaction act;
73
74/*
75 * sighandler()
76 */
77void sighandler(int sig)
78{
Garrett Cooper60fa8012010-11-22 13:50:58 -080079 if (sig == SIGINT)
80 return;
81 return;
subrata_modak266ec972009-10-18 17:56:53 +000082}
subrata_modakfc114ed2009-05-29 12:29:55 +000083
84/* Extern Global Functions */
85/******************************************************************************/
Garrett Cooper60fa8012010-11-22 13:50:58 -080086/* */
87/* Function: cleanup */
88/* */
subrata_modakfc114ed2009-05-29 12:29:55 +000089/* Description: Performs all one time clean up for this test on successful */
Garrett Cooper60fa8012010-11-22 13:50:58 -080090/* completion, premature exit or failure. Closes all temporary */
91/* files, removes all temporary directories exits the test with */
92/* appropriate return code by calling tst_exit() function. */
93/* */
94/* Input: None. */
95/* */
96/* Output: None. */
97/* */
subrata_modakfc114ed2009-05-29 12:29:55 +000098/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
Garrett Cooper60fa8012010-11-22 13:50:58 -080099/* On success - Exits calling tst_exit(). With '0' return code. */
100/* */
subrata_modakfc114ed2009-05-29 12:29:55 +0000101/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400102void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103{
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
Garrett Cooper60fa8012010-11-22 13:50:58 -0800105 tst_rmdir();
subrata_modakfc114ed2009-05-29 12:29:55 +0000106}
107
108/* Local Functions */
109/******************************************************************************/
Garrett Cooper60fa8012010-11-22 13:50:58 -0800110/* */
111/* Function: setup */
112/* */
subrata_modakfc114ed2009-05-29 12:29:55 +0000113/* Description: Performs all one time setup for this test. This function is */
Garrett Cooper60fa8012010-11-22 13:50:58 -0800114/* typically used to capture signals, create temporary dirs */
115/* and temporary files that may be used in the course of this */
116/* test. */
117/* */
118/* Input: None. */
119/* */
120/* Output: None. */
121/* */
122/* Return: On failure - Exits by calling cleanup(). */
123/* On success - returns 0. */
124/* */
subrata_modakfc114ed2009-05-29 12:29:55 +0000125/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400126void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127{
Garrett Cooper2c282152010-12-16 00:55:50 -0800128
Garrett Cooper60fa8012010-11-22 13:50:58 -0800129 /* Capture signals if any */
subrata_modak266ec972009-10-18 17:56:53 +0000130 act.sa_handler = sighandler;
131 sigfillset(&act.sa_mask);
132
133 sigaction(SIGINT, &act, NULL);
Garrett Cooper60fa8012010-11-22 13:50:58 -0800134 /* Create temporary directories */
135 TEST_PAUSE;
136 tst_tmpdir();
subrata_modakfc114ed2009-05-29 12:29:55 +0000137}
138
subrata_modakfc114ed2009-05-29 12:29:55 +0000139/*
140 * Macros
141 */
142#define SYSCALL_NAME "mq_timedsend"
143
subrata_modakfc114ed2009-05-29 12:29:55 +0000144enum test_type {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 NORMAL,
146 FD_NONE,
147 FD_NOT_EXIST,
148 FD_FILE,
149 FULL_QUEUE,
150 SEND_SIGINT,
subrata_modakfc114ed2009-05-29 12:29:55 +0000151};
152
153/*
154 * Data Structure
155 */
156struct test_case {
157 int ttype;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800158 int non_block;
159 int len;
160 unsigned prio;
161 time_t sec;
162 long nsec;
163 int ret;
164 int err;
subrata_modakfc114ed2009-05-29 12:29:55 +0000165};
166
Garrett Cooper60fa8012010-11-22 13:50:58 -0800167#define MAX_MSG 10
subrata_modakfc114ed2009-05-29 12:29:55 +0000168#define MAX_MSGSIZE 8192
169
170/* Test cases
171*
172* test status of errors on man page
173*
Garrett Cooper60fa8012010-11-22 13:50:58 -0800174* EAGAIN v (would block)
175* EBADF v (not a valid descriptor)
176* EINTR v (interrupted by a signal)
177* EINVAL v (1. invalid 'msg_prio' or
178* 2. would block but timeout exists)
179* EMSGSIZE v ('msg_len' exceeds the message size of the queue)
180* ETIMEDOUT v (not block and timeout occured)
subrata_modakfc114ed2009-05-29 12:29:55 +0000181*/
182
183static struct test_case tcase[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 { // case00
185 .ttype = NORMAL,
186 .len = 0, // also success when size equals zero
187 .ret = 0,
188 .err = 0,
189 },
190 { // case01
191 .ttype = NORMAL,
192 .len = 1,
193 .ret = 0,
194 .err = 0,
195 },
196 { // case02
197 .ttype = NORMAL,
198 .len = MAX_MSGSIZE,
199 .ret = 0,
200 .err = 0,
201 },
202 { // case03
203 .ttype = NORMAL,
204 .len = 1,
205 .prio = 32767, // max priority
206 .ret = 0,
207 .err = 0,
208 },
209 { // case04
210 .ttype = NORMAL,
211 .len = MAX_MSGSIZE + 1,
212 .ret = -1,
213 .err = EMSGSIZE,
214 },
215 { // case05
216 .ttype = FD_NONE,
217 .len = 0,
218 .ret = -1,
219 .err = EBADF,
220 },
221 { // case06
222 .ttype = FD_NOT_EXIST,
223 .len = 0,
224 .ret = -1,
225 .err = EBADF,
226 },
227 { // case07
228 .ttype = FD_FILE,
229 .len = 0,
230 .ret = -1,
231 .err = EBADF,
232 },
233 { // case08
234 .ttype = FULL_QUEUE,
235 .non_block = 1,
236 .len = 16,
237 .ret = -1,
238 .err = EAGAIN,
239 },
240 { // case09
241 .ttype = NORMAL,
242 .len = 1,
243 .prio = 32768, // max priority + 1
244 .ret = -1,
245 .err = EINVAL,
246 },
247 { // case10
248 .ttype = FULL_QUEUE,
249 .len = 16,
250 .sec = -1,
251 .nsec = 0,
252 .ret = -1,
253 .err = EINVAL,
254 },
255 { // case11
256 .ttype = FULL_QUEUE,
257 .len = 16,
258 .sec = 0,
259 .nsec = -1,
260 .ret = -1,
261 .err = EINVAL,
262 },
263 { // case12
264 .ttype = FULL_QUEUE,
265 .len = 16,
266 .sec = 0,
267 .nsec = 1000000000,
268 .ret = -1,
269 .err = EINVAL,
270 },
271 { // case13
272 .ttype = FULL_QUEUE,
273 .len = 16,
274 .sec = 0,
275 .nsec = 999999999,
276 .ret = -1,
277 .err = ETIMEDOUT,
278 },
279 { // case14
280 .ttype = SEND_SIGINT,
281 .len = 16,
282 .ret = -1,
283 .sec = 3,
284 .nsec = 0,
285 .err = EINTR,
286 },
subrata_modakfc114ed2009-05-29 12:29:55 +0000287};
288
subrata_modakfc114ed2009-05-29 12:29:55 +0000289/*
290 * do_test()
291 *
292 * Input : TestCase Data
293 * Return : RESULT_OK(0), RESULT_NG(1)
294 *
295 */
296
297static int do_test(struct test_case *tc)
298{
Garrett Cooper60fa8012010-11-22 13:50:58 -0800299 int sys_ret;
300 int sys_errno;
301 int result = RESULT_OK;
subrata_modakfc114ed2009-05-29 12:29:55 +0000302 int oflag;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800303 int i, rc, cmp_ok = 1, fd = -1;
304 char smsg[MAX_MSGSIZE], rmsg[MAX_MSGSIZE];
Wanlong Gao354ebb42012-12-07 10:10:04 +0800305 struct timespec ts = { 0, 0 };
Garrett Cooper60fa8012010-11-22 13:50:58 -0800306 pid_t pid = 0;
307 unsigned prio;
subrata_modakfc114ed2009-05-29 12:29:55 +0000308
Garrett Cooper60fa8012010-11-22 13:50:58 -0800309 /*
310 * When test ended with SIGTERM etc, mq discriptor is left remains.
311 * So we delete it first.
312 */
313 TEST(mq_unlink(QUEUE_NAME));
subrata_modakfc114ed2009-05-29 12:29:55 +0000314
Garrett Cooper60fa8012010-11-22 13:50:58 -0800315 switch (tc->ttype) {
316 case FD_NOT_EXIST:
317 fd = INT_MAX - 1;
318 /* fallthrough */
319 case FD_NONE:
320 break;
subrata_modakfc114ed2009-05-29 12:29:55 +0000321 case FD_FILE:
Garrett Cooper60fa8012010-11-22 13:50:58 -0800322 TEST(fd = open("/", O_RDONLY));
323 if (fd < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800324 tst_resm(TFAIL, "can't open \"/\".- errno = %d : %s\n",
325 TEST_ERRNO, strerror(TEST_ERRNO));
Garrett Cooper60fa8012010-11-22 13:50:58 -0800326 result = 1;
327 goto EXIT;
328 }
329 break;
330 default:
331 /*
332 * Open message queue
333 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800334 oflag = O_CREAT | O_EXCL | O_RDWR;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800335 if (tc->non_block)
336 oflag |= O_NONBLOCK;
subrata_modakfc114ed2009-05-29 12:29:55 +0000337
Garrett Cooper60fa8012010-11-22 13:50:58 -0800338 TEST(fd = mq_open(QUEUE_NAME, oflag, S_IRWXU, NULL));
339 if (TEST_RETURN < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800340 tst_resm(TFAIL, "mq_open failed - errno = %d : %s\n",
341 TEST_ERRNO, strerror(TEST_ERRNO));
Garrett Cooper60fa8012010-11-22 13:50:58 -0800342 result = 1;
343 goto EXIT;
344 }
345 if (tc->ttype == FULL_QUEUE || tc->ttype == SEND_SIGINT) {
346 for (i = 0; i < MAX_MSG; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 TEST(rc =
348 mq_timedsend(fd, smsg, tc->len, 0, &ts));
Garrett Cooper60fa8012010-11-22 13:50:58 -0800349 if (rc < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800350 tst_resm(TFAIL,
351 "mq_timedsend failed - errno = %d : %s\n",
352 TEST_ERRNO,
353 strerror(TEST_ERRNO));
Garrett Cooper60fa8012010-11-22 13:50:58 -0800354 result = 1;
355 goto EXIT;
356 }
357 }
subrata_modakfc114ed2009-05-29 12:29:55 +0000358 if (tc->ttype == SEND_SIGINT) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800359 pid = create_sig_proc(200000, SIGINT, UINT_MAX);
360 if (pid < 0) {
361 result = 1;
362 goto EXIT;
363 }
364 }
365 }
366 break;
367 }
subrata_modakfc114ed2009-05-29 12:29:55 +0000368
Garrett Cooper60fa8012010-11-22 13:50:58 -0800369 /*
370 * Prepare send message
371 */
Carmelo AMOROSO3f3baa22011-07-05 16:24:12 +0200372 for (i = 0; i < tc->len && i < sizeof(smsg); i++)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800373 smsg[i] = i;
subrata_modakfc114ed2009-05-29 12:29:55 +0000374
Garrett Cooper60fa8012010-11-22 13:50:58 -0800375 /*
376 * Set the timeout value
377 */
378 ts.tv_sec = tc->sec;
379 ts.tv_nsec = tc->nsec;
subrata_modak266ec972009-10-18 17:56:53 +0000380 if (tc->sec >= 0 || tc->nsec != 0)
381 ts.tv_sec += time(NULL);
subrata_modakfc114ed2009-05-29 12:29:55 +0000382
Garrett Cooper60fa8012010-11-22 13:50:58 -0800383 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800384 * Execut test system call
Garrett Cooper60fa8012010-11-22 13:50:58 -0800385 */
386 errno = 0;
387 TEST(sys_ret = mq_timedsend(fd, smsg, tc->len, tc->prio, &ts));
388 sys_errno = errno;
389 if (sys_ret < 0)
390 goto TEST_END;
subrata_modakfc114ed2009-05-29 12:29:55 +0000391
Garrett Cooper60fa8012010-11-22 13:50:58 -0800392 /*
393 * Receive echoed message and compare
394 */
subrata_modak266ec972009-10-18 17:56:53 +0000395 ts.tv_sec = 0;
396 ts.tv_nsec = 0;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800397 TEST(rc = mq_timedreceive(fd, rmsg, MAX_MSGSIZE, &prio, &ts));
398 if (rc < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800399 tst_resm(TFAIL, "mq_timedreceive failed - errno = %d : %s\n",
400 TEST_ERRNO, strerror(TEST_ERRNO));
Garrett Cooper60fa8012010-11-22 13:50:58 -0800401 result = 1;
402 goto EXIT;
403 }
404 if (rc != tc->len || tc->prio != prio)
405 cmp_ok = 0;
406 else {
407 for (i = 0; i < tc->len; i++)
408 if (rmsg[i] != smsg[i]) {
409 cmp_ok = 0;
410 break;
411 }
412 }
subrata_modakfc114ed2009-05-29 12:29:55 +0000413TEST_END:
Garrett Cooper60fa8012010-11-22 13:50:58 -0800414 /*
415 * Check results
416 */
417 result |= (sys_errno != tc->err) || !cmp_ok;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800418 PRINT_RESULT_CMP(sys_ret >= 0, tc->ret, tc->err, sys_ret, sys_errno,
419 cmp_ok);
subrata_modakfc114ed2009-05-29 12:29:55 +0000420
421EXIT:
Garrett Cooper60fa8012010-11-22 13:50:58 -0800422 if (fd >= 0) {
423 TEST(close(fd));
424 TEST(mq_unlink(QUEUE_NAME));
425 }
426 if (pid > 0) {
427 int st;
428 kill(pid, SIGTERM);
429 wait(&st);
430 }
431 return result;
subrata_modakfc114ed2009-05-29 12:29:55 +0000432}
433
subrata_modakfc114ed2009-05-29 12:29:55 +0000434/*
subrata_modakfc114ed2009-05-29 12:29:55 +0000435 * main()
436 */
437
Wanlong Gao354ebb42012-12-07 10:10:04 +0800438int main(int ac, char **av)
439{
subrata_modakfc114ed2009-05-29 12:29:55 +0000440 int result = RESULT_OK;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800441 int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200442 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200443 const char *msg;
subrata_modakfc114ed2009-05-29 12:29:55 +0000444
Garrett Cooper60fa8012010-11-22 13:50:58 -0800445 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
446 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakfc114ed2009-05-29 12:29:55 +0000447
Garrett Cooper60fa8012010-11-22 13:50:58 -0800448 setup();
subrata_modakfc114ed2009-05-29 12:29:55 +0000449
Garrett Cooper60fa8012010-11-22 13:50:58 -0800450 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800451 tst_count = 0;
Garrett Cooper60fa8012010-11-22 13:50:58 -0800452 for (testno = 0; testno < TST_TOTAL; ++testno) {
subrata_modakfc114ed2009-05-29 12:29:55 +0000453
Garrett Cooper60fa8012010-11-22 13:50:58 -0800454 /*
455 * Execute test
456 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800457 for (i = 0; i < (int)(sizeof(tcase) / sizeof(tcase[0]));
458 i++) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800459 int ret;
460 tst_resm(TINFO, "(case%02d) START", i);
461 ret = do_test(&tcase[i]);
462 tst_resm(TINFO, "(case%02d) END => %s", i,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800463 (ret == 0) ? "OK" : "NG");
Garrett Cooper60fa8012010-11-22 13:50:58 -0800464 result |= ret;
465 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800466 /*
Garrett Cooper60fa8012010-11-22 13:50:58 -0800467 * Check results
Wanlong Gao354ebb42012-12-07 10:10:04 +0800468 */
469 switch (result) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800470 case RESULT_OK:
471 tst_resm(TPASS, "mq_timedsend call succeeded");
472 break;
subrata_modakfc114ed2009-05-29 12:29:55 +0000473
Garrett Cooper60fa8012010-11-22 13:50:58 -0800474 default:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475 tst_brkm(TFAIL | TTERRNO, cleanup,
476 "mq_timedsend failed");
Garrett Cooper60fa8012010-11-22 13:50:58 -0800477 }
subrata_modakfc114ed2009-05-29 12:29:55 +0000478
Garrett Cooper60fa8012010-11-22 13:50:58 -0800479 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800480 }
Garrett Cooper60fa8012010-11-22 13:50:58 -0800481 cleanup();
subrata_modakfc114ed2009-05-29 12:29:55 +0000482 tst_exit();
Garrett Coopercd324c52010-12-19 10:05:13 -0800483}