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