thestig@chromium.org | ce2f241 | 2012-06-14 08:20:04 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 5 | #include "base/linux_util.h" |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 6 | |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 7 | #include <dirent.h> |
| 8 | #include <errno.h> |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 10 | #include <stdlib.h> |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 11 | #include <sys/stat.h> |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 12 | #include <sys/types.h> |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 13 | #include <unistd.h> |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 14 | |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "base/command_line.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 18 | #include "base/files/file_util.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 19 | #include "base/memory/scoped_ptr.h" |
| 20 | #include "base/memory/singleton.h" |
rsesek@chromium.org | 687756f | 2013-07-26 06:38:23 +0900 | [diff] [blame] | 21 | #include "base/process/launch.h" |
avi@chromium.org | 67d593d | 2013-06-11 04:06:57 +0900 | [diff] [blame] | 22 | #include "base/strings/string_util.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 23 | #include "base/synchronization/lock.h" |
mattm@chromium.org | 625865e | 2009-07-22 09:22:49 +0900 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 27 | // Not needed for OS_CHROMEOS. |
| 28 | #if defined(OS_LINUX) |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 29 | enum LinuxDistroState { |
| 30 | STATE_DID_NOT_CHECK = 0, |
| 31 | STATE_CHECK_STARTED = 1, |
| 32 | STATE_CHECK_FINISHED = 2, |
| 33 | }; |
| 34 | |
| 35 | // Helper class for GetLinuxDistro(). |
| 36 | class LinuxDistroHelper { |
| 37 | public: |
| 38 | // Retrieves the Singleton. |
satish@chromium.org | ce3f487 | 2010-12-14 01:52:35 +0900 | [diff] [blame] | 39 | static LinuxDistroHelper* GetInstance() { |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 40 | return base::Singleton<LinuxDistroHelper>::get(); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // The simple state machine goes from: |
| 44 | // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED. |
| 45 | LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {} |
| 46 | ~LinuxDistroHelper() {} |
| 47 | |
| 48 | // Retrieve the current state, if we're in STATE_DID_NOT_CHECK, |
| 49 | // we automatically move to STATE_CHECK_STARTED so nobody else will |
| 50 | // do the check. |
| 51 | LinuxDistroState State() { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 52 | base::AutoLock scoped_lock(lock_); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 53 | if (STATE_DID_NOT_CHECK == state_) { |
| 54 | state_ = STATE_CHECK_STARTED; |
| 55 | return STATE_DID_NOT_CHECK; |
| 56 | } |
| 57 | return state_; |
| 58 | } |
| 59 | |
| 60 | // Indicate the check finished, move to STATE_CHECK_FINISHED. |
| 61 | void CheckFinished() { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 62 | base::AutoLock scoped_lock(lock_); |
david.mike.futcher@gmail.com | 9eb2aa5 | 2011-04-19 05:07:08 +0900 | [diff] [blame] | 63 | DCHECK_EQ(STATE_CHECK_STARTED, state_); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 64 | state_ = STATE_CHECK_FINISHED; |
| 65 | } |
| 66 | |
| 67 | private: |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 68 | base::Lock lock_; |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 69 | LinuxDistroState state_; |
| 70 | }; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 71 | #endif // if defined(OS_LINUX) |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 72 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 73 | } // namespace |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 74 | |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 75 | namespace base { |
| 76 | |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 77 | // Account for the terminating null character. |
| 78 | static const int kDistroSize = 128 + 1; |
| 79 | |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 80 | // We use this static string to hold the Linux distro info. If we |
| 81 | // crash, the crash handler code will send this in the crash dump. |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 82 | char g_linux_distro[kDistroSize] = |
rbyers@chromium.org | 088a493 | 2012-08-16 05:54:06 +0900 | [diff] [blame] | 83 | #if defined(OS_CHROMEOS) |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 84 | "CrOS"; |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 85 | #elif defined(OS_ANDROID) |
| 86 | "Android"; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 87 | #else // if defined(OS_LINUX) |
| 88 | "Unknown"; |
| 89 | #endif |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 90 | |
| 91 | std::string GetLinuxDistro() { |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 92 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 93 | return g_linux_distro; |
pvalchev@google.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 94 | #elif defined(OS_LINUX) |
satish@chromium.org | ce3f487 | 2010-12-14 01:52:35 +0900 | [diff] [blame] | 95 | LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance(); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 96 | LinuxDistroState state = distro_state_singleton->State(); |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 97 | if (STATE_CHECK_FINISHED == state) |
| 98 | return g_linux_distro; |
| 99 | if (STATE_CHECK_STARTED == state) |
| 100 | return "Unknown"; // Don't wait for other thread to finish. |
| 101 | DCHECK_EQ(state, STATE_DID_NOT_CHECK); |
| 102 | // We do this check only once per process. If it fails, there's |
| 103 | // little reason to believe it will work if we attempt to run |
| 104 | // lsb_release again. |
| 105 | std::vector<std::string> argv; |
| 106 | argv.push_back("lsb_release"); |
| 107 | argv.push_back("-d"); |
| 108 | std::string output; |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 109 | GetAppOutput(CommandLine(argv), &output); |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 110 | if (output.length() > 0) { |
| 111 | // lsb_release -d should return: Description:<tab>Distro Info |
| 112 | const char field[] = "Description:\t"; |
| 113 | if (output.compare(0, strlen(field), field) == 0) { |
| 114 | SetLinuxDistro(output.substr(strlen(field))); |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 115 | } |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 116 | } |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 117 | distro_state_singleton->CheckFinished(); |
| 118 | return g_linux_distro; |
pvalchev@google.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 119 | #else |
| 120 | NOTIMPLEMENTED(); |
robert.nagy@gmail.com | ea54e46 | 2011-10-25 07:05:27 +0900 | [diff] [blame] | 121 | return "Unknown"; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 122 | #endif |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 123 | } |
| 124 | |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 125 | void SetLinuxDistro(const std::string& distro) { |
| 126 | std::string trimmed_distro; |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 127 | TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); |
| 128 | strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 129 | } |
| 130 | |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 131 | pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, |
| 132 | bool* syscall_supported) { |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 133 | char buf[256]; |
| 134 | snprintf(buf, sizeof(buf), "/proc/%d/task", pid); |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 135 | |
| 136 | if (syscall_supported != NULL) |
| 137 | *syscall_supported = false; |
| 138 | |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 139 | DIR* task = opendir(buf); |
| 140 | if (!task) { |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 141 | DLOG(WARNING) << "Cannot open " << buf; |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | std::vector<pid_t> tids; |
| 146 | struct dirent* dent; |
| 147 | while ((dent = readdir(task))) { |
thestig@chromium.org | ce2f241 | 2012-06-14 08:20:04 +0900 | [diff] [blame] | 148 | char* endptr; |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 149 | const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10); |
| 150 | if (tid_ul == ULONG_MAX || *endptr) |
| 151 | continue; |
| 152 | tids.push_back(tid_ul); |
| 153 | } |
| 154 | closedir(task); |
| 155 | |
tfarina@chromium.org | 15bc41d | 2013-01-18 23:21:58 +0900 | [diff] [blame] | 156 | scoped_ptr<char[]> syscall_data(new char[expected_data.length()]); |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 157 | for (std::vector<pid_t>::const_iterator |
| 158 | i = tids.begin(); i != tids.end(); ++i) { |
| 159 | const pid_t current_tid = *i; |
| 160 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid); |
| 161 | int fd = open(buf, O_RDONLY); |
| 162 | if (fd < 0) |
| 163 | continue; |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 164 | if (syscall_supported != NULL) |
| 165 | *syscall_supported = true; |
brettw@chromium.org | 2873d9b | 2013-11-28 08:22:08 +0900 | [diff] [blame] | 166 | bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 167 | close(fd); |
| 168 | if (!read_ret) |
| 169 | continue; |
| 170 | |
| 171 | if (0 == strncmp(expected_data.c_str(), syscall_data.get(), |
| 172 | expected_data.length())) { |
| 173 | return current_tid; |
| 174 | } |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 179 | } // namespace base |