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 | |
| 10 | #include <algorithm> |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 11 | #include <filesystem> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 12 | #include <iostream> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | #include <phosphor-logging/elog.hpp> |
| 15 | #include <phosphor-logging/log.hpp> |
| 16 | #include <string> |
| 17 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace software |
| 21 | { |
| 22 | namespace manager |
| 23 | { |
| 24 | |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 27 | namespace fs = std::filesystem; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 28 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 29 | void Download::downloadViaTFTP(std::string fileName, std::string serverAddress) |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 30 | { |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 31 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 32 | |
| 33 | // Sanitize the fileName string |
Brad Bishop | 8a24dda | 2017-08-31 11:25:20 -0400 | [diff] [blame] | 34 | if (!fileName.empty()) |
| 35 | { |
| 36 | fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'), |
| 37 | fileName.end()); |
| 38 | fileName = fileName.substr(fileName.find_first_not_of('.')); |
| 39 | } |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 40 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 41 | if (fileName.empty()) |
| 42 | { |
| 43 | log<level::ERR>("Error FileName is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 44 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"), |
| 45 | Argument::ARGUMENT_VALUE(fileName.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (serverAddress.empty()) |
| 50 | { |
| 51 | log<level::ERR>("Error ServerAddress is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 52 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"), |
| 53 | Argument::ARGUMENT_VALUE(serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
| 57 | log<level::INFO>("Downloading via TFTP", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 58 | entry("FILENAME=%s", fileName.c_str()), |
| 59 | entry("SERVERADDRESS=%s", serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 60 | |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 61 | // Check if IMAGE DIR exists |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 62 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
| 63 | if (!fs::is_directory(imgDirPath)) |
| 64 | { |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 65 | log<level::ERR>("Error Image Dir does not exist"); |
| 66 | elog<InternalFailure>(); |
| 67 | return; |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 70 | pid_t pid = fork(); |
| 71 | |
| 72 | if (pid == 0) |
| 73 | { |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 74 | pid_t nextPid = fork(); |
| 75 | if (nextPid == 0) |
| 76 | { |
| 77 | // child process |
| 78 | execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), |
| 79 | serverAddress.c_str(), "-l", |
| 80 | (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(), |
| 81 | (char*)0); |
| 82 | // execl only returns on fail |
| 83 | log<level::ERR>("Error occurred during the TFTP call"); |
| 84 | elog<InternalFailure>(); |
| 85 | } |
| 86 | else if (nextPid < 0) |
| 87 | { |
| 88 | log<level::ERR>("Error occurred during fork"); |
| 89 | elog<InternalFailure>(); |
| 90 | } |
| 91 | // do nothing as parent if all is going well |
| 92 | // when parent exits, child will be reparented under init |
| 93 | // and then be reaped properly |
| 94 | exit(0); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 95 | } |
| 96 | else if (pid < 0) |
| 97 | { |
| 98 | log<level::ERR>("Error occurred during fork"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 99 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 100 | } |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 101 | else |
| 102 | { |
| 103 | int status; |
| 104 | if (waitpid(pid, &status, 0) < 0) |
| 105 | { |
| 106 | log<level::ERR>("waitpid error"); |
| 107 | } |
| 108 | else if (WEXITSTATUS(status) != 0) |
| 109 | { |
| 110 | log<level::ERR>("failed to launch tftp"); |
| 111 | } |
| 112 | } |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 113 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | } // namespace manager |
| 118 | } // namespace software |
| 119 | } // namespace phosphor |