Home | History | Annotate | Download | only in tests-m32
      1 /*
      2  * This file is part of net-y-unix strace test.
      3  *
      4  * Copyright (c) 2013-2016 Dmitry V. Levin <ldv (at) 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 <assert.h>
     32 #include <stddef.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <unistd.h>
     37 #include <sys/param.h>
     38 #include <sys/socket.h>
     39 #include <sys/un.h>
     40 
     41 int
     42 main(int ac, const char **av)
     43 {
     44 	assert(ac == 2);
     45 
     46 	struct sockaddr_un addr = { .sun_family = AF_UNIX };
     47 	unsigned int sun_path_len = strlen(av[1]);
     48 	assert(sun_path_len > 0 && sun_path_len <= sizeof(addr.sun_path));
     49 	strncpy(addr.sun_path, av[1], sizeof(addr.sun_path));
     50 	struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr));
     51 
     52 	socklen_t * const len = tail_alloc(sizeof(socklen_t));
     53 	*len = offsetof(struct sockaddr_un, sun_path) + strlen(av[1]) + 1;
     54 	if (*len > sizeof(addr))
     55 		*len = sizeof(addr);
     56 
     57 	int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     58 	if (listen_fd < 0)
     59 		perror_msg_and_skip("socket");
     60 	unsigned long listen_inode = inode_of_sockfd(listen_fd);
     61 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
     62 	       listen_fd, listen_inode);
     63 
     64 	(void) unlink(av[1]);
     65 	if (bind(listen_fd, listen_sa, *len))
     66 		perror_msg_and_skip("bind");
     67 	printf("bind(%d<socket:[%lu]>, {sa_family=AF_UNIX, sun_path=\"%s\"}"
     68 	       ", %u) = 0\n", listen_fd, listen_inode, av[1], (unsigned) *len);
     69 
     70 	if (listen(listen_fd, 1))
     71 		perror_msg_and_skip("listen");
     72 	printf("listen(%d<socket:[%lu]>, 1) = 0\n", listen_fd, listen_inode);
     73 
     74 	unsigned int * const optval = tail_alloc(sizeof(unsigned int));
     75 	*len = sizeof(*optval);
     76 	if (getsockopt(listen_fd, SOL_SOCKET, SO_PASSCRED, optval, len))
     77 		perror_msg_and_fail("getsockopt");
     78 	printf("getsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
     79 	       ", [%u], [%u]) = 0\n",
     80 	       listen_fd, listen_inode, *optval, (unsigned) *len);
     81 
     82 	memset(listen_sa, 0, sizeof(addr));
     83 	*len = sizeof(addr);
     84 	if (getsockname(listen_fd, listen_sa, len))
     85 		perror_msg_and_fail("getsockname");
     86 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
     87 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
     88 	       av[1], (int) sizeof(addr), (int) *len);
     89 
     90 	int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     91 	if (connect_fd < 0)
     92 		perror_msg_and_fail("socket");
     93 	unsigned long connect_inode = inode_of_sockfd(connect_fd);
     94 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
     95 	       connect_fd, connect_inode);
     96 
     97 	if (connect(connect_fd, listen_sa, *len))
     98 		perror_msg_and_fail("connect");
     99 	printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    100 	       ", sun_path=\"%s\"}, %u) = 0\n",
    101 	       connect_fd, connect_inode, av[1], (unsigned) *len);
    102 
    103 	struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
    104 	memset(accept_sa, 0, sizeof(addr));
    105 	*len = sizeof(addr);
    106 	int accept_fd = accept(listen_fd, accept_sa, len);
    107 	if (accept_fd < 0)
    108 		perror_msg_and_fail("accept");
    109 	unsigned long accept_inode = inode_of_sockfd(accept_fd);
    110 	printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX}"
    111 	       ", [%d->%d]) = %d<socket:[%lu]>\n",
    112 	       listen_fd, listen_inode,
    113 	       (int) sizeof(addr), (int) *len,
    114 	       accept_fd, accept_inode);
    115 
    116 	memset(listen_sa, 0, sizeof(addr));
    117 	*len = sizeof(addr);
    118 	if (getpeername(connect_fd, listen_sa, len))
    119 		perror_msg_and_fail("getpeername");
    120 	printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    121 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", connect_fd, connect_inode,
    122 	       av[1], (int) sizeof(addr), (int) *len);
    123 
    124 	char text[] = "text";
    125 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
    126 	       == sizeof(text) - 1);
    127 	printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
    128 	       ", NULL, 0) = %u\n",
    129 	       connect_fd, connect_inode, text,
    130 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
    131 
    132 	assert(close(connect_fd) == 0);
    133 	printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
    134 
    135 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
    136 	       == sizeof(text) - 1);
    137 	printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
    138 	       ", NULL, NULL) = %u\n",
    139 	       accept_fd, accept_inode, text,
    140 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
    141 
    142 	assert(close(accept_fd) == 0);
    143 	printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
    144 
    145 	connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    146 	if (connect_fd < 0)
    147 		perror_msg_and_fail("socket");
    148 	connect_inode = inode_of_sockfd(connect_fd);
    149 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
    150 	       connect_fd, connect_inode);
    151 
    152 	*optval = 1;
    153 	*len = sizeof(*optval);
    154 	if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
    155 		perror_msg_and_fail("setsockopt");
    156 	printf("setsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
    157 	       ", [%u], %u) = 0\n",
    158 	       connect_fd, connect_inode, *optval, (unsigned) *len);
    159 
    160 	memset(listen_sa, 0, sizeof(addr));
    161 	*len = sizeof(addr);
    162 	if (getsockname(listen_fd, listen_sa, len))
    163 		perror_msg_and_fail("getsockname");
    164 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    165 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
    166 	       listen_fd, listen_inode, av[1],
    167 	       (int) sizeof(addr), (int) *len);
    168 
    169 	if (connect(connect_fd, listen_sa, *len))
    170 		perror_msg_and_fail("connect");
    171 	printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    172 	       ", sun_path=\"%s\"}, %u) = 0\n",
    173 	       connect_fd, connect_inode, av[1], (unsigned) *len);
    174 
    175 	memset(accept_sa, 0, sizeof(addr));
    176 	*len = sizeof(addr);
    177 	accept_fd = accept(listen_fd, accept_sa, len);
    178 	if (accept_fd < 0)
    179 		perror_msg_and_fail("accept");
    180 	accept_inode = inode_of_sockfd(accept_fd);
    181 	const char * const sun_path1 =
    182 		((struct sockaddr_un *) accept_sa) -> sun_path + 1;
    183 	printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    184 	       ", sun_path=@\"%s\"}, [%d->%d]) = %d<socket:[%lu]>\n",
    185 	       listen_fd, listen_inode, sun_path1,
    186 	       (int) sizeof(addr), (int) *len,
    187 	       accept_fd, accept_inode);
    188 
    189 	memset(listen_sa, 0, sizeof(addr));
    190 	*len = sizeof(addr);
    191 	if (getpeername(connect_fd, listen_sa, len))
    192 		perror_msg_and_fail("getpeername");
    193 	printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    194 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
    195 	       connect_fd, connect_inode, av[1],
    196 	       (int) sizeof(addr), (int) *len);
    197 
    198 	memset(accept_sa, 0, sizeof(addr));
    199 	*len = sizeof(addr);
    200 	if (getsockname(connect_fd, accept_sa, len))
    201 		perror_msg_and_fail("getsockname");
    202 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
    203 	       ", sun_path=@\"%s\"}, [%d->%d]) = 0\n",
    204 	       connect_fd, connect_inode, sun_path1,
    205 	       (int) sizeof(addr), (int) *len);
    206 
    207 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
    208 	       == sizeof(text) - 1);
    209 	printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
    210 	       ", NULL, 0) = %u\n",
    211 	       connect_fd, connect_inode, text,
    212 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
    213 
    214 	assert(close(connect_fd) == 0);
    215 	printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
    216 
    217 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
    218 	       == sizeof(text) - 1);
    219 	printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
    220 	       ", NULL, NULL) = %u\n",
    221 	       accept_fd, accept_inode, text,
    222 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
    223 
    224 	assert(close(accept_fd) == 0);
    225 	printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
    226 
    227 	assert(unlink(av[1]) == 0);
    228 
    229 	assert(close(listen_fd) == 0);
    230 	printf("close(%d<socket:[%lu]>) = 0\n",
    231 	       listen_fd, listen_inode);
    232 
    233 	puts("+++ exited with 0 +++");
    234 	return 0;
    235 }
    236