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 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 55 | int Manager::processImage(const std::string& tarFilePath) |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 56 | { |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 57 | if (!fs::is_regular_file(tarFilePath)) |
| 58 | { |
| 59 | log<level::ERR>("Error tarball does not exist", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 60 | entry("FILENAME=%s", tarFilePath.c_str())); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 61 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 62 | return -1; |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 63 | } |
| 64 | RemovablePath tarPathRemove(tarFilePath); |
| 65 | fs::path tmpDirPath(std::string{IMG_UPLOAD_DIR}); |
| 66 | tmpDirPath /= "imageXXXXXX"; |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame^] | 67 | auto tmpDir = tmpDirPath.string(); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 68 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame^] | 69 | // Create a tmp dir to extract tarball. |
| 70 | if (!mkdtemp(tmpDir.data())) |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 71 | { |
Gunnar Mills | 601bbf0 | 2017-10-25 13:46:11 -0500 | [diff] [blame] | 72 | log<level::ERR>("Error occurred during mkdtemp", |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 73 | entry("ERRNO=%d", errno)); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 74 | report<InternalFailure>(InternalFail::FAIL("mkdtemp")); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame^] | 78 | tmpDirPath = tmpDir; |
| 79 | RemovablePath tmpDirToRemove(tmpDirPath); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 80 | fs::path manifestPath = tmpDirPath; |
| 81 | manifestPath /= MANIFEST_FILE_NAME; |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 82 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame^] | 83 | // Untar tarball into the tmp dir |
| 84 | auto rc = unTar(tarFilePath, tmpDirPath.string()); |
| 85 | if (rc < 0) |
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 | log<level::ERR>("Error occurred during untar"); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | // Verify the manifest file |
| 92 | if (!fs::is_regular_file(manifestPath)) |
| 93 | { |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 94 | log<level::ERR>("Error No manifest file", |
| 95 | entry("FILENAME=%s", tarFilePath.c_str())); |
| 96 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | // Get version |
| 101 | auto version = Version::getValue(manifestPath.string(), "version"); |
| 102 | if (version.empty()) |
| 103 | { |
| 104 | log<level::ERR>("Error unable to read version from manifest file"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 105 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 109 | // Get purpose |
| 110 | auto purposeString = Version::getValue(manifestPath.string(), "purpose"); |
| 111 | if (purposeString.empty()) |
| 112 | { |
| 113 | log<level::ERR>("Error unable to read purpose from manifest file"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 114 | report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 118 | auto purpose = Version::VersionPurpose::Unknown; |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 119 | try |
| 120 | { |
Leonel Gonzalez | 9a8d14c | 2017-06-22 11:42:24 -0500 | [diff] [blame] | 121 | purpose = Version::convertVersionPurposeFromString(purposeString); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 122 | } |
| 123 | catch (const sdbusplus::exception::InvalidEnumString& e) |
| 124 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 125 | log<level::ERR>("Error: Failed to convert manifest purpose to enum." |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 126 | " Setting to Unknown."); |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 129 | // Compute id |
| 130 | auto id = Version::getId(version); |
| 131 | |
| 132 | fs::path imageDirPath = std::string{IMG_UPLOAD_DIR}; |
| 133 | imageDirPath /= id; |
| 134 | |
Gunnar Mills | f576bca | 2017-05-18 13:40:39 -0500 | [diff] [blame] | 135 | if (fs::exists(imageDirPath)) |
| 136 | { |
| 137 | fs::remove_all(imageDirPath); |
| 138 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 139 | |
Lei YU | 5d93028 | 2019-03-08 16:41:51 +0800 | [diff] [blame^] | 140 | // Rename the temp dir to image dir |
| 141 | fs::rename(tmpDirPath, imageDirPath); |
| 142 | |
| 143 | // Clear the path, so it does not attemp to remove a non-existing path |
| 144 | tmpDirToRemove.path.clear(); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 145 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 146 | // Create Version object |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 147 | auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id; |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 148 | |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 149 | if (versions.find(id) == versions.end()) |
| 150 | { |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 151 | auto versionPtr = std::make_unique<Version>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 152 | bus, objPath, version, purpose, imageDirPath.string(), |
| 153 | std::bind(&Manager::erase, this, std::placeholders::_1)); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 154 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 155 | std::make_unique<phosphor::software::manager::Delete>(bus, objPath, |
| 156 | *versionPtr); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 157 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | { |
| 161 | log<level::INFO>("Software Object with the same version already exists", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 162 | entry("VERSION_ID=%s", id.c_str())); |
Saqib Khan | d377ba1 | 2017-10-16 14:32:30 -0500 | [diff] [blame] | 163 | } |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Leonel Gonzalez | 50d559c | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 167 | void Manager::erase(std::string entryId) |
| 168 | { |
| 169 | auto it = versions.find(entryId); |
| 170 | if (it == versions.end()) |
| 171 | { |
| 172 | return; |
| 173 | } |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 174 | |
| 175 | if (it->second->isFunctional()) |
| 176 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 177 | log<level::ERR>(("Error: Version " + entryId + |
| 178 | " is currently running on the BMC." |
| 179 | " Unable to remove.") |
| 180 | .c_str()); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
Leonel Gonzalez | 50d559c | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 184 | // Delete image dir |
| 185 | fs::path imageDirPath = (*(it->second)).path(); |
| 186 | if (fs::exists(imageDirPath)) |
| 187 | { |
| 188 | fs::remove_all(imageDirPath); |
| 189 | } |
| 190 | this->versions.erase(entryId); |
| 191 | } |
| 192 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 193 | int Manager::unTar(const std::string& tarFilePath, |
| 194 | const std::string& extractDirPath) |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 195 | { |
| 196 | if (tarFilePath.empty()) |
| 197 | { |
| 198 | log<level::ERR>("Error TarFilePath is empty"); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 199 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | if (extractDirPath.empty()) |
| 203 | { |
| 204 | log<level::ERR>("Error ExtractDirPath is empty"); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 205 | report<UnTarFailure>(UnTarFail::PATH(extractDirPath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 209 | log<level::INFO>("Untaring", entry("FILENAME=%s", tarFilePath.c_str()), |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 210 | entry("EXTRACTIONDIR=%s", extractDirPath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 211 | int status = 0; |
| 212 | pid_t pid = fork(); |
| 213 | |
| 214 | if (pid == 0) |
| 215 | { |
| 216 | // child process |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 217 | execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(), "-C", |
| 218 | extractDirPath.c_str(), (char*)0); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 219 | // execl only returns on fail |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 220 | log<level::ERR>("Failed to execute untar file", |
| 221 | entry("FILENAME=%s", tarFilePath.c_str())); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 222 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | else if (pid > 0) |
| 226 | { |
| 227 | waitpid(pid, &status, 0); |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 228 | if (WEXITSTATUS(status)) |
| 229 | { |
| 230 | log<level::ERR>("Failed to untar file", |
| 231 | entry("FILENAME=%s", tarFilePath.c_str())); |
| 232 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
| 233 | return -1; |
| 234 | } |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 235 | } |
| 236 | else |
| 237 | { |
| 238 | log<level::ERR>("fork() failed."); |
Gunnar Mills | b30cadd | 2017-10-06 16:07:32 -0500 | [diff] [blame] | 239 | report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str())); |
Gunnar Mills | 19e4ce5 | 2017-04-27 11:24:19 -0500 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 245 | |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 246 | } // namespace manager |
| 247 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 248 | } // namespace phosphor |