Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of net-y-unix strace test. |
| 3 | * |
| 4 | * Copyright (c) 2013-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 <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 | |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 57 | int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 58 | if (listen_fd < 0) |
| 59 | perror_msg_and_skip("socket"); |
| 60 | unsigned long listen_inode = inode_of_sockfd(listen_fd); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 61 | printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n", |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 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"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 67 | printf("bind(%d<socket:[%lu]>, {sa_family=AF_UNIX, sun_path=\"%s\"}" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 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"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 86 | printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 87 | ", sun_path=\"%s\"}, [%u]) = 0\n", listen_fd, listen_inode, |
| 88 | av[1], (unsigned) *len); |
| 89 | |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 90 | int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 91 | if (connect_fd < 0) |
| 92 | perror_msg_and_fail("socket"); |
| 93 | unsigned long connect_inode = inode_of_sockfd(connect_fd); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 94 | printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n", |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 95 | connect_fd, connect_inode); |
| 96 | |
| 97 | if (connect(connect_fd, listen_sa, *len)) |
| 98 | perror_msg_and_fail("connect"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 99 | printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 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); |
Dmitry V. Levin | f52753a | 2016-06-22 23:40:56 +0000 | [diff] [blame] | 110 | printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX}" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 111 | ", [%u]) = %d<socket:[%lu]>\n", |
| 112 | listen_fd, listen_inode, (unsigned) *len, |
| 113 | accept_fd, accept_inode); |
| 114 | |
| 115 | memset(listen_sa, 0, sizeof(addr)); |
| 116 | *len = sizeof(addr); |
| 117 | if (getpeername(connect_fd, listen_sa, len)) |
| 118 | perror_msg_and_fail("getpeername"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 119 | printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 120 | ", sun_path=\"%s\"}, [%u]) = 0\n", connect_fd, connect_inode, |
| 121 | av[1], (unsigned) *len); |
| 122 | |
| 123 | char text[] = "text"; |
| 124 | assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0) |
| 125 | == sizeof(text) - 1); |
| 126 | printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT" |
| 127 | ", NULL, 0) = %u\n", |
| 128 | connect_fd, connect_inode, text, |
| 129 | (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); |
| 130 | |
| 131 | assert(close(connect_fd) == 0); |
| 132 | printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode); |
| 133 | |
| 134 | assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL) |
| 135 | == sizeof(text) - 1); |
| 136 | printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT" |
| 137 | ", NULL, NULL) = %u\n", |
| 138 | accept_fd, accept_inode, text, |
| 139 | (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); |
| 140 | |
| 141 | assert(close(accept_fd) == 0); |
| 142 | printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode); |
| 143 | |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 144 | connect_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 145 | if (connect_fd < 0) |
| 146 | perror_msg_and_fail("socket"); |
| 147 | connect_inode = inode_of_sockfd(connect_fd); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 148 | printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n", |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 149 | connect_fd, connect_inode); |
| 150 | |
| 151 | *optval = 1; |
| 152 | *len = sizeof(*optval); |
| 153 | if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len)) |
| 154 | perror_msg_and_fail("setsockopt"); |
| 155 | printf("setsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED" |
| 156 | ", [%u], %u) = 0\n", |
| 157 | connect_fd, connect_inode, *optval, (unsigned) *len); |
| 158 | |
| 159 | memset(listen_sa, 0, sizeof(addr)); |
| 160 | *len = sizeof(addr); |
| 161 | if (getsockname(listen_fd, listen_sa, len)) |
| 162 | perror_msg_and_fail("getsockname"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 163 | printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 164 | ", sun_path=\"%s\"}, [%u]) = 0\n", |
| 165 | listen_fd, listen_inode, av[1], (unsigned) *len); |
| 166 | |
| 167 | if (connect(connect_fd, listen_sa, *len)) |
| 168 | perror_msg_and_fail("connect"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 169 | printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 170 | ", sun_path=\"%s\"}, %u) = 0\n", |
| 171 | connect_fd, connect_inode, av[1], (unsigned) *len); |
| 172 | |
| 173 | memset(accept_sa, 0, sizeof(addr)); |
| 174 | *len = sizeof(addr); |
| 175 | accept_fd = accept(listen_fd, accept_sa, len); |
| 176 | if (accept_fd < 0) |
| 177 | perror_msg_and_fail("accept"); |
| 178 | accept_inode = inode_of_sockfd(accept_fd); |
| 179 | const char * const sun_path1 = |
| 180 | ((struct sockaddr_un *) accept_sa) -> sun_path + 1; |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 181 | printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 182 | ", sun_path=@\"%s\"}, [%u]) = %d<socket:[%lu]>\n", |
| 183 | listen_fd, listen_inode, sun_path1, (unsigned) *len, |
| 184 | accept_fd, accept_inode); |
| 185 | |
| 186 | memset(listen_sa, 0, sizeof(addr)); |
| 187 | *len = sizeof(addr); |
| 188 | if (getpeername(connect_fd, listen_sa, len)) |
| 189 | perror_msg_and_fail("getpeername"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 190 | printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 191 | ", sun_path=\"%s\"}, [%u]) = 0\n", |
| 192 | connect_fd, connect_inode, av[1], (unsigned) *len); |
| 193 | |
| 194 | memset(accept_sa, 0, sizeof(addr)); |
| 195 | *len = sizeof(addr); |
| 196 | if (getsockname(connect_fd, accept_sa, len)) |
| 197 | perror_msg_and_fail("getsockname"); |
Dmitry V. Levin | e139cbe | 2016-06-20 22:52:49 +0000 | [diff] [blame] | 198 | printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX" |
Dmitry V. Levin | 751fcb8 | 2016-02-02 19:48:46 +0000 | [diff] [blame] | 199 | ", sun_path=@\"%s\"}, [%u]) = 0\n", |
| 200 | connect_fd, connect_inode, sun_path1, (unsigned) *len); |
| 201 | |
| 202 | assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0) |
| 203 | == sizeof(text) - 1); |
| 204 | printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT" |
| 205 | ", NULL, 0) = %u\n", |
| 206 | connect_fd, connect_inode, text, |
| 207 | (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); |
| 208 | |
| 209 | assert(close(connect_fd) == 0); |
| 210 | printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode); |
| 211 | |
| 212 | assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL) |
| 213 | == sizeof(text) - 1); |
| 214 | printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT" |
| 215 | ", NULL, NULL) = %u\n", |
| 216 | accept_fd, accept_inode, text, |
| 217 | (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); |
| 218 | |
| 219 | assert(close(accept_fd) == 0); |
| 220 | printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode); |
| 221 | |
| 222 | assert(unlink(av[1]) == 0); |
| 223 | |
| 224 | assert(close(listen_fd) == 0); |
| 225 | printf("close(%d<socket:[%lu]>) = 0\n", |
| 226 | listen_fd, listen_inode); |
| 227 | |
| 228 | puts("+++ exited with 0 +++"); |
| 229 | return 0; |
| 230 | } |