Started porting. Getting CPU usage now working. Took a lot of code from
* sysdeps/sun4/{open.c, cpu.c}: Started porting. Getting CPU usage now working. Took a lot of code from top-3.4.
This commit is contained in:
@@ -19,13 +19,162 @@
|
||||
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA. */
|
||||
|
||||
#include <glibtop.h>
|
||||
#include <glibtop/open.h>
|
||||
|
||||
struct nlist _glibtop_nlist[] = {
|
||||
#ifdef i386
|
||||
{ "avenrun" }, /* 0 */
|
||||
{ "ccpu" }, /* 1 */
|
||||
{ "mpid" }, /* 2 */
|
||||
{ "nproc" }, /* 3 */
|
||||
{ "proc" }, /* 4 */
|
||||
{ "total" }, /* 5 */
|
||||
{ "cp_time" }, /* 6 */
|
||||
{ "pages" }, /* 7 */
|
||||
{ "epages" }, /* 8 */
|
||||
#else
|
||||
{ "_avenrun" }, /* 0 */
|
||||
{ "_ccpu" }, /* 1 */
|
||||
{ "_mpid" }, /* 2 */
|
||||
{ "_nproc" }, /* 3 */
|
||||
{ "_proc" }, /* 4 */
|
||||
{ "_total" }, /* 5 */
|
||||
{ "_cp_time" }, /* 6 */
|
||||
{ "_pages" }, /* 7 */
|
||||
{ "_epages" }, /* 8 */
|
||||
#ifdef MULTIPROCESSOR
|
||||
{ "_ncpu" },
|
||||
{ "_mp_time" },
|
||||
#endif
|
||||
#endif
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */
|
||||
|
||||
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
|
||||
|
||||
void
|
||||
glibtop_open (glibtop *server, const char *program_name)
|
||||
{
|
||||
/* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */
|
||||
|
||||
memset (server, 0, sizeof (glibtop));
|
||||
server->name = program_name;
|
||||
|
||||
server->machine.uid = getuid ();
|
||||
server->machine.euid = geteuid ();
|
||||
server->machine.gid = getgid ();
|
||||
server->machine.egid = getegid ();
|
||||
|
||||
/* initialize the kernel interface */
|
||||
|
||||
server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "libgtop");
|
||||
|
||||
if (server->machine.kd == NULL)
|
||||
glibtop_error__r (server, "kvm_open: %s", strerror (errno));
|
||||
|
||||
/* get the list of symbols we want to access in the kernel */
|
||||
|
||||
server->machine.nlist_count = kvm_nlist (server->machine.kd, _glibtop_nlist);
|
||||
|
||||
if (server->machine.nlist_count < 0)
|
||||
glibtop_error__r (server, "nlist: %s", strerror (errno));
|
||||
|
||||
#ifdef MULTIPROCESSOR
|
||||
/* were ncpu and xp_time not found in the nlist? */
|
||||
|
||||
if ((server->machine.nlist_count > 0) && (_glibtop_nlist[X_NCPU].n_type == 0) &&
|
||||
(_glibtop_nlist[X_MP_TIME].n_type == 0)) {
|
||||
/* we were compiled on an MP system but we are not running on one */
|
||||
/* so we will pretend this didn't happen and set ncpu = 1 */
|
||||
server->machine.nlist_count -= 2;
|
||||
server->machine.ncpu = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef solbourne
|
||||
{
|
||||
unsigned int status, type;
|
||||
|
||||
/* Get the number of CPUs on this system. */
|
||||
syscall(SYS_getcpustatus, &status, &server->machine.ncpu, &type);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Make sure all of the symbols were found. */
|
||||
|
||||
if ((server->machine.nlist_count > 0) &&
|
||||
(_glibtop_check_nlist (server, _glibtop_nlist) > 0))
|
||||
_exit (1);
|
||||
|
||||
/* Drop priviledges. */
|
||||
|
||||
if (setreuid (server->machine.euid, server->machine.uid))
|
||||
_exit (1);
|
||||
|
||||
if (setregid (server->machine.egid, server->machine.gid))
|
||||
_exit (1);
|
||||
|
||||
/* !!! END OF SUID ROOT PART !!! */
|
||||
|
||||
/* Our effective uid is now those of the user invoking the server,
|
||||
so we do no longer have any priviledges.
|
||||
*/
|
||||
|
||||
/* NOTE: On SunOS, we do not need to be suid root, we just need to be sgid kmem.
|
||||
* The server will only use setegid() to get back it's priviledges, so it
|
||||
* will fail if it is suid root and not sgid kmem.
|
||||
*/
|
||||
}
|
||||
|
||||
/* Used internally. Returns number of symbols that cannot be found in the nlist. */
|
||||
|
||||
int
|
||||
_glibtop_check_nlist (void *server, register struct nlist *nlst)
|
||||
{
|
||||
register int not_found;
|
||||
|
||||
/* check to see if we got ALL the symbols we requested */
|
||||
/* this will write one line to stderr for every symbol not found */
|
||||
|
||||
not_found = 0;
|
||||
|
||||
while (nlst->n_name != NULL) {
|
||||
|
||||
#ifdef i386
|
||||
if (nlst->n_value == 0) {
|
||||
glibtop_error__r (server, "kernel: no symbol named `%s'", nlst->n_name);
|
||||
not_found++;
|
||||
}
|
||||
#else
|
||||
if (nlst->n_type == 0) {
|
||||
glibtop_error__r (server, "kernel: no symbol named `%s'", nlst->n_name);
|
||||
not_found++;
|
||||
}
|
||||
#endif
|
||||
|
||||
nlst++;
|
||||
}
|
||||
|
||||
return not_found;
|
||||
}
|
||||
|
||||
/* Used internally. Fetches value from kernel. */
|
||||
|
||||
int
|
||||
_glibtop_getkval (void *void_server, unsigned long offset, int *ptr, int size, char *refstr)
|
||||
{
|
||||
glibtop *server = (glibtop *) void_server;
|
||||
|
||||
if (kvm_read (server->machine.kd, offset, ptr, size) != size)
|
||||
{
|
||||
if (*refstr == '!') return 0;
|
||||
|
||||
glibtop_error__r (server, "kvm_read(%s): %s",
|
||||
refstr, strerror (errno));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user