Major code cleanups. We now use open () and read ().
This commit is contained in:
@@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
|
#ifdef _IN_LIBGTOP
|
||||||
|
|
||||||
static inline char *
|
static inline char *
|
||||||
skip_token (const char *p)
|
skip_token (const char *p)
|
||||||
{
|
{
|
||||||
@@ -35,6 +37,17 @@ skip_token (const char *p)
|
|||||||
return (char *)p;
|
return (char *)p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline char *
|
||||||
|
skip_multiple_token (const char *p, int count)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < count; i++)
|
||||||
|
p = skip_token (p);
|
||||||
|
|
||||||
|
return (char *)p;
|
||||||
|
}
|
||||||
|
|
||||||
static inline char *
|
static inline char *
|
||||||
skip_line (const char *p)
|
skip_line (const char *p)
|
||||||
{
|
{
|
||||||
@@ -42,6 +55,57 @@ skip_line (const char *p)
|
|||||||
return (char *) p++;
|
return (char *) p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
|
||||||
|
{
|
||||||
|
char filename [BUFSIZ];
|
||||||
|
int fd, len;
|
||||||
|
|
||||||
|
sprintf (filename, fmt, pid);
|
||||||
|
|
||||||
|
fd = open (filename, O_RDONLY);
|
||||||
|
if (fd < 0) return -1;
|
||||||
|
|
||||||
|
len = read (fd, buffer, BUFSIZ-1);
|
||||||
|
if (len < 0) return -1;
|
||||||
|
|
||||||
|
close (fd);
|
||||||
|
|
||||||
|
buffer [len] = '\0';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
proc_stat_to_buffer (char *buffer, pid_t pid)
|
||||||
|
{
|
||||||
|
return proc_file_to_buffer (buffer, "/proc/%d/stat", pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
proc_status_to_buffer (char *buffer, pid_t pid)
|
||||||
|
{
|
||||||
|
return proc_file_to_buffer (buffer, "/proc/%d/status", pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
proc_statm_to_buffer (char *buffer, pid_t pid)
|
||||||
|
{
|
||||||
|
return proc_file_to_buffer (buffer, "/proc/%d/statm", pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *
|
||||||
|
proc_stat_after_cmd (char *p)
|
||||||
|
{
|
||||||
|
p = strrchr (p, ')');
|
||||||
|
if (!p) return p;
|
||||||
|
|
||||||
|
*p++ = '\0';
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#define GLIBTOP_SUID_CPU 0
|
#define GLIBTOP_SUID_CPU 0
|
||||||
#define GLIBTOP_SUID_MEM 0
|
#define GLIBTOP_SUID_MEM 0
|
||||||
#define GLIBTOP_SUID_SWAP 0
|
#define GLIBTOP_SUID_SWAP 0
|
||||||
|
@@ -42,44 +42,34 @@ glibtop_init_proc_kernel_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
|
glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_KERNEL, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_KERNEL, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_kernel));
|
memset (buf, 0, sizeof (glibtop_proc_kernel));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 6);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->k_flags = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->min_flt = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->cmin_flt = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
buf->maj_flt = strtoul (p, &p, 0);
|
||||||
|
buf->cmaj_flt = strtoul (p, &p, 0);
|
||||||
|
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
p = skip_multiple_token (p, 15);
|
||||||
"%*c %*d %*d %*d %*d %*d %Lu %Lu %Lu %Lu %Lu "
|
|
||||||
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %*u "
|
|
||||||
"%*u %*u %*u %*u %*u %Lu %Lu %*d %*d %*d %*d %Lu",
|
|
||||||
&buf->k_flags, &buf->min_flt, &buf->cmin_flt,
|
|
||||||
&buf->maj_flt, &buf->cmaj_flt, &buf->kstk_esp,
|
|
||||||
&buf->kstk_eip, &buf->wchan);
|
|
||||||
|
|
||||||
fclose (f);
|
buf->kstk_esp = strtoul (p, &p, 0);
|
||||||
|
buf->kstk_eip = strtoul (p, &p, 0);
|
||||||
|
|
||||||
|
p = skip_multiple_token (p, 4);
|
||||||
|
|
||||||
|
buf->nwchan = strtoul (p, &p, 0);
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_kernel;
|
buf->flags = _glibtop_sysdeps_proc_kernel;
|
||||||
}
|
}
|
||||||
|
@@ -24,9 +24,12 @@
|
|||||||
#include <glibtop/procmem.h>
|
#include <glibtop/procmem.h>
|
||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_proc_mem =
|
static const unsigned long _glibtop_sysdeps_proc_mem =
|
||||||
(1 << GLIBTOP_PROC_MEM_SIZE) + (1 << GLIBTOP_PROC_MEM_VSIZE) +
|
(1 << GLIBTOP_PROC_MEM_VSIZE) + (1 << GLIBTOP_PROC_MEM_RSS) +
|
||||||
(1 << GLIBTOP_PROC_MEM_RESIDENT) + (1 << GLIBTOP_PROC_MEM_SHARE) +
|
(1 << GLIBTOP_PROC_MEM_RSS_RLIM);
|
||||||
(1 << GLIBTOP_PROC_MEM_RSS) + (1 << GLIBTOP_PROC_MEM_RSS_RLIM);
|
|
||||||
|
static const unsigned long _glibtop_sysdeps_proc_mem_statm =
|
||||||
|
(1 << GLIBTOP_PROC_MEM_SIZE) + (1 << GLIBTOP_PROC_MEM_RESIDENT) +
|
||||||
|
(1 << GLIBTOP_PROC_MEM_SHARE);
|
||||||
|
|
||||||
#ifndef LOG1024
|
#ifndef LOG1024
|
||||||
#define LOG1024 10
|
#define LOG1024 10
|
||||||
@@ -45,7 +48,8 @@ glibtop_init_proc_mem_s (glibtop *server)
|
|||||||
{
|
{
|
||||||
register int pagesize;
|
register int pagesize;
|
||||||
|
|
||||||
server->sysdeps.proc_mem = _glibtop_sysdeps_proc_mem;
|
server->sysdeps.proc_mem = _glibtop_sysdeps_proc_mem |
|
||||||
|
_glibtop_sysdeps_proc_mem_statm;
|
||||||
|
|
||||||
/* get the page size with "getpagesize" and calculate pageshift
|
/* get the page size with "getpagesize" and calculate pageshift
|
||||||
* from it */
|
* from it */
|
||||||
@@ -62,64 +66,37 @@ glibtop_init_proc_mem_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
|
glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_mem));
|
memset (buf, 0, sizeof (glibtop_proc_mem));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 20);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->vsize = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->rss = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->rss_rlim = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
|
||||||
"%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "
|
|
||||||
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %Lu "
|
|
||||||
"%Lu %Lu", &buf->vsize, &buf->rss, &buf->rss_rlim);
|
|
||||||
|
|
||||||
fclose (f);
|
buf->flags = _glibtop_sysdeps_proc_mem;
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/statm", pid);
|
if (proc_statm_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
buf->size = strtoul (buffer, &p, 0);
|
||||||
|
buf->resident = strtoul (p, &p, 0);
|
||||||
sscanf (input, "%Lu %Lu %Lu",
|
buf->share = strtoul (p, &p, 0);
|
||||||
&buf->size, &buf->resident, &buf->share);
|
|
||||||
|
|
||||||
buf->size <<= pageshift;
|
buf->size <<= pageshift;
|
||||||
buf->resident <<= pageshift;
|
buf->resident <<= pageshift;
|
||||||
buf->share <<= pageshift;
|
buf->share <<= pageshift;
|
||||||
buf->rss <<= pageshift;
|
buf->rss <<= pageshift;
|
||||||
|
|
||||||
fclose (f);
|
buf->flags |= _glibtop_sysdeps_proc_mem_statm;
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_mem;
|
|
||||||
}
|
}
|
||||||
|
@@ -24,14 +24,17 @@
|
|||||||
#include <glibtop/procsegment.h>
|
#include <glibtop/procsegment.h>
|
||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_proc_segment =
|
static const unsigned long _glibtop_sysdeps_proc_segment =
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_TEXT_RSS) +
|
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_SHLIB_RSS) +
|
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_DATA_RSS) +
|
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_DIRTY_SIZE) +
|
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_START_CODE) +
|
(1 << GLIBTOP_PROC_SEGMENT_START_CODE) +
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_END_CODE) +
|
(1 << GLIBTOP_PROC_SEGMENT_END_CODE) +
|
||||||
(1 << GLIBTOP_PROC_SEGMENT_START_STACK);
|
(1 << GLIBTOP_PROC_SEGMENT_START_STACK);
|
||||||
|
|
||||||
|
static const unsigned long _glibtop_sysdeps_proc_segment_statm =
|
||||||
|
(1 << GLIBTOP_PROC_SEGMENT_TEXT_RSS) +
|
||||||
|
/* Disabled due to bug in the Linux Kernel. */
|
||||||
|
/* (1 << GLIBTOP_PROC_SEGMENT_SHLIB_RSS) + */
|
||||||
|
(1 << GLIBTOP_PROC_SEGMENT_DATA_RSS) +
|
||||||
|
(1 << GLIBTOP_PROC_SEGMENT_DIRTY_SIZE);
|
||||||
|
|
||||||
#ifndef LOG1024
|
#ifndef LOG1024
|
||||||
#define LOG1024 10
|
#define LOG1024 10
|
||||||
#endif
|
#endif
|
||||||
@@ -49,7 +52,8 @@ glibtop_init_proc_segment_s (glibtop *server)
|
|||||||
{
|
{
|
||||||
register int pagesize;
|
register int pagesize;
|
||||||
|
|
||||||
server->sysdeps.proc_segment = _glibtop_sysdeps_proc_segment;
|
server->sysdeps.proc_segment = _glibtop_sysdeps_proc_segment |
|
||||||
|
_glibtop_sysdeps_proc_segment_statm;
|
||||||
|
|
||||||
/* get the page size with "getpagesize" and calculate pageshift
|
/* get the page size with "getpagesize" and calculate pageshift
|
||||||
* from it */
|
* from it */
|
||||||
@@ -67,66 +71,43 @@ void
|
|||||||
glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
|
glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
|
||||||
pid_t pid)
|
pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SEGMENT, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SEGMENT, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_segment));
|
memset (buf, 0, sizeof (glibtop_proc_segment));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 23);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->start_code = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->end_code = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->start_stack = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
|
||||||
"%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "
|
|
||||||
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %*u "
|
|
||||||
"%*u %*u %Lu %Lu %Lu", &buf->start_code,
|
|
||||||
&buf->end_code, &buf->start_stack);
|
|
||||||
|
|
||||||
fclose (f);
|
buf->flags = _glibtop_sysdeps_proc_segment;
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/statm", pid);
|
if (proc_statm_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = skip_multiple_token (buffer, 3);
|
||||||
|
|
||||||
sscanf (input, "%*d %*d %*d %Lu %Lu %Lu %Lu",
|
/* This doesn't work very well due to a bug in the Linux kernel.
|
||||||
&buf->text_rss, &buf->shlib_rss,
|
* I'll submit a patch to the kernel mailing list soon. */
|
||||||
&buf->data_rss, &buf->dirty_size);
|
|
||||||
|
buf->text_rss = strtoul (p, &p, 0);
|
||||||
|
buf->shlib_rss = strtoul (p, &p, 0);
|
||||||
|
buf->data_rss = strtoul (p, &p, 0);
|
||||||
|
buf->dirty_size = strtoul (p, &p, 0);
|
||||||
|
|
||||||
buf->text_rss <<= pageshift;
|
buf->text_rss <<= pageshift;
|
||||||
buf->shlib_rss <<= pageshift;
|
buf->shlib_rss <<= pageshift;
|
||||||
buf->data_rss <<= pageshift;
|
buf->data_rss <<= pageshift;
|
||||||
buf->dirty_size <<= pageshift;
|
buf->dirty_size <<= pageshift;
|
||||||
|
|
||||||
fclose (f);
|
buf->flags |= _glibtop_sysdeps_proc_segment_statm;
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_segment;
|
|
||||||
}
|
}
|
||||||
|
@@ -40,42 +40,24 @@ glibtop_init_proc_signal_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
|
glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SIGNAL, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SIGNAL, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_signal));
|
memset (buf, 0, sizeof (glibtop_proc_signal));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 28);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->signal = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->blocked = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->sigignore = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
buf->sigcatch = strtoul (p, &p, 0);
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
|
||||||
"%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "
|
|
||||||
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %*u "
|
|
||||||
"%*u %*u %*u %*u %*u %*u %*u %Lu %Lu %Lu %Lu",
|
|
||||||
&buf->signal, &buf->blocked, &buf->sigignore,
|
|
||||||
&buf->sigcatch);
|
|
||||||
|
|
||||||
fclose (f);
|
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_signal;
|
buf->flags = _glibtop_sysdeps_proc_signal;
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,9 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_proc_state =
|
static const unsigned long _glibtop_sysdeps_proc_state =
|
||||||
(1 << GLIBTOP_PROC_STATE_CMD) + (1 << GLIBTOP_PROC_STATE_STATE) +
|
(1 << GLIBTOP_PROC_STATE_CMD) + (1 << GLIBTOP_PROC_STATE_STATE);
|
||||||
|
|
||||||
|
static const unsigned long _glibtop_sysdeps_proc_state_uid =
|
||||||
(1 << GLIBTOP_PROC_STATE_UID) + (1 << GLIBTOP_PROC_STATE_GID);
|
(1 << GLIBTOP_PROC_STATE_UID) + (1 << GLIBTOP_PROC_STATE_GID);
|
||||||
|
|
||||||
/* Init function. */
|
/* Init function. */
|
||||||
@@ -34,7 +36,8 @@ static const unsigned long _glibtop_sysdeps_proc_state =
|
|||||||
void
|
void
|
||||||
glibtop_init_proc_state_s (glibtop *server)
|
glibtop_init_proc_state_s (glibtop *server)
|
||||||
{
|
{
|
||||||
server->sysdeps.proc_state = _glibtop_sysdeps_proc_state;
|
server->sysdeps.proc_state = _glibtop_sysdeps_proc_state |
|
||||||
|
_glibtop_sysdeps_proc_state_uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Provides detailed information about a process. */
|
/* Provides detailed information about a process. */
|
||||||
@@ -42,52 +45,45 @@ glibtop_init_proc_state_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
|
glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
struct stat statb;
|
struct stat statb;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_STATE, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_STATE, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_state));
|
memset (buf, 0, sizeof (glibtop_proc_state));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
|
||||||
|
|
||||||
/* IMPORTANT NOTICE: For security reasons it is extremely important
|
/* IMPORTANT NOTICE: For security reasons it is extremely important
|
||||||
* that the 'uid' and 'gid' fields have correct
|
* that the 'uid' and 'gid' fields have correct
|
||||||
* values; NEVER set their flags values if this
|
* values; NEVER set their flags values if this
|
||||||
* is not the case !!! */
|
* is not the case !!! */
|
||||||
|
|
||||||
if (stat (input, &statb)) return;
|
sprintf (buffer, "/proc/%d/stat", pid);
|
||||||
|
|
||||||
/* For security reasons we use stat () that is more failsafe than sscanf (). */
|
if (stat (buffer, &statb))
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* For security reasons we use stat () since it is
|
||||||
|
* more failsafe than parsing the file. */
|
||||||
|
|
||||||
buf->uid = statb.st_uid;
|
buf->uid = statb.st_uid;
|
||||||
buf->gid = statb.st_gid;
|
buf->gid = statb.st_gid;
|
||||||
|
|
||||||
f = fopen (input, "r");
|
buf->flags = _glibtop_sysdeps_proc_state_uid;
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
/* Now we read the remaining fields. */
|
||||||
|
|
||||||
if (nread < 0) {
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
fclose (f);
|
p = strrchr (buffer, ')'); *p = '\0';
|
||||||
|
buf->state = p [2];
|
||||||
|
|
||||||
input [nread] = 0;
|
p = skip_token (buffer); p++; /* pid */
|
||||||
|
if (*p++ != '(')
|
||||||
|
glibtop_error_r (server, "Bad data in /proc/%d/stat", pid);
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
strncpy (buf->cmd, p, sizeof (buf->cmd)-1);
|
||||||
|
buf->cmd [sizeof (buf->cmd)-1] = 0;
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->flags |= _glibtop_sysdeps_proc_state;
|
||||||
tmp = strrchr (input, ')');
|
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
|
||||||
memset (buf->cmd, 0, sizeof (buf->cmd));
|
|
||||||
sscanf (input, "%d (%39c", &pid, buf->cmd);
|
|
||||||
sscanf(tmp + 2, "%c", &buf->state); /* skip space after ')' too */
|
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_state;
|
|
||||||
}
|
}
|
||||||
|
@@ -24,10 +24,11 @@
|
|||||||
#include <glibtop/proctime.h>
|
#include <glibtop/proctime.h>
|
||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_proc_time =
|
static const unsigned long _glibtop_sysdeps_proc_time =
|
||||||
(1 << GLIBTOP_PROC_TIME_START_TIME) + (1 << GLIBTOP_PROC_TIME_UTIME) +
|
(1 << GLIBTOP_PROC_TIME_UTIME) + (1 << GLIBTOP_PROC_TIME_CUTIME) +
|
||||||
(1 << GLIBTOP_PROC_TIME_STIME) + (1 << GLIBTOP_PROC_TIME_CUTIME) +
|
(1 << GLIBTOP_PROC_TIME_STIME) + (1 << GLIBTOP_PROC_TIME_CSTIME) +
|
||||||
(1 << GLIBTOP_PROC_TIME_CSTIME) + (1 << GLIBTOP_PROC_TIME_TIMEOUT) +
|
(1 << GLIBTOP_PROC_TIME_RTIME) + (1 << GLIBTOP_PROC_TIME_FREQUENCY) +
|
||||||
(1 << GLIBTOP_PROC_TIME_IT_REAL_VALUE);
|
(1 << GLIBTOP_PROC_TIME_TIMEOUT) + (1 << GLIBTOP_PROC_TIME_IT_REAL_VALUE) +
|
||||||
|
(1 << GLIBTOP_PROC_TIME_START_TIME);
|
||||||
|
|
||||||
/* Init function. */
|
/* Init function. */
|
||||||
|
|
||||||
@@ -42,41 +43,34 @@ glibtop_init_proc_time_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
|
glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_TIME, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_TIME, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_time));
|
memset (buf, 0, sizeof (glibtop_proc_time));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 11);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->utime = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->stime = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->cutime = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
buf->cstime = strtoul (p, &p, 0);
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
|
||||||
"%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "
|
|
||||||
"%Lu %Lu %Lu %Lu %*d %*d %Lu %Lu %Lu",
|
|
||||||
&buf->utime, &buf->stime, &buf->cutime, &buf->cstime,
|
|
||||||
&buf->timeout, &buf->it_real_value, &buf->start_time);
|
|
||||||
|
|
||||||
fclose (f);
|
buf->rtime = buf->utime + buf->stime;
|
||||||
|
|
||||||
|
p = skip_multiple_token (p, 2);
|
||||||
|
|
||||||
|
buf->timeout = strtoul (p, &p, 0);
|
||||||
|
buf->it_real_value = strtoul (p, &p, 0);
|
||||||
|
buf->start_time = strtoul (p, &p, 0);
|
||||||
|
|
||||||
|
buf->frequency = 100;
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_time;
|
buf->flags = _glibtop_sysdeps_proc_time;
|
||||||
}
|
}
|
||||||
|
@@ -25,7 +25,9 @@
|
|||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_proc_uid =
|
static const unsigned long _glibtop_sysdeps_proc_uid =
|
||||||
(1 << GLIBTOP_PROC_UID_UID) + (1 << GLIBTOP_PROC_UID_EUID) +
|
(1 << GLIBTOP_PROC_UID_UID) + (1 << GLIBTOP_PROC_UID_EUID) +
|
||||||
(1 << GLIBTOP_PROC_UID_GID) + (1 << GLIBTOP_PROC_UID_EGID) +
|
(1 << GLIBTOP_PROC_UID_GID) + (1 << GLIBTOP_PROC_UID_EGID);
|
||||||
|
|
||||||
|
static const unsigned long _glibtop_sysdeps_proc_uid_stat =
|
||||||
(1 << GLIBTOP_PROC_UID_PID) + (1 << GLIBTOP_PROC_UID_PPID) +
|
(1 << GLIBTOP_PROC_UID_PID) + (1 << GLIBTOP_PROC_UID_PPID) +
|
||||||
(1 << GLIBTOP_PROC_UID_PGRP) + (1 << GLIBTOP_PROC_UID_SESSION) +
|
(1 << GLIBTOP_PROC_UID_PGRP) + (1 << GLIBTOP_PROC_UID_SESSION) +
|
||||||
(1 << GLIBTOP_PROC_UID_TTY) + (1 << GLIBTOP_PROC_UID_TPGID) +
|
(1 << GLIBTOP_PROC_UID_TTY) + (1 << GLIBTOP_PROC_UID_TPGID) +
|
||||||
@@ -38,7 +40,8 @@ static const unsigned long _glibtop_sysdeps_proc_uid =
|
|||||||
void
|
void
|
||||||
glibtop_init_proc_uid_s (glibtop *server)
|
glibtop_init_proc_uid_s (glibtop *server)
|
||||||
{
|
{
|
||||||
server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid;
|
server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid |
|
||||||
|
_glibtop_sysdeps_proc_uid_stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Provides detailed information about a process. */
|
/* Provides detailed information about a process. */
|
||||||
@@ -46,65 +49,63 @@ glibtop_init_proc_uid_s (glibtop *server)
|
|||||||
void
|
void
|
||||||
glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
|
glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
|
||||||
{
|
{
|
||||||
char input [BUFSIZ], *tmp;
|
char buffer [BUFSIZ], *p;
|
||||||
int nread;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_UID, 0);
|
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_UID, 0);
|
||||||
|
|
||||||
memset (buf, 0, sizeof (glibtop_proc_uid));
|
memset (buf, 0, sizeof (glibtop_proc_uid));
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/status", pid);
|
if (proc_status_to_buffer (buffer, pid))
|
||||||
|
|
||||||
f = fopen (input, "r");
|
|
||||||
if (!f) return;
|
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
|
||||||
|
|
||||||
if (nread < 0) {
|
|
||||||
fclose (f);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
|
||||||
|
|
||||||
/* Search substring 'Pid:' */
|
/* Search substring 'Pid:' */
|
||||||
|
|
||||||
tmp = strstr (input, "Pid:");
|
p = strstr (buffer, "\nPid:");
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
if (tmp == NULL) return;
|
p = skip_token (p); /* "Pid:" */
|
||||||
|
buf->pid = strtoul (p, &p, 0);
|
||||||
|
|
||||||
sscanf (tmp, "\nPid: %u\nPPid: %u\nUid: %u %u %*u %*u\n"
|
p = skip_token (p); /* "PPid:" */
|
||||||
"Gid: %u %u %*u %*u\n", &buf->pid, &buf->ppid,
|
buf->ppid = strtoul (p, &p, 0);
|
||||||
&buf->uid, &buf->euid, &buf->gid, &buf->egid);
|
|
||||||
|
|
||||||
fclose (f);
|
/* Maybe future Linux versions place something between
|
||||||
|
* "PPid" and "Uid", so we catch this here. */
|
||||||
|
p = strstr (p, "\nUid:");
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
sprintf (input, "/proc/%d/stat", pid);
|
p = skip_token (p); /* "Uid:" */
|
||||||
|
buf->uid = strtoul (p, &p, 0);
|
||||||
|
buf->euid = strtoul (p, &p, 0);
|
||||||
|
|
||||||
f = fopen (input, "r");
|
/* We don't know how many entries on the "Uid:" line
|
||||||
if (!f) return;
|
* future Linux version will have, so we catch this here. */
|
||||||
|
p = strstr (p, "\nGid:");
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
nread = fread (input, 1, BUFSIZ, f);
|
p = skip_token (p); /* "Gid:" */
|
||||||
|
buf->gid = strtoul (p, &p, 0);
|
||||||
|
buf->egid = strtoul (p, &p, 0);
|
||||||
|
|
||||||
if (nread < 0) {
|
buf->flags = _glibtop_sysdeps_proc_uid;
|
||||||
fclose (f);
|
|
||||||
|
if (proc_stat_to_buffer (buffer, pid))
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
input [nread] = 0;
|
p = proc_stat_after_cmd (buffer);
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
/* This is from guile-utils/gtop/proc/readproc.c */
|
p = skip_multiple_token (p, 2);
|
||||||
|
|
||||||
/* split into "PID (cmd" and "<rest>" */
|
buf->pgrp = strtoul (p, &p, 0);
|
||||||
tmp = strrchr (input, ')');
|
buf->session = strtoul (p, &p, 0);
|
||||||
*tmp = '\0'; /* replace trailing ')' with NUL */
|
buf->tty = strtoul (p, &p, 0);
|
||||||
/* parse these two strings separately, skipping the leading "(". */
|
buf->tpgid = strtoul (p, &p, 0);
|
||||||
sscanf(tmp + 2, /* skip space after ')' too */
|
|
||||||
"%*c %*d %d %d %d %d %*u %*u %*u %*u %*u "
|
p = skip_multiple_token (p, 9);
|
||||||
"%*d %*d %*d %*d %d %d",
|
|
||||||
&buf->pgrp, &buf->session, &buf->tty, &buf->tpgid,
|
buf->priority = strtoul (p, &p, 0);
|
||||||
&buf->priority, &buf->nice);
|
buf->nice = strtoul (p, &p, 0);
|
||||||
|
|
||||||
if (buf->tty == 0)
|
if (buf->tty == 0)
|
||||||
/* the old notty val, update elsewhere bef. moving to 0 */
|
/* the old notty val, update elsewhere bef. moving to 0 */
|
||||||
@@ -119,7 +120,5 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
|
|||||||
/* when tty wasn't full devno */
|
/* when tty wasn't full devno */
|
||||||
buf->tty = 4*0x100 + buf->tty;
|
buf->tty = 4*0x100 + buf->tty;
|
||||||
|
|
||||||
fclose (f);
|
buf->flags |= _glibtop_sysdeps_proc_uid_stat;
|
||||||
|
|
||||||
buf->flags = _glibtop_sysdeps_proc_uid;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user