diff --git a/src/blackhole.cc b/src/blackhole.cc index 37f05ec..a60b462 100644 --- a/src/blackhole.cc +++ b/src/blackhole.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,7 +14,7 @@ #include #include -static bool is_writable_dir(const std::string &dir) +static bool is_writable_dir(const std::filesystem::path &dir) { int old_errno = errno; @@ -40,7 +41,7 @@ static bool is_writable_dir(const std::string &dir) return S_ISDIR(st.st_mode); } -static std::string get_tmpdir(void) +static std::filesystem::path get_tmpdir(void) { for (const char *tryenv : {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}) { const char *tmpdir = getenv(tryenv); @@ -62,15 +63,10 @@ static std::string get_tmpdir(void) if (is_writable_dir("/var/tmp")) return "/var/tmp"; - int old_errno = errno; - char *workdir = get_current_dir_name(); - errno = old_errno; - if (workdir != nullptr) { - std::string wdir_str(workdir); - free(workdir); - if (is_writable_dir(wdir_str)) - return wdir_str; - } + std::string workdir = std::filesystem::current_path(); + + if (is_writable_dir(workdir)) + return workdir; LOG(FATAL) << "Unable to get temporary directory."; std::abort(); @@ -91,9 +87,9 @@ BlackHole::BlackHole() : tmpdir(std::nullopt) , filepath(std::nullopt) { - static std::string tempdir = get_tmpdir(); + static std::filesystem::path tempdir = get_tmpdir(); - std::string bh_template = tempdir + "/ip2unix.XXXXXX"; + std::string bh_template = tempdir / "ip2unix.XXXXXX"; char *c_bh_template = strdup(bh_template.c_str()); diff --git a/src/rules/parse.cc b/src/rules/parse.cc index 59c90db..e05d27e 100644 --- a/src/rules/parse.cc +++ b/src/rules/parse.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -323,14 +324,6 @@ static void print_arg_error(size_t rulepos, const std::string &arg, size_t pos, << ' ' << msg << std::endl; } -std::string make_absolute(const std::string &path) -{ - if (path.empty() || path[0] == '/') - return path; - - return std::string(get_current_dir_name()) + '/' + path; -} - std::optional parse_rule_arg(size_t rulepos, const std::string &arg) { std::string buf; @@ -346,7 +339,7 @@ std::optional parse_rule_arg(size_t rulepos, const std::string &arg) if (i == arglen || arg[i] == ',') { /* Handle key=value options. */ if (key.value() == "path") { - rule.socket_path = make_absolute(buf); + rule.socket_path = std::filesystem::absolute(buf); #ifdef SYSTEMD_SUPPORT } else if (key.value() == "systemd") { rule.socket_activation = true;