blob: 972278b661d07d661b311e6db89b001923a1d118 [file] [log] [blame]
Gunnar Millsa927de42017-04-06 21:40:50 -05001#include "config.h"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05002
Gunnar Mills701e0212017-04-03 11:21:27 -05003#include "download_manager.hpp"
4
Gunnar Millsb0ce9962018-09-07 13:39:10 -05005#include "xyz/openbmc_project/Common/error.hpp"
6
7#include <sys/wait.h>
8#include <unistd.h>
9
Gunnar Millsb0ce9962018-09-07 13:39:10 -050010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/elog.hpp>
Patrick Williamsc9bb6422021-08-27 06:18:35 -050012#include <phosphor-logging/lg2.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013
14#include <algorithm>
15#include <filesystem>
16#include <iostream>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050017#include <string>
18
Gunnar Mills701e0212017-04-03 11:21:27 -050019namespace phosphor
20{
21namespace software
22{
23namespace manager
24{
25
Gunnar Millsa733df52017-04-11 13:05:52 -050026using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Williamsc9bb6422021-08-27 06:18:35 -050027PHOSPHOR_LOG2_USING;
Gunnar Millsa927de42017-04-06 21:40:50 -050028using namespace phosphor::logging;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050029namespace fs = std::filesystem;
Gunnar Millsa927de42017-04-06 21:40:50 -050030
Adriana Kobylak2285fe02018-02-27 15:36:59 -060031void Download::downloadViaTFTP(std::string fileName, std::string serverAddress)
Gunnar Mills701e0212017-04-03 11:21:27 -050032{
Gunnar Millsc7e2d242017-08-31 14:50:17 -050033 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Gunnar Mills49739432017-04-21 11:03:34 -050034
35 // Sanitize the fileName string
Brad Bishop8a24dda2017-08-31 11:25:20 -040036 if (!fileName.empty())
37 {
38 fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'),
39 fileName.end());
40 fileName = fileName.substr(fileName.find_first_not_of('.'));
41 }
Gunnar Mills49739432017-04-21 11:03:34 -050042
Gunnar Millsa927de42017-04-06 21:40:50 -050043 if (fileName.empty())
44 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050045 error("Filename is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050046 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"),
47 Argument::ARGUMENT_VALUE(fileName.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050048 return;
49 }
50
51 if (serverAddress.empty())
52 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050053 error("ServerAddress is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050054 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"),
55 Argument::ARGUMENT_VALUE(serverAddress.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050056 return;
57 }
58
Patrick Williamsc9bb6422021-08-27 06:18:35 -050059 info("Downloading {PATH} via TFTP: {SERVERADDRESS}", "PATH", fileName,
60 "SERVERADDRESS", serverAddress);
Gunnar Millsa927de42017-04-06 21:40:50 -050061
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060062 // Check if IMAGE DIR exists
Gunnar Mills3a482f62017-04-20 16:07:20 -050063 fs::path imgDirPath(IMG_UPLOAD_DIR);
64 if (!fs::is_directory(imgDirPath))
65 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050066 error("Image Dir {PATH} does not exist", "PATH", imgDirPath);
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060067 elog<InternalFailure>();
68 return;
Gunnar Mills3a482f62017-04-20 16:07:20 -050069 }
70
Gunnar Millsa927de42017-04-06 21:40:50 -050071 pid_t pid = fork();
72
73 if (pid == 0)
74 {
Vernon Maueryb891eb32018-05-10 16:06:23 -070075 pid_t nextPid = fork();
76 if (nextPid == 0)
77 {
78 // child process
79 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
80 serverAddress.c_str(), "-l",
81 (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(),
82 (char*)0);
83 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050084 error("Error ({ERRNO}) occurred during the TFTP call", "ERRNO",
85 errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -070086 elog<InternalFailure>();
87 }
88 else if (nextPid < 0)
89 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050090 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -070091 elog<InternalFailure>();
92 }
93 // do nothing as parent if all is going well
94 // when parent exits, child will be reparented under init
95 // and then be reaped properly
96 exit(0);
Gunnar Millsa927de42017-04-06 21:40:50 -050097 }
98 else if (pid < 0)
99 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500100 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Gunnar Millsa733df52017-04-11 13:05:52 -0500101 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -0500102 }
Vernon Maueryb891eb32018-05-10 16:06:23 -0700103 else
104 {
105 int status;
106 if (waitpid(pid, &status, 0) < 0)
107 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500108 error("Error ({ERRNO}) occurred during waitpid", "ERRNO", errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -0700109 }
110 else if (WEXITSTATUS(status) != 0)
111 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500112
113 error("Failed ({STATUS}) to launch tftp", "STATUS", status);
Vernon Maueryb891eb32018-05-10 16:06:23 -0700114 }
115 }
Gunnar Millsa927de42017-04-06 21:40:50 -0500116
Gunnar Mills701e0212017-04-03 11:21:27 -0500117 return;
118}
119
120} // namespace manager
121} // namespace software
122} // namespace phosphor