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/bind.h"
17 #include "base/callback.h"
18 #include "base/files/scoped_file.h"
19 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread.h"
24 #include "sandbox/linux/services/proc_util.h"
30 const char kAssertSingleThreadedError
[] =
31 "Current process is not mono-threaded!";
33 bool IsSingleThreadedImpl(int proc_fd
) {
35 struct stat task_stat
;
36 int fstat_ret
= fstatat(proc_fd
, "self/task/", &task_stat
, 0);
37 PCHECK(0 == fstat_ret
);
39 // At least "..", "." and the current thread should be present.
40 CHECK_LE(3UL, task_stat
.st_nlink
);
41 // Counting threads via /proc/self/task could be racy. For the purpose of
42 // determining if the current proces is monothreaded it works: if at any
43 // time it becomes monothreaded, it'll stay so.
44 return task_stat
.st_nlink
== 3;
47 bool IsThreadPresentInProcFS(int proc_fd
,
48 const std::string
& thread_id_dir_str
) {
49 struct stat task_stat
;
51 fstatat(proc_fd
, thread_id_dir_str
.c_str(), &task_stat
, 0);
53 PCHECK(ENOENT
== errno
);
59 // Run |cb| in a loop until it returns false. Every time |cb| runs, sleep
60 // for an exponentially increasing amount of time. |cb| is expected to return
61 // false very quickly and this will crash if it doesn't happen within ~64ms on
62 // Debug builds (2s on Release builds).
63 // This is guaranteed to not sleep more than twice as much as the bare minimum
65 void RunWhileTrue(const base::Callback
<bool(void)>& cb
) {
67 // In Release mode, crash after 30 iterations, which means having spent
69 // nanosleep(2) cumulatively.
70 const unsigned int kMaxIterations
= 30U;
72 // In practice, this never goes through more than a couple iterations. In
73 // debug mode, crash after 64ms (+ eventually 25 times the granularity of
74 // the clock) in nanosleep(2). This ensures that this is not becoming too
76 const unsigned int kMaxIterations
= 25U;
79 // Run |cb| with an exponential back-off, sleeping 2^iterations nanoseconds
81 // Note: the clock may not allow for nanosecond granularity, in this case the
82 // first iterations would sleep a tiny bit more instead, which would not
83 // change the calculations significantly.
84 for (unsigned int i
= 0; i
< kMaxIterations
; ++i
) {
89 // Increase the waiting time exponentially.
90 struct timespec ts
= {0, 1L << i
/* nanoseconds */};
91 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts
, &ts
)));
94 LOG(FATAL
) << kAssertSingleThreadedError
<< " (iterations: " << kMaxIterations
100 bool IsMultiThreaded(int proc_fd
) {
101 return !ThreadHelpers::IsSingleThreaded(proc_fd
);
107 bool ThreadHelpers::IsSingleThreaded(int proc_fd
) {
108 DCHECK_LE(0, proc_fd
);
109 return IsSingleThreadedImpl(proc_fd
);
113 bool ThreadHelpers::IsSingleThreaded() {
114 base::ScopedFD
task_fd(ProcUtil::OpenProc());
115 return IsSingleThreaded(task_fd
.get());
119 void ThreadHelpers::AssertSingleThreaded(int proc_fd
) {
120 DCHECK_LE(0, proc_fd
);
121 const base::Callback
<bool(void)> cb
= base::Bind(&IsMultiThreaded
, proc_fd
);
125 void ThreadHelpers::AssertSingleThreaded() {
126 base::ScopedFD
task_fd(ProcUtil::OpenProc());
127 AssertSingleThreaded(task_fd
.get());
131 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_fd
,
132 base::Thread
* thread
) {
133 DCHECK_LE(0, proc_fd
);
135 const base::PlatformThreadId thread_id
= thread
->thread_id();
136 const std::string thread_id_dir_str
=
137 "self/task/" + base::IntToString(thread_id
) + "/";
139 // The kernel is at liberty to wake the thread id futex before updating
140 // /proc. Following Stop(), the thread is joined, but entries in /proc may
141 // not have been updated.
144 const base::Callback
<bool(void)> cb
=
145 base::Bind(&IsThreadPresentInProcFS
, proc_fd
, thread_id_dir_str
);
153 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() {
154 return kAssertSingleThreadedError
;
157 } // namespace sandbox