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
) {
47 open("/proc/self/task/", O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
49 const bool result
= IsSingleThreadedImpl(task_fd
);
50 PCHECK(0 == IGNORE_EINTR(close(task_fd
)));
53 return IsSingleThreadedImpl(proc_self_task
);
57 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task
,
58 base::Thread
* thread
) {
59 DCHECK_LE(0, proc_self_task
);
61 const base::PlatformThreadId thread_id
= thread
->thread_id();
62 const std::string thread_id_dir_str
= base::IntToString(thread_id
) + "/";
64 // The kernel is at liberty to wake the thread id futex before updating
65 // /proc. Following Stop(), the thread is joined, but entries in /proc may
66 // not have been updated.
69 unsigned int iterations
= 0;
70 bool thread_present_in_procfs
= true;
71 // Poll /proc with an exponential back-off, sleeping 2^iterations nanoseconds
73 // Note: the clock may not allow for nanosecond granularity, in this case the
74 // first iterations would sleep a tiny bit more instead, which would not
75 // change the calculations significantly.
76 while (thread_present_in_procfs
) {
77 struct stat task_stat
;
79 fstatat(proc_self_task
, thread_id_dir_str
.c_str(), &task_stat
, 0);
81 PCHECK(ENOENT
== errno
);
82 // The thread disappeared from /proc, we're done.
83 thread_present_in_procfs
= false;
86 // Increase the waiting time exponentially.
87 struct timespec ts
= {0, 1L << iterations
/* nanoseconds */};
88 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts
, &ts
)));
91 // Crash after 30 iterations, which means having spent roughly 2s in
92 // nanosleep(2) cumulatively.
93 CHECK_GT(30U, iterations
);
94 // In practice, this never goes through more than a couple iterations. In
95 // debug mode, crash after 64ms (+ eventually 25 times the granularity of
96 // the clock) in nanosleep(2).
97 DCHECK_GT(25U, iterations
);
103 } // namespace sandbox