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