int create_sock() { int fds[2]; int sock2; int result = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); if (result == -1) { perror("socket"); exit(101); } if (sock == fds[0]) { sock2 = fds[1]; } else if (sock == fds[1]) { sock2 = fds[0]; } else { dup2(fds[0], sock); close(fds[0]); sock2 = fds[1]; } ready = 0; signal(SIGUSR1, set_ready); /* writer: stdin -> socket (when SC exits/fails, receives SIGCHLD and exits) \--> main: shellcode (when exits/fails, sends SIGCHLD to writer and closes socket) \--> reader: sock -> stdout (when SC exits/fails, socket is closed and reader exits) main saves pid1 = reader, pid2 = writer to send them SIGUSR1 right before running shellcode */ pid1 = fork(); if (pid1 == 0) { close(sock); run_reader(sock2); } pid2 = fork(); if (pid2 > 0) { // parent - writer signal(SIGCHLD, exit); close(sock); run_writer(sock2); } pid2 = getppid(); close(sock2); return sock; }
int main(int argc, const char **argv) { log_verbose(" =================== 20140410 \n"); pid_t pidreader; pidreader = fork(); assert(-1 != pidreader); if (0 == pidreader) { run_reader(); } else { run_writer(pidreader); } return 0; }
int main(int argc, const char **argv) { pid_t pidreader; if (argc == 2) { _test_size = atoi(argv[1]); } pidreader = fork(); assert(-1 != pidreader); if (0 == pidreader) { run_reader(); } else { run_writer(pidreader); } return 0; }