• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * 1) msgsnd(2) fails and sets errno to EAGAIN if the message can't be
21  *    sent due to the msg_qbytes limit for the queue and IPC_NOWAIT is
22  *    specified.
23  * 2) msgsnd(2) fails and sets errno to EINTR if msgsnd(2) sleeps on a
24  *    full message queue condition and the process catches a signal.
25  */
26 
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/ipc.h>
31 #include <sys/msg.h>
32 
33 #include "tst_test.h"
34 #include "tst_safe_sysv_ipc.h"
35 #include "libnewipc.h"
36 
37 static key_t msgkey;
38 static int queue_id = -1;
39 static struct buf {
40 	long type;
41 	char text[MSGSIZE];
42 } snd_buf = {1, "hello"};
43 
44 static struct tcase {
45 	int flag;
46 	int exp_err;
47 	/*1: nobody expected  0: root expected */
48 	int exp_user;
49 } tcases[] = {
50 	{IPC_NOWAIT, EAGAIN, 0},
51 	{0, EINTR, 1}
52 };
53 
verify_msgsnd(struct tcase * tc)54 static void verify_msgsnd(struct tcase *tc)
55 {
56 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, tc->flag));
57 	if (TEST_RETURN != -1) {
58 		tst_res(TFAIL, "msgsnd() succeeded unexpectedly");
59 		return;
60 	}
61 
62 	if (TEST_ERRNO == tc->exp_err) {
63 		tst_res(TPASS | TTERRNO, "msgsnd() failed as expected");
64 	} else {
65 		tst_res(TFAIL | TTERRNO, "msgsnd() failed unexpectedly,"
66 			" expected %s", tst_strerrno(tc->exp_err));
67 	}
68 }
69 
sighandler(int sig)70 static void sighandler(int sig)
71 {
72 	if (sig == SIGHUP)
73 		return;
74 	else
75 		_exit(TBROK);
76 }
77 
do_test(unsigned int n)78 static void do_test(unsigned int n)
79 {
80 	pid_t pid;
81 	struct tcase *tc = &tcases[n];
82 
83 	if (tc->exp_user == 0) {
84 		verify_msgsnd(tc);
85 		return;
86 	}
87 
88 	pid = SAFE_FORK();
89 	if (!pid) {
90 		SAFE_SIGNAL(SIGHUP, sighandler);
91 		verify_msgsnd(tc);
92 		_exit(0);
93 	}
94 
95 	TST_PROCESS_STATE_WAIT(pid, 'S');
96 	SAFE_KILL(pid, SIGHUP);
97 	tst_reap_children();
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	msgkey = GETIPCKEY();
103 
104 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
105 
106 	while (msgsnd(queue_id, &snd_buf, MSGSIZE, IPC_NOWAIT) != -1)
107 		snd_buf.type += 1;
108 }
109 
cleanup(void)110 static void cleanup(void)
111 {
112 	if (queue_id != -1)
113 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
114 }
115 
116 static struct tst_test test = {
117 	.tid = "msgsnd05",
118 	.needs_tmpdir = 1,
119 	.needs_root = 1,
120 	.forks_child = 1,
121 	.tcnt = ARRAY_SIZE(tcases),
122 	.setup = setup,
123 	.cleanup = cleanup,
124 	.test = do_test
125 };
126