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