/************************************************* * pumpserv.c * * This daemon simply sits around listening to * port 5678 * * When someone (anyone) connects, we simply * respond with 'on' or 'off'. ************************************************/ #include #include #include #include #include #include #include #include #include #include int main( int argc, char *argv[] ){ int sockfd, newsockfd; struct sockaddr_in cli_addr, srv_addr; /* are we a daemon? */ if ( (argc>1) && (strcmp(argv[1], "-d" ) ==0 ) ){ if ( fork() ){ exit(0); } } /* socket, bind, listen */ sockfd=socket(AF_INET,SOCK_STREAM,0); bzero( (char *)&srv_addr, sizeof( srv_addr ) ); srv_addr.sin_family = AF_INET; srv_addr.sin_addr.s_addr = htonl(INADDR_ANY); srv_addr.sin_port = htons(5678); bind(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr) ); } listen( sockfd, 5 ); /* accept loop */ while( 1 ){ int cli_len = sizeof( cli_addr ); /* hang out until something happens */ newsockfd = accept( sockfd, (struct sockaddr *)&cli_addr, &cli_len ); if ( newsockfd > 0 ){ int len, pumpfd; char buf[256]; bzero( buf, 256 ); pumpfd = open( "/proc/pump", O_RDONLY ); if ( pumpfd < 0 ){ len = sprintf( buf,"Couldn't contact pump"); write( newsockfd, buf, len ); } else { while( (len = read( pumpfd, buf, 255 )) ){ write( newsockfd, buf, len-1 ); } } close(pumpfd); close(newsockfd); } } }