**** Merged from LIBGTOP_1_1_2_PATCHES ****

This commit is contained in:
Martin Baulig
2001-02-14 23:07:41 +00:00
parent 2698263164
commit 0bd5a6c082
29 changed files with 1182 additions and 231 deletions

View File

@@ -23,9 +23,27 @@
#include <glibtop.h>
#include <glibtop_private.h>
#include <glibtop/procuid.h>
#include <errno.h>
#include "safeio.h"
/*
* The differences between old and new procfs API are:
* - old has /proc/<pid> file and ioctl() is used to obtain the data.
* - new has /proc/<pid>/* files and read() & friends are used to
* obtain the data.
* - names of structures and members are different. It's mostly the
* prefix. Old uses `pr' and new uses `ps'.
*
* Since almost every line would be in #ifdef, I think it would
* be more readable to just copy everything twice. This is not a big
* file, after all.
*/
#ifdef HAVE_PROCFS_H
/* Read /proc/<pid>/psinfo. */
int
@@ -35,19 +53,21 @@ glibtop_get_proc_data_psinfo_s (glibtop *server, struct psinfo *psinfo, pid_t pi
char buffer [BUFSIZ];
sprintf (buffer, "/proc/%d/psinfo", (int) pid);
fd = open (buffer, O_RDONLY);
fd = s_open (buffer, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", buffer);
return -1;
}
if (pread (fd, psinfo, sizeof (struct psinfo), 0) != sizeof (struct psinfo)) {
close (fd);
if (s_pread (fd, psinfo, sizeof (struct psinfo), 0) !=
sizeof (struct psinfo))
{
s_close (fd);
glibtop_warn_io_r (server, "pread (%s)", buffer);
return -1;
}
close (fd);
s_close (fd);
return 0;
}
@@ -58,44 +78,63 @@ glibtop_get_proc_data_usage_s (glibtop *server, struct prusage *prusage, pid_t p
char buffer [BUFSIZ];
sprintf (buffer, "/proc/%d/usage", (int) pid);
fd = open (buffer, O_RDONLY);
fd = s_open (buffer, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", buffer);
return -1;
}
if (pread (fd, prusage, sizeof (struct prusage), 0) != sizeof (struct prusage)) {
close (fd);
if (s_pread (fd, prusage, sizeof (struct prusage), 0) !=
sizeof (struct prusage))
{
s_close (fd);
glibtop_warn_io_r (server, "pread (%s)", buffer);
return -1;
}
close (fd);
s_close (fd);
return 0;
}
#if LIBGTOP_VERSION_CODE >= 1001002
int
glibtop_get_proc_credentials_s(glibtop *server, struct prcred *prcred, pid_t pid)
glibtop_get_proc_credentials_s(glibtop *server,
struct prcred *prcred,
gid_t *groups,
pid_t pid)
{
int fd;
size_t toread;
char buffer[BUFSIZ];
sprintf(buffer, "/proc/%d/cred", (int)pid);
if((fd = open(buffer, O_RDONLY)) < 0)
if((fd = s_open(buffer, O_RDONLY)) < 0)
{
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r(server, "open (%s)", buffer);
return -1;
}
if(pread(fd, prcred, sizeof(struct prcred), 0) != sizeof(struct prcred))
if(s_pread(fd, prcred, sizeof(struct prcred), 0) !=
sizeof(struct prcred))
{
close(fd);
s_close(fd);
glibtop_warn_io_r(server, "pread (%s)", buffer);
return -1;
}
close(fd);
if(prcred->pr_ngroups >= 0)
{
if(prcred->pr_ngroups <= GLIBTOP_MAX_GROUPS)
toread = prcred->pr_ngroups * sizeof(gid_t);
else
toread = GLIBTOP_MAX_GROUPS * sizeof(gid_t);
if(s_pread(fd, groups, toread,
&(((struct prcred *)0)->pr_groups[0])) != toread)
prcred->pr_ngroups = 0;
}
s_close(fd);
return 0;
}
#endif
int
glibtop_get_proc_status_s(glibtop *server, struct pstatus *pstatus, pid_t pid)
@@ -104,18 +143,126 @@ glibtop_get_proc_status_s(glibtop *server, struct pstatus *pstatus, pid_t pid)
char buffer[BUFSIZ];
sprintf(buffer, "/proc/%d/status", (int)pid);
if((fd = open(buffer, O_RDONLY)) < 0)
if((fd = s_open(buffer, O_RDONLY)) < 0)
{
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r(server, "open (%s)", buffer);
return -1;
}
if(pread(fd, pstatus, sizeof(struct pstatus), 0) != sizeof(struct pstatus))
if(s_pread(fd, pstatus, sizeof(struct pstatus), 0) !=
sizeof(struct pstatus))
{
close(fd);
s_close(fd);
glibtop_warn_io_r(server, "pread (%s)", buffer);
return -1;
}
close(fd);
s_close(fd);
return 0;
}
#else /* old API */
int
glibtop_get_proc_data_psinfo_s (glibtop *server,
struct prpsinfo *psinfo,
pid_t pid)
{
int fd;
char buffer [BUFSIZ];
sprintf (buffer, "/proc/%d", (int) pid);
fd = s_open (buffer, O_RDONLY);
if (fd < 0) {
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r (server, "open (%s)", buffer);
return -1;
}
if(ioctl(fd, PIOCPSINFO, psinfo) < 0)
{
s_close (fd);
glibtop_warn_io_r (server, "ioctl(%s, PIOCPSINFO)", buffer);
return -1;
}
s_close (fd);
return 0;
}
int
glibtop_get_proc_data_usage_s (glibtop *server,
struct prusage *prusage,
pid_t pid)
{
int fd;
char buffer [BUFSIZ];
sprintf (buffer, "/proc/%d", (int) pid);
fd = s_open (buffer, O_RDONLY);
if (fd < 0) {
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r (server, "open (%s)", buffer);
return -1;
}
if(ioctl(fd, PIOCUSAGE, prusage) < 0)
{
s_close (fd);
glibtop_warn_io_r (server, "ioctl(%s, PIOCUSAGE)", buffer);
return -1;
}
s_close (fd);
return 0;
}
int
glibtop_get_proc_credentials_s(glibtop *server,
struct prcred *prcred,
gid_t *groups,
pid_t pid)
{
int fd;
size_t toread;
char buffer[BUFSIZ];
sprintf(buffer, "/proc/%d", (int)pid);
if((fd = s_open(buffer, O_RDONLY)) < 0)
{
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r(server, "open (%s)", buffer);
return -1;
}
if(ioctl(fd, PIOCCRED, prcred) < 0)
{
s_close(fd);
glibtop_warn_io_r(server, "ioctl(%s, PIOCCRED)", buffer);
return -1;
}
s_close(fd);
return 0;
}
int
glibtop_get_proc_status_s(glibtop *server, struct prstatus *pstatus, pid_t pid)
{
int fd;
char buffer[BUFSIZ];
sprintf(buffer, "/proc/%d", (int)pid);
if((fd = s_open(buffer, O_RDONLY)) < 0)
{
if(errno != EPERM && errno != EACCES)
glibtop_warn_io_r(server, "open (%s)", buffer);
return -1;
}
if(ioctl(fd, PIOCSTATUS, pstatus) < 0)
{
s_close(fd);
glibtop_warn_io_r(server, "ioctl(%s, PIOCSTATUS)", buffer);
return -1;
}
s_close(fd);
return 0;
}
#endif