1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  * test that msgsnd() enqueues a message correctly.
9  */
10 
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/ipc.h>
14 #include <sys/msg.h>
15 
16 #include "tst_test.h"
17 #include "tst_safe_sysv_ipc.h"
18 #include "libnewipc.h"
19 
20 static key_t msgkey;
21 static int queue_id = -1;
22 static struct buf {
23 	long type;
24 	char text[MSGSIZE];
25 } rcv_buf, snd_buf = {MSGTYPE, "hello"};
26 
verify_msgsnd(void)27 static void verify_msgsnd(void)
28 {
29 	struct msqid_ds qs_buf;
30 
31 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
32 	if (TST_RET == -1) {
33 		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
34 		return;
35 	}
36 
37 	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
38 
39 	if (qs_buf.msg_cbytes == MSGSIZE && qs_buf.msg_qnum == 1)
40 		tst_res(TPASS, "queue bytes and number of queues matched");
41 	else
42 		tst_res(TFAIL, "queue bytes or number of queues mismatched");
43 
44 	SAFE_MSGRCV(queue_id, &rcv_buf, MSGSIZE, 1, 0);
45 }
46 
setup(void)47 static void setup(void)
48 {
49 	msgkey = GETIPCKEY();
50 
51 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
52 }
53 
cleanup(void)54 static void cleanup(void)
55 {
56 	if (queue_id != -1)
57 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
58 }
59 
60 static struct tst_test test = {
61 	.setup = setup,
62 	.cleanup = cleanup,
63 	.test_all = verify_msgsnd,
64 	.needs_tmpdir = 1
65 };
66