home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / queuer / part02 / selhost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.6 KB  |  194 lines

  1. /* Copyright 1990  The President and Fellows of Harvard University
  2.  
  3. Permission to use, copy, modify, and distribute this program for any
  4. purpose and without fee is hereby granted, provided that this
  5. copyright and permission notice appear on all copies and supporting
  6. documentation, the name of Harvard University not be used in advertising
  7. or publicity pertaining to distribution of the program, or to results
  8. derived from its use, without specific prior written permission, and notice
  9. be given in supporting documentation that copying and distribution is by
  10. permission of Harvard University.  Harvard University makes no
  11. representations about the suitability of this software for any purpose.
  12. It is provided "as is" without express or implied warranty.    */
  13.  
  14.  
  15. /* selhost.c - Dan Lanciani '85 '89 */
  16.  
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <stdio.h>
  21. #include <netdb.h>
  22. #include <signal.h>
  23. #include <setjmp.h>
  24. #include <pwd.h>
  25. #include "queue.h"
  26.  
  27. extern struct servent *sp;
  28. extern struct passwd *pw;
  29. extern long unid;
  30. extern int tolduser;
  31. static jmp_buf jb;
  32.  
  33. static struct ht {
  34.     char *ht_name;
  35.     int ht_load;
  36. } ht[MAXHCNT];
  37.  
  38. static
  39. cmp(a, b)
  40. struct ht *a, *b;
  41. {
  42.     return(a->ht_load - b->ht_load);
  43. }
  44.  
  45. static
  46. catch()
  47. {
  48.     longjmp(jb, 1);
  49. }
  50.  
  51. static char *
  52. ngetstr(n, buf)
  53. char *buf;
  54. {
  55.     register char *p = buf;
  56.  
  57.     do
  58.         if(read(n, p, 1) != 1)
  59.             return(0);
  60.     while(*p++);
  61.     return(buf);
  62. }
  63.  
  64. static char *argv0;
  65.  
  66. static
  67. doqrm()
  68. {
  69.     char buf[20];
  70.  
  71.     if(argv0 && unid) {
  72.         sprintf(buf, "%ld", unid);
  73.         execl("/usr/local/bin/qrm", "qrm", argv0, buf, 0);
  74.     }
  75.     exit(-1);
  76. }
  77.  
  78. selhost(argc, argv)
  79. char **argv;
  80. {
  81.     register int i, s, u;
  82.     int sz;
  83.     unsigned char buf[BUFSIZ];
  84.     struct sockaddr_in sin;
  85.  
  86.     argv0 = argv[0];
  87.     if(hcnt < 2 && !*qm)
  88.         return;
  89.     for(i = 0; i < hcnt; i++) {
  90.         ht[i].ht_name = hosts[i];
  91.         ht[i].ht_load = getrload(hosts[i]);
  92.     }
  93.     qsort((char *)ht, hcnt, sizeof(ht[0]), cmp);
  94.     for(i = 0; i < hcnt; i++)
  95.         hosts[i] = ht[i].ht_name;
  96.     if(!*qm || !(sp = getservbyname("qmaster", "tcp")) ||(s=qconnect(qm))<0)
  97.         return;
  98.     if((u = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  99.         perror("socket");
  100.         close(s);
  101.         return;
  102.     }
  103.     bzero(&sin, sizeof(sin));
  104.     if(bind(u, &sin, sizeof(sin))) {
  105.         perror("bind");
  106.         close(u);
  107.         close(s);
  108.         return;
  109.     }
  110.     sz = sizeof(sin);
  111.     if(getsockname(u, &sin, &sz)) {
  112.         perror("getsockname");
  113.         close(u);
  114.         close(s);
  115.         return;
  116.     }
  117.     if(setjmp(jb)) {
  118.     out:
  119.         alarm(0);
  120.         signal(SIGALRM, SIG_DFL);
  121.         close(u);
  122.         if(s >= 0)
  123.             close(s);
  124.         signal(SIGPIPE, SIG_DFL);
  125.         return;
  126.     }
  127.     signal(SIGALRM, catch);
  128.     alarm(5*60);
  129.     signal(SIGPIPE, SIG_IGN);
  130.     *buf = 0;
  131.     sprintf(buf + 1, "%d", sin.sin_port);
  132.     write(s, buf, strlen(buf + 1) + 2);
  133.     write(s, argv0, strlen(argv0) + 1);
  134.     write(s, pw->pw_name, strlen(pw->pw_name) + 1);
  135.     signal(SIGPIPE, SIG_DFL);
  136.     if(read(s, buf, 1) != 1)
  137.         goto out;
  138.     i = *buf;
  139.     if(!ngetstr(s, buf))
  140.         goto out;
  141.     if(i == 1) {
  142.         fprintf(stderr, "%s\n", buf);
  143.         exit(1);
  144.     }
  145.     unid = atol(buf);
  146.     if(!isunid(unid)) {
  147.         unid = 0;
  148.         goto out;
  149.     }
  150.     close(s);
  151.     s = -1;
  152.     alarm(0);
  153.     if(mode == QM_BATCH) {
  154.         tolduser++;
  155.         if(fork()) {
  156.             fprintf(stderr, "Job queued.\n");
  157.             exit(0);
  158.         }
  159.     }
  160.     signal(SIGINT, doqrm);
  161.     alarm(5*60);
  162.     while(1) {
  163.         sz = sizeof(sin);
  164.         if(recvfrom(u, buf, sizeof(buf), 0, &sin, &sz) <= 0) {
  165.             i = alarm(0);
  166.             sleep(10);
  167.             alarm(i);
  168.             continue;
  169.         }
  170.         switch(*buf) {
  171.             case 0:
  172.                 alarm(5*60);
  173.                 continue;
  174.             case 1:
  175.                 alarm(0);
  176.                 signal(SIGALRM, SIG_DFL);
  177.                 close(u);
  178.                 i = hcnt;
  179.                 if(i == MAXHCNT)
  180.                     i--;
  181.                 while(i) {
  182.                     hosts[i] = hosts[i - 1];
  183.                     i--;
  184.                 }
  185.                 hosts[0] = newstring(buf + 1);
  186.                 return;
  187.             case 2:
  188.                 exit(0);
  189.             default:
  190.                 goto out;
  191.         }
  192.     }
  193. }
  194.