1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*
7 * DESCRIPTION
8 * 1) msgsnd(2) fails and sets errno to EAGAIN if the message can't be
9 * sent due to the msg_qbytes limit for the queue and IPC_NOWAIT is
10 * specified.
11 * 2) msgsnd(2) fails and sets errno to EINTR if msgsnd(2) sleeps on a
12 * full message queue condition and the process catches a signal.
13 */
14
15 #include <errno.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/ipc.h>
19 #include <sys/msg.h>
20
21 #include "tst_test.h"
22 #include "tst_safe_sysv_ipc.h"
23 #include "libnewipc.h"
24
25 static key_t msgkey;
26 static int queue_id = -1;
27 static struct buf {
28 long type;
29 char text[MSGSIZE];
30 } snd_buf = {1, "hello"};
31
32 static struct tcase {
33 int flag;
34 int exp_err;
35 /*1: nobody expected 0: root expected */
36 int exp_user;
37 } tcases[] = {
38 {IPC_NOWAIT, EAGAIN, 0},
39 {0, EINTR, 1}
40 };
41
verify_msgsnd(struct tcase * tc)42 static void verify_msgsnd(struct tcase *tc)
43 {
44 TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, tc->flag));
45 if (TST_RET != -1) {
46 tst_res(TFAIL, "msgsnd() succeeded unexpectedly");
47 return;
48 }
49
50 if (TST_ERR == tc->exp_err) {
51 tst_res(TPASS | TTERRNO, "msgsnd() failed as expected");
52 } else {
53 tst_res(TFAIL | TTERRNO, "msgsnd() failed unexpectedly,"
54 " expected %s", tst_strerrno(tc->exp_err));
55 }
56 }
57
sighandler(int sig)58 static void sighandler(int sig)
59 {
60 if (sig == SIGHUP)
61 return;
62 else
63 _exit(TBROK);
64 }
65
do_test(unsigned int n)66 static void do_test(unsigned int n)
67 {
68 pid_t pid;
69 struct tcase *tc = &tcases[n];
70
71 if (tc->exp_user == 0) {
72 verify_msgsnd(tc);
73 return;
74 }
75
76 pid = SAFE_FORK();
77 if (!pid) {
78 SAFE_SIGNAL(SIGHUP, sighandler);
79 verify_msgsnd(tc);
80 _exit(0);
81 }
82
83 TST_PROCESS_STATE_WAIT(pid, 'S');
84 SAFE_KILL(pid, SIGHUP);
85 tst_reap_children();
86 }
87
setup(void)88 static void setup(void)
89 {
90 msgkey = GETIPCKEY();
91
92 queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
93
94 while (msgsnd(queue_id, &snd_buf, MSGSIZE, IPC_NOWAIT) != -1)
95 snd_buf.type += 1;
96 }
97
cleanup(void)98 static void cleanup(void)
99 {
100 if (queue_id != -1)
101 SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
102 }
103
104 static struct tst_test test = {
105 .needs_tmpdir = 1,
106 .needs_root = 1,
107 .forks_child = 1,
108 .tcnt = ARRAY_SIZE(tcases),
109 .setup = setup,
110 .cleanup = cleanup,
111 .test = do_test
112 };
113