initctl: refactor pid_cgroup(), return allocated buffer

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2025-05-30 10:42:50 +02:00
1 parent 4d79d84fb8
commit b0b4f7b2ba
3 files changed
+31 -9

No files matched your search

+25 -5
View File
@@ -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)
+1 -1
View File
@@ -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);
+5 -3
View File
@@ -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");