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