blob: 206722c8e3db88c7c23df128fb87fa3306834299 [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 {
62 log<level::ERR>("Error occured during mkdtemp",
63 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 {
146 log<level::ERR>("Error occured during mkdir",
147 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 {
156 log<level::ERR>("Error occured during untar");
157 return -1;
158 }
159
Gunnar Mills3027bba2017-04-27 15:49:03 -0500160 // Create Version object
161 auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
162
163 this->versions.insert(std::make_pair(
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500164 id,
165 std::make_unique<Version>(
166 this->bus,
167 objPath,
168 version,
169 purpose,
170 imageDirPath.string())));
Gunnar Mills3027bba2017-04-27 15:49:03 -0500171
Gunnar Millse91d3212017-04-19 15:42:47 -0500172 return 0;
173}
174
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500175void Manager::erase(std::string entryId)
176{
177 auto it = versions.find(entryId);
178 if (it == versions.end())
179 {
180 return;
181 }
Eddie James6d873712017-09-01 11:29:07 -0500182
183 if (it->second->isFunctional())
184 {
185 log<level::ERR>(("Error: Version " + entryId + \
186 " is currently running on the BMC." \
187 " Unable to remove.").c_str());
188 return;
189 }
190
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500191 // Delete image dir
192 fs::path imageDirPath = (*(it->second)).path();
193 if (fs::exists(imageDirPath))
194 {
195 fs::remove_all(imageDirPath);
196 }
197 this->versions.erase(entryId);
198}
199
Eddie James9440f492017-08-30 11:34:16 -0500200void Manager::removeVersion(sdbusplus::message::message& msg)
201{
202 namespace mesg = sdbusplus::message;
203
204 mesg::object_path objPath;
205
206 msg.read(objPath);
207 std::string path(std::move(objPath));
208
209 // Version id is the last item in the path
210 auto pos = path.rfind("/");
211 if (pos == std::string::npos)
212 {
213 log<level::INFO>("No version id found in object path",
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500214 entry("OBJPATH=%s", path));
Eddie James9440f492017-08-30 11:34:16 -0500215 return;
216 }
217
218 auto versionId = path.substr(pos + 1);
219
220 erase(versionId);
221}
222
Gunnar Mills3027bba2017-04-27 15:49:03 -0500223int Manager::unTar(const std::string& tarFilePath,
224 const std::string& extractDirPath)
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500225{
226 if (tarFilePath.empty())
227 {
228 log<level::ERR>("Error TarFilePath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500229 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500230 return -1;
231 }
232 if (extractDirPath.empty())
233 {
234 log<level::ERR>("Error ExtractDirPath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500235 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500236 return -1;
237 }
238
239 log<level::INFO>("Untaring",
240 entry("FILENAME=%s", tarFilePath),
241 entry("EXTRACTIONDIR=%s", extractDirPath));
242 int status = 0;
243 pid_t pid = fork();
244
245 if (pid == 0)
246 {
247 // child process
248 execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(),
249 "-C", extractDirPath.c_str(), (char*)0);
250 // execl only returns on fail
251 log<level::ERR>("Failed to untar file",
252 entry("FILENAME=%s", tarFilePath));
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500253 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500254 return -1;
255 }
256 else if (pid > 0)
257 {
258 waitpid(pid, &status, 0);
259 }
260 else
261 {
262 log<level::ERR>("fork() failed.");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500263 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500264 return -1;
265 }
266
267 return 0;
268}
Gunnar Mills3027bba2017-04-27 15:49:03 -0500269
Gunnar Millse91d3212017-04-19 15:42:47 -0500270} // namespace manager
271} // namespace software
272} // namepsace phosphor