blob: 29e1980c9755fb3c213a3ca3fb3bdd63dc8fd5cf [file] [log] [blame]
thestig@chromium.orgce2f2412012-06-14 08:20:04 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
sgk@google.come1f4a242009-04-22 02:20:10 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +09005#include "base/linux_util.h"
sgk@google.come1f4a242009-04-22 02:20:10 +09006
thestig@chromium.org2feb1c82009-10-29 13:02:55 +09007#include <dirent.h>
8#include <errno.h>
thestig@chromium.org44836d42010-07-17 04:28:17 +09009#include <fcntl.h>
sgk@google.come1f4a242009-04-22 02:20:10 +090010#include <stdlib.h>
thestig@chromium.org2feb1c82009-10-29 13:02:55 +090011#include <sys/stat.h>
thestig@chromium.org44836d42010-07-17 04:28:17 +090012#include <sys/types.h>
thestig@chromium.org2feb1c82009-10-29 13:02:55 +090013#include <unistd.h>
sgk@google.come1f4a242009-04-22 02:20:10 +090014
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090015#include <vector>
16
17#include "base/command_line.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090018#include "base/files/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090019#include "base/memory/scoped_ptr.h"
20#include "base/memory/singleton.h"
thestig@chromium.orgeea83062009-12-02 17:45:01 +090021#include "base/path_service.h"
rsesek@chromium.org687756f2013-07-26 06:38:23 +090022#include "base/process/launch.h"
avi@chromium.org67d593d2013-06-11 04:06:57 +090023#include "base/strings/string_util.h"
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090024#include "base/synchronization/lock.h"
mattm@chromium.org625865e2009-07-22 09:22:49 +090025
26namespace {
27
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090028// Not needed for OS_CHROMEOS.
29#if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090030enum 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.
satish@chromium.orgce3f4872010-12-14 01:52:35 +090040 static LinuxDistroHelper* GetInstance() {
thestig@chromium.org741196d2009-10-13 11:51:07 +090041 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() {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090053 base::AutoLock scoped_lock(lock_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090054 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() {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090063 base::AutoLock scoped_lock(lock_);
david.mike.futcher@gmail.com9eb2aa52011-04-19 05:07:08 +090064 DCHECK_EQ(STATE_CHECK_STARTED, state_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090065 state_ = STATE_CHECK_FINISHED;
66 }
67
68 private:
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090069 base::Lock lock_;
thestig@chromium.org741196d2009-10-13 11:51:07 +090070 LinuxDistroState state_;
71};
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090072#endif // if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090073
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090074} // namespace
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090075
sgk@google.come1f4a242009-04-22 02:20:10 +090076namespace base {
77
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +090078// Account for the terminating null character.
79static const int kDistroSize = 128 + 1;
80
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090081// 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.
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +090083char g_linux_distro[kDistroSize] =
rbyers@chromium.org088a4932012-08-16 05:54:06 +090084#if defined(OS_CHROMEOS)
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090085 "CrOS";
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090086#elif defined(OS_ANDROID)
87 "Android";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090088#else // if defined(OS_LINUX)
89 "Unknown";
90#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090091
92std::string GetLinuxDistro() {
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090093#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +090094 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +090095#elif defined(OS_LINUX)
satish@chromium.orgce3f4872010-12-14 01:52:35 +090096 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
thestig@chromium.org741196d2009-10-13 11:51:07 +090097 LinuxDistroState state = distro_state_singleton->State();
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090098 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)));
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900116 }
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900117 }
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900118 distro_state_singleton->CheckFinished();
119 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +0900120#else
121 NOTIMPLEMENTED();
robert.nagy@gmail.comea54e462011-10-25 07:05:27 +0900122 return "Unknown";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +0900123#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900124}
125
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900126void SetLinuxDistro(const std::string& distro) {
127 std::string trimmed_distro;
brettw@chromium.org55bf3322014-03-04 04:05:31 +0900128 base::TrimWhitespaceASCII(distro, base::TRIM_ALL, &trimmed_distro);
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900129 base::strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
130}
131
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900132pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
133 bool* syscall_supported) {
thestig@chromium.org44836d42010-07-17 04:28:17 +0900134 char buf[256];
135 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900136
137 if (syscall_supported != NULL)
138 *syscall_supported = false;
139
thestig@chromium.org44836d42010-07-17 04:28:17 +0900140 DIR* task = opendir(buf);
141 if (!task) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900142 DLOG(WARNING) << "Cannot open " << buf;
thestig@chromium.org44836d42010-07-17 04:28:17 +0900143 return -1;
144 }
145
146 std::vector<pid_t> tids;
147 struct dirent* dent;
148 while ((dent = readdir(task))) {
thestig@chromium.orgce2f2412012-06-14 08:20:04 +0900149 char* endptr;
thestig@chromium.org44836d42010-07-17 04:28:17 +0900150 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
tfarina@chromium.org15bc41d2013-01-18 23:21:58 +0900157 scoped_ptr<char[]> syscall_data(new char[expected_data.length()]);
thestig@chromium.org44836d42010-07-17 04:28:17 +0900158 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;
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900165 if (syscall_supported != NULL)
166 *syscall_supported = true;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900167 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
thestig@chromium.org44836d42010-07-17 04:28:17 +0900168 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
sgk@google.come1f4a242009-04-22 02:20:10 +0900180} // namespace base