blob: 21d579954d55c78acf263d886793e1c20920392d [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_request_prep_action.h"
6#include <sys/utsname.h>
7#include <string>
8#include "update_engine/utils.h"
9
10using std::string;
11
12// This gathers local system information and prepares info used by the
13// update check action.
14
15namespace chromeos_update_engine {
16
17void OmahaRequestPrepAction::PerformAction() {
18 // TODO(adlr): honor force_full_update_
19 const string machine_id(GetMachineId());
20 const string version(GetLsbValue("GOOGLE_RELEASE"));
21 const string sp(version + "_" + GetMachineType());
22 const string track(GetLsbValue("GOOGLE_TRACK"));
23
24 UpdateCheckParams out(machine_id, // machine_id
25 machine_id, // user_id (use machine_id)
26 UpdateCheckParams::kOsPlatform,
27 UpdateCheckParams::kOsVersion,
28 sp, // e.g. 0.2.3.3_i686
29 UpdateCheckParams::kAppId,
30 version, // app version (from lsb-release)
31 "en-US", //lang
32 track); // track
33
34 CHECK(HasOutputPipe());
35 SetOutputObject(out);
36 processor_->ActionComplete(this, true);
37}
38
39std::string OmahaRequestPrepAction::GetMachineId() const {
40 FILE* fp = popen("/sbin/ifconfig", "r");
41 if (!fp)
42 return "";
43 string data;
44 for (;;) {
45 char buffer[1000];
46 size_t r = fread(buffer, 1, sizeof(buffer), fp);
47 if (r <= 0)
48 break;
49 data.insert(data.end(), buffer, buffer + r);
50 }
51 fclose(fp);
52 // scan data for MAC address
53 string::size_type pos = data.find(" HWaddr ");
54 if (pos == string::npos)
55 return "";
56 // 3 * 6 - 1 is the number of bytes of the hwaddr.
57 return data.substr(pos + strlen(" HWaddr "), 3 * 6 - 1);
58}
59
60std::string OmahaRequestPrepAction::GetLsbValue(const std::string& key) const {
61 string files[] = {utils::kStatefulPartition + "/etc/lsb-release",
62 "/etc/lsb-release"};
63 for (unsigned int i = 0; i < arraysize(files); i++) {
64 string file_data;
65 if (!utils::ReadFileToString(root_ + files[i], &file_data))
66 continue;
67 string::size_type pos = 0;
68 if (!utils::StringHasPrefix(file_data, key + "=")) {
69 pos = file_data.find(string("\n") + key + "=");
70 if (pos != string::npos)
71 pos++; // advance past \n
72 }
73 if (pos == string::npos)
74 continue;
75 pos += key.size() + 1; // advance past the key and the '='
76 string::size_type endpos = file_data.find('\n', pos);
77 string::size_type length =
78 (endpos == string::npos ? string::npos : endpos - pos);
79 return file_data.substr(pos, length);
80 }
81 // not found
82 return "";
83}
84
85std::string OmahaRequestPrepAction::GetMachineType() const {
86 struct utsname buf;
87 string ret;
88 if (uname(&buf) == 0)
89 ret = buf.machine;
90 return ret;
91}
92
93} // namespace chromeos_update_engine