1 #include "config.h" 2 3 #include "download_manager.hpp" 4 5 #include "xyz/openbmc_project/Common/error.hpp" 6 7 #include <sys/wait.h> 8 #include <unistd.h> 9 10 #include <phosphor-logging/elog-errors.hpp> 11 #include <phosphor-logging/elog.hpp> 12 #include <phosphor-logging/lg2.hpp> 13 14 #include <algorithm> 15 #include <filesystem> 16 #include <iostream> 17 #include <string> 18 #include <system_error> 19 20 namespace phosphor 21 { 22 namespace software 23 { 24 namespace manager 25 { 26 27 using namespace sdbusplus::error::xyz::openbmc_project::common; 28 PHOSPHOR_LOG2_USING; 29 using namespace phosphor::logging; 30 namespace fs = std::filesystem; 31 32 void Download::downloadViaTFTP(std::string fileName, std::string serverAddress) 33 { 34 using Argument = xyz::openbmc_project::common::InvalidArgument; 35 36 // Sanitize the fileName string 37 if (!fileName.empty()) 38 { 39 fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'), 40 fileName.end()); 41 fileName = fileName.substr(fileName.find_first_not_of('.')); 42 } 43 44 if (fileName.empty()) 45 { 46 error("Filename is empty"); 47 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"), 48 Argument::ARGUMENT_VALUE(fileName.c_str())); 49 return; 50 } 51 52 if (serverAddress.empty()) 53 { 54 error("ServerAddress is empty"); 55 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"), 56 Argument::ARGUMENT_VALUE(serverAddress.c_str())); 57 return; 58 } 59 60 info("Downloading {PATH} via TFTP: {SERVERADDRESS}", "PATH", fileName, 61 "SERVERADDRESS", serverAddress); 62 63 // Check if IMAGE DIR exists 64 fs::path imgDirPath(IMG_UPLOAD_DIR); 65 std::error_code ec; 66 if (!fs::is_directory(imgDirPath, ec)) 67 { 68 error("Image Dir {PATH} does not exist: {ERROR_MSG}", "PATH", 69 imgDirPath, "ERROR_MSG", ec.message()); 70 elog<InternalFailure>(); 71 return; 72 } 73 74 pid_t pid = fork(); 75 76 if (pid == 0) 77 { 78 pid_t nextPid = fork(); 79 if (nextPid == 0) 80 { 81 // child process 82 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(), 83 serverAddress.c_str(), "-l", 84 (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(), 85 (char*)0); 86 // execl only returns on fail 87 error("Error ({ERRNO}) occurred during the TFTP call", "ERRNO", 88 errno); 89 elog<InternalFailure>(); 90 } 91 else if (nextPid < 0) 92 { 93 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); 94 elog<InternalFailure>(); 95 } 96 // do nothing as parent if all is going well 97 // when parent exits, child will be reparented under init 98 // and then be reaped properly 99 exit(0); 100 } 101 else if (pid < 0) 102 { 103 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); 104 elog<InternalFailure>(); 105 } 106 else 107 { 108 int status; 109 if (waitpid(pid, &status, 0) < 0) 110 { 111 error("Error ({ERRNO}) occurred during waitpid", "ERRNO", errno); 112 } 113 else if (WEXITSTATUS(status) != 0) 114 { 115 error("Failed ({STATUS}) to launch tftp", "STATUS", status); 116 } 117 } 118 119 return; 120 } 121 122 } // namespace manager 123 } // namespace software 124 } // namespace phosphor 125