Bugs in lu.c

Dan Ts'o dan at rna.UUCP
Tue Nov 27 13:25:14 AEST 1984


abc

	There are a number of bugs in the recently posted "lu"
(list users) program. Our UNIX is a 4.2BSD VAX.
	- When using the "lav" program via a popen("lav"), "lu" managed
	reverse the printing of the 5 and 15 minute loadaverages. This
	seemed to be because (char *) variables were used to read in
	the results from popen(). They have been changed to (double).
	- When not using the "lav", but the internal routine to directly
	read _avenrun from /dev/kmem (which is all that "lav" does),
	"lu" would bomb after several iterations because the /dev/kmem
	file descriptor is never closed.
	- "lu" runs continuous, thus contributing at least 1 to the load
	average itself. The popen("lav") is also inefficient. I inserted
	a sleep() call which defaults to 10 seconds, an emperically compromise.
	- Instead of opening and closing /dev/kmem all the time, I kept
	the file descriptor to /dev/kmem and nlist the kernel once.
	- The #else line involving the parameter LOADAV was not acceptable
	to our C compiler.
	- Its a screen version of
		while :
		do
			w
		done
	which doesn't even tell you what each user is doing.

Oh well... I had a few minutes and needed a light hack snack...

*** lu.c.org	Mon Nov 26 20:54:53 1984
--- lu.c	Mon Nov 26 23:31:02 1984
***************
*** 15,20
   *	lu understands the following options:				*
   *	-f filename	use 'filename' as an alternative to /etc/utmp.	*
   *	-s		don't use standout mode of terminal.		*
   *									*
   ************************************************************************
   *									*

--- 15,21 -----
   *	lu understands the following options:				*
   *	-f filename	use 'filename' as an alternative to /etc/utmp.	*
   *	-s		don't use standout mode of terminal.		*
+  *	-n seconds	seconds to sleep between cycles
   *									*
   ************************************************************************
   *									*
***************
*** 61,66
  	        done (), redraw ();
  	long    now,
  	        time ();
  
  	for (x = 1; x < argc; x++) {
  		if (argv[x][0] != '-')

--- 62,68 -----
  	        done (), redraw ();
  	long    now,
  	        time ();
+ 	int	secs = 10;
  
  	for (x = 1; x < argc; x++) {
  		if (argv[x][0] != '-')
***************
*** 72,77
  			case 's': 	    /* standout mode flag */
  				sflg = 0;
  				break;
  			default: 
  				printf ("usage: %s [-f] [-s]\n", *argv);
  				exit (0);

--- 74,82 -----
  			case 's': 	    /* standout mode flag */
  				sflg = 0;
  				break;
+ 			case 'n':
+ 				secs = atoi(argv[++x]);
+ 				break;
  			default: 
  				fprintf (stderr, "usage: %s [-f utmp_file] [-s] [-n seconds]\n", *argv);
  				exit (0);
***************
*** 73,79
  				sflg = 0;
  				break;
  			default: 
! 				printf ("usage: %s [-f] [-s]\n", *argv);
  				exit (0);
  		}
  	}

--- 78,84 -----
  				secs = atoi(argv[++x]);
  				break;
  			default: 
! 				fprintf (stderr, "usage: %s [-f utmp_file] [-s] [-n seconds]\n", *argv);
  				exit (0);
  		}
  	}
***************
*** 158,163
  		if (sflg)
  			standend ();
  		refresh ();
  		rewind (fp);
  	}
  }

--- 163,170 -----
  		if (sflg)
  			standend ();
  		refresh ();
+ 		if (secs > 0)
+ 			sleep(secs);
  		rewind (fp);
  	}
  }
***************
*** 268,276
  	double  vec[3];
  	FILE * pp;
  	FILE * popen ();
! 	char   *a,
! 	       *b,
! 	       *c;
  
  #ifdef pdp11
  	loadav (vec);

--- 275,281 -----
  	double  vec[3];
  	FILE * pp;
  	FILE * popen ();
! 	static int kmem = 0;
  
  #ifdef pdp11
  	loadav (vec);
***************
*** 275,281
  #ifdef pdp11
  	loadav (vec);
  	sprintf (str, "Load Average: %.02f %.02f %.02f", vec[0], vec[1], vec[2]);
! #else !pdp11 && LOADAV
  	pp = popen (LOADAV, "r");
  	fscanf (pp, "%f%f%f", &a, &b, &c);
  	sprintf (str, "Load Average: %.02f %.02f %.02f", a, b, c);

--- 280,287 -----
  #ifdef pdp11
  	loadav (vec);
  	sprintf (str, "Load Average: %.02f %.02f %.02f", vec[0], vec[1], vec[2]);
! #else
! #ifdef	LOADAV
  	pp = popen (LOADAV, "r");
  	fscanf (pp, "%lf%lf%lf", &vec[0], &vec[1], &vec[2]);
  	sprintf (str, "Load Average: %.02f %.02f %.02f", vec[0], vec[1], vec[2]);
***************
*** 277,284
  	sprintf (str, "Load Average: %.02f %.02f %.02f", vec[0], vec[1], vec[2]);
  #else !pdp11 && LOADAV
  	pp = popen (LOADAV, "r");
! 	fscanf (pp, "%f%f%f", &a, &b, &c);
! 	sprintf (str, "Load Average: %.02f %.02f %.02f", a, b, c);
  	pclose (pp);
  #else
  #	include <nlist.h>

--- 283,290 -----
  #else
  #ifdef	LOADAV
  	pp = popen (LOADAV, "r");
! 	fscanf (pp, "%lf%lf%lf", &vec[0], &vec[1], &vec[2]);
! 	sprintf (str, "Load Average: %.02f %.02f %.02f", vec[0], vec[1], vec[2]);
  	pclose (pp);
  #else
  #	include <nlist.h>
***************
*** 287,293
  				{ "_avenrun" },
  				{ "" }
  			};
- 	register int kmem;
  	long lseek();
  
  	if((kmem = open("/dev/kmem",0)) < 0) {

--- 293,298 -----
  				{ "_avenrun" },
  				{ "" }
  			};
  	long lseek();
  
  	if(kmem == 0) {
***************
*** 290,299
  	register int kmem;
  	long lseek();
  
! 	if((kmem = open("/dev/kmem",0)) < 0) {
! 		finish();
! 		puts("Cant't access /dev/kmem.");
! 		done();
  	}
  	nlist("/vmunix", nlar);
  	if(!nlar[0].n_type) {

--- 295,307 -----
  			};
  	long lseek();
  
! 	if(kmem == 0) {
! 		if ((kmem = open("/dev/kmem",0)) < 0) {
! 			finish();
! 			puts("Can't access /dev/kmem.");
! 			done();
! 		}
! 		nlist("/vmunix", nlar);
  	}
  	if(!nlar[0].n_type) {
  		finish();
***************
*** 295,301
  		puts("Cant't access /dev/kmem.");
  		done();
  	}
- 	nlist("/vmunix", nlar);
  	if(!nlar[0].n_type) {
  		finish();
  		puts("Can't find namelist in vmunix.");

--- 303,308 -----
  		}
  		nlist("/vmunix", nlar);
  	}
  	if(!nlar[0].n_type) {
  		finish();
  		puts("Can't find namelist in vmunix.");
***************
*** 304,309
  	lseek(kmem,(long)nlar[0].n_value,0);
  	read(kmem,vec,sizeof(vec));
  	sprintf(str,"Load Average: %.02f %.02f %.02f", vec[0],vec[1],vec[2]);
  #endif pdp11
  	return (str);
  }

--- 311,317 -----
  	lseek(kmem,(long)nlar[0].n_value,0);
  	read(kmem,vec,sizeof(vec));
  	sprintf(str,"Load Average: %.02f %.02f %.02f", vec[0],vec[1],vec[2]);
+ #endif LOADAV
  #endif pdp11
  	return (str);
  }



More information about the Comp.sources.bugs mailing list