From 1a57cf1d272567ba8c7ccccddd114363e5489213 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Tue, 8 Jan 2019 14:05:15 +0100 Subject: [PATCH] Use systemd API to decode unit name Change-Id: I07538d74d9fcb97f29a0a884a0c728aac85d9807 --- packaging/ode.spec | 1 + server/CMakeLists.txt | 3 ++- server/internal-encryption.cpp | 36 ++++++++++++------------------------ 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/packaging/ode.spec b/packaging/ode.spec index 1eb0b31..dde5b9c 100755 --- a/packaging/ode.spec +++ b/packaging/ode.spec @@ -19,6 +19,7 @@ BuildRequires: pkgconfig(libcrypto) BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(blkid) BuildRequires: pkgconfig(capi-system-device) +BuildRequires: pkgconfig(libsystemd) Requires: cryptsetup %global key_storage_plugin_dir %{_libdir}/ode-key-storage-plugin/ diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index acc97bb..395027f 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +# Copyright (c) 2015-2019 Samsung Electronics Co., Ltd All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ SET(DEPENDENCY klay libcrypto libsmack capi-system-device + libsystemd ) SET(SERVER_NAME ${PROJECT_NAME}d) diff --git a/server/internal-encryption.cpp b/server/internal-encryption.cpp index b115034..96b4049 100644 --- a/server/internal-encryption.cpp +++ b/server/internal-encryption.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015-2019 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ #include #include #include +#include #include "misc.h" #include "logger.h" @@ -144,28 +145,6 @@ void JobWatch::jobRemoved(const dbus::Variant& parameters) jobsCv.notify_one(); } -std::string getDecodedPath(const std::string &path, const std::string &prefix) -{ - std::string ret = path; - size_t pos = 0; - - pos = ret.find(prefix); - if (pos != std::string::npos) - ret = ret.substr(prefix.size(), ret.size()); - - pos = 0; - while ((pos = ret.find("_", pos)) != std::string::npos) { - int a = std::stoi(std::string(1, ret.at(pos+1)), nullptr, 16); - int b = std::stoi(std::string(1, ret.at(pos+2)), nullptr, 16); - if (a < 0 || b < 0) - ret.replace(pos, 3, "_"); - else - ret.replace(pos, 3, std::string(1, ((a << 4) | b))); - pos += 1; - } - return ret; -} - void stopSystemdUnits() { dbus::Connection& systemDBus = dbus::Connection::getSystem(); @@ -238,7 +217,16 @@ void stopSystemdUnits() -1, "(o)", "(u)", (unsigned int)pid) .get("(o)", &unit); - auto unescapedName = getDecodedPath(unit, "/org/freedesktop/systemd1/unit/"); + char* tmp = NULL; + int err = sd_bus_path_decode(unit, "/org/freedesktop/systemd1/unit", &tmp); + if (err <= 0 || tmp == NULL) { + ERROR(SINK, "Failed to decode unit name: " + std::string(unit) + " error: " + + std::to_string(err)); + continue; + } + std::string unescapedName(tmp); + free(tmp); + if (unescapedName.find("dbus", 0, 4) == std::string::npos) unitsToStop.insert(unescapedName); } catch (runtime::Exception &e) { -- 2.7.4