diff --git a/src/cgutil.c b/src/cgutil.c index 8059da4a..3957bb46 100644 --- a/src/cgutil.c +++ b/src/cgutil.c @@ -121,23 +121,43 @@ char *pid_comm(int pid, char *buf, size_t len) return ptr; } -char *pid_cgroup(int pid, char *buf, size_t len) +char *pid_cgroup(int pid) { - char *ptr = NULL; + char *buf, *ptr = NULL; + size_t len; FILE *fp; fp = fopenf("r", "/proc/%d/cgroup", pid); if (!fp) return NULL; + len = flen(fp); + if (len == 0) { + fclose(fp); + return NULL; + } + len++; + + buf = calloc(1, len); + if (!buf) { + fclose(fp); + return NULL; + } + if (fgets(buf, len, fp)) ptr = chomp(buf); fclose(fp); - if (ptr) - ptr = strchr(buf, '/'); + if (ptr) { + ptr = strchr(ptr, '/'); + if (ptr) { + memmove(buf, ptr, strlen(ptr) + 1); + return buf; + } + } - return ptr; + free(buf); + return NULL; } static char *cgroup_val(char *path, char *file, char *buf, size_t len) diff --git a/src/cgutil.h b/src/cgutil.h index 9b17c3c3..77718aee 100644 --- a/src/cgutil.h +++ b/src/cgutil.h @@ -56,7 +56,7 @@ int cgroup_avail(void); char *pid_cmdline (int pid); char *pid_comm (int pid, char *buf, size_t len); -char *pid_cgroup (int pid, char *buf, size_t len); +char *pid_cgroup (int pid); uint64_t cgroup_memory(char *group); diff --git a/src/initctl.c b/src/initctl.c index d1698fc4..a9335052 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -1135,14 +1135,14 @@ static int show_status(char *arg) printf(" Restarts : %d (%d/%d)\n", svc->restart_tot, svc->restart_cnt, svc->restart_max); printf(" Runlevels : %s\n", runlevel_string(runlevel, svc->runlevels)); if (cgrp && svc->pid > 1) { - char grbuf[128]; + const struct cg *cg; char path[256]; - struct cg *cg; char *group; - group = pid_cgroup(svc->pid, grbuf, sizeof(grbuf)); + group = pid_cgroup(svc->pid); if (!group) goto no_cgroup; /* ... or PID doesn't exist (anymore) */ + snprintf(path, sizeof(path), "%s/%s", FINIT_CGPATH, group); cg = cg_conf(path); @@ -1151,6 +1151,8 @@ static int show_status(char *arg) group, cg->cg_cpu.set, cg->cg_cpu.weight, cg->cg_cpu.max, cg->cg_mem.min, cg->cg_mem.max); show_cgroup_tree(group, " "); + + free(group); } no_cgroup: printf("\n");