• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "sandbox/linux/services/thread_helpers.h"
6 
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 
14 #include <string>
15 
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"
22 
23 namespace sandbox {
24 
25 namespace {
26 
IsSingleThreadedImpl(int proc_self_task)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);
32 
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;
39 }
40 
41 }  // namespace
42 
IsSingleThreaded(int proc_self_task)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);
47     PCHECK(0 <= task_fd);
48     const bool result = IsSingleThreadedImpl(task_fd);
49     PCHECK(0 == IGNORE_EINTR(close(task_fd)));
50     return result;
51   } else {
52     return IsSingleThreadedImpl(proc_self_task);
53   }
54 }
55 
StopThreadAndWatchProcFS(int proc_self_task,base::Thread * thread)56 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task,
57                                              base::Thread* thread) {
58   DCHECK_LE(0, proc_self_task);
59   DCHECK(thread);
60   const base::PlatformThreadId thread_id = thread->thread_id();
61   const std::string thread_id_dir_str = base::IntToString(thread_id) + "/";
62 
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.
66   thread->Stop();
67 
68   unsigned int iterations = 0;
69   bool thread_present_in_procfs = true;
70   // Poll /proc with an exponential back-off, sleeping 2^iterations nanoseconds
71   // in nanosleep(2).
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;
77     const int fstat_ret =
78         fstatat(proc_self_task, thread_id_dir_str.c_str(), &task_stat, 0);
79     if (fstat_ret < 0) {
80       PCHECK(ENOENT == errno);
81       // The thread disappeared from /proc, we're done.
82       thread_present_in_procfs = false;
83       break;
84     }
85     // Increase the waiting time exponentially.
86     struct timespec ts = {0, 1L << iterations /* nanoseconds */};
87     PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts)));
88     ++iterations;
89 
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);
97   }
98 
99   return true;
100 }
101 
102 }  // namespace sandbox
103