- sin.sin_port = sp->s_port;
- argc--, argv++;
- if (argc > 0) {
- sin.sin_port = atoi(*argv);
- if (sin.sin_port <= 0) {
- fprintf(stderr, "telnetd: %s: bad port #\n", *argv);
- exit(1);
- }
- sin.sin_port = htons((u_short)sin.sin_port);
- }
-#ifndef DEBUG
- if (fork())
- exit(0);
- for (s = 0; s < 10; s++)
- (void) close(s);
- (void) open("/", 0);
- (void) dup2(0, 1);
- (void) dup2(0, 2);
- { int tt = open("/dev/tty", 2);
- if (tt > 0) {
- ioctl(tt, TIOCNOTTY, 0);
- close(tt);
- }
- }
-#endif
-again:
- s = socket(AF_INET, SOCK_STREAM, 0, 0);
- if (s < 0) {
- perror("telnetd: socket");;
- sleep(5);
- goto again;
- }
- while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
- perror("telnetd: bind");
- sleep(5);
- }
- listen(s, 10);
- for (;;) {
- int s2;
-
- s2 = accept(s, (caddr_t)0, 0, 0);
- if (s2 < 0) {
- perror("telnetd: accept");
- sleep(1);
- continue;
- }
- if ((pid = fork()) < 0)
- printf("Out of processes\n");
- else if (pid == 0)
- doit(s2);
- close(s2);
- while (wait3(status, WNOHANG, 0) > 0)
- continue;
+ if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
+ syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");