1/* 2 * Copyright (c) 2009 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25// 26// kq++ - kqueue/kevent interface 27// 28#ifndef _H_KQPP 29#define _H_KQPP 30 31#include <security_utilities/unix++.h> 32#include <sys/event.h> 33 34namespace Security { 35namespace UnixPlusPlus { 36 37 38class KEvent; 39 40 41class KQueue { 42public: 43 KQueue(); 44 virtual ~KQueue(); 45 46 unsigned operator () (const KEvent *updates, unsigned updateCount, KEvent *events, unsigned eventCount, 47 const timespec *timeout = NULL); 48 unsigned operator () (KEvent *events, unsigned eventCount, const timespec *timeout = NULL) 49 { return operator () (NULL, NULL, events, eventCount, timeout); } 50 51 void update(const KEvent &event, unsigned flags = EV_ADD); 52 bool receive(KEvent &event, const timespec *timeout = NULL); 53 54private: 55 int mQueue; 56}; 57 58 59class KEvent : public PodWrapper<KEvent, kevent64_s> { 60public: 61 KEvent() { clearPod(); } 62 KEvent(int16_t filt) { clearPod(); this->filter = filt; } 63 KEvent(int16_t filt, uint64_t id, uint32_t ffl = 0) 64// { clearPod(); this->ident = id; this->filter = filt; this->fflags = ffl; } 65 { EV_SET64(this, id, filt, 0, ffl, 0, 0, 0, 0); } 66 67 void addTo(KQueue &kq, unsigned flags = 0) 68 { this->flags = EV_ADD | flags; kq.update(*this); } 69 void removeFrom(KQueue &kq, unsigned flags = 0) 70 { this->flags = EV_DELETE | flags; kq.update(*this); } 71 void enable(KQueue &kq, unsigned flags = 0) 72 { this->flags = EV_ENABLE | flags; kq.update(*this); } 73 void disable(KQueue &kq, unsigned flags = 0) 74 { this->flags = EV_DISABLE | flags; kq.update(*this); } 75}; 76 77 78namespace Event { 79 80 81class Vnode : public KEvent { 82public: 83 Vnode() : KEvent(EVFILT_VNODE) { } 84 Vnode(int fd, uint32_t flags) : KEvent(EVFILT_VNODE, fd, flags) { } 85 86 int fd() const { return (int)this->ident; } 87}; 88 89} // namespace Event 90 91 92} // end namespace UnixPlusPlus 93} // end namespace Security 94 95#endif //_H_KQPP 96