chiark / gitweb /
util-lib: save/restore errno in cleanup calls
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Jan 2018 16:21:15 +0000 (17:21 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:50:03 +0000 (07:50 +0200)
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.

src/basic/fs-util.h
src/basic/process-util.c
src/basic/rm-rf.h

index c22e967f952df1e966fb30edadc843608b0081b8..8b03055a39c5f6e33e56178f66a1e373f3664e49 100644 (file)
@@ -29,6 +29,7 @@
 #include <unistd.h>
 
 #include "time-util.h"
+//#include "util.h"
 
 int unlink_noerrno(const char *path);
 
@@ -103,6 +104,7 @@ 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);
 }
@@ -110,7 +112,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
 
 #if 0 /// UNNEEDED by elogind
 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);
index 8c90f666e8358493b09fda6c9797dd4c01eff828..c4a69f26138bde87312d52f8ffeca0284d2995bb 100644 (file)
@@ -803,6 +803,8 @@ void sigkill_wait(pid_t pid) {
 }
 
 void sigkill_waitp(pid_t *pid) {
+        PROTECT_ERRNO;
+
         if (!pid)
                 return;
         if (*pid <= 1)
index 1127e326b2ba810c88836e62f8418d930a30b0a6..3ff1f1a1b2aebd33068d761ee9ab285005f5eb5f 100644 (file)
@@ -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);
 }