mirror of
https://github.com/troglobit/finit.git
synced 2026-11-13 17:00:16 +00:00
initctl: refactor pid_cgroup(), return allocated buffer
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
3 files changed
+31
-9
No files matched your search
+25
-5
@@ -121,23 +121,43 @@ char *pid_comm(int pid, char *buf, size_t len)
|
|||||||
return ptr;
|
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;
|
FILE *fp;
|
||||||
|
|
||||||
fp = fopenf("r", "/proc/%d/cgroup", pid);
|
fp = fopenf("r", "/proc/%d/cgroup", pid);
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return NULL;
|
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))
|
if (fgets(buf, len, fp))
|
||||||
ptr = chomp(buf);
|
ptr = chomp(buf);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
if (ptr)
|
if (ptr) {
|
||||||
ptr = strchr(buf, '/');
|
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)
|
static char *cgroup_val(char *path, char *file, char *buf, size_t len)
|
||||||
|
|||||||
+1
-1
@@ -56,7 +56,7 @@ int cgroup_avail(void);
|
|||||||
|
|
||||||
char *pid_cmdline (int pid);
|
char *pid_cmdline (int pid);
|
||||||
char *pid_comm (int pid, char *buf, size_t len);
|
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);
|
uint64_t cgroup_memory(char *group);
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -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(" Restarts : %d (%d/%d)\n", svc->restart_tot, svc->restart_cnt, svc->restart_max);
|
||||||
printf(" Runlevels : %s\n", runlevel_string(runlevel, svc->runlevels));
|
printf(" Runlevels : %s\n", runlevel_string(runlevel, svc->runlevels));
|
||||||
if (cgrp && svc->pid > 1) {
|
if (cgrp && svc->pid > 1) {
|
||||||
char grbuf[128];
|
const struct cg *cg;
|
||||||
char path[256];
|
char path[256];
|
||||||
struct cg *cg;
|
|
||||||
char *group;
|
char *group;
|
||||||
|
|
||||||
group = pid_cgroup(svc->pid, grbuf, sizeof(grbuf));
|
group = pid_cgroup(svc->pid);
|
||||||
if (!group)
|
if (!group)
|
||||||
goto no_cgroup; /* ... or PID doesn't exist (anymore) */
|
goto no_cgroup; /* ... or PID doesn't exist (anymore) */
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "%s/%s", FINIT_CGPATH, group);
|
snprintf(path, sizeof(path), "%s/%s", FINIT_CGPATH, group);
|
||||||
cg = cg_conf(path);
|
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,
|
group, cg->cg_cpu.set, cg->cg_cpu.weight, cg->cg_cpu.max,
|
||||||
cg->cg_mem.min, cg->cg_mem.max);
|
cg->cg_mem.min, cg->cg_mem.max);
|
||||||
show_cgroup_tree(group, " ");
|
show_cgroup_tree(group, " ");
|
||||||
|
|
||||||
|
free(group);
|
||||||
}
|
}
|
||||||
no_cgroup:
|
no_cgroup:
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|||||||
Reference in new issue
Block a user