aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/libmdsserver/util.c39
-rw-r--r--src/libmdsserver/util.h12
2 files changed, 51 insertions, 0 deletions
diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c
index 01f5cbe..83641f2 100644
--- a/src/libmdsserver/util.c
+++ b/src/libmdsserver/util.c
@@ -28,6 +28,8 @@
#include <sys/socket.h>
#include <errno.h>
#include <ctype.h>
+#include <time.h>
+#include <sys/wait.h>
@@ -294,3 +296,40 @@ int startswith_n(const char* haystack, const char* needle, size_t haystack_n, si
return 1;
}
+
+/**
+ * Wrapper around `waitpid` that never returns on an interruption unless
+ * it is interrupted 100 times within the same second
+ *
+ * @param pid See description of `pid` in the documentation for `waitpid`
+ * @param pid See description of `status` in the documentation for `waitpid`
+ * @param pid See description of `options` in the documentation for `waitpid`
+ * @return See the documentation for `waitpid`
+ */
+pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options)
+{
+ struct timespec time_start;
+ struct timespec time_intr;
+ int intr_count = 0, have_time;
+ pid_t rc;
+
+ have_time = (monotone(&time_start) >= 0);
+
+ rewait:
+ rc = waitpid(pid, status, options);
+ if (rc == (pid_t)-1)
+ {
+ if (errno != EINTR)
+ goto fail;
+ if (have_time && (monotone(&time_intr) >= 0))
+ if (time_start.tv_sec != time_intr.tv_sec)
+ intr_count = 0;
+ if (intr_count++ < 100)
+ goto rewait;
+ /* Don't let the CPU catch fire! */
+ errno = EINTR;
+ }
+ fail:
+ return rc;
+}
+
diff --git a/src/libmdsserver/util.h b/src/libmdsserver/util.h
index 00dfb58..3220adb 100644
--- a/src/libmdsserver/util.h
+++ b/src/libmdsserver/util.h
@@ -21,6 +21,7 @@
#include <stddef.h>
#include <stdint.h>
+#include <sys/types.h>
/**
@@ -113,6 +114,17 @@ char* full_read(int fd);
*/
int startswith_n(const char* haystack, const char* needle, size_t haystack_n, size_t needle_n) __attribute__((pure));
+/**
+ * Wrapper around `waitpid` that never returns on an interruption unless
+ * it is interrupted 100 times within the same second
+ *
+ * @param pid See description of `pid` in the documentation for `waitpid`
+ * @param pid See description of `status` in the documentation for `waitpid`
+ * @param pid See description of `options` in the documentation for `waitpid`
+ * @return See the documentation for `waitpid`
+ */
+pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options);
+
#endif