From: seolheui, kim Date: Mon, 14 Jan 2019 11:06:04 +0000 (+0900) Subject: Print force killed process list for debugging X-Git-Tag: submit/tizen_4.0/20190130.090735^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9db2a689d1b3ff707dedaa5f15a4a44ceb61ca90;p=platform%2Fcore%2Fsecurity%2Fode.git Print force killed process list for debugging Change-Id: Ia4105b6047b4ec419aa83a557114df1844fe8d77 Signed-off-by: seolheui, kim --- diff --git a/server/internal-encryption.cpp b/server/internal-encryption.cpp index 8f40090..7e4ce24 100644 --- a/server/internal-encryption.cpp +++ b/server/internal-encryption.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -311,16 +312,67 @@ void setOptions(unsigned int options) ::vconf_set_bool(VCONFKEY_ODE_FAST_ENCRYPTION, value); } -void execAndWait(const std::string &path, std::vector &args) +void killProcesses(std::vector &args) { - runtime::Process proc(path, args); - int ret = proc.execute(); - if (ret < 0) - ERROR(SINK, "Executing " + path + " failed for " + args.back()); - - ret = proc.waitForFinished(); - if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) - ERROR(SINK, "Waiting for " + path + " failed for " + args.back()); + int pipeFd[2] = {0, }; + pid_t pid = 0; + + if (::pipe(pipeFd) < 0) + throw runtime::Exception("Failed to create pipe"); + + pid = ::fork(); + if (pid < 0) { + ::close(pipeFd[0]); + ::close(pipeFd[1]); + throw runtime::Exception("Failed to fork"); + } + + if (pid == 0) { + ::close(pipeFd[0]); + if (::dup2(pipeFd[1], STDOUT_FILENO) < 0) + ERROR(SINK, "Failed to copy STDOUT_FILENO"); + + if (::dup2(STDOUT_FILENO, STDERR_FILENO) < 0) + ERROR(SINK, "Failed to copy STDERR_FILENO"); + + ::close(pipeFd[1]); + + if (::execv(args[0], const_cast(args.data())) < 0) + ERROR(SINK, "Failed to execute : " + std::to_string(errno)); + + std::quick_exit(EXIT_FAILURE); + } + + ::close(pipeFd[1]); + + for (int status = 0; ::waitpid(pid, &status, 0) == -1;) { + if (errno != EINTR) { + ::close(pipeFd[0]); + throw runtime::Exception("Failed to wait child process"); + } + } + + auto closeFile = [](FILE *fp) { ::fclose(fp); }; + std::unique_ptr fp(::fdopen(pipeFd[0], "r"), closeFile); + if (fp == nullptr) { + ::close(pipeFd[0]); + throw runtime::Exception("Failed to open pipe descriptor"); + } + + char *line = nullptr; + size_t len = 0; + try { + while ((::getline(&line, &len, fp.get())) != -1) { + std::string log(line); + log.erase(std::find_if(log.rbegin(), log.rend(), + std::ptr_fun(std::isgraph)).base(), log.end()); + WARN(SINK, log); + } + } catch (std::exception &e) { + ERROR(SINK, e.what()); + } + ::free(line); + ::close(pipeFd[0]); } bool isPartitionTerminated(const std::string &partition) @@ -355,16 +407,19 @@ void unmountInternalStorage(const std::string& source) do { ::sync(); - static const char *fuserPath = "/usr/bin/fuser"; - std::vector args = { - fuserPath, "-m", "-k", "-s", "-SIGTERM", source, + std::vector args = { + "/usr/bin/fuser", "-m", "-k", "-v", "-SIGTERM", source.c_str(), nullptr }; - execAndWait(fuserPath, args); - ::usleep((useconds_t)((unsigned int)(500)*1000)); + try { + killProcesses(args); + ::usleep((useconds_t)((unsigned int)(500)*1000)); - args[4] = "-SIGKILL"; - execAndWait(fuserPath, args); - ::usleep((useconds_t)((unsigned int)(200)*1000)); + args[4] = "-SIGKILL"; + killProcesses(args); + ::usleep((useconds_t)((unsigned int)(200)*1000)); + } catch (runtime::Exception &e) { + ERROR(e.what()); + } } while (!isPartitionTerminated(source)); }