[email protected] | 46e9acad | 2012-06-13 23:20:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [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 | |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 5 | #include "base/linux_util.h" |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 6 | |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 7 | #include <dirent.h> |
| 8 | #include <errno.h> |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 9 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 10 | #include <limits.h> |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 11 | #include <stdlib.h> |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 12 | #include <sys/stat.h> |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 13 | #include <sys/types.h> |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 14 | #include <unistd.h> |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 15 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 16 | #include <memory> |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | #include "base/command_line.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 21 | #include "base/memory/singleton.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 22 | #include "base/process/launch.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 24 | #include "base/synchronization/lock.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 87fc168 | 2009-07-22 00:22:49 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 29 | // Not needed for OS_CHROMEOS. |
| 30 | #if defined(OS_LINUX) |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 31 | enum LinuxDistroState { |
| 32 | STATE_DID_NOT_CHECK = 0, |
| 33 | STATE_CHECK_STARTED = 1, |
| 34 | STATE_CHECK_FINISHED = 2, |
| 35 | }; |
| 36 | |
| 37 | // Helper class for GetLinuxDistro(). |
| 38 | class LinuxDistroHelper { |
| 39 | public: |
| 40 | // Retrieves the Singleton. |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 41 | static LinuxDistroHelper* GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 42 | return base::Singleton<LinuxDistroHelper>::get(); |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // The simple state machine goes from: |
| 46 | // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED. |
| 47 | LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {} |
| 48 | ~LinuxDistroHelper() {} |
| 49 | |
| 50 | // Retrieve the current state, if we're in STATE_DID_NOT_CHECK, |
| 51 | // we automatically move to STATE_CHECK_STARTED so nobody else will |
| 52 | // do the check. |
| 53 | LinuxDistroState State() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 54 | base::AutoLock scoped_lock(lock_); |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 55 | if (STATE_DID_NOT_CHECK == state_) { |
| 56 | state_ = STATE_CHECK_STARTED; |
| 57 | return STATE_DID_NOT_CHECK; |
| 58 | } |
| 59 | return state_; |
| 60 | } |
| 61 | |
| 62 | // Indicate the check finished, move to STATE_CHECK_FINISHED. |
| 63 | void CheckFinished() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 64 | base::AutoLock scoped_lock(lock_); |
[email protected] | 60ea605 | 2011-04-18 20:07:08 | [diff] [blame] | 65 | DCHECK_EQ(STATE_CHECK_STARTED, state_); |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 66 | state_ = STATE_CHECK_FINISHED; |
| 67 | } |
| 68 | |
| 69 | private: |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 70 | base::Lock lock_; |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 71 | LinuxDistroState state_; |
| 72 | }; |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 73 | #endif // if defined(OS_LINUX) |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 74 | |
[email protected] | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 75 | } // namespace |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 76 | |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 77 | namespace base { |
| 78 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 79 | // Account for the terminating null character. |
| 80 | static const int kDistroSize = 128 + 1; |
| 81 | |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 82 | // We use this static string to hold the Linux distro info. If we |
| 83 | // crash, the crash handler code will send this in the crash dump. |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 84 | char g_linux_distro[kDistroSize] = |
[email protected] | cbf0d1d | 2012-08-15 20:54:06 | [diff] [blame] | 85 | #if defined(OS_CHROMEOS) |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 86 | "CrOS"; |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 87 | #elif defined(OS_ANDROID) |
| 88 | "Android"; |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 89 | #else // if defined(OS_LINUX) |
| 90 | "Unknown"; |
| 91 | #endif |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 92 | |
| 93 | std::string GetLinuxDistro() { |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 94 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 95 | return g_linux_distro; |
[email protected] | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 96 | #elif defined(OS_LINUX) |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 97 | LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance(); |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 98 | LinuxDistroState state = distro_state_singleton->State(); |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 99 | if (STATE_CHECK_FINISHED == state) |
| 100 | return g_linux_distro; |
| 101 | if (STATE_CHECK_STARTED == state) |
| 102 | return "Unknown"; // Don't wait for other thread to finish. |
| 103 | DCHECK_EQ(state, STATE_DID_NOT_CHECK); |
| 104 | // We do this check only once per process. If it fails, there's |
| 105 | // little reason to believe it will work if we attempt to run |
| 106 | // lsb_release again. |
| 107 | std::vector<std::string> argv; |
| 108 | argv.push_back("lsb_release"); |
| 109 | argv.push_back("-d"); |
| 110 | std::string output; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 111 | GetAppOutput(CommandLine(argv), &output); |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 112 | if (output.length() > 0) { |
| 113 | // lsb_release -d should return: Description:<tab>Distro Info |
| 114 | const char field[] = "Description:\t"; |
| 115 | if (output.compare(0, strlen(field), field) == 0) { |
| 116 | SetLinuxDistro(output.substr(strlen(field))); |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 117 | } |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 118 | } |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 119 | distro_state_singleton->CheckFinished(); |
| 120 | return g_linux_distro; |
[email protected] | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 121 | #else |
| 122 | NOTIMPLEMENTED(); |
[email protected] | 167ec82 | 2011-10-24 22:05:27 | [diff] [blame] | 123 | return "Unknown"; |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 124 | #endif |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 127 | void SetLinuxDistro(const std::string& distro) { |
| 128 | std::string trimmed_distro; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 129 | TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); |
| 130 | strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 133 | pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, |
| 134 | bool* syscall_supported) { |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 135 | char buf[256]; |
| 136 | snprintf(buf, sizeof(buf), "/proc/%d/task", pid); |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 137 | |
| 138 | if (syscall_supported != NULL) |
| 139 | *syscall_supported = false; |
| 140 | |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 141 | DIR* task = opendir(buf); |
| 142 | if (!task) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 143 | DLOG(WARNING) << "Cannot open " << buf; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | std::vector<pid_t> tids; |
| 148 | struct dirent* dent; |
| 149 | while ((dent = readdir(task))) { |
[email protected] | 46e9acad | 2012-06-13 23:20:04 | [diff] [blame] | 150 | char* endptr; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 151 | const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10); |
| 152 | if (tid_ul == ULONG_MAX || *endptr) |
| 153 | continue; |
| 154 | tids.push_back(tid_ul); |
| 155 | } |
| 156 | closedir(task); |
| 157 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 158 | std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 159 | for (std::vector<pid_t>::const_iterator |
| 160 | i = tids.begin(); i != tids.end(); ++i) { |
| 161 | const pid_t current_tid = *i; |
| 162 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid); |
| 163 | int fd = open(buf, O_RDONLY); |
| 164 | if (fd < 0) |
| 165 | continue; |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 166 | if (syscall_supported != NULL) |
| 167 | *syscall_supported = true; |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 168 | bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 169 | close(fd); |
| 170 | if (!read_ret) |
| 171 | continue; |
| 172 | |
| 173 | if (0 == strncmp(expected_data.c_str(), syscall_data.get(), |
| 174 | expected_data.length())) { |
| 175 | return current_tid; |
| 176 | } |
| 177 | } |
| 178 | return -1; |
| 179 | } |
| 180 | |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 181 | } // namespace base |