clang-format-21 + make indent

This commit is contained in:
Robert Swiecki
2025-11-23 00:03:41 +01:00
parent df58b2ac1a
commit ae03e11835
3 changed files with 43 additions and 43 deletions

View File

@@ -41,7 +41,7 @@
namespace cgroup2 {
static bool addPidToProcList(const std::string &cgroup_path, pid_t pid) {
static bool addPidToProcList(const std::string& cgroup_path, pid_t pid) {
std::string pid_str = std::to_string(pid);
LOG_D("Adding pid='%s' to cgroup.procs", pid_str.c_str());
@@ -53,14 +53,14 @@ static bool addPidToProcList(const std::string &cgroup_path, pid_t pid) {
return true;
}
static std::string getCgroupPath(nsjconf_t *nsjconf, pid_t pid) {
static std::string getCgroupPath(nsjconf_t* nsjconf, pid_t pid) {
return nsjconf->cgroupv2_mount + "/NSJAIL." + std::to_string(pid);
}
static std::string getJailCgroupPath(nsjconf_t *nsjconf) {
static std::string getJailCgroupPath(nsjconf_t* nsjconf) {
return nsjconf->cgroupv2_mount + "/NSJAIL_SELF." + std::to_string(getpid());
}
static bool createCgroup(const std::string &cgroup_path, pid_t pid) {
static bool createCgroup(const std::string& cgroup_path, pid_t pid) {
LOG_D("Create '%s' for pid=%d", cgroup_path.c_str(), (int)pid);
if (mkdir(cgroup_path.c_str(), 0700) == -1 && errno != EEXIST) {
PLOG_W("mkdir('%s', 0700) failed", cgroup_path.c_str());
@@ -69,7 +69,7 @@ static bool createCgroup(const std::string &cgroup_path, pid_t pid) {
return true;
}
static bool moveSelfIntoChildCgroup(nsjconf_t *nsjconf) {
static bool moveSelfIntoChildCgroup(nsjconf_t* nsjconf) {
/*
* Move ourselves into another group to avoid the 'No internal processes' rule
* https://unix.stackexchange.com/a/713343
@@ -81,7 +81,7 @@ static bool moveSelfIntoChildCgroup(nsjconf_t *nsjconf) {
return true;
}
static bool enableCgroupSubtree(nsjconf_t *nsjconf, const std::string &controller, pid_t pid) {
static bool enableCgroupSubtree(nsjconf_t* nsjconf, const std::string& controller, pid_t pid) {
std::string cgroup_path = nsjconf->cgroupv2_mount;
LOG_D("Enable cgroup.subtree_control +'%s' to '%s' for pid=%d", controller.c_str(),
cgroup_path.c_str(), pid);
@@ -113,7 +113,7 @@ static bool enableCgroupSubtree(nsjconf_t *nsjconf, const std::string &controlle
}
static bool writeToCgroup(
const std::string &cgroup_path, const std::string &resource, const std::string &value) {
const std::string& cgroup_path, const std::string& resource, const std::string& value) {
LOG_I("Setting '%s' to '%s'", resource.c_str(), value.c_str());
if (!util::writeBufToFile(
@@ -124,14 +124,14 @@ static bool writeToCgroup(
return true;
}
static void removeCgroup(const std::string &cgroup_path) {
static void removeCgroup(const std::string& cgroup_path) {
LOG_D("Remove '%s'", cgroup_path.c_str());
if (rmdir(cgroup_path.c_str()) == -1) {
PLOG_W("rmdir('%s') failed", cgroup_path.c_str());
}
}
static bool needMemoryController(nsjconf_t *nsjconf) {
static bool needMemoryController(nsjconf_t* nsjconf) {
/*
* Check if we need 'memory'
* This matches the check in initNsFromParentMem()
@@ -146,11 +146,11 @@ static bool needMemoryController(nsjconf_t *nsjconf) {
return true;
}
static bool needPidsController(nsjconf_t *nsjconf) {
static bool needPidsController(nsjconf_t* nsjconf) {
return nsjconf->cgroup_pids_max != 0;
}
static bool needCpuController(nsjconf_t *nsjconf) {
static bool needCpuController(nsjconf_t* nsjconf) {
return nsjconf->cgroup_cpu_ms_per_sec != 0U;
}
@@ -160,7 +160,7 @@ static bool needCpuController(nsjconf_t *nsjconf) {
*/
#define SUBTREE_CONTROL_BUF_LEN 0x40
bool setup(nsjconf_t *nsjconf) {
bool setup(nsjconf_t* nsjconf) {
/*
* Read from cgroup.subtree_control in the root to see if
* the controllers we need are there.
@@ -195,7 +195,7 @@ bool setup(nsjconf_t *nsjconf) {
return true;
}
bool detectCgroupv2(nsjconf_t *nsjconf) {
bool detectCgroupv2(nsjconf_t* nsjconf) {
/*
* Check cgroupv2_mount, if it is a cgroup2 mount, use it.
*/
@@ -209,7 +209,7 @@ bool detectCgroupv2(nsjconf_t *nsjconf) {
return true;
}
static bool initNsFromParentMem(nsjconf_t *nsjconf, pid_t pid) {
static bool initNsFromParentMem(nsjconf_t* nsjconf, pid_t pid) {
ssize_t swap_max = nsjconf->cgroup_mem_swap_max;
if (nsjconf->cgroup_mem_memsw_max > (size_t)0) {
swap_max = nsjconf->cgroup_mem_memsw_max - nsjconf->cgroup_mem_max;
@@ -236,7 +236,7 @@ static bool initNsFromParentMem(nsjconf_t *nsjconf, pid_t pid) {
return true;
}
static bool initNsFromParentPids(nsjconf_t *nsjconf, pid_t pid) {
static bool initNsFromParentPids(nsjconf_t* nsjconf, pid_t pid) {
if (nsjconf->cgroup_pids_max == 0U) {
return true;
}
@@ -246,7 +246,7 @@ static bool initNsFromParentPids(nsjconf_t *nsjconf, pid_t pid) {
return writeToCgroup(cgroup_path, "pids.max", std::to_string(nsjconf->cgroup_pids_max));
}
static bool initNsFromParentCpu(nsjconf_t *nsjconf, pid_t pid) {
static bool initNsFromParentCpu(nsjconf_t* nsjconf, pid_t pid) {
if (nsjconf->cgroup_cpu_ms_per_sec == 0U) {
return true;
}
@@ -265,13 +265,13 @@ static bool initNsFromParentCpu(nsjconf_t *nsjconf, pid_t pid) {
return writeToCgroup(cgroup_path, "cpu.max", cpu_ms_per_sec_str);
}
bool initNsFromParent(nsjconf_t *nsjconf, pid_t pid) {
bool initNsFromParent(nsjconf_t* nsjconf, pid_t pid) {
RETURN_ON_FAILURE(initNsFromParentMem(nsjconf, pid));
RETURN_ON_FAILURE(initNsFromParentPids(nsjconf, pid));
return initNsFromParentCpu(nsjconf, pid);
}
void finishFromParent(nsjconf_t *nsjconf, pid_t pid) {
void finishFromParent(nsjconf_t* nsjconf, pid_t pid) {
if (nsjconf->cgroup_mem_max != (size_t)0 || nsjconf->cgroup_pids_max != 0U ||
nsjconf->cgroup_cpu_ms_per_sec != 0U) {
removeCgroup(getCgroupPath(nsjconf, pid));

View File

@@ -62,7 +62,7 @@ namespace cmdline {
struct custom_option {
const struct option opt;
const char *const descr;
const char* const descr;
};
// clang-format off
@@ -173,11 +173,11 @@ static const struct custom_option custom_opts[] = {
};
// clang-format on
static const char *logYesNo(bool yes) {
static const char* logYesNo(bool yes) {
return (yes ? "true" : "false");
}
size_t GetConsoleLength(const std::string &str) {
size_t GetConsoleLength(const std::string& str) {
int result = 0;
for (char c : str) {
if (c == '\t') {
@@ -189,13 +189,13 @@ size_t GetConsoleLength(const std::string &str) {
return result;
}
std::string FormatLine(const std::string &line, size_t max_len = 80) {
std::string FormatLine(const std::string& line, size_t max_len = 80) {
std::string indent = line.substr(0, line.find_first_not_of(" \t"));
size_t indent_len = GetConsoleLength(indent);
size_t cursor = 0;
std::string formatted;
std::vector<std::string> words = util::strSplit(line.c_str(), ' ');
for (const auto &word : words) {
for (const auto& word : words) {
size_t wlen = GetConsoleLength(word);
std::string separator = cursor == 0 ? "" : " ";
size_t slen = GetConsoleLength(separator);
@@ -211,17 +211,17 @@ std::string FormatLine(const std::string &line, size_t max_len = 80) {
return formatted;
}
std::string FormatDescription(const char *descr) {
std::string FormatDescription(const char* descr) {
std::string formatted;
std::vector<std::string> lines = util::strSplit(descr, '\n');
for (const auto &line : lines) {
for (const auto& line : lines) {
util::StrAppend(&formatted, "%s\n", FormatLine(std::string("\t") + line).c_str());
}
return formatted;
}
static void cmdlineOptUsage(const struct custom_option *option) {
static void cmdlineOptUsage(const struct custom_option* option) {
if (option->opt.val < 0x80) {
LOG_HELP_BOLD(" --%s%s%c %s", option->opt.name, "|-", option->opt.val,
option->opt.has_arg == required_argument ? "VALUE" : "");
@@ -232,7 +232,7 @@ static void cmdlineOptUsage(const struct custom_option *option) {
LOG_HELP("%s", FormatDescription(option->descr).c_str());
}
static void cmdlineUsage(const char *pname) {
static void cmdlineUsage(const char* pname) {
LOG_HELP_BOLD("Usage: %s [options] -- path_to_command [args]", pname);
LOG_HELP_BOLD("Options:");
for (size_t i = 0; i < ARR_SZ(custom_opts); i++) {
@@ -249,12 +249,12 @@ static void cmdlineUsage(const char *pname) {
LOG_HELP_BOLD(" nsjail -Me --chroot / --disable_proc -- /bin/echo \"ABC\"");
}
void addEnv(nsjconf_t *nsjconf, const std::string &env) {
void addEnv(nsjconf_t* nsjconf, const std::string& env) {
if (env.find('=') != std::string::npos) {
nsjconf->envs.push_back(env);
return;
}
char *e = getenv(env.c_str());
char* e = getenv(env.c_str());
if (!e) {
LOG_W("Requested to use the %s envar, but it's not set. It'll be ignored", QC(env));
return;
@@ -262,7 +262,7 @@ void addEnv(nsjconf_t *nsjconf, const std::string &env) {
nsjconf->envs.push_back(std::string(env).append("=").append(e));
}
void logParams(nsjconf_t *nsjconf) {
void logParams(nsjconf_t* nsjconf) {
switch (nsjconf->mode) {
case MODE_LISTEN_TCP:
LOG_I("Mode: LISTEN_TCP");
@@ -301,11 +301,11 @@ void logParams(nsjconf_t *nsjconf) {
logYesNo(nsjconf->keep_caps), logYesNo(nsjconf->disable_no_new_privs),
nsjconf->max_cpus);
for (const auto &p : nsjconf->mountpts) {
for (const auto& p : nsjconf->mountpts) {
LOG_I(
"%s: %s", p.is_symlink ? "Symlink" : "Mount", mnt::describeMountPt(p).c_str());
}
for (const auto &uid : nsjconf->uids) {
for (const auto& uid : nsjconf->uids) {
LOG_I("Uid map: inside_uid:%lu outside_uid:%lu count:%zu newuidmap:%s",
(unsigned long)uid.inside_id, (unsigned long)uid.outside_id, uid.count,
uid.is_newidmap ? "true" : "false");
@@ -315,7 +315,7 @@ void logParams(nsjconf_t *nsjconf) {
"have user root-level access to files");
}
}
for (const auto &gid : nsjconf->gids) {
for (const auto& gid : nsjconf->gids) {
LOG_I("Gid map: inside_gid:%lu outside_gid:%lu count:%zu newgidmap:%s",
(unsigned long)gid.inside_id, (unsigned long)gid.outside_id, gid.count,
gid.is_newidmap ? "true" : "false");
@@ -327,7 +327,7 @@ void logParams(nsjconf_t *nsjconf) {
}
}
uint64_t parseRLimit(int res, const char *optarg, unsigned long mul) {
uint64_t parseRLimit(int res, const char* optarg, unsigned long mul) {
if (strcasecmp(optarg, "inf") == 0) {
return RLIM64_INFINITY;
}
@@ -355,14 +355,14 @@ uint64_t parseRLimit(int res, const char *optarg, unsigned long mul) {
return val * mul;
}
static std::string argFromVec(const std::vector<std::string> &vec, size_t pos) {
static std::string argFromVec(const std::vector<std::string>& vec, size_t pos) {
if (pos >= vec.size()) {
return "";
}
return vec[pos];
}
static bool setupArgv(nsjconf_t *nsjconf, int argc, char **argv, int optind) {
static bool setupArgv(nsjconf_t* nsjconf, int argc, char** argv, int optind) {
/*
* If user provided cmdline via nsjail [opts] -- [cmdline], then override
* the one from the config file
@@ -399,7 +399,7 @@ static bool setupArgv(nsjconf_t *nsjconf, int argc, char **argv, int optind) {
return true;
}
static bool setupMounts(nsjconf_t *nsjconf) {
static bool setupMounts(nsjconf_t* nsjconf) {
if (!(nsjconf->chroot.empty())) {
if (!mnt::addMountPtHead(nsjconf, nsjconf->chroot, "/", /* fstype= */ "",
/* options= */ "",
@@ -434,7 +434,7 @@ static bool setupMounts(nsjconf_t *nsjconf) {
return true;
}
void setupUsers(nsjconf_t *nsjconf) {
void setupUsers(nsjconf_t* nsjconf) {
if (nsjconf->uids.empty()) {
idmap_t uid;
uid.inside_id = getuid();
@@ -453,7 +453,7 @@ void setupUsers(nsjconf_t *nsjconf) {
}
}
std::string parseMACVlanMode(const char *optarg) {
std::string parseMACVlanMode(const char* optarg) {
if (strcasecmp(optarg, "private") != 0 && strcasecmp(optarg, "vepa") != 0 &&
strcasecmp(optarg, "bridge") != 0 && strcasecmp(optarg, "passthru") != 0) {
LOG_F("macvlan mode can only be one of the values: "
@@ -464,7 +464,7 @@ std::string parseMACVlanMode(const char *optarg) {
return std::string(optarg);
}
std::unique_ptr<nsjconf_t> parseArgs(int argc, char *argv[]) {
std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
std::unique_ptr<nsjconf_t> nsjconf(new nsjconf_t);
nsjconf->use_execveat = false;

6
mnt.cc
View File

@@ -70,7 +70,7 @@ static const std::string flagsToStr(unsigned long flags) {
NS_VALSTR_STRUCT(MS_DIRSYNC),
#if defined(MS_NOSYMFOLLOW)
NS_VALSTR_STRUCT(MS_NOSYMFOLLOW),
#endif /* defined(MS_NOSYMFOLLOW) */
#endif /* defined(MS_NOSYMFOLLOW) */
NS_VALSTR_STRUCT(MS_NOATIME),
NS_VALSTR_STRUCT(MS_NODIRATIME),
NS_VALSTR_STRUCT(MS_BIND),
@@ -92,7 +92,7 @@ static const std::string flagsToStr(unsigned long flags) {
#endif /* defined(MS_ACTIVE) */
#if defined(MS_NOUSER)
NS_VALSTR_STRUCT((uint32_t)MS_NOUSER), // defined as (1<<31)
#endif /* defined(MS_NOUSER) */
#endif /* defined(MS_NOUSER) */
};
unsigned knownFlagMask = 0U;
@@ -268,7 +268,7 @@ static bool remountPt(const mount_t& mpt) {
{MS_RELATIME, ST_RELATIME},
#if defined(MS_NOSYMFOLLOW) && defined(ST_NOSYMFOLLOW)
{MS_NOSYMFOLLOW, ST_NOSYMFOLLOW},
#endif /* defined(MS_NOSYMFOLLOW) && defined(ST_NOSYMFOLLOW) */
#endif /* defined(MS_NOSYMFOLLOW) && defined(ST_NOSYMFOLLOW) */
};
const unsigned long per_mountpoint_flags =