Blame sysdeps/linux/procuid.c

Packit d37888
/* Copyright (C) 1998-99 Martin Baulig
Packit d37888
   This file is part of LibGTop 1.0.
Packit d37888
Packit d37888
   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Packit d37888
Packit d37888
   LibGTop is free software; you can redistribute it and/or modify it
Packit d37888
   under the terms of the GNU General Public License as published by
Packit d37888
   the Free Software Foundation; either version 2 of the License,
Packit d37888
   or (at your option) any later version.
Packit d37888
Packit d37888
   LibGTop is distributed in the hope that it will be useful, but WITHOUT
Packit d37888
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit d37888
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit d37888
   for more details.
Packit d37888
Packit d37888
   You should have received a copy of the GNU General Public License
Packit d37888
   along with LibGTop; see the file COPYING. If not, write to the
Packit d37888
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit d37888
   Boston, MA 02110-1301, USA.
Packit d37888
*/
Packit d37888
Packit d37888
#include <config.h>
Packit d37888
#include <glibtop.h>
Packit d37888
#include <glibtop/error.h>
Packit d37888
#include <glibtop/procuid.h>
Packit d37888
Packit d37888
#include "glibtop_private.h"
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_proc_uid =
Packit d37888
(1L << GLIBTOP_PROC_UID_UID) + (1L << GLIBTOP_PROC_UID_EUID) +
Packit d37888
(1L << GLIBTOP_PROC_UID_GID) + (1L << GLIBTOP_PROC_UID_EGID);
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_proc_uid_stat =
Packit d37888
(1L << GLIBTOP_PROC_UID_PID) + (1L << GLIBTOP_PROC_UID_PPID) +
Packit d37888
(1L << GLIBTOP_PROC_UID_PGRP) + (1L << GLIBTOP_PROC_UID_SESSION) +
Packit d37888
(1L << GLIBTOP_PROC_UID_TTY) + (1L << GLIBTOP_PROC_UID_TPGID) +
Packit d37888
(1L << GLIBTOP_PROC_UID_PRIORITY) + (1L << GLIBTOP_PROC_UID_NICE);
Packit d37888
Packit d37888
Packit d37888
/* Init function. */
Packit d37888
Packit d37888
void
Packit d37888
_glibtop_init_proc_uid_s (glibtop *server)
Packit d37888
{
Packit d37888
	server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid |
Packit d37888
		_glibtop_sysdeps_proc_uid_stat;
Packit d37888
}
Packit d37888
Packit d37888
/* Provides detailed information about a process. */
Packit d37888
Packit d37888
void
Packit d37888
glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
Packit d37888
{
Packit d37888
	char buffer [BUFSIZ], *p;
Packit d37888
Packit d37888
	memset (buf, 0, sizeof (glibtop_proc_uid));
Packit d37888
Packit d37888
	if (proc_status_to_buffer(buffer, sizeof buffer, pid))
Packit d37888
		return;
Packit d37888
Packit d37888
	/* Search substring 'Pid:' */
Packit d37888
Packit d37888
	p = strstr (buffer, "\nPid:");
Packit d37888
	if (!p) return;
Packit d37888
Packit d37888
	p = skip_token (p); /* "Pid:" */
Packit d37888
	buf->pid = strtol (p, &p, 0);
Packit d37888
Packit d37888
	p = skip_token (p); /* "PPid:" */
Packit d37888
	buf->ppid = strtol (p, &p, 0);
Packit d37888
Packit d37888
	/* Maybe future Linux versions place something between
Packit d37888
	 * "PPid" and "Uid", so we catch this here. */
Packit d37888
	p = strstr (p, "\nUid:");
Packit d37888
	if (!p) return;
Packit d37888
Packit d37888
	p = skip_token (p); /* "Uid:" */
Packit d37888
	buf->uid  = strtol (p, &p, 0);
Packit d37888
	buf->euid = strtol (p, &p, 0);
Packit d37888
Packit d37888
	/* We don't know how many entries on the "Uid:" line
Packit d37888
	 * future Linux version will have, so we catch this here. */
Packit d37888
	p = strstr (p, "\nGid:");
Packit d37888
	if (!p) return;
Packit d37888
Packit d37888
	p = skip_token (p); /* "Gid:" */
Packit d37888
	buf->gid  = strtol (p, &p, 0);
Packit d37888
	buf->egid = strtol (p, &p, 0);
Packit d37888
Packit d37888
	buf->flags = _glibtop_sysdeps_proc_uid;
Packit d37888
Packit d37888
	if (proc_stat_to_buffer(buffer, sizeof buffer, pid))
Packit d37888
		return;
Packit d37888
Packit d37888
	p = proc_stat_after_cmd (buffer);
Packit d37888
	if (!p) return;
Packit d37888
Packit d37888
	p = skip_multiple_token (p, 2);
Packit d37888
Packit d37888
	buf->pgrp    = strtol (p, &p, 0);
Packit d37888
	buf->session = strtol (p, &p, 0);
Packit d37888
	buf->tty     = strtol (p, &p, 0);
Packit d37888
	buf->tpgid   = strtol (p, &p, 0);
Packit d37888
Packit d37888
	p = skip_multiple_token (p, 9);
Packit d37888
Packit d37888
	buf->priority = strtol (p, &p, 0);
Packit d37888
	buf->nice     = strtol (p, &p, 0);
Packit d37888
Packit d37888
	if (buf->tty == 0)
Packit d37888
		/* the old notty val, update elsewhere bef. moving to 0 */
Packit d37888
		buf->tty = -1;
Packit d37888
Packit d37888
	if (server->os_version_code < LINUX_VERSION_CODE(1,3,39)) {
Packit d37888
		/* map old meanings to new */
Packit d37888
		buf->priority = 2*15 - buf->priority;
Packit d37888
		buf->nice = 15 - buf->nice;
Packit d37888
	}
Packit d37888
	else if (server->os_version_code < LINUX_VERSION_CODE(1,1,30) && buf->tty != -1)
Packit d37888
		/* when tty wasn't full devno */
Packit d37888
		buf->tty = 4*0x100 + buf->tty;
Packit d37888
Packit d37888
	buf->flags |= _glibtop_sysdeps_proc_uid_stat;
Packit d37888
}