| 1 | // RUN: %clangxx -O0 -g %s -o %t && %run %t |
| 2 | |
| 3 | #include <netinet/in.h> |
| 4 | #include <sys/socket.h> |
| 5 | #include <sys/wait.h> |
| 6 | #include <signal.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | int main(void) { |
| 11 | int child; |
| 12 | int fd, sfd; |
| 13 | socklen_t len; |
| 14 | struct sockaddr_in server = {}, client = {}; |
| 15 | sigset_t set; |
| 16 | |
| 17 | child = fork(); |
| 18 | if (child == 0) { |
| 19 | fd = socket(AF_INET, SOCK_STREAM, protocol: 0); |
| 20 | if (fd == -1) |
| 21 | _exit(status: 1); |
| 22 | |
| 23 | server.sin_family = AF_INET; |
| 24 | server.sin_addr.s_addr = INADDR_ANY; |
| 25 | server.sin_port = htons(hostshort: 2222); |
| 26 | |
| 27 | if (connect(fd: fd, addr: (struct sockaddr *)&server, len: sizeof(server)) == -1) |
| 28 | _exit(status: 1); |
| 29 | |
| 30 | close(fd: fd); |
| 31 | |
| 32 | _exit(status: 0); |
| 33 | } |
| 34 | |
| 35 | fd = socket(AF_INET, SOCK_STREAM, protocol: 0); |
| 36 | if (fd == -1) { |
| 37 | kill(pid: child, SIGKILL); |
| 38 | wait(NULL); |
| 39 | exit(status: 1); |
| 40 | } |
| 41 | |
| 42 | server.sin_family = AF_INET; |
| 43 | server.sin_addr.s_addr = INADDR_ANY; |
| 44 | server.sin_port = htons(hostshort: 2222); |
| 45 | |
| 46 | if (bind(fd: fd, addr: (const struct sockaddr *)&server, len: sizeof(server)) == -1) { |
| 47 | kill(pid: child, SIGKILL); |
| 48 | wait(NULL); |
| 49 | exit(status: 1); |
| 50 | } |
| 51 | |
| 52 | listen(fd: fd, n: 3); |
| 53 | |
| 54 | if (sigemptyset(set: &set) == -1) { |
| 55 | kill(pid: child, SIGKILL); |
| 56 | wait(NULL); |
| 57 | exit(status: 1); |
| 58 | } |
| 59 | |
| 60 | len = sizeof(client); |
| 61 | sfd = paccept(fd, (struct sockaddr *)&client, &len, &set, SOCK_NONBLOCK); |
| 62 | if (sfd == -1) { |
| 63 | kill(pid: child, SIGKILL); |
| 64 | wait(NULL); |
| 65 | exit(status: 1); |
| 66 | } |
| 67 | |
| 68 | wait(NULL); |
| 69 | |
| 70 | close(fd: sfd); |
| 71 | close(fd: fd); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |