refactoring: use common function template for getting label by libsmack 29/61929/2
authorRafal Krypa <r.krypa@samsung.com>
Fri, 11 Mar 2016 08:10:21 +0000 (09:10 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Wed, 20 Apr 2016 15:35:43 +0000 (17:35 +0200)
Merging repeated code pattern where a libsmack function is used to fetch
Smack label, the result must be wrapped into std::string and memory
allocated by libsmack safely freed.

Change-Id: I67136fc5f78fd7974d27feafb0ee2d3164df9461
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
src/common/smack-labels.cpp

index 6674589..6f16901 100755 (executable)
@@ -259,36 +259,35 @@ std::string generateSharedPrivateLabel(const std::string &pkgName, const std::st
     return label;
 }
 
-std::string getSmackLabelFromSocket(int socketFd)
+template<typename FuncType, typename... ArgsType>
+static std::string getSmackLabel(FuncType func, ArgsType... args)
 {
-    char *label = nullptr;
-    ssize_t labelSize = smack_new_label_from_socket(socketFd, &label);
-    std::unique_ptr<char, void(*)(void *)> labelPtr(label, free);
-
-    if (labelSize < 0) {
-        ThrowMsg(SmackException::Base,
-                "smack_new_label_from_socket error for socket: " << socketFd);
-    }
+    char *label;
+    ssize_t labelLen = func(args..., &label);
+    if (labelLen <= 0)
+        ThrowMsg(SmackException::Base, "Error while getting Smack label");
+    std::unique_ptr<char, decltype(free)*> labelPtr(label, free);
+    return std::string(labelPtr.get(), labelLen);
+}
 
-    return std::string(labelPtr.get(), labelSize);
+std::string getSmackLabelFromSocket(int socketFd)
+{
+    return getSmackLabel(&smack_new_label_from_socket, socketFd);
 }
 
 std::string getSmackLabelFromPath(const std::string &path)
 {
-    char *label = nullptr;
-    ssize_t labelSize = smack_new_label_from_path(path.c_str(), XATTR_NAME_SMACK, true, &label);
-    std::unique_ptr<char, void(*)(void *)> labelPtr(label, free);
-
-    if (labelSize < 0) {
-        ThrowMsg(SmackException::FileError,
-                "smack_new_label_from_path error for path: " << path);
-    }
+    return getSmackLabel(&smack_new_label_from_path, path.c_str(), XATTR_NAME_SMACK, true);
+}
 
-    return std::string(labelPtr.get(), labelSize);
+std::string getSmackLabelFromSelf(void)
+{
+    return getSmackLabel(&smack_new_label_from_self);
 }
 
 std::string getSmackLabelFromPid(pid_t pid)
 {
+    // FIXME: libsmack should provide such a function
     std::ifstream smackFileStream("/proc/" + std::to_string(pid) + "/attr/current");
     if (!smackFileStream.is_open())
         ThrowMsg(SmackException::FileError,
@@ -305,17 +304,6 @@ std::string getSmackLabelFromPid(pid_t pid)
     return result;
 }
 
-std::string getSmackLabelFromSelf(void)
-{
-    char *label = nullptr;
-    ssize_t labelSize = smack_new_label_from_self(&label);
-    if (labelSize <= 0)
-        ThrowMsg(SmackException::InvalidLabel, "Error while fetching Smack label for current process");
-
-    std::unique_ptr<char, decltype(free)*> labelPtr(label, free);
-    return std::string(labelPtr.get(), labelSize);
-}
-
 std::string generateAuthorLabel(const int authorId)
 {
     if (authorId < 0) {