1 /*
2  * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2015-2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "tests.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <asm/unistd.h>
35 #include <sys/ipc.h>
36 #include <sys/msg.h>
37 #include <sys/stat.h>
38 
39 #define text_string "STRACE_STRING"
40 #define msgsz sizeof(text_string)
41 
42 static int msqid = -1;
43 
44 #if XLAT_RAW
45 # define str_ipc_creat "0x200"
46 # define str_ipc_private "0"
47 # define str_ipc_rmid "0"
48 # define str_ipc_64 "0x100"
49 #elif XLAT_VERBOSE
50 # define str_ipc_creat "0x200 /\\* IPC_CREAT \\*/"
51 # define str_ipc_private "0 /\\* IPC_PRIVATE \\*/"
52 # define str_ipc_rmid "0 /\\* IPC_RMID \\*/"
53 # define str_ipc_64 "0x100 /\\* IPC_64 \\*/"
54 #else
55 # define str_ipc_creat "IPC_CREAT"
56 # define str_ipc_private "IPC_PRIVATE"
57 # define str_ipc_rmid "IPC_RMID"
58 # define str_ipc_64 "IPC_64"
59 #endif
60 
61 static int
cleanup(void)62 cleanup(void)
63 {
64 	if (msqid != -1) {
65 		int rc = msgctl(msqid, IPC_RMID, 0);
66 		printf("msgctl\\(%d, (%s\\|)?%s, NULL\\) = 0\n",
67 		       msqid, str_ipc_64, str_ipc_rmid);
68 		msqid = -1;
69 		if (rc == -1)
70 			return 77;
71 		puts("\\+\\+\\+ exited with 0 \\+\\+\\+");
72 	}
73 	return 0;
74 }
75 
76 int
sys_msgrcv(int msqid,void * msgp,size_t sz,kernel_long_t msgtyp,int msgflg)77 sys_msgrcv(int msqid, void *msgp, size_t sz, kernel_long_t msgtyp,
78 	   int msgflg)
79 {
80 #if defined __x86_64__ && defined __ILP32__
81 	return syscall(__NR_msgrcv, msqid, msgp, sz, msgtyp, msgflg);
82 #else
83 	return msgrcv(msqid, msgp, sz, msgtyp, msgflg);
84 #endif
85 }
86 
87 int
main(void)88 main(void)
89 {
90 	/* mtype has to be positive */
91 	const kernel_long_t mtype = (kernel_long_t) 0x7facefed5adc0dedULL;
92 	struct {
93 		kernel_long_t mtype;
94 		char mtext[msgsz];
95 	} msg = {
96 		.mtype = mtype,
97 		.mtext = text_string
98 	};
99 	msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
100 	if (msqid == -1)
101 		perror_msg_and_skip("msgget");
102 	printf("msgget\\(%s, %s\\|0700\\) = %d\n",
103 	       str_ipc_private, str_ipc_creat, msqid);
104 
105 	typedef void (*atexit_func)(void);
106 	atexit((atexit_func) cleanup);
107 
108 	printf("msgsnd\\(%d, \\{%lld, \"" text_string "\\\\0\"\\}, 14, 0\\)"
109 	       " = 0\n",
110 	       msqid, (long long) mtype);
111 	if (msgsnd(msqid, &msg, msgsz, 0) == -1)
112 		perror_msg_and_skip("msgsnd");
113 
114 	if (sys_msgrcv(msqid, &msg, msgsz, -mtype, 0) != msgsz)
115 		perror_msg_and_skip("msgrcv");
116 	printf("msgrcv\\(%d, \\{%lld, \"" text_string "\\\\0\"\\}, 14, %lld"
117 	       ", 0\\) = 14\n",
118 	       msqid, (long long) mtype, -(long long) mtype);
119 
120 	return cleanup();
121 }
122