1*7a0c41d5SAlan Somers /*- 2*7a0c41d5SAlan Somers * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation 3*7a0c41d5SAlan Somers * All rights reserved. 4*7a0c41d5SAlan Somers * 5*7a0c41d5SAlan Somers * Redistribution and use in source and binary forms, with or without 6*7a0c41d5SAlan Somers * modification, are permitted provided that the following conditions 7*7a0c41d5SAlan Somers * are met: 8*7a0c41d5SAlan Somers * 1. Redistributions of source code must retain the above copyright 9*7a0c41d5SAlan Somers * notice, this list of conditions, and the following disclaimer, 10*7a0c41d5SAlan Somers * without modification. 11*7a0c41d5SAlan Somers * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12*7a0c41d5SAlan Somers * substantially similar to the "NO WARRANTY" disclaimer below 13*7a0c41d5SAlan Somers * ("Disclaimer") and any redistribution must be conditioned upon 14*7a0c41d5SAlan Somers * including a substantially similar Disclaimer requirement for further 15*7a0c41d5SAlan Somers * binary redistribution. 16*7a0c41d5SAlan Somers * 17*7a0c41d5SAlan Somers * NO WARRANTY 18*7a0c41d5SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19*7a0c41d5SAlan Somers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20*7a0c41d5SAlan Somers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21*7a0c41d5SAlan Somers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22*7a0c41d5SAlan Somers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*7a0c41d5SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*7a0c41d5SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*7a0c41d5SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26*7a0c41d5SAlan Somers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27*7a0c41d5SAlan Somers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*7a0c41d5SAlan Somers * POSSIBILITY OF SUCH DAMAGES. 29*7a0c41d5SAlan Somers * 30*7a0c41d5SAlan Somers * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31*7a0c41d5SAlan Somers * 32*7a0c41d5SAlan Somers * $FreeBSD$ 33*7a0c41d5SAlan Somers */ 34*7a0c41d5SAlan Somers 35*7a0c41d5SAlan Somers /** 36*7a0c41d5SAlan Somers * \file devdctl_consumer.h 37*7a0c41d5SAlan Somers */ 38*7a0c41d5SAlan Somers #ifndef _DEVDCTL_CONSUMER_H_ 39*7a0c41d5SAlan Somers #define _DEVDCTL_CONSUMER_H_ 40*7a0c41d5SAlan Somers 41*7a0c41d5SAlan Somers /*============================ Namespace Control =============================*/ 42*7a0c41d5SAlan Somers namespace DevdCtl 43*7a0c41d5SAlan Somers { 44*7a0c41d5SAlan Somers 45*7a0c41d5SAlan Somers /*=========================== Forward Declarations ===========================*/ 46*7a0c41d5SAlan Somers class Event; 47*7a0c41d5SAlan Somers class EventBuffer; 48*7a0c41d5SAlan Somers class FDReader; 49*7a0c41d5SAlan Somers 50*7a0c41d5SAlan Somers /*============================ Class Declarations ============================*/ 51*7a0c41d5SAlan Somers /*----------------------------- DevdCtl::Consumer ----------------------------*/ 52*7a0c41d5SAlan Somers 53*7a0c41d5SAlan Somers /** 54*7a0c41d5SAlan Somers */ 55*7a0c41d5SAlan Somers class Consumer 56*7a0c41d5SAlan Somers { 57*7a0c41d5SAlan Somers public: 58*7a0c41d5SAlan Somers Consumer(Event::BuildMethod *defBuilder = NULL, 59*7a0c41d5SAlan Somers EventFactory::Record *regEntries = NULL, 60*7a0c41d5SAlan Somers size_t numEntries = 0); 61*7a0c41d5SAlan Somers virtual ~Consumer(); 62*7a0c41d5SAlan Somers 63*7a0c41d5SAlan Somers bool Connected() const; 64*7a0c41d5SAlan Somers 65*7a0c41d5SAlan Somers /** 66*7a0c41d5SAlan Somers * Return file descriptor useful for client's wishing to poll(2) 67*7a0c41d5SAlan Somers * for new events. 68*7a0c41d5SAlan Somers */ 69*7a0c41d5SAlan Somers int GetPollFd(); 70*7a0c41d5SAlan Somers 71*7a0c41d5SAlan Somers /** 72*7a0c41d5SAlan Somers * Queue an event for deferred processing or replay. 73*7a0c41d5SAlan Somers */ 74*7a0c41d5SAlan Somers bool SaveEvent(const Event &event); 75*7a0c41d5SAlan Somers 76*7a0c41d5SAlan Somers /** 77*7a0c41d5SAlan Somers * Reprocess any events saved via the SaveEvent() facility. 78*7a0c41d5SAlan Somers * 79*7a0c41d5SAlan Somers * \param discardUnconsumed If true, events that are not consumed 80*7a0c41d5SAlan Somers * during replay are discarded. 81*7a0c41d5SAlan Somers */ 82*7a0c41d5SAlan Somers void ReplayUnconsumedEvents(bool discardUnconsumed); 83*7a0c41d5SAlan Somers 84*7a0c41d5SAlan Somers /** Return an event, if one is available. */ 85*7a0c41d5SAlan Somers Event *NextEvent(); 86*7a0c41d5SAlan Somers 87*7a0c41d5SAlan Somers /** 88*7a0c41d5SAlan Somers * Extract events and invoke each event's Process method. 89*7a0c41d5SAlan Somers */ 90*7a0c41d5SAlan Somers void ProcessEvents(); 91*7a0c41d5SAlan Somers 92*7a0c41d5SAlan Somers /** Discard all data pending in m_devdSockFD. */ 93*7a0c41d5SAlan Somers void FlushEvents(); 94*7a0c41d5SAlan Somers 95*7a0c41d5SAlan Somers /** 96*7a0c41d5SAlan Somers * Test for data pending in m_devdSockFD 97*7a0c41d5SAlan Somers * 98*7a0c41d5SAlan Somers * \return True if data is pending. Otherwise false. 99*7a0c41d5SAlan Somers */ 100*7a0c41d5SAlan Somers bool EventsPending(); 101*7a0c41d5SAlan Somers 102*7a0c41d5SAlan Somers /** 103*7a0c41d5SAlan Somers * Open a connection to devd's unix domain socket. 104*7a0c41d5SAlan Somers * 105*7a0c41d5SAlan Somers * \return True if the connection attempt is successsful. Otherwise 106*7a0c41d5SAlan Somers * false. 107*7a0c41d5SAlan Somers */ 108*7a0c41d5SAlan Somers bool ConnectToDevd(); 109*7a0c41d5SAlan Somers 110*7a0c41d5SAlan Somers /** 111*7a0c41d5SAlan Somers * Close a connection (if any) to devd's unix domain socket. 112*7a0c41d5SAlan Somers */ 113*7a0c41d5SAlan Somers void DisconnectFromDevd(); 114*7a0c41d5SAlan Somers 115*7a0c41d5SAlan Somers EventFactory GetFactory(); 116*7a0c41d5SAlan Somers 117*7a0c41d5SAlan Somers protected: 118*7a0c41d5SAlan Somers /** 119*7a0c41d5SAlan Somers * \brief Reads the most recent record 120*7a0c41d5SAlan Somers * 121*7a0c41d5SAlan Somers * On error, "" is returned, and errno will be set by the OS 122*7a0c41d5SAlan Somers * 123*7a0c41d5SAlan Somers * \returns A string containing the record 124*7a0c41d5SAlan Somers */ 125*7a0c41d5SAlan Somers std::string ReadEvent(); 126*7a0c41d5SAlan Somers 127*7a0c41d5SAlan Somers enum { 128*7a0c41d5SAlan Somers /* 129*7a0c41d5SAlan Somers * The maximum event size supported by libdevdctl. 130*7a0c41d5SAlan Somers */ 131*7a0c41d5SAlan Somers MAX_EVENT_SIZE = 8192, 132*7a0c41d5SAlan Somers }; 133*7a0c41d5SAlan Somers 134*7a0c41d5SAlan Somers static const char s_devdSockPath[]; 135*7a0c41d5SAlan Somers 136*7a0c41d5SAlan Somers /** 137*7a0c41d5SAlan Somers * File descriptor representing the unix domain socket 138*7a0c41d5SAlan Somers * connection with devd. 139*7a0c41d5SAlan Somers */ 140*7a0c41d5SAlan Somers int m_devdSockFD; 141*7a0c41d5SAlan Somers 142*7a0c41d5SAlan Somers /** 143*7a0c41d5SAlan Somers * Reader tied to the devd socket. 144*7a0c41d5SAlan Somers */ 145*7a0c41d5SAlan Somers FDReader *m_reader; 146*7a0c41d5SAlan Somers 147*7a0c41d5SAlan Somers /** 148*7a0c41d5SAlan Somers * Default EventBuffer connected to m_reader. 149*7a0c41d5SAlan Somers */ 150*7a0c41d5SAlan Somers EventBuffer *m_eventBuffer; 151*7a0c41d5SAlan Somers 152*7a0c41d5SAlan Somers EventFactory m_eventFactory; 153*7a0c41d5SAlan Somers 154*7a0c41d5SAlan Somers /** Queued events for replay. */ 155*7a0c41d5SAlan Somers EventList m_unconsumedEvents; 156*7a0c41d5SAlan Somers 157*7a0c41d5SAlan Somers /** 158*7a0c41d5SAlan Somers * Flag controlling whether events can be queued. This boolean 159*7a0c41d5SAlan Somers * is set during event replay to ensure that previosuly deferred 160*7a0c41d5SAlan Somers * events are not requeued and thus retained forever. 161*7a0c41d5SAlan Somers */ 162*7a0c41d5SAlan Somers bool m_replayingEvents; 163*7a0c41d5SAlan Somers }; 164*7a0c41d5SAlan Somers 165*7a0c41d5SAlan Somers //- Consumer Const Public Inline Methods --------------------------------------- 166*7a0c41d5SAlan Somers inline bool 167*7a0c41d5SAlan Somers Consumer::Connected() const 168*7a0c41d5SAlan Somers { 169*7a0c41d5SAlan Somers return (m_devdSockFD != -1); 170*7a0c41d5SAlan Somers } 171*7a0c41d5SAlan Somers 172*7a0c41d5SAlan Somers //- Consumer Public Inline Methods --------------------------------------------- 173*7a0c41d5SAlan Somers inline int 174*7a0c41d5SAlan Somers Consumer::GetPollFd() 175*7a0c41d5SAlan Somers { 176*7a0c41d5SAlan Somers return (m_devdSockFD); 177*7a0c41d5SAlan Somers } 178*7a0c41d5SAlan Somers 179*7a0c41d5SAlan Somers inline EventFactory 180*7a0c41d5SAlan Somers Consumer::GetFactory() 181*7a0c41d5SAlan Somers { 182*7a0c41d5SAlan Somers return (m_eventFactory); 183*7a0c41d5SAlan Somers } 184*7a0c41d5SAlan Somers 185*7a0c41d5SAlan Somers } // namespace DevdCtl 186*7a0c41d5SAlan Somers #endif /* _DEVDCTL_CONSUMER_H_ */ 187