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