Use systemd API to decode unit name 84/197084/2
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 8 Jan 2019 13:05:15 +0000 (14:05 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 9 Jan 2019 12:45:08 +0000 (13:45 +0100)
Change-Id: I07538d74d9fcb97f29a0a884a0c728aac85d9807

packaging/ode.spec
server/CMakeLists.txt
server/internal-encryption.cpp

index 1eb0b31..dde5b9c 100755 (executable)
@@ -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/
index acc97bb..395027f 100644 (file)
@@ -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)
index b115034..96b4049 100644 (file)
@@ -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 <klay/filesystem.h>
 #include <klay/dbus/connection.h>
 #include <klay/error.h>
+#include <systemd/sd-bus.h>
 
 #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) {