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 | |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 25 | void Download::downloadViaTFTP(std::string fileName, |
| 26 | std::string serverAddress) |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 27 | { |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 28 | |
| 29 | // Sanitize the fileName string |
| 30 | fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'), |
| 31 | fileName.end()); |
| 32 | fileName = fileName.substr(fileName.find_first_not_of('.')); |
| 33 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 34 | if (fileName.empty()) |
| 35 | { |
| 36 | log<level::ERR>("Error FileName is empty"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 37 | elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument:: |
| 38 | ARGUMENT_NAME("FileName"), |
| 39 | xyz::openbmc_project::Common::InvalidArgument:: |
| 40 | ARGUMENT_VALUE(fileName.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (serverAddress.empty()) |
| 45 | { |
| 46 | log<level::ERR>("Error ServerAddress is empty"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 47 | elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument:: |
| 48 | ARGUMENT_NAME("ServerAddress"), |
| 49 | xyz::openbmc_project::Common::InvalidArgument:: |
| 50 | ARGUMENT_VALUE(serverAddress.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | log<level::INFO>("Downloading via TFTP", |
| 55 | entry("FILENAME=%s", fileName), |
| 56 | entry("SERVERADDRESS=%s", serverAddress)); |
| 57 | |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 58 | // Check if IMAGE DIR exists and create if necessary. |
| 59 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
| 60 | if (!fs::is_directory(imgDirPath)) |
| 61 | { |
| 62 | fs::create_directory(imgDirPath); |
| 63 | } |
| 64 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 65 | pid_t pid = fork(); |
| 66 | |
| 67 | if (pid == 0) |
| 68 | { |
| 69 | // child process |
| 70 | execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), |
| 71 | serverAddress.c_str(), "-l", (std::string{IMG_UPLOAD_DIR} + '/' + |
| 72 | fileName).c_str(), (char*)0); |
| 73 | // execl only returns on fail |
| 74 | log<level::ERR>("Error occurred during the TFTP call"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 75 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 76 | } |
| 77 | else if (pid < 0) |
| 78 | { |
| 79 | log<level::ERR>("Error occurred during fork"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 80 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | } // namespace manager |
| 87 | } // namespace software |
| 88 | } // namespace phosphor |
| 89 | |