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
3 // found in the LICENSE file.
5 #include "sandbox/linux/services/thread_helpers.h"
10 #include <sys/types.h>
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/callback.h"
19 #include "base/files/scoped_file.h"
20 #include "base/logging.h"
21 #include "base/posix/eintr_wrapper.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/threading/platform_thread.h"
24 #include "base/threading/thread.h"
25 #include "sandbox/linux/services/proc_util.h"
31 const char kAssertSingleThreadedError
[] =
32 "Current process is not mono-threaded!";
34 bool IsSingleThreadedImpl(int proc_fd
) {
36 struct stat task_stat
;
37 int fstat_ret
= fstatat(proc_fd
, "self/task/", &task_stat
, 0);
38 PCHECK(0 == fstat_ret
);
40 // At least "..", "." and the current thread should be present.
41 CHECK_LE(3UL, task_stat
.st_nlink
);
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
44 // time it becomes monothreaded, it'll stay so.
45 return task_stat
.st_nlink
== 3;
48 bool IsThreadPresentInProcFS(int proc_fd
,
49 const std::string
& thread_id_dir_str
) {
50 struct stat task_stat
;
52 fstatat(proc_fd
, thread_id_dir_str
.c_str(), &task_stat
, 0);
54 PCHECK(ENOENT
== errno
);
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
62 // false very quickly and this will crash if it doesn't happen within ~64ms on
63 // Debug builds (2s on Release builds).
64 // This is guaranteed to not sleep more than twice as much as the bare minimum
66 void RunWhileTrue(const base::Callback
<bool(void)>& cb
) {
68 // In Release mode, crash after 30 iterations, which means having spent
70 // nanosleep(2) cumulatively.
71 const unsigned int kMaxIterations
= 30U;
73 // In practice, this never goes through more than a couple iterations. In
74 // debug mode, crash after 64ms (+ eventually 25 times the granularity of
75 // the clock) in nanosleep(2). This ensures that this is not becoming too
77 const unsigned int kMaxIterations
= 25U;
80 // Run |cb| with an exponential back-off, sleeping 2^iterations nanoseconds
82 // Note: the clock may not allow for nanosecond granularity, in this case the
83 // first iterations would sleep a tiny bit more instead, which would not
84 // change the calculations significantly.
85 for (unsigned int i
= 0; i
< kMaxIterations
; ++i
) {
90 // Increase the waiting time exponentially.
91 struct timespec ts
= {0, 1L << i
/* nanoseconds */};
92 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts
, &ts
)));
95 LOG(FATAL
) << kAssertSingleThreadedError
<< " (iterations: " << kMaxIterations
101 bool IsMultiThreaded(int proc_fd
) {
102 return !ThreadHelpers::IsSingleThreaded(proc_fd
);
108 bool ThreadHelpers::IsSingleThreaded(int proc_fd
) {
109 DCHECK_LE(0, proc_fd
);
110 return IsSingleThreadedImpl(proc_fd
);
114 bool ThreadHelpers::IsSingleThreaded() {
115 base::ScopedFD
task_fd(ProcUtil::OpenProc());
116 return IsSingleThreaded(task_fd
.get());
120 void ThreadHelpers::AssertSingleThreaded(int proc_fd
) {
121 DCHECK_LE(0, proc_fd
);
122 const base::Callback
<bool(void)> cb
= base::Bind(&IsMultiThreaded
, proc_fd
);
126 void ThreadHelpers::AssertSingleThreaded() {
127 base::ScopedFD
task_fd(ProcUtil::OpenProc());
128 AssertSingleThreaded(task_fd
.get());
132 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_fd
,
133 base::Thread
* thread
) {
134 DCHECK_LE(0, proc_fd
);
136 const base::PlatformThreadId thread_id
= thread
->thread_id();
137 const std::string thread_id_dir_str
=
138 "self/task/" + base::IntToString(thread_id
) + "/";
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
142 // not have been updated.
145 const base::Callback
<bool(void)> cb
=
146 base::Bind(&IsThreadPresentInProcFS
, proc_fd
, thread_id_dir_str
);
154 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() {
155 return kAssertSingleThreadedError
;
158 } // namespace sandbox