aboutsummaryrefslogtreecommitdiffstats
path: root/src/syscall-clamp.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2017-11-29 19:22:06 +0000
committerWerner Koch <[email protected]>2017-11-29 19:22:06 +0000
commitadc786d034b63176b941a1ef8d996acbf8d0ea5d (patch)
treeb19d82491cc7666f18cb44d2f7458f2f28fbe936 /src/syscall-clamp.c
parentcore: Implement the spawn functions. (diff)
downloadlibgpg-error-adc786d034b63176b941a1ef8d996acbf8d0ea5d.tar.gz
libgpg-error-adc786d034b63176b941a1ef8d996acbf8d0ea5d.zip
core: Unify syscall_clamp functions.
* src/estream.c (_gpgrt_set_syscall_clamp) (_gpgrt_get_syscall_clamp): Move to ... * src/syscall-clamp.c: new file. (_gpgrt_pre_syscall, _gpgrt_post_syscall): New. * src/Makefile.am (libgpg_error_la_SOURCES): Add that file. * src/estream.c: Replace the syscall wrapper with the new functions. * src/posix-lock.c: Ditto. * src/w32-lock.c: Ditto. * src/posix-thread.c: Ditto. * src/w32-thread.c: Ditto. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'src/syscall-clamp.c')
-rw-r--r--src/syscall-clamp.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/syscall-clamp.c b/src/syscall-clamp.c
new file mode 100644
index 0000000..99748f4
--- /dev/null
+++ b/src/syscall-clamp.c
@@ -0,0 +1,80 @@
+/* syscall-clamp.c - Syscall clamp related stuff
+ * Copyright (C) 2016, 2017 g10 Code GmbH
+ *
+ * This file is part of Libgpg-error.
+ *
+ * Libgpg-error is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgpg-error is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see <https://www.gnu.org/licenses/>.
+ * SPDX-License-Identifier: LGPL-2.1+
+ */
+
+#include <config.h>
+
+#include "gpgrt-int.h"
+
+/*
+ * Functions called before and after blocking syscalls.
+ * gpgrt_set_syscall_clamp is used to set them.
+ */
+static void (*pre_syscall_func)(void);
+static void (*post_syscall_func)(void);
+
+
+/*
+ * Register the syscall clamp. These two functions are called
+ * immediately before and after a possible blocking system call. This
+ * should be used before any I/O happens. The function is commonly
+ * used with the nPth library:
+ *
+ * gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
+ *
+ * These functions may not modify ERRNO.
+ *
+ * Setting the clamp is not thread-safe and should thus be done as
+ * early as possible.
+ */
+void
+_gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void))
+{
+ pre_syscall_func = pre;
+ post_syscall_func = post;
+}
+
+/*
+ * Return the current sycall clamp functions. This can be used by
+ * other libraries which have blocking functions.
+ */
+void
+_gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void))
+{
+ *r_pre = pre_syscall_func;
+ *r_post = post_syscall_func;
+}
+
+
+/* Call this function before a blocking system or libc call. */
+void
+_gpgrt_pre_syscall (void)
+{
+ if (pre_syscall_func)
+ pre_syscall_func ();
+}
+
+
+/* Call this function after a blocking system or libc call. */
+void
+_gpgrt_post_syscall (void)
+{
+ if (post_syscall_func)
+ post_syscall_func ();
+}