A few c++isms more

This commit is contained in:
Robert Swiecki 2018-05-23 18:19:17 +02:00
parent 6d3fb7e5b2
commit 2b6955e48c
5 changed files with 31 additions and 14 deletions

13
caps.cc
View File

@ -167,24 +167,23 @@ static void setInheritable(cap_user_data_t cap_data, unsigned int cap) {
#define PR_CAP_AMBIENT_CLEAR_ALL 4
#endif /* !defined(PR_CAP_AMBIENT) */
static bool initNsKeepCaps(cap_user_data_t cap_data) {
char dbgmsg[4096];
/* Copy all permitted caps to the inheritable set */
dbgmsg[0] = '\0';
std::string dbgmsg1;
for (const auto& i : capNames) {
if (getPermitted(cap_data, i.val)) {
util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
util::StrAppend(&dbgmsg1, " %s", i.name);
setInheritable(cap_data, i.val);
}
}
LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg);
LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg1.c_str());
if (!setCaps(cap_data)) {
return false;
}
/* Make sure the inheritable set is preserved across execve via the ambient set */
dbgmsg[0] = '\0';
std::string dbgmsg2;
for (const auto& i : capNames) {
if (!getPermitted(cap_data, i.val)) {
continue;
@ -193,10 +192,10 @@ static bool initNsKeepCaps(cap_user_data_t cap_data) {
-1) {
PLOG_W("prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, %s)", i.name);
} else {
util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
util::StrAppend(&dbgmsg2, " %s", i.name);
}
}
LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg);
LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg2.c_str());
return true;
}

4
mnt.cc
View File

@ -99,9 +99,7 @@ static const std::string flagsToStr(uintptr_t flags) {
if (((flags & ~(knownFlagMask)) == 0) && !res.empty()) {
res.pop_back();
} else {
char flagstr[32];
snprintf(flagstr, sizeof(flagstr), "%#tx", flags & ~(knownFlagMask));
res.append(flagstr);
util::StrAppend(&res, "%#tx", flags & ~(knownFlagMask));
}
return res;

View File

@ -100,9 +100,7 @@ static const std::string cloneFlagsToStr(uintptr_t flags) {
}
if (flags & ~(knownFlagMask)) {
char flagstr[32];
snprintf(flagstr, sizeof(flagstr), "%#tx|", flags & ~(knownFlagMask));
res.append(flagstr);
util::StrAppend(&res, "%#tx|", flags & ~(knownFlagMask));
}
res.append(util::sigName(flags & CSIGNAL).c_str());
return res;

19
util.cc
View File

@ -171,6 +171,25 @@ int sSnPrintf(char* str, size_t size, const char* format, ...) {
return snprintf(str, size, "%s%s", buf1, buf2);
}
std::string* StrAppend(std::string* str, const char* format, ...) {
char* strp;
va_list args;
va_start(args, format);
int ret = vasprintf(&strp, format, args);
va_end(args);
if (ret == -1) {
PLOG_E("Memory allocation failed during asprintf()");
str->append(" [ERROR: mem_allocation_failed] ");
return str;
}
str->append(strp, ret);
free(strp);
return str;
}
bool isANumber(const char* s) {
for (size_t i = 0; s[i]; s++) {
if (!isdigit(s[i]) && s[i] != 'x') {

5
util.h
View File

@ -38,7 +38,10 @@ ssize_t readFromFile(const char* fname, void* buf, size_t len);
ssize_t writeToFd(int fd, const void* buf, size_t len);
bool writeBufToFile(const char* filename, const void* buf, size_t len, int open_flags);
bool createDirRecursively(const char* dir);
int sSnPrintf(char* str, size_t size, const char* format, ...);
int sSnPrintf(char* str, size_t size, const char* format, ...)
__attribute__((format(printf, 3, 4)));
std::string* StrAppend(std::string* str, const char* format, ...)
__attribute__((format(printf, 2, 3)));
bool isANumber(const char* s);
uint64_t rnd64(void);
const std::string sigName(int signo);