move /proc/pid/cmdline parsing to sigar_util.c

This commit is contained in:
Doug MacEachern 2005-02-09 07:29:31 +00:00
parent 734e766771
commit b2cc0c4c48
3 changed files with 70 additions and 60 deletions

View File

@ -62,6 +62,10 @@ int sigar_proc_fd_count(sigar_t *sigar, sigar_pid_t pid,
int sigar_proc_count(sigar_t *sigar, sigar_uint64_t *total);
/* linux + freebsd */
int sigar_procfs_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs);
int sigar_mem_calc_ram(sigar_t *sigar, sigar_mem_t *mem);
double sigar_file_system_usage_calc_used(sigar_t *sigar,

View File

@ -717,66 +717,7 @@ int sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid,
int sigar_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs)
{
char buffer[BUFSIZ], *buf=NULL, *ptr;
int fd, len, total=0;
(void)SIGAR_PROC_FILENAME(buffer, pid, "/cmdline");
if ((fd = open(buffer, O_RDONLY)) < 0) {
if (errno == ENOENT) {
return ESRCH;
}
return errno;
}
buffer[0] = '\0';
/* XXX: possible to get rid of some mallocs here.
* but, unlikely this will be called often so it
* might not even matter much.
*/
while ((len = read(fd, buffer, sizeof(buffer)-1)) > 0) {
if (len == 0) {
break;
}
if (buf) {
buf = realloc(buf, total+len);
}
else {
buf = malloc(len+1);
}
memcpy(buf+total, buffer, len);
total += len;
}
close(fd);
sigar_proc_args_create(procargs);
//e.g. /proc/2/cmdline
if (total == 0) {
procargs->number = 0;
return SIGAR_OK;
}
buf[total] = '\0';
ptr = buf;
while (*ptr) {
int alen = strlen(ptr)+1;
char *arg = malloc(alen);
SIGAR_PROC_ARGS_GROW(procargs);
memcpy(arg, ptr, alen);
procargs->data[procargs->number++] = arg;
ptr += alen;
}
free(buf);
return SIGAR_OK;
return sigar_procfs_args_get(sigar, pid, procargs);
}
int sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid,

View File

@ -221,6 +221,71 @@ int sigar_proc_count(sigar_t *sigar, sigar_uint64_t *total)
return SIGAR_OK;
}
int sigar_procfs_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs)
{
char buffer[9086], *buf=NULL, *ptr;
int fd, len, total=0;
(void)SIGAR_PROC_FILENAME(buffer, pid, "/cmdline");
if ((fd = open(buffer, O_RDONLY)) < 0) {
if (errno == ENOENT) {
return ESRCH;
}
return errno;
}
buffer[0] = '\0';
/* XXX: possible to get rid of some mallocs here.
* but, unlikely this will be called often so it
* might not even matter much.
*/
while ((len = read(fd, buffer, sizeof(buffer)-1)) > 0) {
if (len == 0) {
break;
}
if (buf) {
buf = realloc(buf, total+len);
}
else {
buf = malloc(len+1);
}
memcpy(buf+total, buffer, len);
total += len;
}
close(fd);
sigar_proc_args_create(procargs);
//e.g. /proc/2/cmdline
if (total == 0) {
procargs->number = 0;
return SIGAR_OK;
}
buf[total] = '\0';
ptr = buf;
while (*ptr) {
int alen = strlen(ptr)+1;
char *arg = malloc(alen);
SIGAR_PROC_ARGS_GROW(procargs);
memcpy(arg, ptr, alen);
procargs->data[procargs->number++] = arg;
ptr += alen;
}
free(buf);
return SIGAR_OK;
}
int sigar_mem_calc_ram(sigar_t *sigar, sigar_mem_t *mem)
{
sigar_uint64_t lram = (mem->total / (1024 * 1024));