changes to procstat malloc behavior

This commit is contained in:
mykola2312 2024-08-28 16:36:01 +03:00
parent 4118ea7292
commit 1606be8556
3 changed files with 24 additions and 11 deletions

View file

@ -25,7 +25,7 @@ void print_status(procstat_status_t* proc)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// test test // map test
{ {
procstat_map_t* maps; procstat_map_t* maps;
size_t map_count; size_t map_count;
@ -64,14 +64,14 @@ int main(int argc, char** argv)
if (procstat_get_threads(parent->pid, &threads, &thread_count)) if (procstat_get_threads(parent->pid, &threads, &thread_count))
{ {
fputs("failed to obtain process threads\n", stderr); fputs("failed to obtain process threads\n", stderr);
free(list); procstat_free_status_list(list, count);
return 1; return 1;
} }
if (procstat_find_active(threads, thread_count, &active)) if (procstat_find_active(threads, thread_count, &active))
{ {
// no active threads - free list and continue // no active threads - free list and continue
free(threads); procstat_free_status_list(threads, thread_count);
usleep(500*1000); usleep(500*1000);
} }
else else
@ -96,8 +96,8 @@ int main(int argc, char** argv)
{ {
fprintf(stderr, "failed to attach: %s\n", strerror(errno)); fprintf(stderr, "failed to attach: %s\n", strerror(errno));
free(threads); procstat_free_status_list(threads, thread_count);
free(list); procstat_free_status_list(list, count);
return 1; return 1;
} }
@ -116,8 +116,8 @@ int main(int argc, char** argv)
process_detach_all(threads, thread_count); process_detach_all(threads, thread_count);
free(threads); procstat_free_status_list(threads, thread_count);
free(list); procstat_free_status_list(list, count);
return 0; return 0;
} }

View file

@ -25,6 +25,8 @@ int procstat_parse_status(pid_t pid, procstat_status_t* status)
if (rd < 1) return 1; if (rd < 1) return 1;
memset(status, '\0', sizeof(procstat_status_t));
char* lineptr = NULL, *line = strtok_r(buffer, "\n", &lineptr); char* lineptr = NULL, *line = strtok_r(buffer, "\n", &lineptr);
while (line != NULL) while (line != NULL)
{ {
@ -33,7 +35,9 @@ int procstat_parse_status(pid_t pid, procstat_status_t* status)
const char* value = (const char*)strtok_r(NULL, ":\t", &fieldptr); const char* value = (const char*)strtok_r(NULL, ":\t", &fieldptr);
if (!strcmp(key, "Name")) if (!strcmp(key, "Name"))
strncpy(status->name, value, MAX_PROCESS_NAME); {
if (value) status->name = strdup(value);
}
else if (!strcmp(key, "Umask")) else if (!strcmp(key, "Umask"))
status->umask = (unsigned int)strtoul(value, NULL, 8); status->umask = (unsigned int)strtoul(value, NULL, 8);
else if (!strcmp(key, "State")) else if (!strcmp(key, "State"))
@ -59,6 +63,13 @@ int procstat_parse_status(pid_t pid, procstat_status_t* status)
return 0; return 0;
} }
void procstat_free_status_list(procstat_status_t* list, size_t count)
{
for (unsigned i = 0; i < count; i++)
if (list[i].name) free(list[i].name);
free(list);
}
static int is_numeric(const char* str) static int is_numeric(const char* str)
{ {
for (const char* p = str; *p; p++) for (const char* p = str; *p; p++)
@ -310,7 +321,7 @@ int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
// path // path
if (pathname) map->path = strdup(pathname); if (pathname) map->path = strdup(pathname);
TRACE("map v_start %lx v_end %lx prot %x flags %x f_offset %x dev_major %u dev_minor %u inode %lu path %s\n", TRACE("map v_start %lx v_end %lx prot %x flags %x f_offset %lx dev_major %u dev_minor %u inode %lu path %s\n",
map->v_start, map->v_end, map->v_start, map->v_end,
map->prot, map->flags, map->prot, map->flags,
map->f_offset, map->f_offset,

View file

@ -15,9 +15,8 @@ typedef enum {
ZOMBIE = 'Z' ZOMBIE = 'Z'
} procstat_state_t; } procstat_state_t;
#define MAX_PROCESS_NAME 256
typedef struct { typedef struct {
char name[MAX_PROCESS_NAME]; char* name;
mode_t umask; mode_t umask;
procstat_state_t state; procstat_state_t state;
pid_t tgid; pid_t tgid;
@ -33,6 +32,9 @@ typedef struct {
// error information obtain from errno // error information obtain from errno
int procstat_parse_status(pid_t pid, procstat_status_t* status); int procstat_parse_status(pid_t pid, procstat_status_t* status);
// free all procstat status list entries
void procstat_free_status_list(procstat_status_t* list, size_t count);
// find any process that matches name, case insensitive. // find any process that matches name, case insensitive.
// list pointer must point to NULL-initialized pointer, and count pointer must pount to initialized 0 // list pointer must point to NULL-initialized pointer, and count pointer must pount to initialized 0
// will skip any process which status couldn't be parsed // will skip any process which status couldn't be parsed