OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <dirent.h> | 5 #include <dirent.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 signal(SIGINT, SIG_DFL); | 145 signal(SIGINT, SIG_DFL); |
146 signal(SIGILL, SIG_DFL); | 146 signal(SIGILL, SIG_DFL); |
147 signal(SIGABRT, SIG_DFL); | 147 signal(SIGABRT, SIG_DFL); |
148 signal(SIGFPE, SIG_DFL); | 148 signal(SIGFPE, SIG_DFL); |
149 signal(SIGBUS, SIG_DFL); | 149 signal(SIGBUS, SIG_DFL); |
150 signal(SIGSEGV, SIG_DFL); | 150 signal(SIGSEGV, SIG_DFL); |
151 signal(SIGSYS, SIG_DFL); | 151 signal(SIGSYS, SIG_DFL); |
152 signal(SIGTERM, SIG_DFL); | 152 signal(SIGTERM, SIG_DFL); |
153 } | 153 } |
154 | 154 |
| 155 TerminationStatus GetTerminationStatusImpl(ProcessHandle handle, |
| 156 bool can_block, |
| 157 int* exit_code) { |
| 158 int status = 0; |
| 159 const pid_t result = HANDLE_EINTR(waitpid(handle, &status, |
| 160 can_block ? 0 : WNOHANG)); |
| 161 if (result == -1) { |
| 162 DPLOG(ERROR) << "waitpid(" << handle << ")"; |
| 163 if (exit_code) |
| 164 *exit_code = 0; |
| 165 return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 166 } else if (result == 0) { |
| 167 // the child hasn't exited yet. |
| 168 if (exit_code) |
| 169 *exit_code = 0; |
| 170 return TERMINATION_STATUS_STILL_RUNNING; |
| 171 } |
| 172 |
| 173 if (exit_code) |
| 174 *exit_code = status; |
| 175 |
| 176 if (WIFSIGNALED(status)) { |
| 177 switch (WTERMSIG(status)) { |
| 178 case SIGABRT: |
| 179 case SIGBUS: |
| 180 case SIGFPE: |
| 181 case SIGILL: |
| 182 case SIGSEGV: |
| 183 return TERMINATION_STATUS_PROCESS_CRASHED; |
| 184 case SIGINT: |
| 185 case SIGKILL: |
| 186 case SIGTERM: |
| 187 return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
| 188 default: |
| 189 break; |
| 190 } |
| 191 } |
| 192 |
| 193 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 194 return TERMINATION_STATUS_ABNORMAL_TERMINATION; |
| 195 |
| 196 return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 197 } |
| 198 |
155 } // anonymous namespace | 199 } // anonymous namespace |
156 | 200 |
157 ProcessId GetCurrentProcId() { | 201 ProcessId GetCurrentProcId() { |
158 return getpid(); | 202 return getpid(); |
159 } | 203 } |
160 | 204 |
161 ProcessHandle GetCurrentProcessHandle() { | 205 ProcessHandle GetCurrentProcessHandle() { |
162 return GetCurrentProcId(); | 206 return GetCurrentProcId(); |
163 } | 207 } |
164 | 208 |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 #endif // defined(OS_MACOSX) | 791 #endif // defined(OS_MACOSX) |
748 | 792 |
749 ProcessMetrics::~ProcessMetrics() { } | 793 ProcessMetrics::~ProcessMetrics() { } |
750 | 794 |
751 void RaiseProcessToHighPriority() { | 795 void RaiseProcessToHighPriority() { |
752 // On POSIX, we don't actually do anything here. We could try to nice() or | 796 // On POSIX, we don't actually do anything here. We could try to nice() or |
753 // setpriority() or sched_getscheduler, but these all require extra rights. | 797 // setpriority() or sched_getscheduler, but these all require extra rights. |
754 } | 798 } |
755 | 799 |
756 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { | 800 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
757 int status = 0; | 801 return GetTerminationStatusImpl(handle, false /* can_block */, exit_code); |
758 const pid_t result = HANDLE_EINTR(waitpid(handle, &status, WNOHANG)); | 802 } |
759 if (result == -1) { | |
760 DPLOG(ERROR) << "waitpid(" << handle << ")"; | |
761 if (exit_code) | |
762 *exit_code = 0; | |
763 return TERMINATION_STATUS_NORMAL_TERMINATION; | |
764 } else if (result == 0) { | |
765 // the child hasn't exited yet. | |
766 if (exit_code) | |
767 *exit_code = 0; | |
768 return TERMINATION_STATUS_STILL_RUNNING; | |
769 } | |
770 | 803 |
771 if (exit_code) | 804 TerminationStatus WaitForTerminationStatus(ProcessHandle handle, |
772 *exit_code = status; | 805 int* exit_code) { |
773 | 806 return GetTerminationStatusImpl(handle, true /* can_block */, exit_code); |
774 if (WIFSIGNALED(status)) { | |
775 switch (WTERMSIG(status)) { | |
776 case SIGABRT: | |
777 case SIGBUS: | |
778 case SIGFPE: | |
779 case SIGILL: | |
780 case SIGSEGV: | |
781 return TERMINATION_STATUS_PROCESS_CRASHED; | |
782 case SIGINT: | |
783 case SIGKILL: | |
784 case SIGTERM: | |
785 return TERMINATION_STATUS_PROCESS_WAS_KILLED; | |
786 default: | |
787 break; | |
788 } | |
789 } | |
790 | |
791 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) | |
792 return TERMINATION_STATUS_ABNORMAL_TERMINATION; | |
793 | |
794 return TERMINATION_STATUS_NORMAL_TERMINATION; | |
795 } | 807 } |
796 | 808 |
797 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { | 809 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { |
798 int status; | 810 int status; |
799 if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) { | 811 if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) { |
800 NOTREACHED(); | 812 NOTREACHED(); |
801 return false; | 813 return false; |
802 } | 814 } |
803 | 815 |
804 if (WIFEXITED(status)) { | 816 if (WIFEXITED(status)) { |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 if (IsChildDead(process)) | 1271 if (IsChildDead(process)) |
1260 return; | 1272 return; |
1261 | 1273 |
1262 BackgroundReaper* reaper = new BackgroundReaper(process, 0); | 1274 BackgroundReaper* reaper = new BackgroundReaper(process, 0); |
1263 PlatformThread::CreateNonJoinable(0, reaper); | 1275 PlatformThread::CreateNonJoinable(0, reaper); |
1264 } | 1276 } |
1265 | 1277 |
1266 #endif // !defined(OS_MACOSX) | 1278 #endif // !defined(OS_MACOSX) |
1267 | 1279 |
1268 } // namespace base | 1280 } // namespace base |
OLD | NEW |