naccyde / rpms / systemd

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