[comp.sources.misc] v06i033: SCO Xenix 286 renice part2/2

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (02/04/89)

Posting-number: Volume 6, Issue 33
Submitted-by: wht@tridom.UUCP (Warren Tucker)
Archive-name: renice.sco/part02

More (re)niceness.

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  libkmem.c
#	  libuxlst.c
#	  uxlst.c
#	  renice.c
#
if test -f libkmem.c; then echo "File libkmem.c exists"; else
echo "x - extracting libkmem.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > libkmem.c &&
X/* CHK=0x7937 */
X/*+-------------------------------------------------------------------------
X	libkmem.c -- /dev/kmem routines for SCO XENIX V/286 (maybe other *NIX)
X
X  Defined functions:
X	kinit(write_needed)
X	kread(caddr,kaddr,len)
X	kwrite(kaddr,caddr,len)
X
X routines were originally written by Mike "Ford" Ditto: kudos!!!
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:10-27-1988-12:44-wht-creation of file */
X
X#include <sys/types.h>
X#include <fcntl.h>
X#include "libkmem.h"
X
Xstatic int fdkmem;
Xoff_t lseek();
X
X/*+-------------------------------------------------------------------------
X	kinit(write_needed)
X--------------------------------------------------------------------------*/
Xvoid
Xkinit(write_needed)
Xint		write_needed;
X{
X	if((fdkmem=open("/dev/kmem",(write_needed) ? O_RDWR : O_RDONLY,0)) < 0)
X	{
X		perror("can't open /dev/kmem");
X		exit(1);
X	}
X
X}	/* end of kinit */
X
X/*+-------------------------------------------------------------------------
X	kread(caddr,kaddr,len)
X--------------------------------------------------------------------------*/
Xvoid
Xkread(caddr,kaddr,len)
Xcaddr_t caddr;
Xoff_t kaddr;
Xoff_t len;
X{
Xchar	s80[80];
X
X	if((lseek(fdkmem,kaddr,0) < 0L) ||
X	    (read(fdkmem,caddr,(unsigned)len) != len))
X	{
X		sprintf(s80,"/dev/kmem read addr %08lx len %08lx",kaddr,len);
X		perror(s80);
X		exit(1);
X	}
X}	/* end of kread */
X
X/*+-------------------------------------------------------------------------
X	kwrite(kaddr,caddr,len)
X--------------------------------------------------------------------------*/
Xvoid
Xkwrite(kaddr,caddr,len)
Xoff_t kaddr;
Xcaddr_t caddr;
Xoff_t len;
X{
Xchar	s80[80];
X
X	if((lseek(fdkmem,kaddr,0) < 0L) ||
X	    (write(fdkmem,caddr,(unsigned int)len) != (unsigned int)len))
X	{
X		sprintf(s80,"/dev/kmem write addr %08lx len %08lx",kaddr,len);
X		perror(s80);
X		exit(1);
X	}
X}	/* end of kwrite */
X
X/* vi: set tabstop=4 shiftwidth=4: */
SHAR_EOF
chmod 0644 libkmem.c || echo "restore of libkmem.c fails"
fi
if test -f libuxlst.c; then echo "File libuxlst.c exists"; else
echo "x - extracting libuxlst.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > libuxlst.c &&
X/* CHK=0x3487 */
X/*+-------------------------------------------------------------------------
X	libuxlst.c -- common runtime for uxlst users
X
X  Defined functions:
X	uxlst_error(err_indicator)
X	uxlst_read()
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:10-27-1988-11:44-wht-creation */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <fcntl.h>
X#include <a.out.h>
X
X#define DEFINE_UXLST
X#include "uxlst.h"
X
X#include "libuxlst.h"
X
X/*+-------------------------------------------------------------------------
X	uxlst_error(err_indicator)
X--------------------------------------------------------------------------*/
Xvoid
Xuxlst_error(err_indicator)
Xint		err_indicator;
X{
X	if(err_indicator < 0)
X		perror(XENIX_UXLST);
X	fprintf(stderr,"run uxlst against %s\n",XENIX_KERNEL);
X	exit(1);
X}	/* end of uxlst_error */
X
X/*+-------------------------------------------------------------------------
X	uxlst_read()
X--------------------------------------------------------------------------*/
Xvoid
Xuxlst_read()
X{
Xint		itmp;
Xint		fduxlst;
Xstruct stat curstat;	/* current /xenix status */
Xstruct stat xstat;		/* /xenix status at uxlst run time */
Xlong	unique;
X
X	if(stat(XENIX_KERNEL,&curstat) < 0)
X	{
X		fputs("cannot stat ",stderr);
X		perror(XENIX_KERNEL);
X		exit(1);
X	}
X
X	if((fduxlst = open(XENIX_UXLST,O_RDONLY,0)) < 0)
X		uxlst_error(fduxlst);
X
X	if((itmp = read(fduxlst,&xstat,sizeof(xstat))) != sizeof(xstat))
X	{
X		fprintf(stderr,"xstat error: ");
X		uxlst_error(itmp);
X	}
X
X	if((itmp = read(fduxlst,uxlst,sizeof(uxlst))) != sizeof(uxlst))
X	{
X		fprintf("uxlst error: ");
X		uxlst_error(itmp);
X	}
X
X	if((itmp = read(fduxlst,&unique,sizeof(unique))) != sizeof(unique))
X	{
X		fprintf("unique error: ");
X		uxlst_error(itmp);
X	}
X
X	close(fduxlst);
X
X	if( (unique != UXLST_UNIQUE) ||
X		(xstat.st_ino != curstat.st_ino) ||
X		(xstat.st_mtime != curstat.st_mtime) ||
X		(xstat.st_size != curstat.st_size))
X	{
X		fprintf(stderr,"%s out of date\n",XENIX_UXLST);
X		uxlst_error(0);
X	}
X
X}	/* end of uxlst_read */
SHAR_EOF
chmod 0644 libuxlst.c || echo "restore of libuxlst.c fails"
fi
if test -f uxlst.c; then echo "File uxlst.c exists"; else
echo "x - extracting uxlst.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > uxlst.c &&
X/* CHK=0xE9C1 */
X/*+-------------------------------------------------------------------------
X	uxlst.c -- utility xlist - fast access to kernel /dev/kmem offsets
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:10-27-1988-10:58-wht-creation */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <fcntl.h>
X#include <a.out.h>
X
X#define DEFINE_UXLST
X#include "uxlst.h"
X
X/*+-------------------------------------------------------------------------
X	uxlst_write_error()
X--------------------------------------------------------------------------*/
Xvoid
Xuxlst_write_error()
X{
X	perror(XENIX_UXLST);
X	exit(1);
X}	/* end of uxlst_write_error */
X
X/*+-------------------------------------------------------------------------
X	main(argc,argv,envp)
X--------------------------------------------------------------------------*/
Xmain(argc,argv,envp)
Xint		argc;
Xchar	**argv;
Xchar	**envp;
X{
Xregister int itmp;
Xregister struct xlist *xx;
Xstruct stat xstat;		/* /xenix status at uxlst run time */
Xint		fduxlst;
Xint		xlist_error = 0;
Xlong	unique;
XFILE	*kludge;
X
X	xlist(XENIX_KERNEL,uxlst);
X
X	xx = uxlst;
X	while(xx->xl_name)
X	{
X		if(xx->xl_type == 0)
X		{
X			printf("%s: can't xlist (try _%s ?)\n",
X				xx->xl_name,xx->xl_name);
X			xlist_error = 1;
X			continue;
X		}
X		printf("%s  type: %04x seg: %04x value: %08lx\n",
X			xx->xl_name,
X			xx->xl_type,
X			xx->xl_seg,
X			xx->xl_value);
X		xx++;
X	}
X
X	if(xlist_error)
X	{
X		fprintf(stderr,"%s NOT produced\n",XENIX_UXLST);
X		exit(1);
X	}
X
X	if((kludge = fopen(XENIX_UXLST,"w")) == NULL)	/* scratch/create */
X		uxlst_write_error(-1);
X	fclose(kludge);
X
X	if((fduxlst = open(XENIX_UXLST,O_WRONLY,0)) < 0)
X		uxlst_write_error(fduxlst);
X
X	if(stat(XENIX_KERNEL,&xstat) < 0)
X	{
X		fputs("cannot stat ",stderr);
X		perror(XENIX_KERNEL);
X		exit(1);
X	}
X
X	if((itmp = write(fduxlst,&xstat,sizeof(xstat))) != sizeof(xstat))
X		uxlst_write_error(itmp);
X
X	if((itmp = write(fduxlst,uxlst,sizeof(uxlst))) != sizeof(uxlst))
X		uxlst_write_error(itmp);
X
X	unique = UXLST_UNIQUE;
X	if((itmp = write(fduxlst,&unique,sizeof(unique))) != sizeof(unique))
X		uxlst_write_error(itmp);
X
X	close(fduxlst);
X	exit(0);
X}	/* end of main */
X
X/* vi: set tabstop=4 shiftwidth=4: */
SHAR_EOF
chmod 0644 uxlst.c || echo "restore of uxlst.c fails"
fi
if test -f renice.c; then echo "File renice.c exists"; else
echo "x - extracting renice.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > renice.c &&
X/* CHK=0x98BA */
X/*+-------------------------------------------------------------------------
X	renice.c -- SCO Xenix V/286
X
X  Defined functions:
X	kread(caddr,kaddr,nbytes)
X	kwrite(kaddr,caddr,nbytes)
X	main(argc,argv)
X	renice(pid,value)
X	kinit()
X	usage()
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:10-27-1988-11:15-wht-use uxlst facilities to speed up program */
X/*:10-26-1988-08:56-wht-fix it */
X/*
X * This program was written by me, Mike "Ford" Ditto, and
X * I hereby release it into the public domain in the interest
X * of promoting the development of free, quality software
X * for the hackers and users of the world.
X *
X * Feel free to use, copy, modify, improve, and redistribute
X * this program, but keep in mind the spirit of this
X * contribution; always provide source, and always allow
X * free redistribution (shareware is fine with me).  If
X * you use a significant part of this code in a program of
X * yours, I would appreciate being given the appropriate
X * amount of credit.
X *				-=] Ford [=-
X *
X *
X *	Modifications
X *	3/3/88	Ported to Plexus Unix System V	pigs!haugj@rpp386.uucp
X *	3/3/88	Ported to SCO Xenix System V	jfh@rpp386.uucp
X *	10/26/88 SCO Xenix V/286 and BSD 4 syntax wht
X ************************************************************/
X
X#include <stdio.h>
X#include <fcntl.h>
X#include <ctype.h>
X#include <errno.h>
X#include <sys/types.h>
X#include <sys/param.h>
X#include <sys/proc.h>
X#include <sys/var.h>
X#include <a.out.h>
X#include "uxlst.h"
X#include "libuxlst.h"
X#include "libkmem.h"
X
Xlong lseek();
Xvoid perror();
Xvoid exit();
X
Xvoid kread();
Xvoid kwrite();
X
Xchar buf[BUFSIZ];
X
Xint myuid;
Xint NPROC;
Xstruct proc proctab;
Xstruct var v;
X
Xvoid
Xusage()
X{
X	fprintf(stderr,"usage: renice <nice> pid ...\n");
X	fprintf(stderr,"<nice> must be in range of -20 through 19\n");
X	exit(-1);
X}
X
X
X/* change the nice value of process (value from 0 to 39) */
Xint
Xrenice(pid,value)
Xint pid,value;
X{
X	register i;
X	int oldnice;
X	long kaddr;
X
X	if(value > 39)
X		value = 39;
X	if(value < 0)
X		value = 0;
X
X	for (i=0 ; i<NPROC ; ++i)
X	{
X	    kaddr = procaddr + (i * sizeof(struct proc));
X		kread((char *)&proctab,kaddr,(long)sizeof(proctab));
X		if(proctab.p_pid == pid)
X		{
X			if(myuid && ((myuid != proctab.p_uid) || (value < proctab.p_nice)))
X			{
X				errno = EACCES;
X				sprintf(buf,"%d",pid);
X				perror(buf);
X				return(1);
X			}
X
X			oldnice = proctab.p_nice;
X			proctab.p_nice = value;
X
X			kaddr = procaddr + (i * sizeof(struct proc))
X			    + ((char *)&proctab.p_nice - (char *)&proctab),
X			kwrite(kaddr,(char *)&proctab.p_nice,(long)sizeof(proctab.p_nice));
X			printf("%d: old priority %d, new priority %d\n",
X				pid,oldnice - 20,value - 20);
X			return(0);
X		}
X	}
X
X	printf("%d: no such process\n",pid);
X	return(1);
X}
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
Xint		status = 0;
Xint		pid;
Xint		value;
Xint		iargv;
X
X	uxlst_read();
X	kinit(1);
X	kread((char *)&v,vaddr,(long)sizeof(v));
X	NPROC = v.v_proc;
X	myuid = getuid();
X	setuid(myuid);
X
X	if(argc < 3)
X		usage();
X
X	value = atoi(argv[1]);
X	if(value < -20)
X		usage();
X	if(value > 19)
X		usage();
X
X	value += 20;
X
X	for(iargv = 2; iargv < argc; iargv++)
X	{
X		pid = atoi(argv[iargv]);
X		renice(pid,value);
X	}
X
X	exit(0);
X}
X
X/* vi: set tabstop=4 shiftwidth=4: */
SHAR_EOF
chmod 0644 renice.c || echo "restore of renice.c fails"
fi
exit 0
----------------------------------------------------------
W. Tucker ...!gatech!{emory,kd4nc}!tridom!wht