home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / Unix2src.shar / server / sloadav.c < prev    next >
C/C++ Source or Header  |  1989-05-02  |  1KB  |  79 lines

  1.  
  2. /*
  3.  *    SLOADAV.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *    Reports the load average every 5 minutes or until the connection
  8.  *    is closed.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <sys/wait.h>
  13. #include <sys/time.h>
  14. #include <sys/resource.h>
  15. #include <errno.h>
  16. #include <signal.h>
  17.  
  18. #include "servers.h"
  19.  
  20. chandler()
  21. {
  22.     union wait stat;
  23.     struct rusage rus;
  24.     while (wait3(&stat, WNOHANG, &rus) > 0);
  25. }
  26.  
  27. main(ac,av)
  28. char *av[];
  29. {
  30.     long chann = DListen(PORT_LOADAV);
  31.     int fd;
  32.     int n;
  33.     char buf[256];
  34.     extern int errno;
  35.  
  36.     if (av[1])
  37.     chdir(av[1]);
  38.     signal(SIGCHLD, chandler);
  39.     signal(SIGPIPE, SIG_IGN);
  40.     for (;;) {
  41.     fd = DAccept(chann);
  42.     if (fd < 0) {
  43.         if (errno == EINTR)
  44.         continue;
  45.         break;
  46.     }
  47.     if (fork() == NULL) {
  48.         do_loadav(fd);
  49.         close(fd);
  50.         _exit(1);
  51.     }
  52.     close(fd);
  53.     }
  54.     perror("SLOADAV");
  55. }
  56.  
  57. do_loadav(fd)
  58. {
  59.     char dummy;
  60.     char buf[256];
  61.     FILE *fi;
  62.  
  63.     while (ggread(fd, &dummy, 1) == 1) {
  64.     fi = popen("uptime", "r");
  65.     if (fi == NULL)
  66.         break;
  67.     if (fgets(buf, 256, fi)) {
  68.         dummy = strlen(buf);
  69.         buf[dummy-1] = 0;
  70.         gwrite(fd, &dummy, 1);
  71.         gwrite(fd, buf, dummy);
  72.     }
  73.     if (ferror(fi))
  74.         break;
  75.     pclose(fi);
  76.     }
  77. }
  78.  
  79.