ac3a84
From 3850d27f47a887a958ded828f6ce8de4e791037c Mon Sep 17 00:00:00 2001
ac3a84
From: Lennart Poettering <lennart@poettering.net>
ac3a84
Date: Tue, 22 Nov 2022 15:23:34 +0100
ac3a84
Subject: [PATCH] io-util: document EINTR situation a bit
ac3a84
ac3a84
(cherry picked from commit ffbcc8d423671ad2fe827e4823a8032dc1f0a8b3)
ac3a84
ac3a84
Related: #2137584
ac3a84
---
ac3a84
 src/basic/io-util.c | 18 ++++++++++++++++++
ac3a84
 1 file changed, 18 insertions(+)
ac3a84
ac3a84
diff --git a/src/basic/io-util.c b/src/basic/io-util.c
ac3a84
index cdad939aa6..f642beca3a 100644
ac3a84
--- a/src/basic/io-util.c
ac3a84
+++ b/src/basic/io-util.c
ac3a84
@@ -161,6 +161,21 @@ int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
ac3a84
 
ac3a84
         assert(fds || nfds == 0);
ac3a84
 
ac3a84
+        /* This is a wrapper around ppoll() that does primarily two things:
ac3a84
+         *
ac3a84
+         *  ✅ Takes a usec_t instead of a struct timespec
ac3a84
+         *
ac3a84
+         *  ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to
ac3a84
+         *     EBADF). This is done because EBADF is a programming error usually, and hence should bubble up
ac3a84
+         *     as error, and not be eaten up as non-error POLLNVAL event.
ac3a84
+         *
ac3a84
+         *  ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget
ac3a84
+         *  poll()/ppoll() will return with EINTR on any received signal always, there is no automatic
ac3a84
+         *  restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error,
ac3a84
+         *  but just as reason to restart things, under the assumption you use a more appropriate mechanism
ac3a84
+         *  to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️
ac3a84
+         */
ac3a84
+
ac3a84
         if (nfds == 0)
ac3a84
                 return 0;
ac3a84
 
ac3a84
@@ -188,6 +203,9 @@ int fd_wait_for_event(int fd, int event, usec_t timeout) {
ac3a84
         };
ac3a84
         int r;
ac3a84
 
ac3a84
+        /* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see
ac3a84
+         * ppoll_usec() above! ⚠️ ⚠️ ⚠️ */
ac3a84
+
ac3a84
         r = ppoll_usec(&pollfd, 1, timeout);
ac3a84
         if (r <= 0)
ac3a84
                 return r;