config-file: search multiple directories
Restricting the config file to `/usr/share` makes testing difficult
and prevents the config file from being present on a, typically for
the BMC, read-write volume. Change the config loading code to search:
1. pwd
2. /var/lib/phosphor-virtual-sensor
3. /usr/share/phosphor-virtual-sensor
Tested:
Ran strace on the changes and observed the expected file search.
```
getcwd("pwd/phosphor-virtual-sensor/builddir", 4096) = 75
newfstatat(AT_FDCWD, "pwd/phosphor-virtual-sensor/builddir/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/var/lib/phosphor-virtual-sensor/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/usr/share/phosphor-virtual-sensor/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I70854d565a3b13cd48c27a80da84de54c00a0603
diff --git a/README.md b/README.md
index 7421b48..4bfe564 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,20 @@
# phosphor-virtual-sensor
-phosphor-virtual-sensor reads in virtual_sensor_config.json
-There are two types of data in this file:
+phosphor-virtual-sensor reads the configuration file
+`virtual_sensor_config.json` from one of three locations:
+
+1. The current directory.
+2. `/var/lib/phosphor-virtual-sensor`
+3. `/usr/share/phosphor-virtual-sensor`
+
+By default the repository will install a sample config into (3).
+
+There are two types of data in this file.
## virtual sensor configuration information
-See virtual_sensor_config.json in this repository for an example. Sensors added
-this way can use any expression that is accepted by exprtk.
+See `virtual_sensor_config.json` in this repository for an example. Sensors
+added this way can use any expression that is accepted by exprtk.
## information to get a virtual sensor configuraton from D-Bus
@@ -21,14 +29,14 @@
}
```
-Sensors added this way can only use a set of restricted calculations. At this
-stage the only type supported is modifiedMedian.
+Sensors added this way can only use a set of restricted calculations. Currently
+the only type supported is `modifiedMedian`.
-The virtual sensor configuration information needs to be added into the
-relevant hardware configuration file in entity-manager. This method of adding a
-virtual sensor allows a recipe that builds for different hardware
-configurations to have different virtual sensors for each configuration.
+The virtual sensor configuration information needs to be added into the relevant
+hardware configuration file in entity-manager. This method of adding a virtual
+sensor allows a recipe that builds for different hardware configurations to have
+different virtual sensors for each configuration.
-The virtual sensor configuration in entity manager follows a different format
-to the JSON in virtual_sensor_config.json (specified in
+The virtual sensor configuration in entity manager follows a different format to
+the JSON in `virtual_sensor_config.json` (specified in
[entity-manager/schemas/VirtualSensor.json](https://github.com/openbmc/entity-manager/blob/master/schemas/virtual_sensor.json)).
diff --git a/meson.build b/meson.build
index 05fbfc3..40362a3 100644
--- a/meson.build
+++ b/meson.build
@@ -55,19 +55,7 @@
meson.project_name(),
)
-configfile = 'virtual_sensor_config.json'
-confpath = '"' + join_paths(
- packagedir,
- configfile,
-) + '"'
-
-install_data(sources : configfile, install_dir : packagedir)
-
-conf_data = configuration_data()
-conf_data.set('VIRTUAL_SENSOR_CONFIG_FILE', confpath)
-
-configure_file(output : 'config.hpp',
- configuration : conf_data)
+install_data(sources : 'virtual_sensor_config.json', install_dir : packagedir)
systemd = dependency('systemd')
conf_data = configuration_data()
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index e2e0399..e8fc218 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -1,7 +1,5 @@
#include "virtualSensor.hpp"
-#include "config.hpp"
-
#include <phosphor-logging/lg2.hpp>
#include <fstream>
@@ -772,8 +770,25 @@
}
/** @brief Parsing Virtual Sensor config JSON file */
-Json VirtualSensors::parseConfigFile(const std::string& configFile)
+Json VirtualSensors::parseConfigFile()
{
+ using path = std::filesystem::path;
+ auto configFile = []() -> path {
+ static constexpr auto name = "virtual_sensor_config.json";
+
+ for (auto pathSeg : {std::filesystem::current_path(),
+ path{"/var/lib/phosphor-virtual-sensor"},
+ path{"/usr/share/phosphor-virtual-sensor"}})
+ {
+ auto file = pathSeg / name;
+ if (std::filesystem::exists(file))
+ {
+ return file;
+ }
+ }
+ return name;
+ }();
+
std::ifstream jsonFile(configFile);
if (!jsonFile.is_open())
{
@@ -955,7 +970,7 @@
{
static const Json empty{};
- auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE);
+ auto data = parseConfigFile();
// print values
if (DEBUG)
diff --git a/virtualSensor.hpp b/virtualSensor.hpp
index d85303a..921aee2 100644
--- a/virtualSensor.hpp
+++ b/virtualSensor.hpp
@@ -281,7 +281,7 @@
/** @brief Get virual sensor config from DBus**/
ManagedObjectType getObjectsFromDBus();
/** @brief Parsing virtual sensor config JSON file */
- Json parseConfigFile(const std::string& configFile);
+ Json parseConfigFile();
/** @brief Matches for virtual sensors */
std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;