|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
1 | 2 | /*
|
| 3 | + * Copyright (c) International Business Machines Corp., 2001 |
2 | 4 | *
|
3 |
| - * Copyright (c) International Business Machines Corp., 2001 |
4 |
| - * |
5 |
| - * This program is free software; you can redistribute it and/or modify |
6 |
| - * it under the terms of the GNU General Public License as published by |
7 |
| - * the Free Software Foundation; either version 2 of the License, or |
8 |
| - * (at your option) any later version. |
9 |
| - * |
10 |
| - * This program is distributed in the hope that it will be useful, |
11 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
13 |
| - * the GNU General Public License for more details. |
14 |
| - * |
15 |
| - * You should have received a copy of the GNU General Public License |
16 |
| - * along with this program; if not, write to the Free Software |
17 |
| - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 |
| - */ |
19 |
| - |
20 |
| -/* |
21 |
| - * NAME |
22 |
| - * msgrcv01.c |
23 |
| - * |
24 |
| - * DESCRIPTION |
25 |
| - * msgrcv01 - test that msgrcv() receives the expected message |
26 |
| - * |
27 |
| - * ALGORITHM |
28 |
| - * create a message queue |
29 |
| - * initialize a message buffer with a known message and type |
30 |
| - * loop if that option was specified |
31 |
| - * fork a child to receive the message |
32 |
| - * parent enqueues the message then exits |
33 |
| - * check the return code |
34 |
| - * if failure, issue a FAIL message. |
35 |
| - * otherwise, |
36 |
| - * if doing functionality testing |
37 |
| - * build a new message and compare it to the one received |
38 |
| - * if they are the same, |
39 |
| - * issue a PASS message |
40 |
| - * otherwise |
41 |
| - * issue a FAIL message |
42 |
| - * call cleanup |
43 |
| - * |
44 |
| - * USAGE: <for command-line> |
45 |
| - * msgrcv01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
46 |
| - * where, -c n : Run n copies concurrently. |
47 |
| - * -f : Turn off functionality Testing. |
48 |
| - * -i n : Execute test n times. |
49 |
| - * -I x : Execute test for x seconds. |
50 |
| - * -P x : Pause for x seconds between iterations. |
51 |
| - * -t : Turn on syscall timing. |
52 |
| - * |
53 |
| - * HISTORY |
54 |
| - * 03/2001 - Written by Wayne Boyer |
55 |
| - * |
56 |
| - * RESTRICTIONS |
57 |
| - * none |
| 5 | + * msgrcv01 - test that msgrcv() receives the expected message |
58 | 6 | */
|
59 | 7 |
|
60 | 8 | #include <string.h>
|
61 | 9 | #include <sys/wait.h>
|
62 |
| - |
63 |
| -#include "test.h" |
64 |
| - |
65 |
| -#include "ipcmsg.h" |
66 |
| - |
67 |
| -void cleanup(void); |
68 |
| -void setup(void); |
69 |
| -void do_child(void); |
70 |
| - |
71 |
| -char *TCID = "msgrcv01"; |
72 |
| -int TST_TOTAL = 1; |
73 |
| - |
74 |
| -int msg_q_1; |
75 |
| -MSGBUF snd_buf, rcv_buf, cmp_buf; |
76 |
| - |
77 |
| -pid_t c_pid; |
78 |
| - |
79 |
| -int main(int ac, char **av) |
| 10 | +#include "tst_test.h" |
| 11 | +#include "tst_safe_sysv_ipc.h" |
| 12 | +#include "libnewipc.h" |
| 13 | + |
| 14 | +static key_t msgkey; |
| 15 | +static int queue_id = -1; |
| 16 | +static struct buf { |
| 17 | + long type; |
| 18 | + char mtext[MSGSIZE]; |
| 19 | +} rcv_buf, snd_buf = {MSGTYPE, "hello"}; |
| 20 | + |
| 21 | +static void verify_msgrcv(void) |
80 | 22 | {
|
81 |
| - int lc; |
82 |
| - void check_functionality(void); |
83 |
| - int status, e_code; |
84 |
| - |
85 |
| - tst_parse_opts(ac, av, NULL, NULL); |
86 |
| - |
87 |
| -#ifdef UCLINUX |
88 |
| - maybe_run_child(&do_child, "d", &msg_q_1); |
89 |
| -#endif |
90 |
| - |
91 |
| - setup(); /* global setup */ |
92 |
| - |
93 |
| - /* The following loop checks looping state if -i option given */ |
94 |
| - |
95 |
| - for (lc = 0; TEST_LOOPING(lc); lc++) { |
96 |
| - /* reset tst_count in case we are looping */ |
97 |
| - tst_count = 0; |
98 |
| - |
99 |
| - /* |
100 |
| - * fork a child to read from the queue while the parent |
101 |
| - * enqueues the message to be read. |
102 |
| - */ |
103 |
| - if ((c_pid = FORK_OR_VFORK()) == -1) { |
104 |
| - tst_brkm(TBROK, cleanup, "could not fork"); |
105 |
| - } |
106 |
| - |
107 |
| - if (c_pid == 0) { /* child */ |
108 |
| -#ifdef UCLINUX |
109 |
| - if (self_exec(av[0], "d", msg_q_1) < 0) { |
110 |
| - tst_brkm(TBROK, cleanup, "could not self_exec"); |
111 |
| - } |
112 |
| -#else |
113 |
| - do_child(); |
114 |
| -#endif |
115 |
| - } else { /* parent */ |
116 |
| - /* put the message on the queue */ |
117 |
| - if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) { |
118 |
| - tst_brkm(TBROK, cleanup, |
119 |
| - "Couldn't enqueue" " message"); |
120 |
| - } |
121 |
| - /* wait for the child to finish */ |
122 |
| - wait(&status); |
123 |
| - /* make sure the child returned a good exit status */ |
124 |
| - e_code = status >> 8; |
125 |
| - if (e_code != 0) { |
126 |
| - tst_resm(TFAIL, "Failures reported above"); |
127 |
| - } |
| 23 | + SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0); |
128 | 24 |
|
129 |
| - } |
| 25 | + TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0)); |
| 26 | + if (TST_RET == -1) { |
| 27 | + tst_res(TFAIL | TTERRNO, "msgrcv failed"); |
| 28 | + return; |
130 | 29 | }
|
131 |
| - |
132 |
| - cleanup(); |
133 |
| - tst_exit(); |
134 |
| - |
135 |
| - /** NOT REACHED **/ |
136 |
| - |
| 30 | + if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0) |
| 31 | + tst_res(TPASS, "message received(%s) = message sent(%s)", |
| 32 | + rcv_buf.mtext, snd_buf.mtext); |
| 33 | + else |
| 34 | + tst_res(TFAIL, "message received(%s) != message sent(%s)", |
| 35 | + rcv_buf.mtext, snd_buf.mtext); |
137 | 36 | }
|
138 | 37 |
|
139 |
| -/* |
140 |
| - * do_child() |
141 |
| - */ |
142 |
| -void do_child(void) |
| 38 | +static void setup(void) |
143 | 39 | {
|
144 |
| - int retval = 0; |
145 |
| - |
146 |
| - TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0)); |
147 |
| - |
148 |
| - if (TEST_RETURN == -1) { |
149 |
| - retval = 1; |
150 |
| - tst_resm(TFAIL, "%s call failed - errno = %d : %s", |
151 |
| - TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
152 |
| - } else { |
153 |
| - /* |
154 |
| - * Build a new message and compare it |
155 |
| - * with the one received. |
156 |
| - */ |
157 |
| - init_buf(&cmp_buf, MSGTYPE, MSGSIZE); |
158 |
| - |
159 |
| - if (strcmp(rcv_buf.mtext, cmp_buf.mtext) == 0) { |
160 |
| - tst_resm(TPASS, |
161 |
| - "message received = " "message sent"); |
162 |
| - } else { |
163 |
| - retval = 1; |
164 |
| - tst_resm(TFAIL, |
165 |
| - "message received != " "message sent"); |
166 |
| - } |
167 |
| - } |
168 |
| - exit(retval); |
| 40 | + msgkey = GETIPCKEY(); |
| 41 | + queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW); |
169 | 42 | }
|
170 | 43 |
|
171 |
| -/* |
172 |
| - * setup() - performs all the ONE TIME setup for this test. |
173 |
| - */ |
174 |
| -void setup(void) |
| 44 | +static void cleanup(void) |
175 | 45 | {
|
176 |
| - |
177 |
| - tst_sig(FORK, DEF_HANDLER, cleanup); |
178 |
| - |
179 |
| - TEST_PAUSE; |
180 |
| - |
181 |
| - /* |
182 |
| - * Create a temporary directory and cd into it. |
183 |
| - * This helps to ensure that a unique msgkey is created. |
184 |
| - * See libs/libltpipc/libipc.c for more information. |
185 |
| - */ |
186 |
| - tst_tmpdir(); |
187 |
| - |
188 |
| - msgkey = getipckey(); |
189 |
| - |
190 |
| - /* create a message queue with read/write permission */ |
191 |
| - if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) { |
192 |
| - tst_brkm(TBROK, cleanup, "Can't create message queue"); |
193 |
| - } |
194 |
| - |
195 |
| - /* initialize the message buffer */ |
196 |
| - init_buf(&snd_buf, MSGTYPE, MSGSIZE); |
| 46 | + if (queue_id != -1) |
| 47 | + SAFE_MSGCTL(queue_id, IPC_RMID, NULL); |
197 | 48 | }
|
198 | 49 |
|
199 |
| -/* |
200 |
| - * cleanup() - performs all the ONE TIME cleanup for this test at completion |
201 |
| - * or premature exit. |
202 |
| - */ |
203 |
| -void cleanup(void) |
204 |
| -{ |
205 |
| - /* if it exists, remove the message queue that was created */ |
206 |
| - rm_queue(msg_q_1); |
207 |
| - |
208 |
| - tst_rmdir(); |
209 |
| - |
210 |
| -} |
| 50 | +static struct tst_test test = { |
| 51 | + .setup = setup, |
| 52 | + .cleanup = cleanup, |
| 53 | + .test_all = verify_msgrcv, |
| 54 | + .needs_tmpdir = 1, |
| 55 | +}; |
0 commit comments