--- old/src/hotspot/os/linux/osContainer_linux.cpp 2019-03-11 10:58:22.486422070 -0400 +++ new/src/hotspot/os/linux/osContainer_linux.cpp 2019-03-11 10:58:21.691375690 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -99,14 +99,14 @@ buf[MAXPATHLEN-1] = '\0'; _path = os::strdup(buf); } else { - char *p = strstr(_root, cgroup_path); + char *p = strstr(cgroup_path, _root); if (p != NULL && p == _root) { if (strlen(cgroup_path) > strlen(_root)) { int buflen; strncpy(buf, _mount_point, MAXPATHLEN); buf[MAXPATHLEN-1] = '\0'; buflen = strlen(buf); - if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) { + if ((buflen + strlen(cgroup_path) - strlen(_root)) > (MAXPATHLEN-1)) { return; } strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen); @@ -217,16 +217,11 @@ * we are running under cgroup control. */ void OSContainer::init() { - int mountid; - int parentid; - int major; - int minor; FILE *mntinfo = NULL; FILE *cgroup = NULL; char buf[MAXPATHLEN+1]; char tmproot[MAXPATHLEN+1]; char tmpmount[MAXPATHLEN+1]; - char tmpbase[MAXPATHLEN+1]; char *p; jlong mem_limit; @@ -260,85 +255,24 @@ return; } - while ( (p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) { - // Look for the filesystem type and see if it's cgroup - char fstype[MAXPATHLEN+1]; - fstype[0] = '\0'; - char *s = strstr(p, " - "); - if (s != NULL && - sscanf(s, " - %s", fstype) == 1 && - strcmp(fstype, "cgroup") == 0) { - - if (strstr(p, "memory") != NULL) { - int matched = sscanf(p, "%d %d %d:%d %s %s", - &mountid, - &parentid, - &major, - &minor, - tmproot, - tmpmount); - if (matched == 6) { - memory = new CgroupSubsystem(tmproot, tmpmount); - } - else - log_debug(os, container)("Incompatible str containing cgroup and memory: %s", p); - } else if (strstr(p, "cpuset") != NULL) { - int matched = sscanf(p, "%d %d %d:%d %s %s", - &mountid, - &parentid, - &major, - &minor, - tmproot, - tmpmount); - if (matched == 6) { - cpuset = new CgroupSubsystem(tmproot, tmpmount); - } - else { - log_debug(os, container)("Incompatible str containing cgroup and cpuset: %s", p); - } - } else if (strstr(p, "cpu,cpuacct") != NULL || strstr(p, "cpuacct,cpu") != NULL) { - int matched = sscanf(p, "%d %d %d:%d %s %s", - &mountid, - &parentid, - &major, - &minor, - tmproot, - tmpmount); - if (matched == 6) { - cpu = new CgroupSubsystem(tmproot, tmpmount); - cpuacct = new CgroupSubsystem(tmproot, tmpmount); - } - else { - log_debug(os, container)("Incompatible str containing cgroup and cpu,cpuacct: %s", p); - } - } else if (strstr(p, "cpuacct") != NULL) { - int matched = sscanf(p, "%d %d %d:%d %s %s", - &mountid, - &parentid, - &major, - &minor, - tmproot, - tmpmount); - if (matched == 6) { - cpuacct = new CgroupSubsystem(tmproot, tmpmount); - } - else { - log_debug(os, container)("Incompatible str containing cgroup and cpuacct: %s", p); - } - } else if (strstr(p, "cpu") != NULL) { - int matched = sscanf(p, "%d %d %d:%d %s %s", - &mountid, - &parentid, - &major, - &minor, - tmproot, - tmpmount); - if (matched == 6) { - cpu = new CgroupSubsystem(tmproot, tmpmount); - } - else { - log_debug(os, container)("Incompatible str containing cgroup and cpu: %s", p); - } + while ((p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) { + char tmpcgroups[MAXPATHLEN+1]; + char *cptr = tmpcgroups; + char *token; + + // mountinfo format is documented at https://www.kernel.org/doc/Documentation/filesystems/proc.txt + if (sscanf(p, "%*d %*d %*d:%*d %s %s %*[^-]- cgroup %*s %s", tmproot, tmpmount, tmpcgroups) != 3) { + continue; + } + while ((token = strsep(&cptr, ",")) != NULL) { + if (strcmp(token, "memory") == 0) { + memory = new CgroupSubsystem(tmproot, tmpmount); + } else if (strcmp(token, "cpuset") == 0) { + cpuset = new CgroupSubsystem(tmproot, tmpmount); + } else if (strcmp(token, "cpu") == 0) { + cpu = new CgroupSubsystem(tmproot, tmpmount); + } else if (strcmp(token, "cpuacct") == 0) { + cpuacct= new CgroupSubsystem(tmproot, tmpmount); } } } @@ -392,30 +326,30 @@ return; } - while ( (p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) { - int cgno; - int matched; - char *controller; + while ((p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) { + char *controllers; + char *token; char *base; /* Skip cgroup number */ strsep(&p, ":"); - /* Get controller and base */ - controller = strsep(&p, ":"); + /* Get controllers and base */ + controllers = strsep(&p, ":"); base = strsep(&p, "\n"); - if (controller != NULL) { - if (strstr(controller, "memory") != NULL) { + if (controllers == NULL) { + continue; + } + + while ((token = strsep(&controllers, ",")) != NULL) { + if (strcmp(token, "memory") == 0) { memory->set_subsystem_path(base); - } else if (strstr(controller, "cpuset") != NULL) { + } else if (strcmp(token, "cpuset") == 0) { cpuset->set_subsystem_path(base); - } else if (strstr(controller, "cpu,cpuacct") != NULL || strstr(controller, "cpuacct,cpu") != NULL) { + } else if (strcmp(token, "cpu") == 0) { cpu->set_subsystem_path(base); + } else if (strcmp(token, "cpuacct") == 0) { cpuacct->set_subsystem_path(base); - } else if (strstr(controller, "cpuacct") != NULL) { - cpuacct->set_subsystem_path(base); - } else if (strstr(controller, "cpu") != NULL) { - cpu->set_subsystem_path(base); } } }