diff --git a/cereal b/cereal index bab2f2b9..48795401 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit bab2f2b95e38da4c968efc31369163056e938b30 +Subproject commit 48795401f2153ef0519e67f5f70231f587b5c971 diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 8bf11689..87d338ed 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -315,18 +315,7 @@ int Params::readAll(std::map *params) { std::lock_guard lk(file_lock); std::string key_path = params_path + "/d"; - DIR *d = opendir(key_path.c_str()); - if (!d) return -1; - - struct dirent *de = NULL; - while ((de = readdir(d))) { - if (isalnum(de->d_name[0])) { - (*params)[de->d_name] = util::read_file(key_path + "/" + de->d_name); - } - } - - closedir(d); - return 0; + return util::read_files_in_dir(key_path, params); } void Params::clearAll(ParamKeyType key_type) { diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index e26acc45..689a6a90 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -72,6 +72,20 @@ std::string read_file(const std::string& fn) { return buffer.str(); } +int read_files_in_dir(std::string path, std::map *contents) { + DIR *d = opendir(path.c_str()); + if (!d) return -1; + + struct dirent *de = NULL; + while ((de = readdir(d))) { + if (isalnum(de->d_name[0])) { + (*contents)[de->d_name] = util::read_file(path + "/" + de->d_name); + } + } + + return 0; +} + int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) { int fd = open(path, flags, mode); if (fd == -1) { @@ -82,4 +96,5 @@ int write_file(const char* path, const void* data, size_t size, int flags, mode_ return (n >= 0 && (size_t)n == size) ? 0 : -1; } + } // namespace util diff --git a/selfdrive/common/util.h b/selfdrive/common/util.h index f7a7d550..d4d27fb9 100644 --- a/selfdrive/common/util.h +++ b/selfdrive/common/util.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #ifndef sighandler_t typedef void (*sighandler_t)(int sig); @@ -53,6 +55,8 @@ inline std::string string_format(const std::string& format, Args... args) { std::string read_file(const std::string &fn); +int read_files_in_dir(std::string path, std::map *contents); + int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0777); inline std::string tohex(const uint8_t* buf, size_t buf_size) { diff --git a/selfdrive/loggerd/bootlog.cc b/selfdrive/loggerd/bootlog.cc index 74f61323..a54c3be0 100644 --- a/selfdrive/loggerd/bootlog.cc +++ b/selfdrive/loggerd/bootlog.cc @@ -12,11 +12,18 @@ static kj::Array build_boot_log() { boot.setWallTimeNanos(nanos_since_epoch()); - std::string lastKmsg = util::read_file("/sys/fs/pstore/console-ramoops"); - boot.setLastKmsg(capnp::Data::Reader((const kj::byte*)lastKmsg.data(), lastKmsg.size())); + std::string pstore = "/sys/fs/pstore"; + std::map pstore_map; + util::read_files_in_dir(pstore, &pstore_map); - std::string lastPmsg = util::read_file("/sys/fs/pstore/pmsg-ramoops-0"); - boot.setLastPmsg(capnp::Data::Reader((const kj::byte*)lastPmsg.data(), lastPmsg.size())); + auto lpstore = boot.initPstore().initEntries(pstore_map.size()); + int i = 0; + for (auto& kv : pstore_map) { + auto lentry = lpstore[i]; + lentry.setKey(kv.first); + lentry.setValue(capnp::Data::Reader((const kj::byte*)kv.second.data(), kv.second.size())); + i++; + } std::string launchLog = util::read_file("/tmp/launch_log"); boot.setLaunchLog(capnp::Text::Reader(launchLog.data(), launchLog.size())); diff --git a/selfdrive/loggerd/tests/test_loggerd.py b/selfdrive/loggerd/tests/test_loggerd.py index 6bc6fa9d..f9823629 100755 --- a/selfdrive/loggerd/tests/test_loggerd.py +++ b/selfdrive/loggerd/tests/test_loggerd.py @@ -153,12 +153,12 @@ class TestLoggerd(unittest.TestCase): assert abs(boot.wallTimeNanos - time.time_ns()) < 5*1e9 # within 5s assert boot.launchLog == launch_log - for field, path in [("lastKmsg", "console-ramoops"), ("lastPmsg", "pmsg-ramoops-0")]: - path = Path(os.path.join("/sys/fs/pstore/", path)) - val = b"" + for fn in ["console-ramoops", "pmsg-ramoops-0"]: + path = Path(os.path.join("/sys/fs/pstore/", fn)) if path.is_file(): - val = open(path, "rb").read() - self.assertEqual(getattr(boot, field), val) + expected_val = open(path, "rb").read() + bootlog_val = [e.value for e in boot.pstore.entries if e.key == fn][0] + self.assertEqual(expected_val, bootlog_val) def test_qlog(self): qlog_services = [s for s in CEREAL_SERVICES if service_list[s].decimation is not None]