# #include "util.h" #include "pkts.h" #include #define NFIL 20 #define TERM 15 #define SIGAIO 17 #define NINTRUP 01 int netf; int net; char devnet[] = {"/dev/net"}; char server[] = {"/etc/stelnet"}; char dir[] = {"/etc"}; char tmp[] = {"/etc/locks/lsntmp"}; char lock[] = {"/etc/locks/listen"}; /* Listen is a stub process which waits for a packet to TCP socket 23 and then forks a server telnet - it forks twice to prevent the accumulation of "defunct" processes. Defunct processes come about when the process which forks it 1) does not wait (i.e. wait()) for its child to finish or 2) does not exit before its child finishes. For case 2) the child becomes a child of process 1 which is always waiting for its children. */ main() { char netfds[20]; int status; short mode, term(); register int nserver, i; chdir(dir); for (i = 0; i < NFIL; i++) close(i); open("/dev/null", 0); /* Make next file channel 1 */ creat(tmp, 0444); printf("%d\n", getpid()); fflush(stdout); close(1); open("/dev/cty", 1); if (link(tmp, lock) == -1) { unlink(tmp); printf("update: another daemon exists\n"); exit(0); } signal(TERM, &term); unlink(tmp); nserver = 0; netf = 0; signal(SIGAIO, &netint); if (!tel_listen(&netfds)) exit(); while(1) { mode = NINTRUP; ioctl(net, NIOCSETL, &mode); while (netf == 0) pause(); nserver++; netf = 0; mode = 0; ioctl(net, NIOCSETL, &mode); if (fork() != 0) { wait(&status); continue; } else { if (fork() != 0) exit(); execl(server, server, netfds, 0); } } } netint() { netf++; } tel_listen(nfds) char *nfds; { struct netdf { short n_prot1; short n_prot2; long n_fhost; short n_fskt; short n_lskt; } netdf; register struct netdf *ndp; ndp = &netdf; ndp->n_prot1 = INETPROT; ndp->n_prot2 = TCPPROT; ndp->n_fhost = 0; ndp->n_fskt = 0; ndp->n_lskt = TELNETICP; if ((net = netopen(devnet, ndp)) == ERR) { printf("tel_listen cannot open to net error %d\n", errno); return(FALSE); } sprintf(nfds, "%d", net); return(TRUE); } term() { unlink(lock); exit(0); }