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
46 const char *TCID = "msgrcv08";
47 const int TST_TOTAL = 1;
48
49 #if __WORDSIZE == 32
50
51 struct msgbuf {
52 long mtype; /* message type, must be > 0 */
53 char mtext[16]; /* message data */
54 };
55
msr(int msqid)56 static void msr(int msqid)
57 {
58 struct msgbuf msbs;
59 struct msgbuf msbr;
60 ssize_t sret;
61 long mtype = 121;
62
63 memset(&msbs, 0, sizeof(msbs));
64 msbs.mtype = mtype;
65
66 if (msgsnd(msqid, &msbs, sizeof(msbs.mtext), IPC_NOWAIT))
67 tst_brkm(TBROK | TERRNO, NULL, "msgsnd error");
68
69 sret = msgrcv(msqid, &msbr, sizeof(msbr.mtext), -mtype, IPC_NOWAIT | MSG_NOERROR);
70
71 if (sret < 0) {
72 tst_resm(TFAIL, "Bug: No message of desired type.");
73 return;
74 }
75
76 if (msbr.mtype != mtype)
77 tst_brkm(TBROK, NULL,
78 "found mtype %ld, expected %ld\n", msbr.mtype, mtype);
79
80 if ((size_t)sret != sizeof(msbs.mtext))
81 tst_brkm(TBROK, NULL, "received %zi, expected %zu\n",
82 sret, sizeof(msbs.mtext));
83
84 tst_resm(TPASS, "No regression found.");
85 }
86
msgrcv_test(void)87 static void msgrcv_test(void)
88 {
89 int msqid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666);
90
91 if (msqid < 0)
92 tst_brkm(TBROK | TERRNO, NULL, "msgget error");
93
94 msr(msqid);
95
96 if (msgctl(msqid, IPC_RMID, 0))
97 tst_brkm(TBROK | TERRNO, NULL, "msgctl error");
98 }
99
main(int argc,char * argv[])100 int main(int argc, char *argv[])
101 {
102 int lc;
103
104 tst_parse_opts(argc, argv, NULL, NULL);
105
106 for (lc = 0; TEST_LOOPING(lc); lc++)
107 msgrcv_test();
108
109 tst_exit();
110 }
111
112 #else /* no 64-bit */
main(void)113 int main(void)
114 {
115 tst_brkm(TCONF, NULL, "not works when compiled as 64-bit application.");
116 }
117 #endif
118