Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <unistd.h> |
| 4 | #include <sys/wait.h> |
| 5 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 6 | #include <experimental/filesystem> |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 7 | #include <algorithm> |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 8 | #include "config.h" |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/elog.hpp> |
| 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include "xyz/openbmc_project/Common/error.hpp" |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 12 | #include "download_manager.hpp" |
| 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace software |
| 17 | { |
| 18 | namespace manager |
| 19 | { |
| 20 | |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 22 | using namespace phosphor::logging; |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 23 | namespace fs = std::experimental::filesystem; |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 24 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 25 | void Download::downloadViaTFTP(std::string fileName, std::string serverAddress) |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 26 | { |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 27 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 28 | |
| 29 | // Sanitize the fileName string |
Brad Bishop | 8a24dda | 2017-08-31 11:25:20 -0400 | [diff] [blame] | 30 | if (!fileName.empty()) |
| 31 | { |
| 32 | fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'), |
| 33 | fileName.end()); |
| 34 | fileName = fileName.substr(fileName.find_first_not_of('.')); |
| 35 | } |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 36 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 37 | if (fileName.empty()) |
| 38 | { |
| 39 | log<level::ERR>("Error FileName is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 40 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"), |
| 41 | Argument::ARGUMENT_VALUE(fileName.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (serverAddress.empty()) |
| 46 | { |
| 47 | log<level::ERR>("Error ServerAddress is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 48 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"), |
| 49 | Argument::ARGUMENT_VALUE(serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | log<level::INFO>("Downloading via TFTP", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 54 | entry("FILENAME=%s", fileName.c_str()), |
| 55 | entry("SERVERADDRESS=%s", serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 56 | |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 57 | // Check if IMAGE DIR exists |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 58 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
| 59 | if (!fs::is_directory(imgDirPath)) |
| 60 | { |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 61 | log<level::ERR>("Error Image Dir does not exist"); |
| 62 | elog<InternalFailure>(); |
| 63 | return; |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 66 | pid_t pid = fork(); |
| 67 | |
| 68 | if (pid == 0) |
| 69 | { |
| 70 | // child process |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 71 | execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 72 | serverAddress.c_str(), "-l", |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 73 | (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(), (char*)0); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 74 | // execl only returns on fail |
| 75 | log<level::ERR>("Error occurred during the TFTP call"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 76 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 77 | } |
| 78 | else if (pid < 0) |
| 79 | { |
| 80 | log<level::ERR>("Error occurred during fork"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 81 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | } // namespace manager |
| 88 | } // namespace software |
| 89 | } // namespace phosphor |