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 | { |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 70 | pid_t nextPid = fork(); |
| 71 | if (nextPid == 0) |
| 72 | { |
| 73 | // child process |
| 74 | execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), |
| 75 | serverAddress.c_str(), "-l", |
| 76 | (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(), |
| 77 | (char*)0); |
| 78 | // execl only returns on fail |
| 79 | log<level::ERR>("Error occurred during the TFTP call"); |
| 80 | elog<InternalFailure>(); |
| 81 | } |
| 82 | else if (nextPid < 0) |
| 83 | { |
| 84 | log<level::ERR>("Error occurred during fork"); |
| 85 | elog<InternalFailure>(); |
| 86 | } |
| 87 | // do nothing as parent if all is going well |
| 88 | // when parent exits, child will be reparented under init |
| 89 | // and then be reaped properly |
| 90 | exit(0); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 91 | } |
| 92 | else if (pid < 0) |
| 93 | { |
| 94 | log<level::ERR>("Error occurred during fork"); |
Gunnar Mills | a733df5 | 2017-04-11 13:05:52 -0500 | [diff] [blame] | 95 | elog<InternalFailure>(); |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 96 | } |
Vernon Mauery | b891eb3 | 2018-05-10 16:06:23 -0700 | [diff] [blame] | 97 | else |
| 98 | { |
| 99 | int status; |
| 100 | if (waitpid(pid, &status, 0) < 0) |
| 101 | { |
| 102 | log<level::ERR>("waitpid error"); |
| 103 | } |
| 104 | else if (WEXITSTATUS(status) != 0) |
| 105 | { |
| 106 | log<level::ERR>("failed to launch tftp"); |
| 107 | } |
| 108 | } |
Gunnar Mills | a927de4 | 2017-04-06 21:40:50 -0500 | [diff] [blame] | 109 | |
Gunnar Mills | 701e021 | 2017-04-03 11:21:27 -0500 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | } // namespace manager |
| 114 | } // namespace software |
| 115 | } // namespace phosphor |