summaryrefslogtreecommitdiff
path: root/tests/gpg
diff options
context:
space:
mode:
authorNIIBE Yutaka <gniibe@fsij.org>2021-11-25 11:13:17 +0900
committerNIIBE Yutaka <gniibe@fsij.org>2021-11-25 11:13:17 +0900
commit8148237cb4ae20755c06a44d71761c7030973c3d (patch)
treeaaa3d581d5094f436a6ec353f390b25f902d206d /tests/gpg
parent4583ab77e5af3f0da5b307a169d07b58dddf652f (diff)
downloadgpgme-8148237cb4ae20755c06a44d71761c7030973c3d.tar.gz
posix: Use poll instead, when available, removing use of select.
* configure.ac (HAVE_POLL_H): Add the check. * src/ath.c [!HAVE_POLL_H] (ath_select): Enable conditionally. * src/posix-io.c [HAVE_POLL_H] (_gpgme_io_select_poll): Use poll. * tests/gpg/t-cancel.c [HAVE_POLL_H] (do_select): Use poll. * tests/gpg/t-eventloop.c [HAVE_POLL_H] (do_select): Use poll. -- GnuPG-bug-id: 2385 Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to 'tests/gpg')
-rw-r--r--tests/gpg/t-cancel.c67
-rw-r--r--tests/gpg/t-eventloop.c63
2 files changed, 127 insertions, 3 deletions
diff --git a/tests/gpg/t-cancel.c b/tests/gpg/t-cancel.c
index 48158fbd..d5fb6664 100644
--- a/tests/gpg/t-cancel.c
+++ b/tests/gpg/t-cancel.c
@@ -38,8 +38,16 @@
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
-#ifdef HAVE_SYS_SELECT_H
-# include <sys/select.h>
+#ifdef HAVE_POLL_H
+# include <poll.h>
+#else
+# ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+# else
+# ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+# endif
+# endif
#endif
#include <gpgme.h>
@@ -116,6 +124,60 @@ io_event (void *data, gpgme_event_io_t type, void *type_data)
}
+#ifdef HAVE_POLL_H
+static int
+do_select (void)
+{
+ struct pollfd poll_fds[FDLIST_MAX];
+ nfds_t poll_nfds;
+ int i, n;
+ int any = 0;
+
+ pthread_mutex_lock (&lock);
+ poll_nfds = 0;
+ for (i = 0; i < FDLIST_MAX; i++)
+ if (fdlist[i].fd != -1)
+ {
+ poll_fds[poll_nfds].fd = fdlist[i].fd;
+ poll_fds[poll_nfds].events = 0;
+ poll_fds[poll_nfds].revents = 0;
+ if (fdlist[i].dir)
+ poll_fds[poll_nfds].events |= POLLIN;
+ else
+ poll_fds[poll_nfds].events |= POLLOUT;
+ poll_nfds++;
+ }
+ pthread_mutex_unlock (&lock);
+
+ do
+ {
+ n = poll (poll_fds, poll_nfds, 1000);
+ }
+ while (n < 0 && (errno == EINTR || errno == EAGAIN));
+
+ if (n < 0)
+ return n; /* Error or timeout. */
+
+ pthread_mutex_lock (&lock);
+ poll_nfds = 0;
+ for (i = 0; i < FDLIST_MAX && n; i++)
+ {
+ if (fdlist[i].fd != -1)
+ {
+ if ((poll_fds[poll_nfds++].revents
+ & (fdlist[i].dir ? (POLLIN|POLLHUP) : POLLOUT)))
+ {
+ assert (n);
+ n--;
+ any = 1;
+ (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd);
+ }
+ }
+ }
+ pthread_mutex_unlock (&lock);
+ return any;
+}
+#else
static int
do_select (void)
{
@@ -162,6 +224,7 @@ do_select (void)
pthread_mutex_unlock (&lock);
return any;
}
+#endif
static int
my_wait (void)
diff --git a/tests/gpg/t-eventloop.c b/tests/gpg/t-eventloop.c
index b31764ea..0106670b 100644
--- a/tests/gpg/t-eventloop.c
+++ b/tests/gpg/t-eventloop.c
@@ -31,7 +31,17 @@
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
-#include <sys/select.h>
+#ifdef HAVE_POLL_H
+# include <poll.h>
+#else
+# ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+# else
+# ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+# endif
+# endif
+#endif
#include <gpgme.h>
@@ -104,6 +114,56 @@ io_event (void *data, gpgme_event_io_t type, void *type_data)
}
+#ifdef HAVE_POLL_H
+int
+do_select (void)
+{
+ struct pollfd poll_fds[FDLIST_MAX];
+ nfds_t poll_nfds;
+ int i, n;
+ int any = 0;
+
+ poll_nfds = 0;
+ for (i = 0; i < FDLIST_MAX; i++)
+ if (fdlist[i].fd != -1)
+ {
+ poll_fds[poll_nfds].fd = fdlist[i].fd;
+ poll_fds[poll_nfds].events = 0;
+ poll_fds[poll_nfds].revents = 0;
+ if (fdlist[i].dir)
+ poll_fds[poll_nfds].events |= POLLIN;
+ else
+ poll_fds[poll_nfds].events |= POLLOUT;
+ poll_nfds++;
+ }
+
+ do
+ {
+ n = poll (poll_fds, poll_nfds, 1000);
+ }
+ while (n < 0 && (errno == EINTR || errno == EAGAIN));
+
+ if (n < 0)
+ return n; /* Error or timeout. */
+
+ poll_nfds = 0;
+ for (i = 0; i < FDLIST_MAX && n; i++)
+ {
+ if (fdlist[i].fd != -1)
+ {
+ if ((poll_fds[poll_nfds++].revents
+ & (fdlist[i].dir ? (POLLIN|POLLHUP) : POLLOUT)))
+ {
+ assert (n);
+ n--;
+ any = 1;
+ (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd);
+ }
+ }
+ }
+ return any;
+}
+#else
int
do_select (void)
{
@@ -146,6 +206,7 @@ do_select (void)
}
return any;
}
+#endif
int
my_wait (void)