mirror of https://github.com/lindes/ttyload.git
now using sysget() insetad of /dev/kmem, which means no suid or sgid. yay!
This commit is contained in:
parent
c8686ba144
commit
86f03ae0be
|
@ -7,62 +7,24 @@
|
|||
|
||||
#include "ttyload.h"
|
||||
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/sysmp.h> /* for sysmp() */
|
||||
#include <sys/types.h> /* for lseek() */
|
||||
#include <unistd.h> /* for lseek() */
|
||||
|
||||
// where's /dev/kmem? (just in case this ever changed)
|
||||
#define KMEMFILE "/dev/kmem"
|
||||
#include <sys/types.h> /* sys/syget.h needs this */
|
||||
#include <sys/sysget.h> /* for sysget() */
|
||||
|
||||
void getload(load_list *loadavgs)
|
||||
{
|
||||
static int kmemfd = -1;
|
||||
static load_t loadaddr;
|
||||
sgt_cookie_t cookie;
|
||||
|
||||
if(kmemfd < 0) /* initialize, first-time only: */
|
||||
{
|
||||
/* first, figure out where in /dev/kmem space the load average
|
||||
* data gets stored... */
|
||||
loadaddr = sysmp(MP_KERNADDR, MPKA_AVENRUN);
|
||||
SGT_COOKIE_INIT(&cookie);
|
||||
SGT_COOKIE_SET_KSYM(&cookie, "avenrun");
|
||||
|
||||
/* and make sure that worked: */
|
||||
if(loadaddr == -1)
|
||||
{
|
||||
perror("Couldn't determine load address");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* then, if that worked, try to open kmem: */
|
||||
kmemfd = open(KMEMFILE, O_RDONLY);
|
||||
|
||||
/* and verify: */
|
||||
if(kmemfd < 0)
|
||||
{
|
||||
perror("Couldn't open " KMEMFILE);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* assuming the above went well, we now pull out the load average
|
||||
* data from kmem via a seek/read pair: */
|
||||
|
||||
/* first seek: */
|
||||
if((lseek(kmemfd, loadaddr, SEEK_SET)) != loadaddr)
|
||||
{
|
||||
perror("Couldn't seek to load address");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* then read: */
|
||||
if((read(kmemfd,loadavgs,3*sizeof(load_t))) != 3*sizeof(load_t))
|
||||
{
|
||||
perror("Couldn't read load data");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* and since IRIX uses an integer data type for this stuff, which is
|
||||
/* since IRIX uses an integer data type for this stuff, which is
|
||||
* 1024 times the floating-point equiv, we don't need to do any
|
||||
* conversions, because that's what load_list uses (since ttyload
|
||||
* was originally written for IRIX in the first place). */
|
||||
if(sysget(SGT_KSYM, (char*)loadavgs, 3 * sizeof(load_t),
|
||||
SGT_READ | SGT_SUM, &cookie) != (3 * sizeof(load_t)))
|
||||
{
|
||||
perror("Couldn't read load data, sysget failed");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue