Make accept(2) and recvfrom(2) cancellation points on Mac OS X.
authorTobias Brunner <tobias@strongswan.org>
Fri, 24 Jul 2009 08:58:27 +0000 (10:58 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 30 Jul 2009 12:06:26 +0000 (14:06 +0200)
src/charon/plugins/stroke/stroke_socket.c
src/libstrongswan/utils/mutex.h

index f61171e..9b6a8a3 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <processing/jobs/callback_job.h>
 #include <daemon.h>
+#include <utils/mutex.h> /* for Mac OS X compatible accept */
 
 #include "stroke_config.h"
 #include "stroke_control.h"
index c5c6679..8a8162a 100644 (file)
@@ -31,6 +31,33 @@ typedef enum rwlock_type_t rwlock_type_t;
 
 #include <library.h>
 
+#ifdef __APPLE__
+/* on Mac OS X 10.5 several system calls we use are no cancellation points.
+ * fortunately, select isn't one of them, so we wrap some of the others with
+ * calls to select(2).
+ */
+#include <sys/socket.h>
+#include <sys/select.h>
+
+#define WRAP_WITH_SELECT(func, socket, ...)\
+       fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\
+       if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\
+       return func(socket, __VA_ARGS__)
+
+static inline int cancellable_accept(int socket, struct sockaddr *address,
+                                                                        socklen_t *address_len)
+{
+       WRAP_WITH_SELECT(accept, socket, address, address_len);
+}
+#define accept cancellable_accept
+static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
+                               int flags, struct sockaddr *address, socklen_t *address_len)
+{
+       WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len);
+}
+#define recvfrom cancellable_recvfrom
+#endif /* __APPLE__ */
+
 /**
  * Type of mutex.
  */