22
33import de .thetaphi .forbiddenapis .SuppressForbidden ;
44import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
5+ import java .io .File ;
56import java .io .IOException ;
67import java .nio .file .Files ;
7- import java .nio .file .Path ;
88import java .nio .file .Paths ;
99import java .text .ParseException ;
1010import java .util .ArrayList ;
@@ -28,9 +28,9 @@ public class ContainerInfo {
2828
2929 private static final Logger log = LoggerFactory .getLogger (ContainerInfo .class );
3030
31- private static final Path CGROUP_DEFAULT_PROCFILE = Paths . get ( "/proc/self/cgroup" ) ;
32- private static final Path DEFAULT_CGROUP_MOUNT_PATH = Paths . get ( "/sys/fs/cgroup" ) ;
33- private static final Path HOST_GROUP_NAMESPACE = Paths . get ( "/proc/self/ns/cgroup" ) ;
31+ private static final String CGROUP_DEFAULT_PROCFILE = "/proc/self/cgroup" ;
32+ private static final String DEFAULT_CGROUP_MOUNT_PATH = "/sys/fs/cgroup" ;
33+ private static final String HOST_GROUP_NAMESPACE = "/proc/self/ns/cgroup" ;
3434
3535 // The second part is the PCF/Garden regexp. We assume no suffix ($) to avoid matching pod UIDs
3636 // See https://github.com/DataDog/datadog-agent/blob/7.40.x/pkg/util/cgroups/reader.go#L50
@@ -106,7 +106,7 @@ public void setcGroups(List<CGroupInfo> cGroups) {
106106 }
107107
108108 static @ Nullable String readEntityID (
109- ContainerInfo containerInfo , boolean isHostCgroupNamespace , Path cgroupMountPath ) {
109+ ContainerInfo containerInfo , boolean isHostCgroupNamespace , String cgroupMountPath ) {
110110 String cid = containerInfo .getContainerId ();
111111 if (cid != null && !cid .isEmpty ()) {
112112 return "cid-" + cid ;
@@ -129,13 +129,18 @@ static boolean isHostCgroupNamespace() {
129129 * Returns the inode of the cgroup controller if it exists, otherwise an empty string. The inode
130130 * is prefixed with "in-" and is used by the agent to retrieve the container ID.
131131 */
132- static @ Nullable String getCgroupInode (Path cgroupMountPath , List <CGroupInfo > cgroups ) {
132+ static @ Nullable String getCgroupInode (String cgroupMountPath , List <CGroupInfo > cgroups ) {
133133 // It first tries to get the cGroupV1 "memory" controller inode, and if that fails, it tries to
134134 // get the cGroupV2 inode.
135135 for (String controller : Arrays .asList (CGROUPV1_BASE_CONTROLLER , CGROUPV2_BASE_CONTROLLER )) {
136136 for (CGroupInfo cgroup : cgroups ) {
137137 if (cgroup .getControllers ().contains (controller )) {
138- Path path = Paths .get (cgroupMountPath .toString (), controller , cgroup .getPath ());
138+ String path =
139+ cgroupMountPath
140+ + File .pathSeparatorChar
141+ + controller
142+ + File .pathSeparatorChar
143+ + cgroup .getPath ();
139144 long inode = readInode (path );
140145 // ignore invalid and root inode
141146 if (inode > 2 ) {
@@ -148,9 +153,10 @@ static boolean isHostCgroupNamespace() {
148153 }
149154
150155 /** @return 0 - if it couldn't read inode */
151- static long readInode (Path path ) {
156+ static long readInode (String path ) {
152157 try {
153- return (long ) Files .getAttribute (path , "unix:ino" );
158+ File f = new File (path );
159+ return f .exists () ? (long ) Files .getAttribute (f .toPath (), "unix:ino" ) : 0L ;
154160 } catch (Exception e ) {
155161 log .debug ("Unable to read cgroup inode of {} because of {}" , path , e .getClass ());
156162 }
@@ -214,15 +220,15 @@ public static String getEntityId() {
214220 }
215221
216222 public static boolean isRunningInContainer () {
217- return Files . isReadable (CGROUP_DEFAULT_PROCFILE );
223+ return new File (CGROUP_DEFAULT_PROCFILE ). canRead ( );
218224 }
219225
220226 public static ContainerInfo fromDefaultProcFile () throws IOException , ParseException {
221227 return fromProcFile (CGROUP_DEFAULT_PROCFILE );
222228 }
223229
224- static ContainerInfo fromProcFile (Path path ) throws IOException , ParseException {
225- final String content = new String (Files .readAllBytes (path ));
230+ static ContainerInfo fromProcFile (String path ) throws IOException , ParseException {
231+ final String content = new String (Files .readAllBytes (Paths . get ( path ) ));
226232 if (content .isEmpty ()) {
227233 log .debug ("Proc file is empty" );
228234 return new ContainerInfo ();
0 commit comments