blob: 1e9bbeb9b9354197d1092b30488919f3ad0675b3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Xiao Yang97f11832016-12-13 15:39:03 +08002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Xiao Yang97f11832016-12-13 15:39:03 +08004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Xiao Yang97f11832016-12-13 15:39:03 +08009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Xiao Yang97f11832016-12-13 15:39:03 +080014 * You should have received a copy of the GNU General Public License
15 * along with this program.
plars865695b2001-08-27 22:15:12 +000016 */
17
18/*
plars865695b2001-08-27 22:15:12 +000019 * DESCRIPTION
Xiao Yang97f11832016-12-13 15:39:03 +080020 * create a message queue, write a message to it and
21 * read it back.
plars865695b2001-08-27 22:15:12 +000022 */
23
Xiao Yang97f11832016-12-13 15:39:03 +080024#include <errno.h>
plars865695b2001-08-27 22:15:12 +000025#include <string.h>
Xiao Yang97f11832016-12-13 15:39:03 +080026#include <sys/types.h>
27#include <sys/ipc.h>
28#include <sys/msg.h>
plars865695b2001-08-27 22:15:12 +000029
Xiao Yang97f11832016-12-13 15:39:03 +080030#include "tst_test.h"
Xiao Yang7d3e1662017-01-23 18:31:15 +080031#include "tst_safe_sysv_ipc.h"
Xiao Yang97f11832016-12-13 15:39:03 +080032#include "libnewipc.h"
plars865695b2001-08-27 22:15:12 +000033
Xiao Yang97f11832016-12-13 15:39:03 +080034static int queue_id = -1;
35static key_t msgkey;
plars865695b2001-08-27 22:15:12 +000036
Xiao Yang97f11832016-12-13 15:39:03 +080037static struct buf {
38 long type;
39 char text[MSGSIZE];
40} rcv_buf, snd_buf = {MSGTYPE, "hello, world"};
41
42static void verify_msgget(void)
plars865695b2001-08-27 22:15:12 +000043{
Xiao Yang97f11832016-12-13 15:39:03 +080044 TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
45 if (TEST_RETURN == -1) {
46 tst_res(TFAIL | TTERRNO, "msgget() failed");
47 return;
plars865695b2001-08-27 22:15:12 +000048 }
49
Xiao Yang97f11832016-12-13 15:39:03 +080050 queue_id = TEST_RETURN;
51
Xiao Yang7d3e1662017-01-23 18:31:15 +080052 SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
Xiao Yang97f11832016-12-13 15:39:03 +080053
Xiao Yang7d3e1662017-01-23 18:31:15 +080054 SAFE_MSGRCV(queue_id, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT);
Xiao Yang97f11832016-12-13 15:39:03 +080055
56 if (strcmp(snd_buf.text, rcv_buf.text) == 0)
57 tst_res(TPASS, "message received = message sent");
58 else
59 tst_res(TFAIL, "message received != message sent");
plars865695b2001-08-27 22:15:12 +000060}
61
Xiao Yang97f11832016-12-13 15:39:03 +080062static void setup(void)
plars865695b2001-08-27 22:15:12 +000063{
Xiao Yang97f11832016-12-13 15:39:03 +080064 msgkey = GETIPCKEY();
65}
plars865695b2001-08-27 22:15:12 +000066
Xiao Yang97f11832016-12-13 15:39:03 +080067static void cleanup(void)
68{
69 if (queue_id != -1 && msgctl(queue_id, IPC_RMID, NULL)) {
70 tst_res(TWARN | TERRNO, "failed to delete message queue %i",
71 queue_id);
plars865695b2001-08-27 22:15:12 +000072 }
73}
74
Xiao Yang97f11832016-12-13 15:39:03 +080075static struct tst_test test = {
76 .tid = "msgget01",
77 .setup = setup,
78 .cleanup = cleanup,
79 .test_all = verify_msgget,
80 .needs_tmpdir = 1
81};