| 6 |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/wait.h> |
| 11 | #include <unistd.h> |
| 12 |
| 13 | #include "base/debug/activity_tracker.h" |
| 14 | #include "base/files/file_util.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/macros.h" |
| 17 | #include "base/posix/eintr_wrapper.h" |
| 18 | #include "base/process/process_iterator.h" |
| 19 | #include "base/task/post_task.h" |
| 20 | #include "base/threading/platform_thread.h" |
| 21 | #include "build/build_config.h" |
| 22 |
| 23 | namespace base { |
| 24 |
| 25 | namespace { |
| 26 |
| 27 | TerminationStatus GetTerminationStatusImpl(ProcessHandle handle, |
| 28 | bool can_block, |
| 29 | int* exit_code) { |
| 30 | DCHECK(exit_code); |
| 31 |
| 32 | int status = 0; |
| 33 | const pid_t result = HANDLE_EINTR(waitpid(handle, &status, |
| 34 | can_block ? 0 : WNOHANG)); |
| 35 | if (result == -1) { |
| 36 | DPLOG(ERROR) << "waitpid(" << handle << ")"; |
| 37 | *exit_code = 0; |
| 38 | return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 39 | } |
| 40 | if (result == 0) { |
| 41 | // the child hasn't exited yet. |
| 42 | *exit_code = 0; |
| 43 | return TERMINATION_STATUS_STILL_RUNNING; |
| 44 | } |
| 45 |
| 46 | *exit_code = status; |
| 47 |
| 48 | if (WIFSIGNALED(status)) { |
| 49 | switch (WTERMSIG(status)) { |
| 50 | case SIGABRT: |
| 51 | case SIGBUS: |
| 52 | case SIGFPE: |
| 53 | case SIGILL: |
| 54 | case SIGSEGV: |
| 55 | case SIGTRAP: |
| 56 | case SIGSYS: |
| 57 | return TERMINATION_STATUS_PROCESS_CRASHED; |
| 58 | case SIGKILL: |
| 59 | #if defined(OS_CHROMEOS) |
| 60 | // On ChromeOS, only way a process gets kill by SIGKILL |
| 61 | // is by oom-killer. |
| 62 | return TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM; |
| 63 | #endif |
| 64 | case SIGINT: |
| 65 | case SIGTERM: |
| 66 | return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
| 67 | default: |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 |
| 72 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 73 | return TERMINATION_STATUS_ABNORMAL_TERMINATION; |
| 74 |
| 75 | return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 76 | } |
| 77 |
| 78 | } // namespace |
| 79 |
| 80 | #if !defined(OS_NACL_NONSFI) |
| 81 | bool KillProcessGroup(ProcessHandle process_group_id) { |
| 82 | bool result = kill(-1 * process_group_id, SIGKILL) == 0; |
| 83 | if (!result) |
| 84 | DPLOG(ERROR) << "Unable to terminate process group " << process_group_id; |
| 85 | return result; |
| 86 | } |
| 87 | #endif // !defined(OS_NACL_NONSFI) |
| 88 |
| 89 | TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
| 90 | return GetTerminationStatusImpl(handle, false /* can_block */, exit_code); |
| 91 | } |
| 92 |
| 93 | TerminationStatus GetKnownDeadTerminationStatus(ProcessHandle handle, |
| 94 | int* exit_code) { |
| 95 | bool result = kill(handle, SIGKILL) == 0; |
| 96 |
| 97 | if (!result) |
| 98 | DPLOG(ERROR) << "Unable to terminate process " << handle; |
| 99 |
| 100 | return GetTerminationStatusImpl(handle, true /* can_block */, exit_code); |
| 101 | } |
| 102 |
| 103 | #if !defined(OS_NACL_NONSFI) |
| 104 | bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
| 105 | TimeDelta wait, |
| 106 | const ProcessFilter* filter) { |
| 107 | bool result = false; |
| 108 |
| 109 | // TODO(port): This is inefficient, but works if there are multiple procs. |
| 110 | // TODO(port): use waitpid to avoid leaving zombies around |
| 111 |
| 112 | TimeTicks end_time = TimeTicks::Now() + wait; |
| 113 | do { |
| 114 | NamedProcessIterator iter(executable_name, filter); |
| 115 | if (!iter.NextProcessEntry()) { |
| 116 | result = true; |
| 117 | break; |
| 118 | } |
| 119 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
| 120 | } while ((end_time - TimeTicks::Now()) > TimeDelta()); |
| 121 |
| 122 | return result; |
| 123 | } |
| 124 |
| 125 | bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 126 | TimeDelta wait, |
| 127 | int exit_code, |
| 128 | const ProcessFilter* filter) { |
| 129 | bool exited_cleanly = WaitForProcessesToExit(executable_name, wait, filter); |
| 130 | if (!exited_cleanly) |
| 131 | KillProcesses(executable_name, exit_code, filter); |
| 132 | return exited_cleanly; |
| 133 | } |
| 134 |
| 135 | #if !defined(OS_MACOSX) |
| 136 |
| 137 | namespace { |
| 138 |
| 139 | class BackgroundReaper : public PlatformThread::Delegate { |
| 140 | public: |
| 141 | BackgroundReaper(base::Process child_process, const TimeDelta& wait_time) |
| 142 | : child_process_(std::move(child_process)), wait_time_(wait_time) {} |
| 143 |
| 144 | void ThreadMain() override { |
| 145 | if (!wait_time_.is_zero()) { |
| 146 | child_process_.WaitForExitWithTimeout(wait_time_, nullptr); |
| 147 | kill(child_process_.Handle(), SIGKILL); |
| 148 | } |
| 149 | child_process_.WaitForExit(nullptr); |
| 150 | delete this; |
| 151 | } |
| 152 |
| 153 | private: |
| 154 | Process child_process_; |
| 155 | const TimeDelta wait_time_; |
| 156 | DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); |
| 157 | }; |
| 158 |
| 159 | } // namespace |
| 160 |
| 161 | void EnsureProcessTerminated(Process process) { |
| 162 | DCHECK(!process.is_current()); |
| 163 |
| 164 | if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) |
| 165 | return; |
| 166 |
| 167 | PlatformThread::CreateNonJoinable( |
| 168 | 0, new BackgroundReaper(std::move(process), TimeDelta::FromSeconds(2))); |
| 169 | } |
| 170 |
| 171 | #if defined(OS_LINUX) |
| 172 | void EnsureProcessGetsReaped(Process process) { |
| 173 | DCHECK(!process.is_current()); |
| 174 |
| 175 | // If the child is already dead, then there's nothing to do. |
| 176 | if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) |
| 177 | return; |
| 178 |
| 179 | PlatformThread::CreateNonJoinable( |
| 180 | 0, new BackgroundReaper(std::move(process), TimeDelta())); |
| 181 | } |
| 182 | #endif // defined(OS_LINUX) |
| 183 |
| 184 | #endif // !defined(OS_MACOSX) |
| 185 | #endif // !defined(OS_NACL_NONSFI) |
| 186 |
| 187 | } // namespace base |
| 188 |