1 /*
2 * Copyright (c) 2015 Author: Gabriellla Schmidt <gsc@bruker.de>
3 * Modify: Li Wang <liwang@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * you should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 /*
19 * Description:
20 *
21 * A regression test for:
22 * commit e7ca2552369c1dfe0216c626baf82c3d83ec36bb
23 * Author: Mateusz Guzik <mguzik@redhat.com>
24 * Date: Mon Jan 27 17:07:11 2014 -0800
25 *
26 * ipc: fix compat msgrcv with negative msgtyp
27 *
28 * Reproduce:
29 *
30 * 32-bit application using the msgrcv() system call
31 * gives the error message:
32 *
33 * msgrcv: No message of desired type
34 *
35 * If this progarm is compiled as 64-bit application it works.
36 */
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/ipc.h>
43 #include <sys/msg.h>
44 #include "test.h"
45 #include "lapi/abisize.h"
46
47 const char *TCID = "msgrcv08";
48 const int TST_TOTAL = 1;
49
50 #ifdef TST_ABI32
51
52 struct mbuf {
53 long mtype; /* message type, must be > 0 */
54 char mtext[16]; /* message data */
55 };
56
msr(int msqid)57 static void msr(int msqid)
58 {
59 struct mbuf msbs;
60 struct mbuf msbr;
61 ssize_t sret;
62 long mtype = 121;
63
64 memset(&msbs, 0, sizeof(msbs));
65 msbs.mtype = mtype;
66
67 if (msgsnd(msqid, &msbs, sizeof(msbs.mtext), IPC_NOWAIT))
68 tst_brkm(TBROK | TERRNO, NULL, "msgsnd error");
69
70 sret = msgrcv(msqid, &msbr, sizeof(msbr.mtext), -mtype, IPC_NOWAIT | MSG_NOERROR);
71
72 if (sret < 0) {
73 tst_resm(TFAIL, "Bug: No message of desired type.");
74 return;
75 }
76
77 if (msbr.mtype != mtype)
78 tst_brkm(TBROK, NULL,
79 "found mtype %ld, expected %ld\n", msbr.mtype, mtype);
80
81 if ((size_t)sret != sizeof(msbs.mtext))
82 tst_brkm(TBROK, NULL, "received %zi, expected %zu\n",
83 sret, sizeof(msbs.mtext));
84
85 tst_resm(TPASS, "No regression found.");
86 }
87
msgrcv_test(void)88 static void msgrcv_test(void)
89 {
90 int msqid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666);
91
92 if (msqid < 0)
93 tst_brkm(TBROK | TERRNO, NULL, "msgget error");
94
95 msr(msqid);
96
97 if (msgctl(msqid, IPC_RMID, 0))
98 tst_brkm(TBROK | TERRNO, NULL, "msgctl error");
99 }
100
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 int lc;
104
105 tst_parse_opts(argc, argv, NULL, NULL);
106
107 for (lc = 0; TEST_LOOPING(lc); lc++)
108 msgrcv_test();
109
110 tst_exit();
111 }
112
113 #else /* no 64-bit */
main(void)114 int main(void)
115 {
116 tst_brkm(TCONF, NULL, "not works when compiled as 64-bit application.");
117 }
118 #endif
119