blob: 71a52551aca78754d1b8ceaa939eb0221051cc02 [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>
George Liu44b9fef2023-02-07 14:31:32 +080018#include <system_error>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050019
Gunnar Mills701e0212017-04-03 11:21:27 -050020namespace phosphor
21{
22namespace software
23{
24namespace manager
25{
26
Adriana Kobylakce82de52024-01-16 13:56:38 -060027using namespace sdbusplus::error::xyz::openbmc_project::common;
Patrick Williamsc9bb6422021-08-27 06:18:35 -050028PHOSPHOR_LOG2_USING;
Gunnar Millsa927de42017-04-06 21:40:50 -050029using namespace phosphor::logging;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050030namespace fs = std::filesystem;
Gunnar Millsa927de42017-04-06 21:40:50 -050031
Adriana Kobylak2285fe02018-02-27 15:36:59 -060032void Download::downloadViaTFTP(std::string fileName, std::string serverAddress)
Gunnar Mills701e0212017-04-03 11:21:27 -050033{
Adriana Kobylakce82de52024-01-16 13:56:38 -060034 using Argument = xyz::openbmc_project::common::InvalidArgument;
Gunnar Mills49739432017-04-21 11:03:34 -050035
36 // Sanitize the fileName string
Brad Bishop8a24dda2017-08-31 11:25:20 -040037 if (!fileName.empty())
38 {
39 fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'),
40 fileName.end());
41 fileName = fileName.substr(fileName.find_first_not_of('.'));
42 }
Gunnar Mills49739432017-04-21 11:03:34 -050043
Gunnar Millsa927de42017-04-06 21:40:50 -050044 if (fileName.empty())
45 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050046 error("Filename is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050047 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"),
48 Argument::ARGUMENT_VALUE(fileName.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050049 return;
50 }
51
52 if (serverAddress.empty())
53 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050054 error("ServerAddress is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050055 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"),
56 Argument::ARGUMENT_VALUE(serverAddress.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050057 return;
58 }
59
Patrick Williamsc9bb6422021-08-27 06:18:35 -050060 info("Downloading {PATH} via TFTP: {SERVERADDRESS}", "PATH", fileName,
61 "SERVERADDRESS", serverAddress);
Gunnar Millsa927de42017-04-06 21:40:50 -050062
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060063 // Check if IMAGE DIR exists
Gunnar Mills3a482f62017-04-20 16:07:20 -050064 fs::path imgDirPath(IMG_UPLOAD_DIR);
George Liu44b9fef2023-02-07 14:31:32 +080065 std::error_code ec;
66 if (!fs::is_directory(imgDirPath, ec))
Gunnar Mills3a482f62017-04-20 16:07:20 -050067 {
George Liu44b9fef2023-02-07 14:31:32 +080068 error("Image Dir {PATH} does not exist: {ERROR_MSG}", "PATH",
69 imgDirPath, "ERROR_MSG", ec.message());
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060070 elog<InternalFailure>();
71 return;
Gunnar Mills3a482f62017-04-20 16:07:20 -050072 }
73
Gunnar Millsa927de42017-04-06 21:40:50 -050074 pid_t pid = fork();
75
76 if (pid == 0)
77 {
Vernon Maueryb891eb32018-05-10 16:06:23 -070078 pid_t nextPid = fork();
79 if (nextPid == 0)
80 {
81 // child process
82 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
83 serverAddress.c_str(), "-l",
84 (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(),
85 (char*)0);
86 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050087 error("Error ({ERRNO}) occurred during the TFTP call", "ERRNO",
88 errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -070089 elog<InternalFailure>();
90 }
91 else if (nextPid < 0)
92 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050093 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -070094 elog<InternalFailure>();
95 }
96 // do nothing as parent if all is going well
97 // when parent exits, child will be reparented under init
98 // and then be reaped properly
99 exit(0);
Gunnar Millsa927de42017-04-06 21:40:50 -0500100 }
101 else if (pid < 0)
102 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500103 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Gunnar Millsa733df52017-04-11 13:05:52 -0500104 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -0500105 }
Vernon Maueryb891eb32018-05-10 16:06:23 -0700106 else
107 {
108 int status;
109 if (waitpid(pid, &status, 0) < 0)
110 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500111 error("Error ({ERRNO}) occurred during waitpid", "ERRNO", errno);
Vernon Maueryb891eb32018-05-10 16:06:23 -0700112 }
113 else if (WEXITSTATUS(status) != 0)
114 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500115 error("Failed ({STATUS}) to launch tftp", "STATUS", status);
Vernon Maueryb891eb32018-05-10 16:06:23 -0700116 }
117 }
Gunnar Millsa927de42017-04-06 21:40:50 -0500118
Gunnar Mills701e0212017-04-03 11:21:27 -0500119 return;
120}
121
122} // namespace manager
123} // namespace software
124} // namespace phosphor