conmon: Fix cgroup subsystem parsing

The code as is doesn't handle merged controllers.
For instance, I have this in my /proc/self/cgrous:

4:cpu,cpuacct:/user.slice/user-0.slice/session-4.scope

The current code fails to match "cpuacct" wit this line, and
additionally it just does a prefix match so if you were looking
for say "cpu", it would match this:

2:cpuset:/

I also removed some ninfo spew that didn't seem very useful.

Signed-off-by: Alexander Larsson <alexl@redhat.com>
This commit is contained in:
Alexander Larsson 2017-06-02 14:02:38 +02:00
parent d34c5829f8
commit fe80f857ca

View file

@ -72,10 +72,17 @@ static inline void gstring_free_cleanup(GString **string)
g_string_free(*string, TRUE); g_string_free(*string, TRUE);
} }
static inline void strv_cleanup(char ***strv)
{
if (strv)
g_strfreev (*strv);
}
#define _cleanup_free_ _cleanup_(freep) #define _cleanup_free_ _cleanup_(freep)
#define _cleanup_close_ _cleanup_(closep) #define _cleanup_close_ _cleanup_(closep)
#define _cleanup_fclose_ _cleanup_(fclosep) #define _cleanup_fclose_ _cleanup_(fclosep)
#define _cleanup_gstring_ _cleanup_(gstring_free_cleanup) #define _cleanup_gstring_ _cleanup_(gstring_free_cleanup)
#define _cleanup_strv_ _cleanup_(strv_cleanup)
#define BUF_SIZE 256 #define BUF_SIZE 256
#define CMD_SIZE 1024 #define CMD_SIZE 1024
@ -350,27 +357,32 @@ static char *process_cgroup_subsystem_path(int pid, const char *subsystem) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
ssize_t read; ssize_t read;
size_t len = 0; size_t len = 0;
char *ptr; char *ptr, *path;
char *subsystem_path = NULL; char *subsystem_path = NULL;
int i;
while ((read = getline(&line, &len, fp)) != -1) { while ((read = getline(&line, &len, fp)) != -1) {
_cleanup_strv_ char **subsystems = NULL;
ptr = strchr(line, ':'); ptr = strchr(line, ':');
if (ptr == NULL) { if (ptr == NULL) {
nwarn("Error parsing cgroup, ':' not found: %s", line); nwarn("Error parsing cgroup, ':' not found: %s", line);
return NULL; return NULL;
} }
ptr++; ptr++;
if (!strncmp(ptr, subsystem, strlen(subsystem))) { path = strchr(ptr, ':');
char *path = strchr(ptr, '/');
if (path == NULL) { if (path == NULL) {
nwarn("Error finding path in cgroup: %s", line); nwarn("Error parsing cgroup, second ':' not found: %s", line);
return NULL; return NULL;
} }
ninfo("PATH: %s", path); *path = 0;
const char *subpath = strchr(subsystem, '='); path++;
subsystems = g_strsplit (ptr, ",", -1);
for (i = 0; subsystems[i] != NULL; i++) {
if (strcmp (subsystems[i], subsystem) == 0) {
char *subpath = strchr(subsystems[i], '=');
if (subpath == NULL) { if (subpath == NULL) {
subpath = subsystem; subpath = ptr;
} else { } else {
subpath++; *subpath = 0;
} }
rc = asprintf(&subsystem_path, "%s/%s%s", CGROUP_ROOT, subpath, path); rc = asprintf(&subsystem_path, "%s/%s%s", CGROUP_ROOT, subpath, path);
@ -378,11 +390,12 @@ static char *process_cgroup_subsystem_path(int pid, const char *subsystem) {
nwarn("Failed to allocate memory for subsystemd path"); nwarn("Failed to allocate memory for subsystemd path");
return NULL; return NULL;
} }
ninfo("SUBSYSTEM_PATH: %s", subsystem_path);
subsystem_path[strlen(subsystem_path) - 1] = '\0'; subsystem_path[strlen(subsystem_path) - 1] = '\0';
return subsystem_path; return subsystem_path;
} }
} }
}
return NULL; return NULL;
} }