mnt: quote paths in log messages

This commit is contained in:
Robert Swiecki 2022-08-09 12:05:33 +02:00
parent e98dc415fc
commit f628f74b00
4 changed files with 28 additions and 18 deletions

33
mnt.cc
View File

@ -147,14 +147,17 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
} }
if (mpt->is_symlink) { if (mpt->is_symlink) {
LOG_D("symlink('%s', '%s')", srcpath, dstpath); LOG_D("symlink(%s, %s)", util::StrQuote(srcpath).c_str(),
util::StrQuote(dstpath).c_str());
if (symlink(srcpath, dstpath) == -1) { if (symlink(srcpath, dstpath) == -1) {
if (mpt->is_mandatory) { if (mpt->is_mandatory) {
PLOG_E("symlink('%s', '%s')", srcpath, dstpath); PLOG_E("symlink('%s', '%s')", util::StrQuote(srcpath).c_str(),
util::StrQuote(dstpath).c_str());
return false; return false;
} else { } else {
PLOG_W("symlink('%s', '%s'), but it's not mandatory, continuing", PLOG_W("symlink('%s', '%s'), but it's not mandatory, continuing",
srcpath, dstpath); util::StrQuote(srcpath).c_str(),
util::StrQuote(dstpath).c_str());
} }
} }
return true; return true;
@ -162,14 +165,15 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
if (mpt->is_dir) { if (mpt->is_dir) {
if (mkdir(dstpath, 0711) == -1 && errno != EEXIST) { if (mkdir(dstpath, 0711) == -1 && errno != EEXIST) {
PLOG_W("mkdir('%s')", dstpath); PLOG_W("mkdir(%s)", util::StrQuote(dstpath).c_str());
} }
} else { } else {
int fd = TEMP_FAILURE_RETRY(open(dstpath, O_CREAT | O_RDONLY | O_CLOEXEC, 0644)); int fd = TEMP_FAILURE_RETRY(open(dstpath, O_CREAT | O_RDONLY | O_CLOEXEC, 0644));
if (fd >= 0) { if (fd >= 0) {
close(fd); close(fd);
} else { } else {
PLOG_W("open('%s', O_CREAT|O_RDONLY|O_CLOEXEC, 0644)", dstpath); PLOG_W("open(%s, O_CREAT|O_RDONLY|O_CLOEXEC, 0644)",
util::StrQuote(dstpath).c_str());
} }
} }
@ -605,18 +609,15 @@ bool addMountPtTail(nsjconf_t* nsjconf, const std::string& src, const std::strin
const std::string describeMountPt(const mount_t& mpt) { const std::string describeMountPt(const mount_t& mpt) {
std::string descr; std::string descr;
descr.append(mpt.src.empty() ? "" : "'") descr.append(mpt.src.empty() ? "" : util::StrQuote(mpt.src))
.append(mpt.src.empty() ? "" : mpt.src) .append(mpt.src.empty() ? "" : " -> ")
.append(mpt.src.empty() ? "" : "' -> ") .append(util::StrQuote(mpt.dst))
.append("'") .append(" flags:")
.append(mpt.dst)
.append("' flags:")
.append(flagsToStr(mpt.flags)) .append(flagsToStr(mpt.flags))
.append(" type:'") .append(" type:")
.append(mpt.fs_type) .append(util::StrQuote(mpt.fs_type))
.append("' options:'") .append(" options:")
.append(mpt.options) .append(util::StrQuote(mpt.options));
.append("'");
if (mpt.is_dir) { if (mpt.is_dir) {
descr.append(" dir:true"); descr.append(" dir:true");

View File

@ -146,7 +146,7 @@ static const std::string concatArgs(const std::vector<const char*>& argv) {
if (!ret.empty()) { if (!ret.empty()) {
ret.append(", "); ret.append(", ");
} }
ret.append("'").append(s).append("'"); ret.append(util::StrQuote(s));
} }
} }
return ret; return ret;
@ -203,7 +203,8 @@ static void subprocNewProc(
} }
argv.push_back(nullptr); argv.push_back(nullptr);
LOG_D("Exec: '%s', Args: [%s]", nsjconf->exec_file.c_str(), concatArgs(argv).c_str()); LOG_D("Exec: %s, Args: [%s]", util::StrQuote(nsjconf->exec_file).c_str(),
concatArgs(argv).c_str());
/* Should be the last one in the sequence */ /* Should be the last one in the sequence */
if (!sandbox::applyPolicy(nsjconf)) { if (!sandbox::applyPolicy(nsjconf)) {

View File

@ -40,6 +40,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <iomanip>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -193,6 +194,12 @@ std::string StrPrintf(const char* format, ...) {
return str; return str;
} }
const std::string StrQuote(const std::string& str) {
std::ostringstream ss;
ss << std::quoted(str, '\'');
return ss.str();
}
bool isANumber(const char* s) { bool isANumber(const char* s) {
for (size_t i = 0; s[i]; s++) { for (size_t i = 0; s[i]; s++) {
if (!isdigit(s[i]) && s[i] != 'x') { if (!isdigit(s[i]) && s[i] != 'x') {

1
util.h
View File

@ -49,6 +49,7 @@ bool createDirRecursively(const char* dir);
std::string* StrAppend(std::string* str, const char* format, ...) std::string* StrAppend(std::string* str, const char* format, ...)
__attribute__((format(printf, 2, 3))); __attribute__((format(printf, 2, 3)));
std::string StrPrintf(const char* format, ...) __attribute__((format(printf, 1, 2))); std::string StrPrintf(const char* format, ...) __attribute__((format(printf, 1, 2)));
const std::string StrQuote(const std::string& str);
bool isANumber(const char* s); bool isANumber(const char* s);
uint64_t rnd64(void); uint64_t rnd64(void);
const std::string sigName(int signo); const std::string sigName(int signo);