plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +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 | * |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +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 | * |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | * DESCRIPTION |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 20 | * create a message queue, write a message to it and |
| 21 | * read it back. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 24 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | #include <string.h> |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/ipc.h> |
| 28 | #include <sys/msg.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 30 | #include "tst_test.h" |
Xiao Yang | 7d3e166 | 2017-01-23 18:31:15 +0800 | [diff] [blame^] | 31 | #include "tst_safe_sysv_ipc.h" |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 32 | #include "libnewipc.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 34 | static int queue_id = -1; |
| 35 | static key_t msgkey; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 37 | static struct buf { |
| 38 | long type; |
| 39 | char text[MSGSIZE]; |
| 40 | } rcv_buf, snd_buf = {MSGTYPE, "hello, world"}; |
| 41 | |
| 42 | static void verify_msgget(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | { |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 44 | TEST(msgget(msgkey, IPC_CREAT | MSG_RW)); |
| 45 | if (TEST_RETURN == -1) { |
| 46 | tst_res(TFAIL | TTERRNO, "msgget() failed"); |
| 47 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 50 | queue_id = TEST_RETURN; |
| 51 | |
Xiao Yang | 7d3e166 | 2017-01-23 18:31:15 +0800 | [diff] [blame^] | 52 | SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0); |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 53 | |
Xiao Yang | 7d3e166 | 2017-01-23 18:31:15 +0800 | [diff] [blame^] | 54 | SAFE_MSGRCV(queue_id, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT); |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 55 | |
| 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 62 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | { |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 64 | msgkey = GETIPCKEY(); |
| 65 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 67 | static 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Xiao Yang | 97f1183 | 2016-12-13 15:39:03 +0800 | [diff] [blame] | 75 | static struct tst_test test = { |
| 76 | .tid = "msgget01", |
| 77 | .setup = setup, |
| 78 | .cleanup = cleanup, |
| 79 | .test_all = verify_msgget, |
| 80 | .needs_tmpdir = 1 |
| 81 | }; |