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