LogSecureDebug("Entering function: " << __func__);
struct stat buf;
- char *target;
// check if it's a link
if ( !S_ISLNK(ftsent->fts_statp->st_mode))
return FileDecision::SKIP;
- target = realpath(ftsent->fts_path, NULL);
- if (!target) {
+ std::unique_ptr<char, std::function<void(void*)>> target(realpath(ftsent->fts_path, NULL), free);
+
+ if (!target.get()) {
LogSecureError("Getting link target for " << ftsent->fts_path << " failed (Error = " << strerror(errno) << ")");
return FileDecision::ERROR;
}
- if (-1 == stat(target, &buf)) {
- LogSecureError("stat failed for " << target << " (Error = " << strerror(errno) << ")");
+
+ if (-1 == stat(target.get(), &buf)) {
+ LogSecureError("stat failed for " << target.get() << " (Error = " << strerror(errno) << ")");
return FileDecision::ERROR;
}
// skip if link target is not a regular executable file
if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG)) {
- LogSecureDebug(target << "is not a regular executable file. Skipping.");
+ LogSecureDebug(target.get() << "is not a regular executable file. Skipping.");
return FileDecision::SKIP;
}
#include <sys/smack.h>
#include <fcntl.h>
#include <fstream>
+#include <cstring>
#include <dpl/log/log.h>
int fd;
bool ret = true;
- fd = open(path.c_str(), O_RDONLY);
+ fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY));
if (fd == -1) {
LogError("Failed to open file: %s" << path);
return false;
ret = false;
}
- close(fd);
+ if (close(fd) == -1) {
+ // don't change the return code, the descriptor should be closed despite the error.
+ LogWarning("Error while closing the file: " << path << ", error: " << strerror(errno));
+ }
+
return ret;
}
int fd;
bool ret = true;
- fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644));
if (fd == -1) {
LogError("Failed to create file: %s" << path);
return false;
ret = false;
}
- close(fd);
+ if (close(fd) == -1) {
+ if (errno == EIO) {
+ LogError("I/O Error occured while closing the file: " << path << ", error: " << strerror(errno));
+ unlink(path.c_str());
+ return false;
+ } else {
+ // non critical error
+ // don't change the return code, the descriptor should be closed despite the error.
+ LogWarning("Error while closing the file: " << path << ", error: " << strerror(errno));
+ }
+ }
+
return ret;
}