Skip to content

Commit 532ea33

Browse files
committed
Refactor mount info matching to helper function
1 parent 1d1a42e commit 532ea33

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

src/hotspot/os/linux/cgroupSubsystem_linux.cpp

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,46 @@ static bool find_ro_opt(char* mount_opts) {
170170
return false;
171171
}
172172

173+
/*
174+
* Read values of a /proc/self/mountinfo line into variables. For cgroups v1
175+
* super options are needed. On cgroups v2 super options are not used.
176+
*
177+
* The scanning of a single mountinfo line entry is as follows:
178+
*
179+
* 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
180+
* (1) (2) (3):(4) (5) (6) (7) (8) (9) (10) (11) (12)
181+
*
182+
* The numbers in parentheses are labels for the descriptions below:
183+
*
184+
* (1) mount ID: matched with '%*d' and discarded
185+
* (2) parent ID: matched with '%*d' and discarded
186+
* (3) major: ---,---> major, minor separated by ':'. matched with '%*d:%*d' and discarded
187+
* (4) minor: ---'
188+
* (5) root: matched with '%s' and captured in 'tmproot'. Must be non-empty.
189+
* (6) mount point: matched with '%s' and captured in 'tmpmount'. Must be non-empty.
190+
* (7) mount options: matched with '%s' and captured in 'mount_opts'. Must be non-empty.
191+
* (8) optional fields: ---,---> matched with '%*[^-]-'. Anything not a hyphen, followed by a hyphen
192+
* (9) separator: ---' and discarded. Note: The discarded match is space characters if there
193+
* are no optionals. Otherwise it includes the optional fields as well.
194+
* (10) filesystem type: matched with '%s' and captured in 'tmp_fs_type'
195+
* (11) mount source: matched with '%*s' and discarded
196+
* (12) super options: matched with '%s' and captured in 'tmpcgroups'
197+
*/
198+
static inline bool match_mount_info_line(char* line,
199+
char* tmproot,
200+
char* tmpmount,
201+
char* mount_opts,
202+
char* tmp_fs_type,
203+
char* tmpcgroups) {
204+
return sscanf(line,
205+
"%*d %*d %*d:%*d %s %s %s%*[^-]- %s %*s %s",
206+
tmproot,
207+
tmpmount,
208+
mount_opts,
209+
tmp_fs_type,
210+
tmpcgroups) == 5;
211+
}
212+
173213
bool CgroupSubsystemFactory::determine_type(CgroupInfo* cg_infos,
174214
const char* proc_cgroups,
175215
const char* proc_self_cgroup,
@@ -349,29 +389,13 @@ bool CgroupSubsystemFactory::determine_type(CgroupInfo* cg_infos,
349389
*
350390
* We collect the read only mount option in the cgroup infos so as to have that
351391
* info ready when determining is_containerized().
352-
*
353-
* The scanning of a single mountinfo line entry is as follows:
354-
*
355-
* 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
356-
* (1) (2) (3):(4) (5) (6) (7) (8) (9) (10) (11) (12)
357-
*
358-
* The numbers in parentheses are labels for the descriptions below:
359-
*
360-
* (1) mount ID: matched with '%*d' and discarded
361-
* (2) parent ID: matched with '%*d' and discarded
362-
* (3) major: ---,---> major, minor separated by ':'. matched with '%*d:%*d' and discarded
363-
* (4) minor: ---'
364-
* (5) root: matched with '%s' and captured in 'tmproot'. Must be non-empty.
365-
* (6) mount point: matched with '%s' and captured in 'tmpmount'. Must be non-empty.
366-
* (7) mount options: matched with '%s' and captured in 'mount_opts'. Must be non-empty.
367-
* (8) optional fields: ---,---> matched with '%*[^-]-'. Anything not a hyphen, followed by a hyphen
368-
* (9) separator: ---' and discarded. Note: The discarded match is space characters if there
369-
* are no optionals. Otherwise it includes the optional fields as well.
370-
* (10) filesystem type: matched with '%s' and captured in 'tmp_fs_type'
371-
* (11) mount source: matched with '%*s' and discarded
372-
* (12) super options: matched with '%*s' and discarded
373392
*/
374-
if (is_cgroupsV2 && sscanf(p, "%*d %*d %*d:%*d %s %s %s%*[^-]- %s %*s %*s", tmproot, tmpmount, mount_opts, tmp_fs_type) == 4) {
393+
if (is_cgroupsV2 && match_mount_info_line(p,
394+
tmproot,
395+
tmpmount,
396+
mount_opts,
397+
tmp_fs_type,
398+
tmpcgroups /* unused */)) {
375399
// we likely have an early match return (e.g. cgroup fs match), be sure we have cgroup2 as fstype
376400
if (strcmp("cgroup2", tmp_fs_type) == 0) {
377401
cgroupv2_mount_point_found = true;
@@ -398,28 +422,8 @@ bool CgroupSubsystemFactory::determine_type(CgroupInfo* cg_infos,
398422
*
399423
* 44 31 0:39 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:23 - cgroup cgroup rw,pids
400424
*
401-
* The scanning of a single mountinfo line entry is as follows:
402-
*
403-
* 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
404-
* (1) (2) (3):(4) (5) (6) (7) (8) (9) (10) (11) (12)
405-
*
406-
* The numbers in parentheses are labels for the descriptions below:
407-
*
408-
* (1) mount ID: matched with '%*d' and discarded
409-
* (2) parent ID: matched with '%*d' and discarded
410-
* (3) major: ---,---> major, minor separated by ':'. matched with '%*d:%*d' and discarded
411-
* (4) minor: ---'
412-
* (5) root: matched with '%s' and captured in 'tmproot'. Must be non-empty.
413-
* (6) mount point: matched with '%s' and captured in 'tmpmount'. Must be non-empty.
414-
* (7) mount options: matched with '%s' and captured in 'mount_opts'. Must be non-empty.
415-
* (8) optional fields: ---,---> matched with '%*[^-]-'. Anything not a hyphen, followed by a hyphen
416-
* (9) separator: ---' and discarded. Note: The discarded match is space characters if there
417-
* are no optionals. Otherwise it includes the optional fields as well.
418-
* (10) filesystem type: matched with '%s' and captured in 'tmp_fs_type'
419-
* (11) mount source: matched with '%*s' and discarded
420-
* (12) super options: matched with '%s' and captured in 'tmpcgroups'
421425
*/
422-
if (sscanf(p, "%*d %*d %*d:%*d %s %s %s%*[^-]- %s %*s %s", tmproot, tmpmount, mount_opts, tmp_fs_type, tmpcgroups) == 5) {
426+
if (match_mount_info_line(p, tmproot, tmpmount, mount_opts, tmp_fs_type, tmpcgroups)) {
423427
if (strcmp("cgroup", tmp_fs_type) != 0) {
424428
// Skip cgroup2 fs lines on hybrid or unified hierarchy.
425429
continue;

0 commit comments

Comments
 (0)