diff --git a/agent-system/src/SystemAgent.cc b/agent-system/src/SystemAgent.cc index 75ab41389..a8cf1331f 100644 --- a/agent-system/src/SystemAgent.cc +++ b/agent-system/src/SystemAgent.cc @@ -109,23 +109,48 @@ remove_directory (const string& path, int depth) */ SystemAgent::SystemAgent () { - // #237481: problems with pids with too many digits: 64bits: max 20 digits - char tmp1[19+20]; - snprintf (tmp1, sizeof(tmp1), "/tmp/YaST2-%05d-XXXXXX", getpid ()); +} + +/** + * @brief Temporary directory path + * + * Returns the temporary directory path, it creates the directory at the first + * call. The directory is automatically deleted in the destructor. + * + * @return string The returned path includes the current root (chroot) prefix. + */ +string SystemAgent::tempdir () +{ + if (!_tempdir.empty()) { + return _tempdir; + } - const char* tmp2 = mkdtemp (tmp1); + // build the directory pattern: /<"tmp" if present>/YaST2--XXXXXX + string dir_pattern(root()); + + // check if the "tmp" directory is present, if yes then use it + const char *tmp = "/tmp"; + struct stat buf; + if (lstat((dir_pattern + tmp).c_str(), &buf) == 0 && S_ISDIR (buf.st_mode)) + { + dir_pattern += tmp; + } + + dir_pattern += "/YaST2-" + to_string(getpid()) + "-XXXXXX"; + + const char* tmp2 = mkdtemp ((char *)dir_pattern.c_str()); if (!tmp2) { - std::ostringstream stm; - stm << "Cannot create temporary directory " << tmp1 << ':' - << strerror (errno); - y2error ("%s", stm.str().c_str()); - // #343258: terminate will print the uncaught exception too, unlike plain exit - throw std::runtime_error (stm.str()); + string error = "Cannot create temporary directory " + dir_pattern + ':' + strerror(errno); + y2error ("%s", error.c_str()); + // #343258: terminate will print the uncaught exception too, unlike plain exit + throw std::runtime_error (error); } - tempdir = tmp2; - y2debug ("tmp directory is %s", tempdir.c_str ()); + _tempdir = tmp2; + y2debug ("tmp directory is %s", _tempdir.c_str ()); + + return _tempdir; } @@ -135,7 +160,9 @@ SystemAgent::SystemAgent () SystemAgent::~SystemAgent () { // remove temp directory and all its subdirectories. - remove_directory (tempdir.c_str(), 20); + if (!_tempdir.empty()) { + remove_directory (_tempdir.c_str(), 20); + } } @@ -332,7 +359,8 @@ SystemAgent::Read (const YCPPath& path, const YCPValue& arg, const YCPValue&) * @example Read (.target.tmpdir) -> "/some/temp/dir" */ - return YCPString (tempdir); + // skip the chroot prefix, return the location relative to the current root + return YCPString (tempdir().substr(strlen(root()))); } if (arg.isNull()) @@ -1022,7 +1050,7 @@ SystemAgent::Execute (const YCPPath& path, const YCPValue& value, } else if (cmd == "bash_output") { - return shellcommand_output (root(), exports + bashcommand, tempdir); + return shellcommand_output (root(), exports + bashcommand, tempdir()); } else if (cmd == "bash_background") { diff --git a/agent-system/src/SystemAgent.h b/agent-system/src/SystemAgent.h index 674d394d9..b7eb14a4a 100644 --- a/agent-system/src/SystemAgent.h +++ b/agent-system/src/SystemAgent.h @@ -53,8 +53,9 @@ class SystemAgent : public SCRAgent private: - string tempdir; - + // temporary directory, an absolute path with the current root (chroot) prefix! + string _tempdir; + string tempdir(); }; diff --git a/package/yast2-core.changes b/package/yast2-core.changes index c05d8a2fb..0108a4113 100644 --- a/package/yast2-core.changes +++ b/package/yast2-core.changes @@ -1,3 +1,12 @@ +------------------------------------------------------------------- +Mon Jul 25 14:19:37 UTC 2022 - Ladislav Slezák + +- Fixed ".target.tmpdir": + - Create the temporary directory in the target root (bsc#1199840) + - The temporary directory is now created only when needed + - Check if the target /tmp directory exists +- 4.5.3 + ------------------------------------------------------------------- Mon Jul 4 13:46:48 UTC 2022 - Martin Liška diff --git a/package/yast2-core.spec b/package/yast2-core.spec index 33dcb9188..8dfa69009 100644 --- a/package/yast2-core.spec +++ b/package/yast2-core.spec @@ -26,7 +26,7 @@ %bcond_with werror Name: yast2-core -Version: 4.5.2 +Version: 4.5.3 Release: 0 Url: https://github.com/yast/yast-core