Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 2 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 3 | #include "download_manager.hpp" |
| 4 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
| 7 | #include <sys/wait.h> |
| 8 | #include <unistd.h> |
| 9 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include <phosphor-logging/elog.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
| 15 | #include <filesystem> |
| 16 | #include <iostream> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 17 | #include <string> |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 18 | #include <system_error> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 19 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace software |
| 23 | { |
| 24 | namespace manager |
| 25 | { |
| 26 | |
Adriana Kobylak | ce82de5 | 2024-01-16 13:56:38 -0600 | [diff] [blame] | 27 | using namespace sdbusplus::error::xyz::openbmc_project::common; |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 28 | PHOSPHOR_LOG2_USING; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 30 | namespace fs = std::filesystem; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 31 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 32 | void Download::downloadViaTFTP(std::string fileName, std::string serverAddress) |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 33 | { |
Adriana Kobylak | ce82de5 | 2024-01-16 13:56:38 -0600 | [diff] [blame] | 34 | using Argument = xyz::openbmc_project::common::InvalidArgument; |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 35 | |
| 36 | // Sanitize the fileName string |
Brad Bishop | 8a24dda | 2017-08-31 11:25:20 -0400 | [diff] [blame] | 37 | 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 Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 43 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 44 | if (fileName.empty()) |
| 45 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 46 | error("Filename is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 47 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"), |
| 48 | Argument::ARGUMENT_VALUE(fileName.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (serverAddress.empty()) |
| 53 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 54 | error("ServerAddress is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 55 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"), |
| 56 | Argument::ARGUMENT_VALUE(serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 60 | info("Downloading {PATH} via TFTP: {SERVERADDRESS}", "PATH", fileName, |
| 61 | "SERVERADDRESS", serverAddress); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 62 | |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 63 | // Check if IMAGE DIR exists |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 64 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 65 | std::error_code ec; |
| 66 | if (!fs::is_directory(imgDirPath, ec)) |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 67 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 68 | error("Image Dir {PATH} does not exist: {ERROR_MSG}", "PATH", |
| 69 | imgDirPath, "ERROR_MSG", ec.message()); |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 70 | elog<InternalFailure>(); |
| 71 | return; |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 74 | pid_t pid = fork(); |
| 75 | |
| 76 | if (pid == 0) |
| 77 | { |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 78 | 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 Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 87 | error("Error ({ERRNO}) occurred during the TFTP call", "ERRNO", |
| 88 | errno); |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 89 | elog<InternalFailure>(); |
| 90 | } |
| 91 | else if (nextPid < 0) |
| 92 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 93 | error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 94 | 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 Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 100 | } |
| 101 | else if (pid < 0) |
| 102 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 103 | error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 104 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 105 | } |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 106 | else |
| 107 | { |
| 108 | int status; |
| 109 | if (waitpid(pid, &status, 0) < 0) |
| 110 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 111 | error("Error ({ERRNO}) occurred during waitpid", "ERRNO", errno); |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 112 | } |
| 113 | else if (WEXITSTATUS(status) != 0) |
| 114 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 115 | error("Failed ({STATUS}) to launch tftp", "STATUS", status); |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 118 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | } // namespace manager |
| 123 | } // namespace software |
| 124 | } // namespace phosphor |