• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of ipc syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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 <asm/unistd.h>
32 
33 #if defined __NR_ipc && defined HAVE_LINUX_IPC_H
34 
35 # include <errno.h>
36 # include <stdio.h>
37 # include <unistd.h>
38 # include <linux/ipc.h>
39 
40 # ifndef SEMCTL
41 #  define SEMCTL 3
42 # endif
43 # ifndef MSGRCV
44 #  define MSGRCV 12
45 # endif
46 
47 static int
ipc_call(const unsigned short version,const unsigned short call,long a1,long a2,long a3,long a4,long a5)48 ipc_call(const unsigned short version, const unsigned short call,
49 	 long a1, long a2, long a3, long a4, long a5)
50 {
51 	const unsigned long val =
52 		(unsigned long) 0xfacefeed00000000ULL |
53 		(unsigned int) version << 16 |
54 		call;
55 
56 	return syscall(__NR_ipc, val, a1, a2, a3, a4, a5);
57 }
58 
59 static int
ipc_call0(const unsigned short version,const unsigned short call)60 ipc_call0(const unsigned short version, const unsigned short call)
61 {
62 	int rc = ipc_call(version, call, 0, 0, 0, 0, 0);
63 	int saved_errno = errno;
64 	printf("ipc(");
65 	if (version)
66 		printf("%hu<<16|", version);
67 	errno = saved_errno;
68 	printf("%hu, 0, 0, 0, 0%s) = %d %s (%m)\n", call,
69 # ifdef __s390__
70 	       "",
71 # else
72 	       ", 0",
73 # endif
74 	       rc, errno2name());
75 	return rc;
76 }
77 
78 int
main(void)79 main(void)
80 {
81 	void *const efault = tail_alloc(1) + 1;
82 
83 	int rc = ipc_call(0, SEMCTL, 0, 0, 0, (long) efault, 0);
84 	if (rc != -1 || EFAULT != errno)
85 		perror_msg_and_skip("ipc");
86 	printf("semctl(0, 0, IPC_RMID, %p) = -1 EFAULT (%m)\n", efault);
87 
88 	unsigned short call;
89 	for (call = 0; call <= 40; call += 10) {
90 		ipc_call0(0, call);
91 		ipc_call0(42, call);
92 	}
93 
94 	rc = ipc_call(42, SEMCTL, 0, 0, 0, (long) efault, 0);
95 	int test_version = EFAULT == errno;
96 	if (test_version)
97 		printf("semctl(0, 0, IPC_RMID, %p) = %d %s (%m)\n",
98 		       efault, rc, errno2name());
99 	else
100 		printf("ipc(42<<16|SEMCTL, 0, 0, 0, %p) = %d %s (%m)\n",
101 		       efault, rc, errno2name());
102 
103 	if (test_version) {
104 		const int msqid = -2;
105 		const long msgsz = -3;
106 		const long msgtyp = 0;
107 
108 		rc = ipc_call(1, MSGRCV,
109 			      msqid, msgsz, IPC_NOWAIT, (long) efault, msgtyp);
110 		printf("msgrcv(%d, %p, %lu, %ld, IPC_NOWAIT) = %d %s (%m)\n",
111 		       msqid, efault, msgsz, msgtyp, rc, errno2name());
112 	}
113 
114 	puts("+++ exited with 0 +++");
115 	return 0;
116 }
117 
118 #else
119 
120 SKIP_MAIN_UNDEFINED("__NR_ipc && HAVE_LINUX_IPC_H")
121 
122 #endif
123