blob: 36710f2813719e06e0454272b3877efc64eff513 [file] [log] [blame]
[email protected]46e9acad2012-06-13 23:20:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]75ae5422009-04-21 17:20:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]912c6452009-07-17 05:55:515#include "base/linux_util.h"
[email protected]75ae5422009-04-21 17:20:106
[email protected]85ebe8f2009-10-29 04:02:557#include <dirent.h>
8#include <errno.h>
[email protected]662183142010-07-16 19:28:179#include <fcntl.h>
[email protected]75ae5422009-04-21 17:20:1010#include <stdlib.h>
[email protected]85ebe8f2009-10-29 04:02:5511#include <sys/stat.h>
[email protected]662183142010-07-16 19:28:1712#include <sys/types.h>
[email protected]85ebe8f2009-10-29 04:02:5513#include <unistd.h>
[email protected]75ae5422009-04-21 17:20:1014
[email protected]912c6452009-07-17 05:55:5115#include <vector>
16
17#include "base/command_line.h"
[email protected]662183142010-07-16 19:28:1718#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/scoped_ptr.h"
20#include "base/memory/singleton.h"
[email protected]9e9b6e8e2009-12-02 08:45:0121#include "base/path_service.h"
[email protected]dd4b51262013-07-25 21:38:2322#include "base/process/launch.h"
[email protected]d529cb02013-06-10 19:06:5723#include "base/strings/string_util.h"
[email protected]20305ec2011-01-21 04:55:5224#include "base/synchronization/lock.h"
[email protected]87fc1682009-07-22 00:22:4925
26namespace {
27
[email protected]78f6f512009-10-21 19:04:1728// Not needed for OS_CHROMEOS.
29#if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0730enum LinuxDistroState {
31 STATE_DID_NOT_CHECK = 0,
32 STATE_CHECK_STARTED = 1,
33 STATE_CHECK_FINISHED = 2,
34};
35
36// Helper class for GetLinuxDistro().
37class LinuxDistroHelper {
38 public:
39 // Retrieves the Singleton.
[email protected]dc8caba2010-12-13 16:52:3540 static LinuxDistroHelper* GetInstance() {
[email protected]19467c02009-10-13 02:51:0741 return Singleton<LinuxDistroHelper>::get();
42 }
43
44 // The simple state machine goes from:
45 // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED.
46 LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {}
47 ~LinuxDistroHelper() {}
48
49 // Retrieve the current state, if we're in STATE_DID_NOT_CHECK,
50 // we automatically move to STATE_CHECK_STARTED so nobody else will
51 // do the check.
52 LinuxDistroState State() {
[email protected]20305ec2011-01-21 04:55:5253 base::AutoLock scoped_lock(lock_);
[email protected]19467c02009-10-13 02:51:0754 if (STATE_DID_NOT_CHECK == state_) {
55 state_ = STATE_CHECK_STARTED;
56 return STATE_DID_NOT_CHECK;
57 }
58 return state_;
59 }
60
61 // Indicate the check finished, move to STATE_CHECK_FINISHED.
62 void CheckFinished() {
[email protected]20305ec2011-01-21 04:55:5263 base::AutoLock scoped_lock(lock_);
[email protected]60ea6052011-04-18 20:07:0864 DCHECK_EQ(STATE_CHECK_STARTED, state_);
[email protected]19467c02009-10-13 02:51:0765 state_ = STATE_CHECK_FINISHED;
66 }
67
68 private:
[email protected]20305ec2011-01-21 04:55:5269 base::Lock lock_;
[email protected]19467c02009-10-13 02:51:0770 LinuxDistroState state_;
71};
[email protected]78f6f512009-10-21 19:04:1772#endif // if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0773
[email protected]9bc8cff2010-04-03 01:05:3974} // namespace
[email protected]912c6452009-07-17 05:55:5175
[email protected]75ae5422009-04-21 17:20:1076namespace base {
77
[email protected]6dde9d72010-08-26 08:55:2278// Account for the terminating null character.
79static const int kDistroSize = 128 + 1;
80
[email protected]912c6452009-07-17 05:55:5181// We use this static string to hold the Linux distro info. If we
82// crash, the crash handler code will send this in the crash dump.
[email protected]6dde9d72010-08-26 08:55:2283char g_linux_distro[kDistroSize] =
[email protected]cbf0d1d2012-08-15 20:54:0684#if defined(OS_CHROMEOS)
[email protected]78f6f512009-10-21 19:04:1785 "CrOS";
[email protected]84a1ab5a2012-07-18 01:49:2286#elif defined(OS_ANDROID)
87 "Android";
[email protected]78f6f512009-10-21 19:04:1788#else // if defined(OS_LINUX)
89 "Unknown";
90#endif
[email protected]912c6452009-07-17 05:55:5191
92std::string GetLinuxDistro() {
[email protected]84a1ab5a2012-07-18 01:49:2293#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]6dde9d72010-08-26 08:55:2294 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:4395#elif defined(OS_LINUX)
[email protected]dc8caba2010-12-13 16:52:3596 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
[email protected]19467c02009-10-13 02:51:0797 LinuxDistroState state = distro_state_singleton->State();
[email protected]84a1ab5a2012-07-18 01:49:2298 if (STATE_CHECK_FINISHED == state)
99 return g_linux_distro;
100 if (STATE_CHECK_STARTED == state)
101 return "Unknown"; // Don't wait for other thread to finish.
102 DCHECK_EQ(state, STATE_DID_NOT_CHECK);
103 // We do this check only once per process. If it fails, there's
104 // little reason to believe it will work if we attempt to run
105 // lsb_release again.
106 std::vector<std::string> argv;
107 argv.push_back("lsb_release");
108 argv.push_back("-d");
109 std::string output;
110 base::GetAppOutput(CommandLine(argv), &output);
111 if (output.length() > 0) {
112 // lsb_release -d should return: Description:<tab>Distro Info
113 const char field[] = "Description:\t";
114 if (output.compare(0, strlen(field), field) == 0) {
115 SetLinuxDistro(output.substr(strlen(field)));
[email protected]912c6452009-07-17 05:55:51116 }
[email protected]912c6452009-07-17 05:55:51117 }
[email protected]84a1ab5a2012-07-18 01:49:22118 distro_state_singleton->CheckFinished();
119 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43120#else
121 NOTIMPLEMENTED();
[email protected]167ec822011-10-24 22:05:27122 return "Unknown";
[email protected]78f6f512009-10-21 19:04:17123#endif
[email protected]912c6452009-07-17 05:55:51124}
125
[email protected]6dde9d72010-08-26 08:55:22126void SetLinuxDistro(const std::string& distro) {
127 std::string trimmed_distro;
[email protected]8af69c6c2014-03-03 19:05:31128 base::TrimWhitespaceASCII(distro, base::TRIM_ALL, &trimmed_distro);
[email protected]6dde9d72010-08-26 08:55:22129 base::strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
130}
131
[email protected]cb7d53e2011-06-21 04:21:06132pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
133 bool* syscall_supported) {
[email protected]662183142010-07-16 19:28:17134 char buf[256];
135 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
[email protected]cb7d53e2011-06-21 04:21:06136
137 if (syscall_supported != NULL)
138 *syscall_supported = false;
139
[email protected]662183142010-07-16 19:28:17140 DIR* task = opendir(buf);
141 if (!task) {
[email protected]a42d4632011-10-26 21:48:00142 DLOG(WARNING) << "Cannot open " << buf;
[email protected]662183142010-07-16 19:28:17143 return -1;
144 }
145
146 std::vector<pid_t> tids;
147 struct dirent* dent;
148 while ((dent = readdir(task))) {
[email protected]46e9acad2012-06-13 23:20:04149 char* endptr;
[email protected]662183142010-07-16 19:28:17150 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
151 if (tid_ul == ULONG_MAX || *endptr)
152 continue;
153 tids.push_back(tid_ul);
154 }
155 closedir(task);
156
[email protected]604eb052013-01-18 14:21:58157 scoped_ptr<char[]> syscall_data(new char[expected_data.length()]);
[email protected]662183142010-07-16 19:28:17158 for (std::vector<pid_t>::const_iterator
159 i = tids.begin(); i != tids.end(); ++i) {
160 const pid_t current_tid = *i;
161 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid);
162 int fd = open(buf, O_RDONLY);
163 if (fd < 0)
164 continue;
[email protected]cb7d53e2011-06-21 04:21:06165 if (syscall_supported != NULL)
166 *syscall_supported = true;
[email protected]b264eab2013-11-27 23:22:08167 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
[email protected]662183142010-07-16 19:28:17168 close(fd);
169 if (!read_ret)
170 continue;
171
172 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
173 expected_data.length())) {
174 return current_tid;
175 }
176 }
177 return -1;
178}
179
[email protected]75ae5422009-04-21 17:20:10180} // namespace base