blob: d94588f98c6a2933bbbe3acc0a4a090bd531850b [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"
rsesek@chromium.org687756f2013-07-26 06:38:23 +090021#include "base/process/launch.h"
avi@chromium.org67d593d2013-06-11 04:06:57 +090022#include "base/strings/string_util.h"
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090023#include "base/synchronization/lock.h"
mattm@chromium.org625865e2009-07-22 09:22:49 +090024
25namespace {
26
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090027// Not needed for OS_CHROMEOS.
28#if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090029enum LinuxDistroState {
30 STATE_DID_NOT_CHECK = 0,
31 STATE_CHECK_STARTED = 1,
32 STATE_CHECK_FINISHED = 2,
33};
34
35// Helper class for GetLinuxDistro().
36class LinuxDistroHelper {
37 public:
38 // Retrieves the Singleton.
satish@chromium.orgce3f4872010-12-14 01:52:35 +090039 static LinuxDistroHelper* GetInstance() {
olli.raula5ba94892015-09-10 20:14:22 +090040 return base::Singleton<LinuxDistroHelper>::get();
thestig@chromium.org741196d2009-10-13 11:51:07 +090041 }
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.orgabe477a2011-01-21 13:55:52 +090052 base::AutoLock scoped_lock(lock_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090053 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.orgabe477a2011-01-21 13:55:52 +090062 base::AutoLock scoped_lock(lock_);
david.mike.futcher@gmail.com9eb2aa52011-04-19 05:07:08 +090063 DCHECK_EQ(STATE_CHECK_STARTED, state_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090064 state_ = STATE_CHECK_FINISHED;
65 }
66
67 private:
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090068 base::Lock lock_;
thestig@chromium.org741196d2009-10-13 11:51:07 +090069 LinuxDistroState state_;
70};
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090071#endif // if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090072
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090073} // namespace
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090074
sgk@google.come1f4a242009-04-22 02:20:10 +090075namespace base {
76
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +090077// Account for the terminating null character.
78static const int kDistroSize = 128 + 1;
79
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090080// 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.org8584b6d2010-08-26 17:55:22 +090082char g_linux_distro[kDistroSize] =
rbyers@chromium.org088a4932012-08-16 05:54:06 +090083#if defined(OS_CHROMEOS)
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090084 "CrOS";
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090085#elif defined(OS_ANDROID)
86 "Android";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090087#else // if defined(OS_LINUX)
88 "Unknown";
89#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090090
91std::string GetLinuxDistro() {
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090092#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +090093 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +090094#elif defined(OS_LINUX)
satish@chromium.orgce3f4872010-12-14 01:52:35 +090095 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
thestig@chromium.org741196d2009-10-13 11:51:07 +090096 LinuxDistroState state = distro_state_singleton->State();
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +090097 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.raula5ba94892015-09-10 20:14:22 +0900109 GetAppOutput(CommandLine(argv), &output);
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900110 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.orga5a2b6e2009-07-17 14:55:51 +0900115 }
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900116 }
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900117 distro_state_singleton->CheckFinished();
118 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +0900119#else
120 NOTIMPLEMENTED();
robert.nagy@gmail.comea54e462011-10-25 07:05:27 +0900121 return "Unknown";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +0900122#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900123}
124
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900125void SetLinuxDistro(const std::string& distro) {
126 std::string trimmed_distro;
olli.raula5ba94892015-09-10 20:14:22 +0900127 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
128 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900129}
130
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900131pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
132 bool* syscall_supported) {
thestig@chromium.org44836d42010-07-17 04:28:17 +0900133 char buf[256];
134 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900135
136 if (syscall_supported != NULL)
137 *syscall_supported = false;
138
thestig@chromium.org44836d42010-07-17 04:28:17 +0900139 DIR* task = opendir(buf);
140 if (!task) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900141 DLOG(WARNING) << "Cannot open " << buf;
thestig@chromium.org44836d42010-07-17 04:28:17 +0900142 return -1;
143 }
144
145 std::vector<pid_t> tids;
146 struct dirent* dent;
147 while ((dent = readdir(task))) {
thestig@chromium.orgce2f2412012-06-14 08:20:04 +0900148 char* endptr;
thestig@chromium.org44836d42010-07-17 04:28:17 +0900149 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.org15bc41d2013-01-18 23:21:58 +0900156 scoped_ptr<char[]> syscall_data(new char[expected_data.length()]);
thestig@chromium.org44836d42010-07-17 04:28:17 +0900157 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.orge406ed72011-06-21 13:21:06 +0900164 if (syscall_supported != NULL)
165 *syscall_supported = true;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900166 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
thestig@chromium.org44836d42010-07-17 04:28:17 +0900167 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.come1f4a242009-04-22 02:20:10 +0900179} // namespace base