aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-01-10 17:21:15 +0100
committerMike Gilbert <floppym@gentoo.org>2018-01-13 12:19:22 -0500
commitb9fcf6f8db136a8e64a338dda4f2cec31ec49b33 (patch)
tree7849d49cb861d3fd2cafe123741da32217b54898
parentsd-radv: avoid redefinition of struct in6_addr (diff)
downloadsystemd-b9fcf6f8db136a8e64a338dda4f2cec31ec49b33.tar.gz
systemd-b9fcf6f8db136a8e64a338dda4f2cec31ec49b33.tar.bz2
systemd-b9fcf6f8db136a8e64a338dda4f2cec31ec49b33.zip
util-lib: save/restore errno in cleanup calls
We should be careful with errno in cleanup functions, and not alter it under any circumstances. In the safe_close cleanup handlers we are already safe in that regard, but let's add similar protections on other cleanup handlers that invoke system calls. Why bother? Cleanup handlers insert code at function return in non-obvious ways. Hence, code that sets errno and returns should not be confused by us overrding the errno from a cleanup handler. This is a paranoia fix only, I am not aware where this actually mattered in real-life situations. (cherry picked from commit dfd14786b5aa49c3c8e3866c0ecfa6d90c531eb6)
-rw-r--r--src/basic/fs-util.h4
-rw-r--r--src/basic/process-util.c2
-rw-r--r--src/basic/rm-rf.h3
3 files changed, 8 insertions, 1 deletions
diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h
index a7ba61625..82a5b2028 100644
--- a/src/basic/fs-util.h
+++ b/src/basic/fs-util.h
@@ -29,6 +29,7 @@
#include <unistd.h>
#include "time-util.h"
+#include "util.h"
int unlink_noerrno(const char *path);
@@ -89,13 +90,14 @@ int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flag
/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
static inline void rmdir_and_free(char *p) {
+ PROTECT_ERRNO;
(void) rmdir(p);
free(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
static inline void unlink_and_free(char *p) {
- (void) unlink(p);
+ (void) unlink_noerrno(p);
free(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 17c94f44a..c8c2fff73 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -777,6 +777,8 @@ void sigkill_wait(pid_t pid) {
}
void sigkill_waitp(pid_t *pid) {
+ PROTECT_ERRNO;
+
if (!pid)
return;
if (*pid <= 1)
diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h
index 1127e326b..ad63e9be4 100644
--- a/src/basic/rm-rf.h
+++ b/src/basic/rm-rf.h
@@ -22,6 +22,8 @@
#include <sys/stat.h>
+#include "util.h"
+
typedef enum RemoveFlags {
REMOVE_ONLY_DIRECTORIES = 1,
REMOVE_ROOT = 2,
@@ -34,6 +36,7 @@ int rm_rf(const char *path, RemoveFlags flags);
/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */
static inline void rm_rf_physical_and_free(char *p) {
+ PROTECT_ERRNO;
(void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
free(p);
}