1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.
16 */
17
18 /*
19 * DESCRIPTION
20 * Tests if EIDRM is returned when message queue was removed while
21 * msgsnd() was trying to send a message.
22 */
23
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/ipc.h>
28 #include <sys/msg.h>
29
30 #include "tst_test.h"
31 #include "tst_safe_sysv_ipc.h"
32 #include "libnewipc.h"
33
34 static key_t msgkey;
35 static int queue_id = -1;
36 static struct buf {
37 long type;
38 char text[MSGSIZE];
39 } snd_buf = {1, "hello"};
40
verify_msgsnd(void)41 static void verify_msgsnd(void)
42 {
43 TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
44 if (TST_RET != -1) {
45 tst_res(TFAIL, "msgsnd() succeeded unexpectedly");
46 return;
47 }
48
49 if (TST_ERR == EIDRM) {
50 tst_res(TPASS | TTERRNO, "msgsnd() failed as expected");
51 } else {
52 tst_res(TFAIL | TTERRNO,
53 "msgsnd() failed unexpectedly, expected EIDRM");
54 }
55 }
56
do_test(void)57 static void do_test(void)
58 {
59 pid_t pid;
60
61 queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
62
63 while (msgsnd(queue_id, &snd_buf, MSGSIZE, IPC_NOWAIT) != -1)
64 snd_buf.type += 1;
65
66 pid = SAFE_FORK();
67 if (!pid) {
68 verify_msgsnd();
69 _exit(0);
70 }
71
72 TST_PROCESS_STATE_WAIT(pid, 'S');
73
74 SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
75
76 tst_reap_children();
77 }
78
setup(void)79 static void setup(void)
80 {
81 msgkey = GETIPCKEY();
82 }
83
cleanup(void)84 static void cleanup(void)
85 {
86 if (queue_id != -1)
87 SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
88 }
89
90 static struct tst_test test = {
91 .needs_tmpdir = 1,
92 .needs_root = 1,
93 .forks_child = 1,
94 .setup = setup,
95 .cleanup = cleanup,
96 .test_all = do_test
97 };
98