Notify and throw if cryptsetup fails 18/149118/1
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Mon, 11 Sep 2017 11:14:45 +0000 (13:14 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Mon, 11 Sep 2017 12:23:29 +0000 (14:23 +0200)
Fix bracket's style in the same function

Change-Id: I22973c995cc489124c57acac23d531605e9f3700

server/engine/encryption/cryptsetup-engine.cpp

index 5ad0f39..ef7d32b 100644 (file)
@@ -44,21 +44,24 @@ void forkAndWrite(const char *const* argv, const CryptsetupEngine::data *d)
        // pipe
        int pipefd[2];
        if (d != NULL) {
-               if (0 != pipe(pipefd))
+               if (0 != pipe(pipefd)) {
                        throw runtime::Exception("pipe() failed:" + std::to_string(errno));
+               }
        }
 
        pid_t pid = ::fork();
-       if (pid == -1)
+       if (pid == -1) {
                throw runtime::Exception("fork() failed: " + std::to_string(errno));
+       }
 
        if (pid == 0) {
                if (d != NULL) {
                        ::close(pipefd[1]);
 
                        int fd = TEMP_FAILURE_RETRY(dup2(pipefd[0], STDIN_FILENO));
-                       if (fd == -1)
+                       if (fd == -1) {
                                exit(EXIT_FAILURE);
+                       }
 
                        ::close(pipefd[0]);
                }
@@ -72,8 +75,9 @@ void forkAndWrite(const char *const* argv, const CryptsetupEngine::data *d)
 
                // write the pipefd[1]
                ssize_t ret = TEMP_FAILURE_RETRY(write(pipefd[1], d->data(), d->size()));
-               if (ret < 0 || (size_t)ret != d->size())
+               if (ret < 0 || (size_t)ret != d->size()) {
                        throw runtime::Exception("write() failed");
+               }
 
                ::close(pipefd[1]);
        }
@@ -84,6 +88,13 @@ void forkAndWrite(const char *const* argv, const CryptsetupEngine::data *d)
                        throw runtime::Exception("waitpid() failed: " + std::to_string(errno));
                }
        }
+       if (!WIFEXITED(status)) {
+               throw runtime::Exception(std::string(argv[0]) + " terminated abnormally");
+       }
+       int ret = WEXITSTATUS(status);
+       if (ret != 0) {
+               throw runtime::Exception(std::string(argv[0]) + " failed with: " + std::to_string(ret));
+       }
 }
 
 } // anonymous namespace