Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 2 | |
| 3 | #include "image_manager.hpp" |
| 4 | |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 5 | #include "version.hpp" |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 6 | #include "watch.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/wait.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <cstring> |
| 16 | #include <elog-errors.hpp> |
| 17 | #include <experimental/filesystem> |
| 18 | #include <phosphor-logging/elog.hpp> |
| 19 | #include <phosphor-logging/log.hpp> |
| 20 | #include <string> |
Adriana Kobylak | 43699ca | 2018-10-17 14:56:29 -0500 | [diff] [blame] | 21 | #include <xyz/openbmc_project/Software/Image/error.hpp> |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace software |
| 26 | { |
| 27 | namespace manager |
| 28 | { |
| 29 | |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 30 | using namespace phosphor::logging; |
Adriana Kobylak | 43699ca | 2018-10-17 14:56:29 -0500 | [diff] [blame] | 31 | using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error; |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 32 | namespace Software = phosphor::logging::xyz::openbmc_project::Software; |
Adriana Kobylak | 43699ca | 2018-10-17 14:56:29 -0500 | [diff] [blame] | 33 | using ManifestFail = Software::Image::ManifestFileFailure; |
| 34 | using UnTarFail = Software::Image::UnTarFailure; |
| 35 | using InternalFail = Software::Image::InternalFailure; |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 36 | namespace fs = std::experimental::filesystem; |
| 37 | |
| 38 | struct RemovablePath |
| 39 | { |
| 40 | fs::path path; |
| 41 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 42 | RemovablePath(const fs::path& path) : path(path) |
| 43 | { |
| 44 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 45 | ~RemovablePath() |
| 46 | { |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 47 | if (!path.empty()) |
Adriana Kobylak | 2b2cd9f | 2018-02-12 16:20:13 -0600 | [diff] [blame] | 48 | { |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 49 | std::error_code ec; |
| 50 | fs::remove_all(path, ec); |
Adriana Kobylak | 2b2cd9f | 2018-02-12 16:20:13 -0600 | [diff] [blame] | 51 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | |
Lei YU | f6144e9 | 2019-10-18 17:13:49 +0800 | [diff] [blame] | 55 | namespace // anonymous |
| 56 | { |
| 57 | |
| 58 | std::vector<std::string> getSoftwareObjects(sdbusplus::bus::bus& bus) |
| 59 | { |
| 60 | std::vector<std::string> paths; |
| 61 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 62 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 63 | method.append(SOFTWARE_OBJPATH); |
| 64 | method.append(0); // Depth 0 to search all |
| 65 | method.append(std::vector<std::string>({VERSION_BUSNAME})); |
| 66 | auto reply = bus.call(method); |
| 67 | reply.read(paths); |
| 68 | return paths; |
| 69 | } |
| 70 | |
| 71 | } // namespace |
| 72 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 73 | int Manager::processImage(const std::string& tarFilePath) |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 74 | { |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 75 | if (!fs::is_regular_file(tarFilePath)) |
| 76 | { |
| 77 | log<level::ERR>("Error tarball does not exist", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 78 | entry("FILENAME=%s", tarFilePath.c_str())); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 79 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 80 | return -1; |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 81 | } |
| 82 | RemovablePath tarPathRemove(tarFilePath); |
| 83 | fs::path tmpDirPath(std::string{IMG_UPLOAD_DIR}); |
| 84 | tmpDirPath /= "imageXXXXXX"; |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 85 | auto tmpDir = tmpDirPath.string(); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 86 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 87 | // Create a tmp dir to extract tarball. |
| 88 | if (!mkdtemp(tmpDir.data())) |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 89 | { |
Gunnar Mills | 601bbf0 | 2017-10-25 13:46:11 -0500 | [diff] [blame] | 90 | log<level::ERR>("Error occurred during mkdtemp", |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 91 | entry("ERRNO=%d", errno)); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 92 | report<InternalFailure>(InternalFail::FAIL("mkdtemp")); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 96 | tmpDirPath = tmpDir; |
| 97 | RemovablePath tmpDirToRemove(tmpDirPath); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 98 | fs::path manifestPath = tmpDirPath; |
| 99 | manifestPath /= MANIFEST_FILE_NAME; |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 100 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 101 | // Untar tarball into the tmp dir |
| 102 | auto rc = unTar(tarFilePath, tmpDirPath.string()); |
| 103 | if (rc < 0) |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 104 | { |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 105 | log<level::ERR>("Error occurred during untar"); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | // Verify the manifest file |
| 110 | if (!fs::is_regular_file(manifestPath)) |
| 111 | { |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 112 | log<level::ERR>("Error No manifest file", |
| 113 | entry("FILENAME=%s", tarFilePath.c_str())); |
| 114 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | // Get version |
| 119 | auto version = Version::getValue(manifestPath.string(), "version"); |
| 120 | if (version.empty()) |
| 121 | { |
| 122 | log<level::ERR>("Error unable to read version from manifest file"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 123 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
Vijay Khemka | b7c062e | 2019-09-18 17:15:57 -0700 | [diff] [blame] | 127 | // Get running machine name |
| 128 | std::string currMachine = Version::getBMCMachine(OS_RELEASE_FILE); |
| 129 | if (currMachine.empty()) |
| 130 | { |
| 131 | log<level::ERR>("Failed to read machine name from osRelease", |
| 132 | entry("FILENAME=%s", OS_RELEASE_FILE)); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | // Get machine name for image to be upgraded |
| 137 | std::string machineStr = |
| 138 | Version::getValue(manifestPath.string(), "MachineName"); |
| 139 | if (!machineStr.empty()) |
| 140 | { |
| 141 | if (machineStr != currMachine) |
| 142 | { |
| 143 | log<level::ERR>("BMC upgrade: Machine name doesn't match", |
| 144 | entry("CURR_MACHINE=%s", currMachine.c_str()), |
| 145 | entry("NEW_MACHINE=%s", machineStr.c_str())); |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | log<level::WARNING>("No machine name in Manifest file"); |
| 152 | } |
| 153 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 154 | // Get purpose |
| 155 | auto purposeString = Version::getValue(manifestPath.string(), "purpose"); |
| 156 | if (purposeString.empty()) |
| 157 | { |
| 158 | log<level::ERR>("Error unable to read purpose from manifest file"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 159 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 163 | auto purpose = Version::VersionPurpose::Unknown; |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 164 | try |
| 165 | { |
Leonel Gonzalez | 9a8d14c | 2017-06-22 11:42:24 -0500 | [diff] [blame] | 166 | purpose = Version::convertVersionPurposeFromString(purposeString); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 167 | } |
| 168 | catch (const sdbusplus::exception::InvalidEnumString& e) |
| 169 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 170 | log<level::ERR>("Error: Failed to convert manifest purpose to enum." |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 171 | " Setting to Unknown."); |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 174 | // Compute id |
| 175 | auto id = Version::getId(version); |
| 176 | |
| 177 | fs::path imageDirPath = std::string{IMG_UPLOAD_DIR}; |
| 178 | imageDirPath /= id; |
| 179 | |
Gunnar Mills | f576bca | 2017-05-18 13:40:39 -0500 | [diff] [blame] | 180 | if (fs::exists(imageDirPath)) |
| 181 | { |
| 182 | fs::remove_all(imageDirPath); |
| 183 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 184 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame] | 185 | // Rename the temp dir to image dir |
| 186 | fs::rename(tmpDirPath, imageDirPath); |
| 187 | |
| 188 | // Clear the path, so it does not attemp to remove a non-existing path |
| 189 | tmpDirToRemove.path.clear(); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 190 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 191 | auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id; |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 192 | |
Lei YU | f6144e9 | 2019-10-18 17:13:49 +0800 | [diff] [blame] | 193 | // This service only manages the uploaded versions, and there could be |
| 194 | // active versions on D-Bus that is not managed by this service. |
| 195 | // So check D-Bus if there is an existing version. |
| 196 | auto allSoftwareObjs = getSoftwareObjects(bus); |
| 197 | auto it = |
| 198 | std::find(allSoftwareObjs.begin(), allSoftwareObjs.end(), objPath); |
| 199 | if (versions.find(id) == versions.end() && it == allSoftwareObjs.end()) |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 200 | { |
Lei YU | f6144e9 | 2019-10-18 17:13:49 +0800 | [diff] [blame] | 201 | // Create Version object |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 202 | auto versionPtr = std::make_unique<Version>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 203 | bus, objPath, version, purpose, imageDirPath.string(), |
| 204 | std::bind(&Manager::erase, this, std::placeholders::_1)); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 205 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 206 | std::make_unique<phosphor::software::manager::Delete>(bus, objPath, |
| 207 | *versionPtr); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 208 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 209 | } |
| 210 | else |
| 211 | { |
| 212 | log<level::INFO>("Software Object with the same version already exists", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 213 | entry("VERSION_ID=%s", id.c_str())); |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 214 | } |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Leonel Gonzalez | 50d559c | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 218 | void Manager::erase(std::string entryId) |
| 219 | { |
| 220 | auto it = versions.find(entryId); |
| 221 | if (it == versions.end()) |
| 222 | { |
| 223 | return; |
| 224 | } |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 225 | |
| 226 | if (it->second->isFunctional()) |
| 227 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 228 | log<level::ERR>(("Error: Version " + entryId + |
| 229 | " is currently running on the BMC." |
| 230 | " Unable to remove.") |
| 231 | .c_str()); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
Leonel Gonzalez | 50d559c | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 235 | // Delete image dir |
| 236 | fs::path imageDirPath = (*(it->second)).path(); |
| 237 | if (fs::exists(imageDirPath)) |
| 238 | { |
| 239 | fs::remove_all(imageDirPath); |
| 240 | } |
| 241 | this->versions.erase(entryId); |
| 242 | } |
| 243 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 244 | int Manager::unTar(const std::string& tarFilePath, |
| 245 | const std::string& extractDirPath) |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 246 | { |
| 247 | if (tarFilePath.empty()) |
| 248 | { |
| 249 | log<level::ERR>("Error TarFilePath is empty"); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 250 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 251 | return -1; |
| 252 | } |
| 253 | if (extractDirPath.empty()) |
| 254 | { |
| 255 | log<level::ERR>("Error ExtractDirPath is empty"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 256 | report<UnTarFailure>(UnTarFail::PATH(extractDirPath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 257 | return -1; |
| 258 | } |
| 259 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 260 | log<level::INFO>("Untaring", entry("FILENAME=%s", tarFilePath.c_str()), |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 261 | entry("EXTRACTIONDIR=%s", extractDirPath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 262 | int status = 0; |
| 263 | pid_t pid = fork(); |
| 264 | |
| 265 | if (pid == 0) |
| 266 | { |
| 267 | // child process |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 268 | execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(), "-C", |
| 269 | extractDirPath.c_str(), (char*)0); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 270 | // execl only returns on fail |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 271 | log<level::ERR>("Failed to execute untar file", |
| 272 | entry("FILENAME=%s", tarFilePath.c_str())); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 273 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | else if (pid > 0) |
| 277 | { |
| 278 | waitpid(pid, &status, 0); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 279 | if (WEXITSTATUS(status)) |
| 280 | { |
| 281 | log<level::ERR>("Failed to untar file", |
| 282 | entry("FILENAME=%s", tarFilePath.c_str())); |
| 283 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
| 284 | return -1; |
| 285 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
| 289 | log<level::ERR>("fork() failed."); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 290 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 296 | |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 297 | } // namespace manager |
| 298 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 299 | } // namespace phosphor |