• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of accept syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 
33 #include <unistd.h>
34 
35 #include <asm/unistd.h>
36 
37 #if defined __NR_accept
38 
39 # ifndef TEST_SYSCALL_NAME
40 #  define TEST_SYSCALL_NAME do_accept
41 
42 #  ifndef TEST_SYSCALL_STR
43 #   define TEST_SYSCALL_STR "accept"
44 #  endif
45 
do_accept(int sockfd,void * addr,void * addrlen)46 int do_accept(int sockfd, void *addr, void *addrlen)
47 {
48 	return syscall(__NR_accept, sockfd, addr, addrlen);
49 }
50 # endif /* !TEST_SYSCALL_NAME */
51 
52 #else /* !__NR_accept */
53 
54 # ifndef TEST_SYSCALL_NAME
55 #  define TEST_SYSCALL_NAME accept
56 # endif
57 
58 #endif /* __NR_accept */
59 
60 #define TEST_SYSCALL_PREPARE connect_un()
61 static void connect_un(void);
62 #include "sockname.c"
63 
64 static void
connect_un(void)65 connect_un(void)
66 {
67 	int cfd = socket(AF_UNIX, SOCK_STREAM, 0);
68 	if (cfd < 0)
69 		perror_msg_and_skip("socket");
70 
71 	struct sockaddr_un un = {
72 		.sun_family = AF_UNIX,
73 		.sun_path = TEST_SOCKET ".connect"
74 	};
75 
76 	(void) unlink(un.sun_path);
77 	if (bind(cfd, (const void *) &un, sizeof(un)))
78 		perror_msg_and_skip("bind");
79 	(void) unlink(un.sun_path);
80 
81 	un.sun_path[sizeof(TEST_SOCKET) - 1] = '\0';
82 	if (connect(cfd, (const void *) &un, sizeof(un)))
83 		perror_msg_and_skip("connect");
84 }
85 
86 int
main(void)87 main(void)
88 {
89 	int lfd = socket(AF_UNIX, SOCK_STREAM, 0);
90 	if (lfd < 0)
91 		perror_msg_and_skip("socket");
92 
93 	(void) unlink(TEST_SOCKET);
94 
95 	const struct sockaddr_un un = {
96 		.sun_family = AF_UNIX,
97 		.sun_path = TEST_SOCKET
98 	};
99 
100 	if (bind(lfd, (const void *) &un, sizeof(un)))
101 		perror_msg_and_skip("bind");
102 	if (listen(lfd, 16))
103 		perror_msg_and_skip("listen");
104 
105 	test_sockname_syscall(lfd);
106 
107 	(void) unlink(TEST_SOCKET);
108 
109 	puts("+++ exited with 0 +++");
110 	return 0;
111 }
112