home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / musbus / part03 / limit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  2.5 KB  |  150 lines

  1. /*
  2.  *  Force a UNIX system to the per process and per user limits
  3.  *
  4.  *  $Header: limit.c,v 3.4 87/06/22 14:25:11 kjmcdonell Beta $
  5.  */
  6.  
  7. #define    CLICK    1024
  8. #define    MAXCHN    100
  9.  
  10. #include <signal.h>
  11. #include <setjmp.h>
  12.  
  13. int    parent;        /* parent's pid */
  14. int    child;        /* child's pid */
  15. int    pid[MAXCHN];
  16. int    ncall;
  17. int    level;
  18. jmp_buf    env;
  19.  
  20. main(argc, argv)
  21. int    argc;
  22. char    *argv[];
  23. {
  24.     char    *top;
  25.     int    pad;
  26.     int    end;
  27.     int    i;
  28.     int    status;
  29.     float    f;
  30.     int    flag();
  31.     int    wakeup();
  32.     long    last;
  33.  
  34.     /* open files (file descriptors) */
  35.     for (i = 3; open(".", 0) > 0; i++) ;
  36.     printf("Maximum open files per process: %d\n", i);
  37.     while (--i > 2)
  38.         close(i);
  39.  
  40.     /* process address space */
  41.     top = (char *)sbrk(0);
  42. #if debug
  43.     printf("inital top of program: 0x%x\n", top);
  44. #endif
  45.     pad = (((int)top+CLICK-1)/CLICK)*CLICK - (int)top;
  46.     sbrk(pad);
  47.     for (i = 0; (char *)sbrk(CLICK) != (char *)-1; i++) ;
  48. #if debug
  49.     printf("final top of program: 0x%x\n", sbrk(0));
  50. #endif
  51.     brk(top);
  52. #if debug
  53.     printf("top of program restored to: 0x%x\n", sbrk(0));
  54. #endif
  55.     end = (((int)top+pad)/CLICK) + i;
  56.     f = ((float)end * CLICK) / 1024;
  57.     printf("Process address space limit: ");
  58.     if (f < 1024)
  59.         printf("%.2f Kbytes\n", f);
  60.     else {
  61.         f /= 1024;
  62.         printf("%.2f Mbytes\n", f);
  63.     }
  64.  
  65.     /* process creations */
  66.     printf("Maximum number of child processes:");
  67.     i = 0;
  68.     while (1) {
  69. #if debug
  70.         printf("about to fork\n");
  71. #endif
  72.         if ((pid[i] = fork()) == -1) {
  73. #if debug
  74.             perror("fork failed");
  75. #endif
  76.             break;
  77.         } else if (pid[i] != 0) {
  78. #if debug
  79.             printf("child %d: pid=%d\n", i+1, pid[i]);
  80. #endif
  81.             i++;
  82.             if (i >= MAXCHN) {
  83.                 printf(" more than");
  84.                 break;
  85.             }
  86.         } else {
  87. #if debug
  88.             printf("child %d pausing\n", getpid());
  89. #endif
  90.             pause();
  91. #if debug
  92.             printf("child %d exiting\n", getpid());
  93. #endif
  94.             exit(1);
  95.         }
  96.     }
  97.     printf(" %d\n", i);
  98.     while (--i >= 0) {
  99.         kill(pid[i], SIGKILL);
  100.         wait(0);
  101.     }
  102.  
  103.     ncall = level = 0;
  104.     parent = getpid();
  105.     signal(SIGTERM, flag);
  106.     if ((child = fork()) == 0) {
  107.         signal(SIGALRM, wakeup);
  108.         recurse();
  109.         exit(4);
  110.     }
  111.     while ((i = wait(&status)) == -1) {
  112.     }
  113.     printf("Estimated maximum stack size: %d Kbytes\n", level);
  114.     exit(0);
  115. }
  116.  
  117. recurse()
  118. {
  119.     int    temp[1024 / sizeof(int)];
  120. #if debug
  121.     printf("recursion @ level %d\n", ncall);
  122. #endif
  123.     temp[1024 / sizeof(int) - 1] = 1;
  124.     ncall++;
  125.     kill(parent, SIGTERM);
  126.     while (ncall > level) {
  127.         alarm(2);
  128.         pause();
  129.     }
  130.     if (ncall < 8000)
  131.         /* less than 8M bytes of temp storage! */
  132.         recurse();
  133.     else
  134.         /* give up! */
  135.         exit(0);
  136. }
  137.  
  138. flag()
  139. {
  140.     signal(SIGTERM, flag);
  141.     level++;
  142.     if (child != 0)
  143.         kill(child, SIGTERM);
  144. }
  145.  
  146. wakeup()
  147. {
  148.     signal(SIGALRM, wakeup);
  149. }
  150.