blob: c9bc8e4d7439ff1a26506c4d39872ec637680a1e [file] [log] [blame]
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +00001#include <assert.h>
Dmitry V. Levin56ef5ef2013-05-08 14:03:38 +00002#include <stddef.h>
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +00003#include <string.h>
Dmitry V. Levinda66e252015-03-05 17:30:23 +00004#include <signal.h>
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +00005#include <unistd.h>
6#include <sys/wait.h>
7#include <sys/socket.h>
8#include <sys/un.h>
9
Dmitry V. Levinda66e252015-03-05 17:30:23 +000010static void
11handler(int sig)
12{
13 assert(close(1) == 0);
14 _exit(0);
15}
16
17int
18main(int ac, const char **av)
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000019{
20 struct sockaddr_un addr = {
21 .sun_family = AF_UNIX,
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000022 };
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000023 socklen_t len;
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000024
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000025 assert(ac == 2);
26 assert(strlen(av[1]) > 0);
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000027
Dmitry V. Levinc9e24182015-01-29 15:19:21 +000028 strncpy(addr.sun_path, av[1], sizeof(addr.sun_path));
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000029 len = offsetof(struct sockaddr_un, sun_path) + strlen(av[1]) + 1;
Dmitry V. Levinc9e24182015-01-29 15:19:21 +000030 if (len > sizeof(addr))
31 len = sizeof(addr);
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000032
33 unlink(av[1]);
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000034 close(0);
35 close(1);
36
37 assert(socket(PF_LOCAL, SOCK_STREAM, 0) == 0);
38 assert(bind(0, (struct sockaddr *) &addr, len) == 0);
39 assert(listen(0, 5) == 0);
40
41 memset(&addr, 0, sizeof addr);
42 assert(getsockname(0, (struct sockaddr *) &addr, &len) == 0);
Dmitry V. Levinc9e24182015-01-29 15:19:21 +000043 if (len > sizeof(addr))
44 len = sizeof(addr);
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000045
46 pid_t pid = fork();
47 assert(pid >= 0);
48
49 if (pid) {
50 assert(accept(0, (struct sockaddr *) &addr, &len) == 1);
51 assert(close(0) == 0);
Dmitry V. Levinda66e252015-03-05 17:30:23 +000052 assert(kill(pid, SIGUSR1) == 0);
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000053 int status;
54 assert(waitpid(pid, &status, 0) == pid);
55 assert(status == 0);
56 assert(close(1) == 0);
57 } else {
Dmitry V. Levinda66e252015-03-05 17:30:23 +000058 sigset_t set;
59 sigemptyset(&set);
60 sigaddset(&set, SIGUSR1);
61
62 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
63 assert(signal(SIGUSR1, handler) != SIG_ERR);
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000064 assert(socket(PF_LOCAL, SOCK_STREAM, 0) == 1);
65 assert(close(0) == 0);
Dmitry V. Levin56ef5ef2013-05-08 14:03:38 +000066 assert(connect(1, (struct sockaddr *) &addr, len) == 0);
Dmitry V. Levinda66e252015-03-05 17:30:23 +000067 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
68 assert(pause() == 99);
69 return 1;
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000070 }
71
Dmitry V. Levinc9cc4cb2015-01-07 20:14:19 +000072 unlink(av[1]);
Dmitry V. Levin47b0dcc2013-05-07 23:32:01 +000073 return 0;
74}