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/logging.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/threading/platform_thread.h"
21 #include "base/threading/thread.h"
27 bool IsSingleThreadedImpl(int proc_self_task
) {
28 CHECK_LE(0, proc_self_task
);
29 struct stat task_stat
;
30 int fstat_ret
= fstat(proc_self_task
, &task_stat
);
31 PCHECK(0 == fstat_ret
);
33 // At least "..", "." and the current thread should be present.
34 CHECK_LE(3UL, task_stat
.st_nlink
);
35 // Counting threads via /proc/self/task could be racy. For the purpose of
36 // determining if the current proces is monothreaded it works: if at any
37 // time it becomes monothreaded, it'll stay so.
38 return task_stat
.st_nlink
== 3;
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
);
48 const bool result
= IsSingleThreadedImpl(task_fd
);
49 PCHECK(0 == IGNORE_EINTR(close(task_fd
)));
52 return IsSingleThreadedImpl(proc_self_task
);
56 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task
,
57 base::Thread
* thread
) {
58 DCHECK_LE(0, proc_self_task
);
60 const base::PlatformThreadId thread_id
= thread
->thread_id();
61 const std::string thread_id_dir_str
= base::IntToString(thread_id
) + "/";
63 // The kernel is at liberty to wake the thread id futex before updating
64 // /proc. Following Stop(), the thread is joined, but entries in /proc may
65 // not have been updated.
68 unsigned int iterations
= 0;
69 bool thread_present_in_procfs
= true;
70 // Poll /proc with an exponential back-off, sleeping 2^iterations nanoseconds
72 // Note: the clock may not allow for nanosecond granularity, in this case the
73 // first iterations would sleep a tiny bit more instead, which would not
74 // change the calculations significantly.
75 while (thread_present_in_procfs
) {
76 struct stat task_stat
;
78 fstatat(proc_self_task
, thread_id_dir_str
.c_str(), &task_stat
, 0);
80 PCHECK(ENOENT
== errno
);
81 // The thread disappeared from /proc, we're done.
82 thread_present_in_procfs
= false;
85 // Increase the waiting time exponentially.
86 struct timespec ts
= {0, 1L << iterations
/* nanoseconds */};
87 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts
, &ts
)));
90 // Crash after 30 iterations, which means having spent roughly 2s in
91 // nanosleep(2) cumulatively.
92 CHECK_GT(30U, iterations
);
93 // In practice, this never goes through more than a couple iterations. In
94 // debug mode, crash after 64ms (+ eventually 25 times the granularity of
95 // the clock) in nanosleep(2).
96 DCHECK_GT(25U, iterations
);
102 } // namespace sandbox