Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 1 | #include <assert.h> |
Dmitry V. Levin | 56ef5ef | 2013-05-08 14:03:38 +0000 | [diff] [blame] | 2 | #include <stddef.h> |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 3 | #include <string.h> |
Dmitry V. Levin | da66e25 | 2015-03-05 17:30:23 +0000 | [diff] [blame^] | 4 | #include <signal.h> |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <sys/socket.h> |
| 8 | #include <sys/un.h> |
| 9 | |
Dmitry V. Levin | da66e25 | 2015-03-05 17:30:23 +0000 | [diff] [blame^] | 10 | static void |
| 11 | handler(int sig) |
| 12 | { |
| 13 | assert(close(1) == 0); |
| 14 | _exit(0); |
| 15 | } |
| 16 | |
| 17 | int |
| 18 | main(int ac, const char **av) |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 19 | { |
| 20 | struct sockaddr_un addr = { |
| 21 | .sun_family = AF_UNIX, |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 22 | }; |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 23 | socklen_t len; |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 24 | |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 25 | assert(ac == 2); |
| 26 | assert(strlen(av[1]) > 0); |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 27 | |
Dmitry V. Levin | c9e2418 | 2015-01-29 15:19:21 +0000 | [diff] [blame] | 28 | strncpy(addr.sun_path, av[1], sizeof(addr.sun_path)); |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 29 | len = offsetof(struct sockaddr_un, sun_path) + strlen(av[1]) + 1; |
Dmitry V. Levin | c9e2418 | 2015-01-29 15:19:21 +0000 | [diff] [blame] | 30 | if (len > sizeof(addr)) |
| 31 | len = sizeof(addr); |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 32 | |
| 33 | unlink(av[1]); |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 34 | 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. Levin | c9e2418 | 2015-01-29 15:19:21 +0000 | [diff] [blame] | 43 | if (len > sizeof(addr)) |
| 44 | len = sizeof(addr); |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 45 | |
| 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. Levin | da66e25 | 2015-03-05 17:30:23 +0000 | [diff] [blame^] | 52 | assert(kill(pid, SIGUSR1) == 0); |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 53 | int status; |
| 54 | assert(waitpid(pid, &status, 0) == pid); |
| 55 | assert(status == 0); |
| 56 | assert(close(1) == 0); |
| 57 | } else { |
Dmitry V. Levin | da66e25 | 2015-03-05 17:30:23 +0000 | [diff] [blame^] | 58 | 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. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 64 | assert(socket(PF_LOCAL, SOCK_STREAM, 0) == 1); |
| 65 | assert(close(0) == 0); |
Dmitry V. Levin | 56ef5ef | 2013-05-08 14:03:38 +0000 | [diff] [blame] | 66 | assert(connect(1, (struct sockaddr *) &addr, len) == 0); |
Dmitry V. Levin | da66e25 | 2015-03-05 17:30:23 +0000 | [diff] [blame^] | 67 | assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0); |
| 68 | assert(pause() == 99); |
| 69 | return 1; |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Dmitry V. Levin | c9cc4cb | 2015-01-07 20:14:19 +0000 | [diff] [blame] | 72 | unlink(av[1]); |
Dmitry V. Levin | 47b0dcc | 2013-05-07 23:32:01 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |