blob: 69df9ddb24882ecfb591a55bdaa99406f4972dd1 [file] [log] [blame]
Gunnar Millse91d3212017-04-19 15:42:47 -05001#include <string>
Gunnar Mills19e4ce52017-04-27 11:24:19 -05002#include <experimental/filesystem>
3#include <stdlib.h>
4#include <cstring>
5#include <stdio.h>
6#include <unistd.h>
Gunnar Mills3027bba2017-04-27 15:49:03 -05007#include <algorithm>
Gunnar Mills19e4ce52017-04-27 11:24:19 -05008#include <sys/wait.h>
9#include <sys/stat.h>
10#include <phosphor-logging/log.hpp>
Gunnar Mills93c12d32017-05-10 13:11:53 -050011#include <phosphor-logging/elog.hpp>
12#include <elog-errors.hpp>
13#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Mills19e4ce52017-04-27 11:24:19 -050014#include "config.h"
15#include "version.hpp"
Gunnar Mills3027bba2017-04-27 15:49:03 -050016#include "watch.hpp"
Gunnar Millse91d3212017-04-19 15:42:47 -050017#include "image_manager.hpp"
18
19namespace phosphor
20{
21namespace software
22{
23namespace manager
24{
25
Gunnar Mills19e4ce52017-04-27 11:24:19 -050026using namespace phosphor::logging;
Gunnar Mills93c12d32017-05-10 13:11:53 -050027using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Gunnar Millsb30cadd2017-10-06 16:07:32 -050028namespace Software = phosphor::logging::xyz::openbmc_project::Software;
29using ManifestFail = Software::Version::ManifestFileFailure;
30using UnTarFail = Software::Version::UnTarFailure;
31using InternalFail= Software::Version::InternalFailure;
Gunnar Mills19e4ce52017-04-27 11:24:19 -050032namespace fs = std::experimental::filesystem;
33
34struct RemovablePath
35{
36 fs::path path;
37
38 RemovablePath(const fs::path& path) : path(path) {}
39 ~RemovablePath()
40 {
Adriana Kobylak2b2cd9f2018-02-12 16:20:13 -060041 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 Mills19e4ce52017-04-27 11:24:19 -050051 }
52};
53
Gunnar Mills3027bba2017-04-27 15:49:03 -050054int Manager::processImage(const std::string& tarFilePath)
Gunnar Millse91d3212017-04-19 15:42:47 -050055{
Gunnar Mills19e4ce52017-04-27 11:24:19 -050056 if (!fs::is_regular_file(tarFilePath))
57 {
58 log<level::ERR>("Error tarball does not exist",
59 entry("FILENAME=%s", tarFilePath));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050060 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050061 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 Mills601bbf02017-10-25 13:46:11 -050071 log<level::ERR>("Error occurred during mkdtemp",
Gunnar Mills19e4ce52017-04-27 11:24:19 -050072 entry("ERRNO=%d", errno));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050073 report<InternalFailure>(InternalFail::FAIL("mkdtemp"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050074 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 Millsb30cadd2017-10-06 16:07:32 -050092 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050093 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 Millsb30cadd2017-10-06 16:07:32 -0500102 report<InternalFailure>(InternalFail::FAIL("fork"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500103 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 Millsb30cadd2017-10-06 16:07:32 -0500110 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500111 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 Millsb30cadd2017-10-06 16:07:32 -0500119 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500120 return -1;
121 }
122
Gunnar Mills3027bba2017-04-27 15:49:03 -0500123 // 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 Millsb30cadd2017-10-06 16:07:32 -0500128 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills3027bba2017-04-27 15:49:03 -0500129 return -1;
130 }
131
Gunnar Mills3027bba2017-04-27 15:49:03 -0500132 auto purpose = Version::VersionPurpose::Unknown;
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500133 try
134 {
Leonel Gonzalez9a8d14c2017-06-22 11:42:24 -0500135 purpose = Version::convertVersionPurposeFromString(purposeString);
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500136 }
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 Mills3027bba2017-04-27 15:49:03 -0500141 }
142
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500143 // Compute id
144 auto id = Version::getId(version);
145
146 fs::path imageDirPath = std::string{IMG_UPLOAD_DIR};
147 imageDirPath /= id;
148
Gunnar Millsf576bca2017-05-18 13:40:39 -0500149 if (fs::exists(imageDirPath))
150 {
151 fs::remove_all(imageDirPath);
152 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500153 if (mkdir(imageDirPath.c_str(), S_IRWXU) != 0)
154 {
Gunnar Mills601bbf02017-10-25 13:46:11 -0500155 log<level::ERR>("Error occurred during mkdir",
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500156 entry("ERRNO=%d", errno));
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500157 report<InternalFailure>(InternalFail::FAIL("mkdir"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500158 return -1;
159 }
160
161 // Untar tarball
Gunnar Mills9ec18ef2017-05-24 11:10:46 -0500162 auto rc = unTar(tarFilePath, imageDirPath.string());
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500163 if (rc < 0)
164 {
Gunnar Mills601bbf02017-10-25 13:46:11 -0500165 log<level::ERR>("Error occurred during untar");
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500166 return -1;
167 }
168
Gunnar Mills3027bba2017-04-27 15:49:03 -0500169 // Create Version object
170 auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
171
Saqib Khand377ba12017-10-16 14:32:30 -0500172 if (versions.find(id) == versions.end())
173 {
Saqib Khanee13e832017-10-23 12:53:11 -0500174 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 Khand377ba12017-10-16 14:32:30 -0500187 }
188 else
189 {
190 log<level::INFO>("Software Object with the same version already exists",
191 entry("VERSION_ID=%s", id));
192 }
Gunnar Millse91d3212017-04-19 15:42:47 -0500193 return 0;
194}
195
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500196void Manager::erase(std::string entryId)
197{
198 auto it = versions.find(entryId);
199 if (it == versions.end())
200 {
201 return;
202 }
Eddie James6d873712017-09-01 11:29:07 -0500203
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 Gonzalez50d559c2017-07-07 14:38:25 -0500212 // 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 Mills3027bba2017-04-27 15:49:03 -0500221int Manager::unTar(const std::string& tarFilePath,
222 const std::string& extractDirPath)
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500223{
224 if (tarFilePath.empty())
225 {
226 log<level::ERR>("Error TarFilePath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500227 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500228 return -1;
229 }
230 if (extractDirPath.empty())
231 {
232 log<level::ERR>("Error ExtractDirPath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500233 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500234 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 Millsb30cadd2017-10-06 16:07:32 -0500251 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500252 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 Millsb30cadd2017-10-06 16:07:32 -0500261 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500262 return -1;
263 }
264
265 return 0;
266}
Gunnar Mills3027bba2017-04-27 15:49:03 -0500267
Gunnar Millse91d3212017-04-19 15:42:47 -0500268} // namespace manager
269} // namespace software
270} // namepsace phosphor