home *** CD-ROM | disk | FTP | other *** search
- /* getld.c
- * This program must be setgid to memory (or whoever is allowed
- * to read /dev/kmem). It exits with with the load average, or
- * a negative number to indicate an error.
- * It serves primarily to be called by the library routine
- * getload();
- */
-
- #include <stdio.h>
- #include <nlist.h>
- #include "loadstuff.h"
-
- struct nlist avenrun[] =
- {
- { "_avenrun" },
- { 0 }
- };
-
- main(argc,argv)
- int argc;
- char **argv;
- {
-
- register int kmem, whatave, returnave;
- double avg[3];
-
- if((kmem = open("/dev/kmem", 0)) < 0)
- exit(NOKMEM);
-
- nlist(NAMELIST, avenrun); /* Find where our kernel keeps it */
- if(avenrun[0].n_type == 0)
- exit(NONAMELST);
-
- whatave = atoi(argv[1]);
- --whatave;
- if(whatave == 4) whatave = 1;
- if(whatave == 14) whatave = 2;
-
- if(whatave <0 || whatave > 2)
- whatave = 0;
-
- lseek(kmem, (long) avenrun[0].n_value, 0);
- read(kmem, avg, sizeof (avg));
-
- returnave = avg[whatave] * 100;
- /* Now we have the load average to 2 floating point accuracy * 100
- * However, we want to return it with 1 point accuracy that is correctly
- * rounded up or down. So we divide it by 10 and see what the
- * remainder is and do the rounding (for the integer).
- */
- if(returnave % 10 >= 5) /* Round up */
- returnave = returnave / 10 + 1;
- else returnave = returnave / 10;
-
- if(returnave >= 250) /* We cannot return with a number > 255 */
- returnave = 250; /* and 255 = -1, 254 = -2 */
-
- exit(returnave); /* Exit with an integer load average */
-
- }
-