Annotation of embedaddon/strongswan/src/libstrongswan/utils/compat/apple.h, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2014 Martin Willi
        !             3:  * Copyright (C) 2014 revosec AG
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: /**
        !            17:  * @defgroup apple apple
        !            18:  * @{ @ingroup compat
        !            19:  */
        !            20: 
        !            21: #ifndef APPLE_H_
        !            22: #define APPLE_H_
        !            23: 
        !            24: #include <poll.h>
        !            25: #include <sys/socket.h>
        !            26: #include <sys/select.h>
        !            27: 
        !            28: /* thread_create is a syscall used to create Mach kernel threads and although
        !            29:  * there are no errors or warnings during compilation or linkage the dynamic
        !            30:  * linker does not use our implementation, therefore we rename it here
        !            31:  */
        !            32: #define thread_create(main, arg) strongswan_thread_create(main, arg)
        !            33: 
        !            34: /* Mach uses a semaphore_create() call, use a different name for ours */
        !            35: #define semaphore_create(x) strongswan_semaphore_create(x)
        !            36: 
        !            37: /* Since OS X 10.10 XPC includes some additional conflicting Mach types */
        !            38: #define host_t strongswan_host_t
        !            39: #define processor_t strongswan_processor_t
        !            40: #define task_t strongswan_task_t
        !            41: #define thread_t strongswan_thread_t
        !            42: 
        !            43: /* forward declaration, see below */
        !            44: static inline int precancellable_poll(struct pollfd fds[], nfds_t nfds,
        !            45:                                                                          int timeout);
        !            46: 
        !            47: /* on Mac OS X 10.5 several system calls we use are no cancellation points.
        !            48:  * fortunately, select isn't one of them, so we wrap some of the others with
        !            49:  * calls to select(2).
        !            50:  */
        !            51: 
        !            52: #define WRAP_WITH_POLL(func, socket, ...) \
        !            53:        struct pollfd pfd = { \
        !            54:                .fd = socket, \
        !            55:                .events = POLLIN, \
        !            56:        }; \
        !            57:        if (precancellable_poll(&pfd, 1, -1) <= 0) \
        !            58:        {\
        !            59:                return -1; \
        !            60:        }\
        !            61:        return func(socket, __VA_ARGS__)
        !            62: 
        !            63: static inline int cancellable_accept(int socket, struct sockaddr *address,
        !            64:                                                                         socklen_t *address_len)
        !            65: {
        !            66:        WRAP_WITH_POLL(accept, socket, address, address_len);
        !            67: }
        !            68: #define accept cancellable_accept
        !            69: static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
        !            70:                                int flags, struct sockaddr *address, socklen_t *address_len)
        !            71: {
        !            72:        WRAP_WITH_POLL(recvfrom, socket, buffer, length, flags, address, address_len);
        !            73: }
        !            74: #define recvfrom cancellable_recvfrom
        !            75: 
        !            76: #include <threading/thread.h>
        !            77: 
        !            78: /*
        !            79:  * While select() is a cancellation point, it seems that OS X does not honor
        !            80:  * pending cancellation points when entering the function. We manually test for
        !            81:  * and honor pending cancellation requests, but this obviously can't prevent
        !            82:  * some race conditions where the the cancellation happens after the check,
        !            83:  * but before the select.
        !            84:  */
        !            85: static inline int precancellable_select(int nfds, fd_set *restrict readfds,
        !            86:                                                fd_set *restrict writefds, fd_set *restrict errorfds,
        !            87:                                                struct timeval *restrict timeout)
        !            88: {
        !            89:        if (thread_cancelability(TRUE))
        !            90:        {
        !            91:                thread_cancellation_point();
        !            92:        }
        !            93:        else
        !            94:        {
        !            95:                thread_cancelability(FALSE);
        !            96:        }
        !            97:        return select(nfds, readfds, writefds, errorfds, timeout);
        !            98: }
        !            99: #define select precancellable_select
        !           100: 
        !           101: /*
        !           102:  * The same as to select(2) applies to poll(2)
        !           103:  */
        !           104: static inline int precancellable_poll(struct pollfd fds[], nfds_t nfds,
        !           105:                                                                          int timeout)
        !           106: {
        !           107:        if (thread_cancelability(TRUE))
        !           108:        {
        !           109:                thread_cancellation_point();
        !           110:        }
        !           111:        else
        !           112:        {
        !           113:                thread_cancelability(FALSE);
        !           114:        }
        !           115:        return poll(fds, nfds, timeout);
        !           116: }
        !           117: #define poll precancellable_poll
        !           118: 
        !           119: #endif /** APPLE_H_ @}*/

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>