blob: 4a27260afe5c381447b7a127e6e2f0c005b8598a [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 {
41 fs::remove_all(path);
42 }
43};
44
Gunnar Mills3027bba2017-04-27 15:49:03 -050045int Manager::processImage(const std::string& tarFilePath)
Gunnar Millse91d3212017-04-19 15:42:47 -050046{
Gunnar Mills19e4ce52017-04-27 11:24:19 -050047 if (!fs::is_regular_file(tarFilePath))
48 {
49 log<level::ERR>("Error tarball does not exist",
50 entry("FILENAME=%s", tarFilePath));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050051 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050052 return -1;
53
54 }
55 RemovablePath tarPathRemove(tarFilePath);
56 fs::path tmpDirPath(std::string{IMG_UPLOAD_DIR});
57 tmpDirPath /= "imageXXXXXX";
58
59 // Need tmp dir to write MANIFEST file to.
60 if (!mkdtemp(const_cast<char*>(tmpDirPath.c_str())))
61 {
Gunnar Mills601bbf02017-10-25 13:46:11 -050062 log<level::ERR>("Error occurred during mkdtemp",
Gunnar Mills19e4ce52017-04-27 11:24:19 -050063 entry("ERRNO=%d", errno));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050064 report<InternalFailure>(InternalFail::FAIL("mkdtemp"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050065 return -1;
66 }
67
68 RemovablePath tmpDirRemove(tmpDirPath);
69 fs::path manifestPath = tmpDirPath;
70 manifestPath /= MANIFEST_FILE_NAME;
71 int status = 0;
72 pid_t pid = fork();
73
74 // Get the MANIFEST FILE
75 if (pid == 0)
76 {
77 // child process
78 execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(), MANIFEST_FILE_NAME,
79 "-C", tmpDirPath.c_str(), (char*)0);
80 // execl only returns on fail
81 log<level::ERR>("Failed to untar file",
82 entry("FILENAME=%s", tarFilePath));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050083 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050084 return -1;
85 }
86 else if (pid > 0)
87 {
88 waitpid(pid, &status, 0);
89 }
90 else
91 {
92 log<level::ERR>("fork() failed.");
Gunnar Millsb30cadd2017-10-06 16:07:32 -050093 report<InternalFailure>(InternalFail::FAIL("fork"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050094 return -1;
95 }
96
97 // Verify the manifest file
98 if (!fs::is_regular_file(manifestPath))
99 {
100 log<level::ERR>("Error No manifest file");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500101 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500102 return -1;
103 }
104
105 // Get version
106 auto version = Version::getValue(manifestPath.string(), "version");
107 if (version.empty())
108 {
109 log<level::ERR>("Error unable to read version from 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
Gunnar Mills3027bba2017-04-27 15:49:03 -0500114 // Get purpose
115 auto purposeString = Version::getValue(manifestPath.string(), "purpose");
116 if (purposeString.empty())
117 {
118 log<level::ERR>("Error unable to read purpose from manifest file");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500119 report<ManifestFileFailure>(ManifestFail::PATH(manifestPath.c_str()));
Gunnar Mills3027bba2017-04-27 15:49:03 -0500120 return -1;
121 }
122
Gunnar Mills3027bba2017-04-27 15:49:03 -0500123 auto purpose = Version::VersionPurpose::Unknown;
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500124 try
125 {
Leonel Gonzalez9a8d14c2017-06-22 11:42:24 -0500126 purpose = Version::convertVersionPurposeFromString(purposeString);
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500127 }
128 catch (const sdbusplus::exception::InvalidEnumString& e)
129 {
130 log<level::ERR>("Error: Failed to convert manifest purpose to enum." \
131 " Setting to Unknown.");
Gunnar Mills3027bba2017-04-27 15:49:03 -0500132 }
133
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500134 // Compute id
135 auto id = Version::getId(version);
136
137 fs::path imageDirPath = std::string{IMG_UPLOAD_DIR};
138 imageDirPath /= id;
139
Gunnar Millsf576bca2017-05-18 13:40:39 -0500140 if (fs::exists(imageDirPath))
141 {
142 fs::remove_all(imageDirPath);
143 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500144 if (mkdir(imageDirPath.c_str(), S_IRWXU) != 0)
145 {
Gunnar Mills601bbf02017-10-25 13:46:11 -0500146 log<level::ERR>("Error occurred during mkdir",
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500147 entry("ERRNO=%d", errno));
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500148 report<InternalFailure>(InternalFail::FAIL("mkdir"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500149 return -1;
150 }
151
152 // Untar tarball
Gunnar Mills9ec18ef2017-05-24 11:10:46 -0500153 auto rc = unTar(tarFilePath, imageDirPath.string());
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500154 if (rc < 0)
155 {
Gunnar Mills601bbf02017-10-25 13:46:11 -0500156 log<level::ERR>("Error occurred during untar");
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500157 return -1;
158 }
159
Gunnar Mills3027bba2017-04-27 15:49:03 -0500160 // Create Version object
161 auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
162
Saqib Khand377ba12017-10-16 14:32:30 -0500163 if (versions.find(id) == versions.end())
164 {
Saqib Khanee13e832017-10-23 12:53:11 -0500165 auto versionPtr = std::make_unique<Version>(
166 bus,
167 objPath,
168 version,
169 purpose,
170 imageDirPath.string(),
171 std::bind(&Manager::erase,
172 this,
173 std::placeholders::_1));
174 versionPtr->deleteObject =
175 std::make_unique<phosphor::software::manager::Delete>(
176 bus, objPath, *versionPtr);
177 versions.insert(std::make_pair(id, std::move(versionPtr)));
Saqib Khand377ba12017-10-16 14:32:30 -0500178 }
179 else
180 {
181 log<level::INFO>("Software Object with the same version already exists",
182 entry("VERSION_ID=%s", id));
183 }
Gunnar Millse91d3212017-04-19 15:42:47 -0500184 return 0;
185}
186
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500187void Manager::erase(std::string entryId)
188{
189 auto it = versions.find(entryId);
190 if (it == versions.end())
191 {
192 return;
193 }
Eddie James6d873712017-09-01 11:29:07 -0500194
195 if (it->second->isFunctional())
196 {
197 log<level::ERR>(("Error: Version " + entryId + \
198 " is currently running on the BMC." \
199 " Unable to remove.").c_str());
200 return;
201 }
202
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500203 // Delete image dir
204 fs::path imageDirPath = (*(it->second)).path();
205 if (fs::exists(imageDirPath))
206 {
207 fs::remove_all(imageDirPath);
208 }
209 this->versions.erase(entryId);
210}
211
Gunnar Mills3027bba2017-04-27 15:49:03 -0500212int Manager::unTar(const std::string& tarFilePath,
213 const std::string& extractDirPath)
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500214{
215 if (tarFilePath.empty())
216 {
217 log<level::ERR>("Error TarFilePath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500218 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500219 return -1;
220 }
221 if (extractDirPath.empty())
222 {
223 log<level::ERR>("Error ExtractDirPath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500224 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500225 return -1;
226 }
227
228 log<level::INFO>("Untaring",
229 entry("FILENAME=%s", tarFilePath),
230 entry("EXTRACTIONDIR=%s", extractDirPath));
231 int status = 0;
232 pid_t pid = fork();
233
234 if (pid == 0)
235 {
236 // child process
237 execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(),
238 "-C", extractDirPath.c_str(), (char*)0);
239 // execl only returns on fail
240 log<level::ERR>("Failed to untar file",
241 entry("FILENAME=%s", tarFilePath));
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500242 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500243 return -1;
244 }
245 else if (pid > 0)
246 {
247 waitpid(pid, &status, 0);
248 }
249 else
250 {
251 log<level::ERR>("fork() failed.");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500252 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500253 return -1;
254 }
255
256 return 0;
257}
Gunnar Mills3027bba2017-04-27 15:49:03 -0500258
Gunnar Millse91d3212017-04-19 15:42:47 -0500259} // namespace manager
260} // namespace software
261} // namepsace phosphor