/*================ Daemon process of paw server =========================== * * 21-MAY-1991 M. Nakahata *==========================================================================/ /* * INCLUDE FILES */ #include #include #include #include #include #include extern int os9fork(); extern char **environ; char *argblk[] = { "rc_serv", 0, }; /*------ Port number of paw server ------ */ #define PORT_NUMBER 1036 /*--------------------------------------------------------------------*/ main() { int sock_2, sock_3; /* sockets */ static char message[80]; static struct sockaddr_in sock2_name; /* Addr struct for socket2. */ static struct sockaddr_in retsock2_name; /* Add struct for socket2. */ struct hostent hostentstruct; /* Storage for hostent data.*/ struct hostent *hostentptr; /* Pointer to hostent data. */ char hostname[256]; /* Name of local host. */ int retval; /* helpful for debugging */ int namelength; int res, on = 1; int status; /* * Open socket 2: AF_INET, SOCK_STREAM. */ if ((sock_2 = socket (AF_INET, SOCK_STREAM, 0)) == -1) { fprintf(stderr, "error at open socket\n"); exit(); } /* Reuse address suggested by A.Kotanski */ res = setsockopt(sock_2, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on); /* * Get the host local name. */ retval = gethostname(hostname,sizeof hostname); if (retval) { fprintf(stderr, "Error at gethost name\n"); cleanup (1, sock_2, 0); } /* * Get pointer to network data structure for socket 2. */ if ((hostentptr = gethostbyname (hostname)) == NULL) { fprintf(stderr, "Error at gethostbyname\n"); cleanup(1, sock_2, 0); } /* * Copy hostent data to safe storage. */ hostentstruct = *hostentptr; /* * Fill in the name & address structure for socket 2. */ sock2_name.sin_family = hostentstruct.h_addrtype; sock2_name.sin_port = htons(PORT_NUMBER); sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr); /* * Bind name to socket 2. */ retval = bind (sock_2, &sock2_name, sizeof sock2_name); if (retval<0) { fprintf(stderr,"Error at bind."); cleanup(1, sock_2, 0); } /* * Listen on socket 2 for connections. */ retval = listen (sock_2, 5); if (retval<0) { fprintf(stderr,"Error at listen."); cleanup(1, sock_2, 0); } /* * Accept connection from socket 2: * accepted connection will be on socket 3. */ do { wait_accept: namelength = sizeof (sock2_name); sock_3 = accept (sock_2, &sock2_name, &namelength); if (sock_3 == -1) { fprintf(stderr,"Error at accept."); cleanup( 2, sock_2, sock_3); } close(0); /* Close stdin */ dup(sock_3); /* connect socket port to stdin */ if (os9exec(os9fork, argblk[0], argblk, environ, 0, 0) == -1) { fprintf(stderr,"Error at os9exec."); } close(sock_3); close(0); /* Close stdin */ dup(2); close(1); /* Close stdout */ dup(2); check_children(); /* check activity of children */ goto wait_accept; } while (1); /* * Call cleanup to shutdown and close sockets. */ cleanup(2, sock_2, sock_3); } /* end main */ /*-----------------------------------------------------------*/ cleanup(how_many, sock1, sock2) int how_many; int sock1, sock2; { int retval; /* * Shutdown and close sock1 completely. */ retval = shutdown(sock1,2); if (retval == -1) fprintf(stderr,"Error at shutdown."); retval = close (sock1); if (retval) fprintf(stderr,"Error at close."); /* * If given, shutdown and close sock2. */ if (how_many == 2) { retval = shutdown(sock2,2); if (retval == -1) fprintf(stderr,"Error at shutdown."); retval = close (sock2); if (retval) fprintf(stderr,"Error at close."); } exit(); } /* end cleanup*/