summaryrefslogtreecommitdiff
path: root/src/estream.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2017-11-29 20:22:06 +0100
committerWerner Koch <wk@gnupg.org>2017-11-29 20:22:06 +0100
commitadc786d034b63176b941a1ef8d996acbf8d0ea5d (patch)
treeb19d82491cc7666f18cb44d2f7458f2f28fbe936 /src/estream.c
parent1865c0ba1769b407a3c504f1ab0a4278704a9fc1 (diff)
downloadlibgpg-error-adc786d034b63176b941a1ef8d996acbf8d0ea5d.tar.gz
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 <wk@gnupg.org>
Diffstat (limited to 'src/estream.c')
-rw-r--r--src/estream.c124
1 files changed, 32 insertions, 92 deletions
diff --git a/src/estream.c b/src/estream.c
index 2f29cdb..dd2b289 100644
--- a/src/estream.c
+++ b/src/estream.c
@@ -201,14 +201,6 @@ GPGRT_LOCK_DEFINE (estream_list_lock);
/*
- * 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);
-
-
-/*
* Error code replacements.
*/
#ifndef EOPNOTSUPP
@@ -491,7 +483,7 @@ do_list_remove (estream_t stream, int with_locked_list)
/*
- * The atexit handler for this estream module.
+ * The atexit handler for the entire gpgrt.
*/
static void
do_deinit (void)
@@ -508,10 +500,7 @@ do_deinit (void)
we keep the list and let the OS clean it up at process end. */
/* Reset the syscall clamp. */
- pre_syscall_func = NULL;
- post_syscall_func = NULL;
- _gpgrt_thread_set_syscall_clamp (NULL, NULL);
- _gpgrt_lock_set_lock_clamp (NULL, NULL);
+ _gpgrt_set_syscall_clamp (NULL, NULL);
}
@@ -531,37 +520,6 @@ _gpgrt_estream_init (void)
return 0;
}
-/*
- * 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.
- */
-void
-_gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void))
-{
- pre_syscall_func = pre;
- post_syscall_func = post;
- _gpgrt_thread_set_syscall_clamp (pre, post);
- _gpgrt_lock_set_lock_clamp (pre, 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;
-}
-
-
/*
* Implementation of memory based I/O.
@@ -971,15 +929,13 @@ func_fd_read (void *cookie, void *buffer, size_t size)
}
else
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
do
{
bytes_read = read (file_cookie->fd, buffer, size);
}
while (bytes_read == -1 && errno == EINTR);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
}
trace_errno (bytes_read == -1, ("leave: bytes_read=%d", (int)bytes_read));
@@ -1005,15 +961,13 @@ func_fd_write (void *cookie, const void *buffer, size_t size)
}
else if (buffer)
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
do
{
bytes_written = write (file_cookie->fd, buffer, size);
}
while (bytes_written == -1 && errno == EINTR);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
}
else
bytes_written = size; /* Note that for a flush SIZE should be 0. */
@@ -1041,11 +995,9 @@ func_fd_seek (void *cookie, gpgrt_off_t *offset, int whence)
}
else
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
offset_new = lseek (file_cookie->fd, *offset, whence);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
if (offset_new == -1)
err = -1;
else
@@ -1216,8 +1168,8 @@ func_w32_read (void *cookie, void *buffer, size_t size)
}
else
{
- if (pre_syscall_func && !w32_cookie->no_syscall_clamp)
- pre_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_pre_syscall ();
do
{
DWORD nread, ec;
@@ -1239,8 +1191,8 @@ func_w32_read (void *cookie, void *buffer, size_t size)
bytes_read = (int)nread;
}
while (bytes_read == -1 && errno == EINTR);
- if (post_syscall_func && !w32_cookie->no_syscall_clamp)
- post_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_post_syscall ();
}
trace_errno (bytes_read==-1,("leave: bytes_read=%d", (int)bytes_read));
@@ -1269,8 +1221,8 @@ func_w32_write (void *cookie, const void *buffer, size_t size)
}
else if (buffer)
{
- if (pre_syscall_func && !w32_cookie->no_syscall_clamp)
- pre_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_pre_syscall ();
do
{
DWORD nwritten;
@@ -1287,8 +1239,8 @@ func_w32_write (void *cookie, const void *buffer, size_t size)
bytes_written = (int)nwritten;
}
while (bytes_written == -1 && errno == EINTR);
- if (post_syscall_func && !w32_cookie->no_syscall_clamp)
- post_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_post_syscall ();
}
else
bytes_written = size; /* Note that for a flush SIZE should be 0. */
@@ -1338,17 +1290,16 @@ func_w32_seek (void *cookie, gpgrt_off_t *offset, int whence)
#ifdef HAVE_W32CE_SYSTEM
# warning need to use SetFilePointer
#else
- if (pre_syscall_func && !w32_cookie->no_syscall_clamp)
- pre_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_pre_syscall ();
if (!SetFilePointerEx (w32_cookie->hd, distance, &newoff, method))
{
_set_errno (map_w32_to_errno (GetLastError ()));
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
return -1;
}
- if (post_syscall_func && !w32_cookie->no_syscall_clamp)
- post_syscall_func ();
+ if (!w32_cookie->no_syscall_clamp)
+ _gpgrt_post_syscall ();
#endif
/* Note that gpgrt_off_t is always 64 bit. */
*offset = (gpgrt_off_t)newoff.QuadPart;
@@ -1473,11 +1424,9 @@ func_fp_read (void *cookie, void *buffer, size_t size)
if (file_cookie->fp)
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
bytes_read = fread (buffer, 1, size, file_cookie->fp);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
}
else
bytes_read = 0;
@@ -1498,8 +1447,7 @@ func_fp_write (void *cookie, const void *buffer, size_t size)
if (file_cookie->fp)
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
if (buffer)
{
#ifdef HAVE_W32_SYSTEM
@@ -1527,8 +1475,7 @@ func_fp_write (void *cookie, const void *buffer, size_t size)
bytes_written = size;
fflush (file_cookie->fp);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
}
else
bytes_written = size; /* Successfully written to the bit bucket. */
@@ -1554,20 +1501,17 @@ func_fp_seek (void *cookie, gpgrt_off_t *offset, int whence)
return -1;
}
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
if ( fseek (file_cookie->fp, (long int)*offset, whence) )
{
/* fprintf (stderr, "\nfseek failed: errno=%d (%s)\n", */
/* errno,strerror (errno)); */
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
return -1;
}
offset_new = ftell (file_cookie->fp);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
if (offset_new == -1)
{
/* fprintf (stderr, "\nftell failed: errno=%d (%s)\n", */
@@ -1592,11 +1536,9 @@ func_fp_destroy (void *cookie)
{
if (fp_cookie->fp)
{
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
fflush (fp_cookie->fp);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
err = fp_cookie->no_close? 0 : fclose (fp_cookie->fp);
}
else
@@ -4909,8 +4851,7 @@ _gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout)
}
}
- if (pre_syscall_func)
- pre_syscall_func ();
+ _gpgrt_pre_syscall ();
do
{
struct timeval timeout_val;
@@ -4924,8 +4865,7 @@ _gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout)
timeout == -1 ? NULL : &timeout_val);
}
while (ret == -1 && errno == EINTR);
- if (post_syscall_func)
- post_syscall_func ();
+ _gpgrt_post_syscall ();
if (ret == -1)
{