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 | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 28 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 29 | |
| 30 | // Sanitize the fileName string |
Brad Bishop | 8a24dda | 2017-08-31 11:25:20 -0400 | [diff] [blame] | 31 | if (!fileName.empty()) |
| 32 | { |
| 33 | fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'), |
| 34 | fileName.end()); |
| 35 | fileName = fileName.substr(fileName.find_first_not_of('.')); |
| 36 | } |
Gunnar Mills | 4973943 | 2017-04-21 11:03:34 -0500 | [diff] [blame] | 37 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 38 | if (fileName.empty()) |
| 39 | { |
| 40 | log<level::ERR>("Error FileName is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 41 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"), |
| 42 | Argument::ARGUMENT_VALUE(fileName.c_str())); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
| 46 | if (serverAddress.empty()) |
| 47 | { |
| 48 | log<level::ERR>("Error ServerAddress is empty"); |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 49 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"), |
| 50 | Argument::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 | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame^] | 58 | // Check if IMAGE DIR exists |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 59 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
| 60 | if (!fs::is_directory(imgDirPath)) |
| 61 | { |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame^] | 62 | log<level::ERR>("Error Image Dir does not exist"); |
| 63 | elog<InternalFailure>(); |
| 64 | return; |
Gunnar Mills | 3a482f6 | 2017-04-20 16:07:20 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 67 | pid_t pid = fork(); |
| 68 | |
| 69 | if (pid == 0) |
| 70 | { |
| 71 | // child process |
| 72 | execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), |
Gunnar Mills | c7e2d24 | 2017-08-31 14:50:17 -0500 | [diff] [blame] | 73 | serverAddress.c_str(), "-l", |
| 74 | (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(), |
| 75 | (char*)0); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 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 | |