View Javadoc
1   /*
2    * Copyright 2020-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util.platform.linux;
6   
7   import java.io.File;
8   
9   import oshi.annotation.concurrent.ThreadSafe;
10  import oshi.util.GlobalConfig;
11  
12  /**
13   * Provides constants for paths in the {@code /proc} filesystem on Linux.
14   * <p>
15   * If the user desires to configure a custom {@code /proc} path, it must be declared in the OSHI configuration file or
16   * updated in the {@link GlobalConfig} class prior to initializing this class.
17   */
18  @ThreadSafe
19  public final class ProcPath {
20  
21      /**
22       * The /proc filesystem location.
23       */
24      public static final String PROC = queryProcConfig();
25  
26      public static final String ASOUND = PROC + "/asound/";
27      public static final String AUXV = PROC + "/self/auxv";
28      public static final String CPUINFO = PROC + "/cpuinfo";
29      public static final String DISKSTATS = PROC + "/diskstats";
30      public static final String MEMINFO = PROC + "/meminfo";
31      public static final String MODEL = PROC + "/device-tree/model";
32      public static final String MOUNTS = PROC + "/mounts";
33      public static final String NET = PROC + "/net";
34      public static final String PID_CMDLINE = PROC + "/%d/cmdline";
35      public static final String PID_CWD = PROC + "/%d/cwd";
36      public static final String PID_EXE = PROC + "/%d/exe";
37      public static final String PID_ENVIRON = PROC + "/%d/environ";
38      public static final String PID_FD = PROC + "/%d/fd";
39      public static final String PID_IO = PROC + "/%d/io";
40      public static final String PID_STAT = PROC + "/%d/stat";
41      public static final String PID_STATM = PROC + "/%d/statm";
42      public static final String PID_STATUS = PROC + "/%d/status";
43      public static final String SELF_STAT = PROC + "/self/stat";
44      public static final String STAT = PROC + "/stat";
45      public static final String SYS_FS_FILE_NR = PROC + "/sys/fs/file-nr";
46      public static final String SYS_FS_FILE_MAX = PROC + "/sys/fs/file-max";
47      public static final String TASK_PATH = PROC + "/%d/task";
48      public static final String TASK_COMM = TASK_PATH + "/%d/comm";
49      public static final String TASK_STATUS = TASK_PATH + "/%d/status";
50      public static final String TASK_STAT = TASK_PATH + "/%d/stat";
51      public static final String THREAD_SELF = PROC + "/thread-self";
52      public static final String UPTIME = PROC + "/uptime";
53      public static final String VERSION = PROC + "/version";
54      public static final String VMSTAT = PROC + "/vmstat";
55  
56      private ProcPath() {
57      }
58  
59      private static String queryProcConfig() {
60          String procPath = GlobalConfig.get(GlobalConfig.OSHI_UTIL_PROC_PATH, "/proc");
61          // Ensure prefix begins with path separator, but doesn't end with one
62          procPath = '/' + procPath.replaceAll("/$|^/", "");
63          if (!new File(procPath).exists()) {
64              throw new GlobalConfig.PropertyException(GlobalConfig.OSHI_UTIL_PROC_PATH, "The path does not exist");
65          }
66          return procPath;
67      }
68  }