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 <fcntl.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
25 #include "sandbox/linux/services/proc_util.h" | 25 #include "sandbox/linux/services/proc_util.h" |
26 | 26 |
27 namespace sandbox { | 27 namespace sandbox { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const char kAssertSingleThreadedError[] = | 31 const char kAssertSingleThreadedError[] = |
32 "Current process is not mono-threaded!"; | 32 "Current process is not mono-threaded!"; |
33 | 33 |
34 bool IsSingleThreadedImpl(int proc_self_task) { | 34 bool IsSingleThreadedImpl(int proc_fd) { |
35 CHECK_LE(0, proc_self_task); | 35 CHECK_LE(0, proc_fd); |
36 struct stat task_stat; | 36 struct stat task_stat; |
37 int fstat_ret = fstat(proc_self_task, &task_stat); | 37 int fstat_ret = fstatat(proc_fd, "self/task/", &task_stat, 0); |
38 PCHECK(0 == fstat_ret); | 38 PCHECK(0 == fstat_ret); |
39 | 39 |
40 // At least "..", "." and the current thread should be present. | 40 // At least "..", "." and the current thread should be present. |
41 CHECK_LE(3UL, task_stat.st_nlink); | 41 CHECK_LE(3UL, task_stat.st_nlink); |
42 // Counting threads via /proc/self/task could be racy. For the purpose of | 42 // Counting threads via /proc/self/task could be racy. For the purpose of |
43 // determining if the current proces is monothreaded it works: if at any | 43 // determining if the current proces is monothreaded it works: if at any |
44 // time it becomes monothreaded, it'll stay so. | 44 // time it becomes monothreaded, it'll stay so. |
45 return task_stat.st_nlink == 3; | 45 return task_stat.st_nlink == 3; |
46 } | 46 } |
47 | 47 |
48 bool IsThreadPresentInProcFS(int proc_self_task, | 48 bool IsThreadPresentInProcFS(int proc_fd, |
49 const std::string& thread_id_dir_str) { | 49 const std::string& thread_id_dir_str) { |
50 struct stat task_stat; | 50 struct stat task_stat; |
51 const int fstat_ret = | 51 const int fstat_ret = |
52 fstatat(proc_self_task, thread_id_dir_str.c_str(), &task_stat, 0); | 52 fstatat(proc_fd, thread_id_dir_str.c_str(), &task_stat, 0); |
53 if (fstat_ret < 0) { | 53 if (fstat_ret < 0) { |
54 PCHECK(ENOENT == errno); | 54 PCHECK(ENOENT == errno); |
55 return false; | 55 return false; |
56 } | 56 } |
57 return true; | 57 return true; |
58 } | 58 } |
59 | 59 |
60 // Run |cb| in a loop until it returns false. Every time |cb| runs, sleep | 60 // Run |cb| in a loop until it returns false. Every time |cb| runs, sleep |
61 // for an exponentially increasing amount of time. |cb| is expected to return | 61 // for an exponentially increasing amount of time. |cb| is expected to return |
62 // false very quickly and this will crash if it doesn't happen within ~64ms on | 62 // false very quickly and this will crash if it doesn't happen within ~64ms on |
(...skipping 28 matching lines...) Expand all Loading... |
91 struct timespec ts = {0, 1L << i /* nanoseconds */}; | 91 struct timespec ts = {0, 1L << i /* nanoseconds */}; |
92 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts))); | 92 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts))); |
93 } | 93 } |
94 | 94 |
95 LOG(FATAL) << kAssertSingleThreadedError << " (iterations: " << kMaxIterations | 95 LOG(FATAL) << kAssertSingleThreadedError << " (iterations: " << kMaxIterations |
96 << ")"; | 96 << ")"; |
97 | 97 |
98 NOTREACHED(); | 98 NOTREACHED(); |
99 } | 99 } |
100 | 100 |
101 bool IsMultiThreaded(int proc_self_task) { | 101 bool IsMultiThreaded(int proc_fd) { |
102 return !ThreadHelpers::IsSingleThreaded(proc_self_task); | 102 return !ThreadHelpers::IsSingleThreaded(proc_fd); |
103 } | 103 } |
104 | 104 |
105 } // namespace | 105 } // namespace |
106 | 106 |
107 // static | 107 // static |
108 bool ThreadHelpers::IsSingleThreaded(int proc_self_task) { | 108 bool ThreadHelpers::IsSingleThreaded(int proc_fd) { |
109 DCHECK_LE(0, proc_self_task); | 109 DCHECK_LE(0, proc_fd); |
110 return IsSingleThreadedImpl(proc_self_task); | 110 return IsSingleThreadedImpl(proc_fd); |
111 } | 111 } |
112 | 112 |
113 // static | 113 // static |
114 bool ThreadHelpers::IsSingleThreaded() { | 114 bool ThreadHelpers::IsSingleThreaded() { |
115 base::ScopedFD task_fd(ProcUtil::OpenProcSelfTask()); | 115 base::ScopedFD task_fd(ProcUtil::OpenProc()); |
116 return IsSingleThreaded(task_fd.get()); | 116 return IsSingleThreaded(task_fd.get()); |
117 } | 117 } |
118 | 118 |
119 // static | 119 // static |
120 void ThreadHelpers::AssertSingleThreaded(int proc_self_task) { | 120 void ThreadHelpers::AssertSingleThreaded(int proc_fd) { |
121 DCHECK_LE(0, proc_self_task); | 121 DCHECK_LE(0, proc_fd); |
122 const base::Callback<bool(void)> cb = | 122 const base::Callback<bool(void)> cb = base::Bind(&IsMultiThreaded, proc_fd); |
123 base::Bind(&IsMultiThreaded, proc_self_task); | |
124 RunWhileTrue(cb); | 123 RunWhileTrue(cb); |
125 } | 124 } |
126 | 125 |
127 void ThreadHelpers::AssertSingleThreaded() { | 126 void ThreadHelpers::AssertSingleThreaded() { |
128 base::ScopedFD task_fd(ProcUtil::OpenProcSelfTask()); | 127 base::ScopedFD task_fd(ProcUtil::OpenProc()); |
129 AssertSingleThreaded(task_fd.get()); | 128 AssertSingleThreaded(task_fd.get()); |
130 } | 129 } |
131 | 130 |
132 // static | 131 // static |
133 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task, | 132 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_fd, |
134 base::Thread* thread) { | 133 base::Thread* thread) { |
135 DCHECK_LE(0, proc_self_task); | 134 DCHECK_LE(0, proc_fd); |
136 DCHECK(thread); | 135 DCHECK(thread); |
137 const base::PlatformThreadId thread_id = thread->thread_id(); | 136 const base::PlatformThreadId thread_id = thread->thread_id(); |
138 const std::string thread_id_dir_str = base::IntToString(thread_id) + "/"; | 137 const std::string thread_id_dir_str = |
| 138 "self/task/" + base::IntToString(thread_id) + "/"; |
139 | 139 |
140 // The kernel is at liberty to wake the thread id futex before updating | 140 // The kernel is at liberty to wake the thread id futex before updating |
141 // /proc. Following Stop(), the thread is joined, but entries in /proc may | 141 // /proc. Following Stop(), the thread is joined, but entries in /proc may |
142 // not have been updated. | 142 // not have been updated. |
143 thread->Stop(); | 143 thread->Stop(); |
144 | 144 |
145 const base::Callback<bool(void)> cb = | 145 const base::Callback<bool(void)> cb = |
146 base::Bind(&IsThreadPresentInProcFS, proc_self_task, thread_id_dir_str); | 146 base::Bind(&IsThreadPresentInProcFS, proc_fd, thread_id_dir_str); |
147 | 147 |
148 RunWhileTrue(cb); | 148 RunWhileTrue(cb); |
149 | 149 |
150 return true; | 150 return true; |
151 } | 151 } |
152 | 152 |
153 // static | 153 // static |
154 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() { | 154 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() { |
155 return kAssertSingleThreadedError; | 155 return kAssertSingleThreadedError; |
156 } | 156 } |
157 | 157 |
158 } // namespace sandbox | 158 } // namespace sandbox |
OLD | NEW |