2 * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
37 #include <sys/cdefs.h>
39 #include <sys/socket.h>
56 #include "event_factory.h"
57 #include "exception.h"
60 /*================================== Macros ==================================*/
61 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
63 /*============================ Namespace Control =============================*/
68 /*============================= Class Definitions ============================*/
69 /*----------------------------- DevdCtl::Consumer ----------------------------*/
70 //- Consumer Static Private Data -----------------------------------------------
71 const char Consumer::s_devdSockPath[] = "/var/run/devd.seqpacket.pipe";
73 //- Consumer Public Methods ----------------------------------------------------
74 Consumer::Consumer(Event::BuildMethod *defBuilder,
75 EventFactory::Record *regEntries,
78 m_eventFactory(defBuilder),
79 m_replayingEvents(false)
81 m_eventFactory.UpdateRegistry(regEntries, numEntries);
90 Consumer::ConnectToDevd()
92 struct sockaddr_un devdAddr;
96 if (m_devdSockFD != -1) {
97 /* Already connected. */
98 syslog(LOG_DEBUG, "%s: Already connected.", __func__);
101 syslog(LOG_INFO, "%s: Connecting to devd.", __func__);
103 memset(&devdAddr, 0, sizeof(devdAddr));
104 devdAddr.sun_family= AF_UNIX;
105 strlcpy(devdAddr.sun_path, s_devdSockPath, sizeof(devdAddr.sun_path));
106 sLen = SUN_LEN(&devdAddr);
108 m_devdSockFD = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);
109 if (m_devdSockFD == -1)
110 err(1, "Unable to create socket");
111 result = connect(m_devdSockFD,
112 reinterpret_cast<sockaddr *>(&devdAddr),
115 syslog(LOG_INFO, "Unable to connect to devd");
116 DisconnectFromDevd();
120 syslog(LOG_INFO, "Connection to devd successful");
125 Consumer::DisconnectFromDevd()
127 if (m_devdSockFD != -1) {
128 syslog(LOG_INFO, "Disconnecting from devd.");
135 Consumer::ReadEvent()
137 char buf[MAX_EVENT_SIZE + 1];
140 len = ::recv(m_devdSockFD, buf, MAX_EVENT_SIZE, MSG_WAITALL);
142 return (std::string(""));
144 /* NULL-terminate the result */
146 return (std::string(buf));
151 Consumer::ReplayUnconsumedEvents(bool discardUnconsumed)
153 EventList::iterator event(m_unconsumedEvents.begin());
154 bool replayed_any = (event != m_unconsumedEvents.end());
156 m_replayingEvents = true;
158 syslog(LOG_INFO, "Started replaying unconsumed events");
159 while (event != m_unconsumedEvents.end()) {
160 bool consumed((*event)->Process());
161 if (consumed || discardUnconsumed) {
163 event = m_unconsumedEvents.erase(event);
169 syslog(LOG_INFO, "Finished replaying unconsumed events");
170 m_replayingEvents = false;
174 Consumer::SaveEvent(const Event &event)
176 if (m_replayingEvents)
178 m_unconsumedEvents.push_back(event.DeepCopy());
183 Consumer::NextEvent()
192 evString = ReadEvent();
193 if (! evString.empty()) {
194 Event::TimestampEventString(evString);
195 event = Event::CreateEvent(m_eventFactory, evString);
197 } catch (const Exception &exp) {
199 DisconnectFromDevd();
204 /* Capture and process buffered events. */
206 Consumer::ProcessEvents()
209 while ((event = NextEvent()) != NULL) {
210 if (event->Process())
217 Consumer::FlushEvents()
223 while (! s.empty()) ;
227 Consumer::EventsPending()
229 struct pollfd fds[1];
233 fds->fd = m_devdSockFD;
234 fds->events = POLLIN;
236 result = poll(fds, NUM_ELEMENTS(fds), /*timeout*/0);
237 } while (result == -1 && errno == EINTR);
240 err(1, "Polling for devd events failed");
242 if ((fds->revents & POLLERR) != 0)
243 throw Exception("Consumer::EventsPending(): "
244 "POLLERR detected on devd socket.");
246 if ((fds->revents & POLLHUP) != 0)
247 throw Exception("Consumer::EventsPending(): "
248 "POLLHUP detected on devd socket.");
250 return ((fds->revents & POLLIN) != 0);
253 } // namespace DevdCtl