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