void server_main(int ac, char **av) { int newsock, sock; if (ac != 2) { fprintf(stderr, "usage: %s -s\n", av[0]); exit(1); } GO_AWAY; signal(SIGCHLD, child); sock = tcp_server(TCP_XACT, SOCKOPT_NONE); for (;;) { newsock = tcp_accept(sock, SOCKOPT_NONE); switch (fork()) { case -1: perror("fork"); break; case 0: doserver(newsock); exit(0); default: close(newsock); break; } } /* NOTREACHED */ }
void server_main() { int newsock, sock; GO_AWAY; signal(SIGCHLD, sigchld_wait_handler); sock = tcp_server(TCP_XACT, SOCKOPT_REUSE); for (;;) { newsock = tcp_accept(sock, SOCKOPT_NONE); #ifdef CONFIG_NOMMU switch (vfork()) { #else switch (fork()) { #endif case -1: perror("fork"); break; case 0: doserver(newsock); #ifdef CONFIG_NOMMU _exit(0); #else exit(0); #endif default: close(newsock); break; } } /* NOTREACHED */ } void doserver(int sock) { int n; if (read(sock, &n, sizeof(int)) == sizeof(int)) { int msize = ntohl(n); char* buf = (char*)malloc(msize); for (n = 0; read(sock, buf, msize) > 0; n++) { write(sock, buf, msize); } free(buf); } else { /* * A connection with no data means shut down. */ tcp_done(TCP_XACT); kill(getppid(), SIGTERM); exit(0); } }