Skip to content

Commit b0b4f7b

Browse files
committed
initctl: refactor pid_cgroup(), return allocated buffer
Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 4d79d84 commit b0b4f7b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/cgutil.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,43 @@ char *pid_comm(int pid, char *buf, size_t len)
121121
return ptr;
122122
}
123123

124-
char *pid_cgroup(int pid, char *buf, size_t len)
124+
char *pid_cgroup(int pid)
125125
{
126-
char *ptr = NULL;
126+
char *buf, *ptr = NULL;
127+
size_t len;
127128
FILE *fp;
128129

129130
fp = fopenf("r", "/proc/%d/cgroup", pid);
130131
if (!fp)
131132
return NULL;
132133

134+
len = flen(fp);
135+
if (len == 0) {
136+
fclose(fp);
137+
return NULL;
138+
}
139+
len++;
140+
141+
buf = calloc(1, len);
142+
if (!buf) {
143+
fclose(fp);
144+
return NULL;
145+
}
146+
133147
if (fgets(buf, len, fp))
134148
ptr = chomp(buf);
135149
fclose(fp);
136150

137-
if (ptr)
138-
ptr = strchr(buf, '/');
151+
if (ptr) {
152+
ptr = strchr(ptr, '/');
153+
if (ptr) {
154+
memmove(buf, ptr, strlen(ptr) + 1);
155+
return buf;
156+
}
157+
}
139158

140-
return ptr;
159+
free(buf);
160+
return NULL;
141161
}
142162

143163
static char *cgroup_val(char *path, char *file, char *buf, size_t len)

src/cgutil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int cgroup_avail(void);
5656

5757
char *pid_cmdline (int pid);
5858
char *pid_comm (int pid, char *buf, size_t len);
59-
char *pid_cgroup (int pid, char *buf, size_t len);
59+
char *pid_cgroup (int pid);
6060

6161
uint64_t cgroup_memory(char *group);
6262

src/initctl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,14 +1135,14 @@ static int show_status(char *arg)
11351135
printf(" Restarts : %d (%d/%d)\n", svc->restart_tot, svc->restart_cnt, svc->restart_max);
11361136
printf(" Runlevels : %s\n", runlevel_string(runlevel, svc->runlevels));
11371137
if (cgrp && svc->pid > 1) {
1138-
char grbuf[128];
1138+
const struct cg *cg;
11391139
char path[256];
1140-
struct cg *cg;
11411140
char *group;
11421141

1143-
group = pid_cgroup(svc->pid, grbuf, sizeof(grbuf));
1142+
group = pid_cgroup(svc->pid);
11441143
if (!group)
11451144
goto no_cgroup; /* ... or PID doesn't exist (anymore) */
1145+
11461146
snprintf(path, sizeof(path), "%s/%s", FINIT_CGPATH, group);
11471147
cg = cg_conf(path);
11481148

@@ -1151,6 +1151,8 @@ static int show_status(char *arg)
11511151
group, cg->cg_cpu.set, cg->cg_cpu.weight, cg->cg_cpu.max,
11521152
cg->cg_mem.min, cg->cg_mem.max);
11531153
show_cgroup_tree(group, " ");
1154+
1155+
free(group);
11541156
}
11551157
no_cgroup:
11561158
printf("\n");

0 commit comments

Comments
 (0)