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) {
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 (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;
} else {
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;
@ -162,14 +165,15 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
if (mpt->is_dir) {
if (mkdir(dstpath, 0711) == -1 && errno != EEXIST) {
PLOG_W("mkdir('%s')", dstpath);
PLOG_W("mkdir(%s)", util::StrQuote(dstpath).c_str());
}
} else {
int fd = TEMP_FAILURE_RETRY(open(dstpath, O_CREAT | O_RDONLY | O_CLOEXEC, 0644));
if (fd >= 0) {
close(fd);
} 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) {
std::string descr;
descr.append(mpt.src.empty() ? "" : "'")
.append(mpt.src.empty() ? "" : mpt.src)
.append(mpt.src.empty() ? "" : "' -> ")
.append("'")
.append(mpt.dst)
.append("' flags:")
descr.append(mpt.src.empty() ? "" : util::StrQuote(mpt.src))
.append(mpt.src.empty() ? "" : " -> ")
.append(util::StrQuote(mpt.dst))
.append(" flags:")
.append(flagsToStr(mpt.flags))
.append(" type:'")
.append(mpt.fs_type)
.append("' options:'")
.append(mpt.options)
.append("'");
.append(" type:")
.append(util::StrQuote(mpt.fs_type))
.append(" options:")
.append(util::StrQuote(mpt.options));
if (mpt.is_dir) {
descr.append(" dir:true");

View File

@ -146,7 +146,7 @@ static const std::string concatArgs(const std::vector<const char*>& argv) {
if (!ret.empty()) {
ret.append(", ");
}
ret.append("'").append(s).append("'");
ret.append(util::StrQuote(s));
}
}
return ret;
@ -203,7 +203,8 @@ static void subprocNewProc(
}
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 */
if (!sandbox::applyPolicy(nsjconf)) {

View File

@ -40,6 +40,7 @@
#include <time.h>
#include <unistd.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
@ -193,6 +194,12 @@ std::string StrPrintf(const char* format, ...) {
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) {
for (size_t i = 0; s[i]; s++) {
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, ...)
__attribute__((format(printf, 2, 3)));
std::string StrPrintf(const char* format, ...) __attribute__((format(printf, 1, 2)));
const std::string StrQuote(const std::string& str);
bool isANumber(const char* s);
uint64_t rnd64(void);
const std::string sigName(int signo);