diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-08-11 05:47:10 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-08-11 05:47:10 +0200 |
commit | 4f8ac368dbead32aaa30684c29df3afa0a8ba17f (patch) | |
tree | 60a1007dba7a0f2456c44f7152e68b274f4bb3f2 /src/libmdsserver/util.c | |
parent | m (diff) | |
download | mds-4f8ac368dbead32aaa30684c29df3afa0a8ba17f.tar.gz mds-4f8ac368dbead32aaa30684c29df3afa0a8ba17f.tar.bz2 mds-4f8ac368dbead32aaa30684c29df3afa0a8ba17f.tar.xz |
add fork_for_safety option + beginning of kernel-based keyboard
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/util.c | 39 |
1 files changed, 39 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; +} + |