• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tst_clocks.h"
19 #include "libnewipc.h"
20 
21 static key_t msgkey;
22 static int queue_id = -1, pid;
23 static struct buf {
24 	long type;
25 	char text[MSGSIZE];
26 } rcv_buf, snd_buf = {MSGTYPE, "hello"};
27 
verify_msgsnd(void)28 static void verify_msgsnd(void)
29 {
30 	struct msqid_ds qs_buf;
31 	time_t before_snd, after_snd;
32 
33 	before_snd = tst_get_fs_timestamp();
34 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
35 	if (TST_RET == -1) {
36 		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
37 		return;
38 	}
39 	after_snd = tst_get_fs_timestamp();
40 
41 	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
42 
43 	if (qs_buf.msg_cbytes == MSGSIZE && qs_buf.msg_qnum == 1)
44 		tst_res(TPASS, "queue bytes and number of queues matched");
45 	else
46 		tst_res(TFAIL, "queue bytes or number of queues mismatched");
47 
48 	if (qs_buf.msg_lspid == pid)
49 		tst_res(TPASS, "PID of last msgsnd(2) matched");
50 	else
51 		tst_res(TFAIL, "PID of last msgsnd(2) mismatched");
52 
53 	if (qs_buf.msg_stime >= before_snd && qs_buf.msg_stime <= after_snd) {
54 		tst_res(TPASS, "msg_stime = %lu in [%lu, %lu]",
55 			(unsigned long)qs_buf.msg_stime,
56 			(unsigned long)before_snd, (unsigned long)after_snd);
57 	} else {
58 		tst_res(TFAIL, "msg_stime = %lu out of [%lu, %lu]",
59 			(unsigned long)qs_buf.msg_stime,
60 			(unsigned long)before_snd, (unsigned long)after_snd);
61 	}
62 
63 	SAFE_MSGRCV(queue_id, &rcv_buf, MSGSIZE, 1, 0);
64 }
65 
setup(void)66 static void setup(void)
67 {
68 	msgkey = GETIPCKEY();
69 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
70 	pid = getpid();
71 }
72 
cleanup(void)73 static void cleanup(void)
74 {
75 	if (queue_id != -1)
76 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
77 }
78 
79 static struct tst_test test = {
80 	.setup = setup,
81 	.cleanup = cleanup,
82 	.test_all = verify_msgsnd,
83 	.needs_tmpdir = 1
84 };
85