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