View Javadoc
1   /*
2    * Copyright 2024 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 /sys} filesystem on Linux.
14   * <p>
15   * If the user desires to configure a custom {@code /sys} 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 SysPath {
20  
21      /**
22       * The /sys filesystem location.
23       */
24      public static final String SYS = querySysConfig() + "/";
25  
26      public static final String CPU = SYS + "devices/system/cpu/";
27      public static final String DMI_ID = SYS + "devices/virtual/dmi/id/";
28      public static final String NET = SYS + "class/net/";
29      public static final String MODEL = SYS + "firmware/devicetree/base/model";
30      public static final String POWER_SUPPLY = SYS + "class/power_supply";
31      public static final String HWMON = SYS + "class/hwmon/";
32      public static final String THERMAL = SYS + "class/thermal/thermal_zone";
33  
34      private SysPath() {
35      }
36  
37      private static String querySysConfig() {
38          String sysPath = GlobalConfig.get(GlobalConfig.OSHI_UTIL_SYS_PATH, "/sys");
39          // Ensure prefix begins with path separator, but doesn't end with one
40          sysPath = '/' + sysPath.replaceAll("/$|^/", "");
41          if (!new File(sysPath).exists()) {
42              throw new GlobalConfig.PropertyException(GlobalConfig.OSHI_UTIL_SYS_PATH, "The path does not exist");
43          }
44          return sysPath;
45      }
46  }