Fix: Check if file exist before umount is made 03/156403/1
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 18 Oct 2017 08:13:31 +0000 (10:13 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 18 Oct 2017 08:13:31 +0000 (10:13 +0200)
Change-Id: I03aaa60dd23021fd19d716ccf995a0ff737f108c

src/common/filesystem.cpp
src/common/include/filesystem.h
src/common/service_impl.cpp

index dda400c..fe44295 100644 (file)
@@ -167,6 +167,20 @@ int removeDirectory(const std::string &path)
     return SECURITY_MANAGER_SUCCESS;
 }
 
+int fileStatus(const std::string &path)
+{
+    struct stat st;
+    if (-1 == lstat(path.c_str(), &st)) {
+        if (ENOENT == errno)
+            return 0;  // no, file does not exist
+        return -1;     // error
+
+    }
+    if (S_ISREG(st.st_mode))
+        return 1;      // yes, file exists
+    return -1;         // this is not a file -> error
+}
+
 int createFile(const std::string &path)
 {
     int fd = open(path.c_str(), O_RDONLY|O_CREAT, 0644);
index ee681aa..ba0fe36 100644 (file)
@@ -43,6 +43,7 @@ std::string getParentDirectoryName(const std::string &path);
 int directoryStatus(const std::string &path);
 int createDirectory(const std::string &path);
 int removeDirectory(const std::string &path);
+int fileStatus(const std::string &path);
 int createFile(const std::string &path);
 int removeFile(const std::string &path);
 
index a8278f2..b07ce23 100644 (file)
@@ -1973,11 +1973,13 @@ int ServiceImpl::appBindNamespace(const Credentials &creds, const std::string &a
         }
     }
 
-    ret = FS::createFile(appFile);
-    if (ret != SECURITY_MANAGER_SUCCESS)
-        return ret;
-
-    MountNS::uMount(appFile);
+    if (FS::fileStatus(appFile) == 0) { // file does not exist
+        ret = FS::createFile(appFile);
+        if (ret != SECURITY_MANAGER_SUCCESS)
+            return ret;
+    } else {
+        MountNS::uMount(appFile);
+    }
 
     ret = MountNS::bindMount(procFile, appFile);
     if (ret != SECURITY_MANAGER_SUCCESS) {