home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / inetray / intrystr.c < prev    next >
C/C++ Source or Header  |  1992-06-13  |  3KB  |  147 lines

  1. /*======================================================================
  2.                     I N E T R A Y . S T A R T . C 
  3.                     doc: Fri Mar 13 12:21:13 1992
  4.                     dlm: Sat Jun 13 19:38:49 1992
  5.                     (c) 1992 ant@julia
  6.                     uE-Info: 102 55 T 0 0 72 2 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. #define        DAEMON
  10.  
  11. #include    <stdio.h>
  12. #include    <signal.h>
  13. #include    <syslog.h>
  14. #include    <termio.h>
  15. #include    <sys/types.h>
  16. #include    <sys/stat.h>
  17. #include    <rpc/rpc.h>
  18. #include    "inetray.start.h"
  19.  
  20. static int     nRunning = 0;        /* number of running servers */
  21. static char     *av0 = NULL;        /* server path */
  22.  
  23. extern void starter_1();        /* generated dispatch routine */
  24.  
  25. main(ac,av)
  26. int ac; char *av[];
  27. {
  28.     int     i,pid,sock,proto;
  29.     register SVCXPRT *transp;
  30.     struct stat buf;
  31.     
  32.     if (ac != 2) {
  33.         fprintf(stderr,"Usage: %s <executable>\n",av[0]);
  34.         exit(1);
  35.     }
  36.     av0 = av[1];            /* set server path */
  37.     if (stat(av0,&buf) < 0) {
  38.         perror("stat: cannot find executable");
  39.         exit(1);
  40.     }
  41.  
  42. #ifdef DAEMON
  43.     pid = fork();
  44.         if (pid < 0) {
  45.                 perror("cannot fork");
  46.                 exit(1);
  47.         }
  48.         if (pid) exit(0);
  49.         for (i = 0 ; i < 20; i++)
  50.                 (void) close(i);
  51.         i = open("/dev/console", 2);
  52.         (void) dup2(i, 1);
  53.         (void) dup2(i, 2); 
  54. #ifndef AUX_QUIRK                /* sigio stops working! */
  55. #ifndef SNAKE_QUIRK                /* TIOCNOTTY undef'd */
  56.         i = open("/dev/tty", 2);
  57.         if (i >= 0) {
  58.                 (void) ioctl(i, TIOCNOTTY, (char *)NULL);
  59.                 (void) close(i);
  60.         }
  61. #endif
  62. #endif
  63. #endif
  64. #ifdef LOG_DAEMON
  65.     openlog("inetray.start", LOG_PID, LOG_DAEMON);
  66. #else
  67.     openlog("inetray.start", LOG_PID);
  68. #endif
  69.     sock = RPC_ANYSOCK;
  70.     proto = IPPROTO_UDP;
  71.     (void) pmap_unset(STARTER, IRSV1);
  72.  
  73.     transp = svcudp_create(sock);
  74.     if (transp == NULL) {
  75.         syslog("cannot create udp service.");
  76.         exit(1);
  77.     }
  78.     if (!svc_register(transp, STARTER, IRSV1, starter_1, proto)) {
  79.         syslog("unable to register (STARTER, IRSV1, udp).");
  80.         exit(1);
  81.     }
  82.     svc_run();
  83.     syslog("svc_run returned");
  84.     exit(1);
  85.     /* NOTREACHED */
  86. }
  87.  
  88. static void funeral()                /* bury dead children */
  89. {
  90.     int    status;
  91.     
  92. #ifndef DAEMON
  93.     printf("first child died!\n");
  94. #endif
  95.     signal(SIGCHLD,SIG_IGN);        /* wait for all now */
  96.     while (nRunning > 0) {
  97.         wait(&status);            /* moment of rememberance */
  98. #ifndef DAEMON
  99.         printf("child returnded status %d\n",status);
  100. #endif
  101.         if (status != 0) 
  102.             syslog(LOG_ERR,"child returned status %d",status);
  103.         nRunning--;            /* accounting */
  104.     }
  105. #ifndef DAEMON
  106.     printf("all children died!\n");
  107. #endif
  108. }
  109.  
  110. void *start_1(param)                /* start # of servers */
  111. sPrm *param;
  112. {
  113.     int    i,pid;
  114.     char    uid[16],wid[16];
  115.     static char result;
  116.  
  117. #ifndef DAEMON
  118.     printf("START request received...\n");
  119. #endif
  120.     if (nRunning != 0)            /* already servicing */
  121.         return((void *)NULL);
  122.     sprintf(uid,"%d",param->uid);        /* make parameter */
  123.     signal(SIGCHLD,funeral);        /* wait for children */
  124.     for (i=0; i<param->nSvcs; i++) {
  125.         pid = fork();
  126.         if (pid < 0) {
  127.             syslog(LOG_ERR,"fork: %m");
  128.             exit(1);
  129.         }
  130.         if (pid == 0) {            /* worker */
  131.             sprintf(wid,"%d",i);
  132. #ifndef DAEMON
  133.             printf("execl: %s rpc.inetrayd %s %s\n",av0,wid,uid);
  134. #endif
  135.             if (execl(av0,"rpc.inetrayd",wid,uid,NULL) < 0) {
  136.                 syslog("execl: %m");
  137.                 exit(1);
  138.             }
  139.         }
  140. #ifndef DAEMON
  141.         printf("pid %d forked...\n",pid);
  142. #endif    
  143.         nRunning++;            /* one started */
  144.     }
  145. }
  146.  
  147.