proc_modules refactor to prevent possible phandle leaks

This commit is contained in:
Doug MacEachern 2004-07-30 02:02:41 +00:00
parent 23f6fd91b3
commit 5d4736e043
1 changed files with 19 additions and 18 deletions

View File

@ -790,24 +790,6 @@ static int sigar_pgrab_modules(sigar_t *sigar, sigar_pid_t pid,
struct stat statbuf; struct stat statbuf;
char buffer[BUFSIZ]; char buffer[BUFSIZ];
if ((pstatus = sigar_init_libproc(sigar)) != SIGAR_OK) {
return pstatus;
}
if (!(phandle = sigar->pgrab(pid, 0x01, &pstatus))) {
switch (pstatus) {
case G_NOPROC:
return ESRCH;
case G_PERM:
return EACCES;
default:
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] Pgrab error=%d",
SIGAR_FUNC, pstatus);
return ENOTSUP; /*XXX*/
}
}
(void)SIGAR_PROC_FILENAME(buffer, pid, "/xmap"); (void)SIGAR_PROC_FILENAME(buffer, pid, "/xmap");
if ((fd = open(buffer, O_RDONLY)) < 0) { if ((fd = open(buffer, O_RDONLY)) < 0) {
@ -827,12 +809,31 @@ static int sigar_pgrab_modules(sigar_t *sigar, sigar_pid_t pid,
SIGAR_FUNC, pid, map_size); SIGAR_FUNC, pid, map_size);
} }
if ((pstatus = sigar_init_libproc(sigar)) != SIGAR_OK) {
return pstatus;
}
if (!(phandle = sigar->pgrab(pid, 0x01, &pstatus))) {
switch (pstatus) {
case G_NOPROC:
return ESRCH;
case G_PERM:
return EACCES;
default:
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] Pgrab error=%d",
SIGAR_FUNC, pstatus);
return ENOTSUP; /*XXX*/
}
}
for (nread=0; nread<statbuf.st_size; ) { for (nread=0; nread<statbuf.st_size; ) {
off_t wanted = map_size > sizeof(xmaps) ? sizeof(xmaps) : map_size; off_t wanted = map_size > sizeof(xmaps) ? sizeof(xmaps) : map_size;
int total = wanted / sizeof(prxmap_t); int total = wanted / sizeof(prxmap_t);
if (pread(fd, xmaps, wanted, nread) != wanted) { if (pread(fd, xmaps, wanted, nread) != wanted) {
close(fd); close(fd);
sigar->pfree(phandle);
return errno; return errno;
} }