OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sandbox/linux/services/thread_helpers.h" | 5 #include "sandbox/linux/services/thread_helpers.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> |
8 #include <signal.h> | 9 #include <signal.h> |
9 #include <sys/types.h> | 10 #include <sys/types.h> |
10 #include <sys/stat.h> | 11 #include <sys/stat.h> |
11 #include <unistd.h> | 12 #include <unistd.h> |
12 | 13 |
13 #include <string> | 14 #include <string> |
14 | 15 |
15 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
21 | 22 |
22 namespace sandbox { | 23 namespace sandbox { |
23 | 24 |
24 bool ThreadHelpers::IsSingleThreaded(int proc_self_task) { | 25 namespace { |
| 26 |
| 27 bool IsSingleThreadedImpl(int proc_self_task) { |
25 CHECK_LE(0, proc_self_task); | 28 CHECK_LE(0, proc_self_task); |
26 struct stat task_stat; | 29 struct stat task_stat; |
27 int fstat_ret = fstat(proc_self_task, &task_stat); | 30 int fstat_ret = fstat(proc_self_task, &task_stat); |
28 PCHECK(0 == fstat_ret); | 31 PCHECK(0 == fstat_ret); |
29 | 32 |
30 // At least "..", "." and the current thread should be present. | 33 // At least "..", "." and the current thread should be present. |
31 CHECK_LE(3UL, task_stat.st_nlink); | 34 CHECK_LE(3UL, task_stat.st_nlink); |
32 // Counting threads via /proc/self/task could be racy. For the purpose of | 35 // Counting threads via /proc/self/task could be racy. For the purpose of |
33 // determining if the current proces is monothreaded it works: if at any | 36 // determining if the current proces is monothreaded it works: if at any |
34 // time it becomes monothreaded, it'll stay so. | 37 // time it becomes monothreaded, it'll stay so. |
35 return task_stat.st_nlink == 3; | 38 return task_stat.st_nlink == 3; |
36 } | 39 } |
37 | 40 |
| 41 } // namespace |
| 42 |
| 43 bool ThreadHelpers::IsSingleThreaded(int proc_self_task) { |
| 44 DCHECK_LE(-1, proc_self_task); |
| 45 if (-1 == proc_self_task) { |
| 46 const int task_fd = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); |
| 47 PCHECK(0 <= task_fd); |
| 48 const bool result = IsSingleThreadedImpl(task_fd); |
| 49 PCHECK(0 == IGNORE_EINTR(close(task_fd))); |
| 50 return result; |
| 51 } else { |
| 52 return IsSingleThreadedImpl(proc_self_task); |
| 53 } |
| 54 } |
| 55 |
38 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task, | 56 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task, |
39 base::Thread* thread) { | 57 base::Thread* thread) { |
40 DCHECK_LE(0, proc_self_task); | 58 DCHECK_LE(0, proc_self_task); |
41 DCHECK(thread); | 59 DCHECK(thread); |
42 const base::PlatformThreadId thread_id = thread->thread_id(); | 60 const base::PlatformThreadId thread_id = thread->thread_id(); |
43 const std::string thread_id_dir_str = base::IntToString(thread_id) + "/"; | 61 const std::string thread_id_dir_str = base::IntToString(thread_id) + "/"; |
44 | 62 |
45 // The kernel is at liberty to wake the thread id futex before updating | 63 // The kernel is at liberty to wake the thread id futex before updating |
46 // /proc. Following Stop(), the thread is joined, but entries in /proc may | 64 // /proc. Following Stop(), the thread is joined, but entries in /proc may |
47 // not have been updated. | 65 // not have been updated. |
(...skipping 27 matching lines...) Expand all Loading... |
75 // In practice, this never goes through more than a couple iterations. In | 93 // In practice, this never goes through more than a couple iterations. In |
76 // debug mode, crash after 64ms (+ eventually 25 times the granularity of | 94 // debug mode, crash after 64ms (+ eventually 25 times the granularity of |
77 // the clock) in nanosleep(2). | 95 // the clock) in nanosleep(2). |
78 DCHECK_GT(25U, iterations); | 96 DCHECK_GT(25U, iterations); |
79 } | 97 } |
80 | 98 |
81 return true; | 99 return true; |
82 } | 100 } |
83 | 101 |
84 } // namespace sandbox | 102 } // namespace sandbox |
OLD | NEW |