2 * Copyright (c) 2011, 2012, 2013, 2016 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)
36 * Implementation of the class hierarchy used to express events
37 * received via the devdctl API.
39 #include <sys/cdefs.h>
41 #include <sys/filio.h>
42 #include <sys/param.h>
63 #include "event_factory.h"
64 #include "exception.h"
65 /*================================== Macros ==================================*/
66 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
68 /*============================ Namespace Control =============================*/
72 using std::stringstream;
77 /*=========================== Class Implementations ==========================*/
78 /*----------------------------------- Event ----------------------------------*/
79 //- Event Static Protected Data ------------------------------------------------
80 const string Event::s_theEmptyString;
82 Event::EventTypeRecord Event::s_typeTable[] =
84 { Event::NOTIFY, "Notify" },
85 { Event::NOMATCH, "No Driver Match" },
86 { Event::ATTACH, "Attach" },
87 { Event::DETACH, "Detach" }
90 //- Event Static Public Methods ------------------------------------------------
92 Event::Builder(Event::Type type, NVPairMap &nvPairs,
93 const string &eventString)
95 return (new Event(type, nvPairs, eventString));
99 Event::CreateEvent(const EventFactory &factory, const string &eventString)
101 NVPairMap &nvpairs(*new NVPairMap);
102 Type type(static_cast<Event::Type>(eventString[0]));
105 ParseEventString(type, eventString, nvpairs);
106 } catch (const ParseException &exp) {
107 if (exp.GetType() == ParseException::INVALID_FORMAT)
113 * Allow entries in our table for events with no system specified.
114 * These entries should specify the string "none".
116 NVPairMap::iterator system_item(nvpairs.find("system"));
117 if (system_item == nvpairs.end())
118 nvpairs["system"] = "none";
120 return (factory.Build(type, nvpairs, eventString));
124 Event::DevName(std::string &name) const
129 /* TODO: simplify this function with C++-11 <regex> methods */
131 Event::IsDiskDev() const
133 const int numDrivers = 2;
134 static const char *diskDevNames[numDrivers] =
142 if (! DevName(devName))
145 size_t find_start = devName.rfind('/');
146 if (find_start == string::npos) {
149 /* Just after the last '/'. */
153 for (dName = &diskDevNames[0];
154 dName <= &diskDevNames[numDrivers - 1]; dName++) {
156 size_t loc(devName.find(*dName, find_start));
157 if (loc == find_start) {
158 size_t prefixLen(strlen(*dName));
160 if (devName.length() - find_start >= prefixLen
161 && isdigit(devName[find_start + prefixLen]))
170 Event::TypeToString(Event::Type type)
172 EventTypeRecord *rec(s_typeTable);
173 EventTypeRecord *lastRec(s_typeTable + NUM_ELEMENTS(s_typeTable) - 1);
175 for (; rec <= lastRec; rec++) {
176 if (rec->m_type == type)
177 return (rec->m_typeName);
182 //- Event Public Methods -------------------------------------------------------
184 Event::Value(const string &varName) const
186 NVPairMap::const_iterator item(m_nvPairs.find(varName));
187 if (item == m_nvPairs.end())
188 return (s_theEmptyString);
190 return (item->second);
194 Event::Contains(const string &varName) const
196 return (m_nvPairs.find(varName) != m_nvPairs.end());
200 Event::ToString() const
204 NVPairMap::const_iterator devName(m_nvPairs.find("device-name"));
205 if (devName != m_nvPairs.end())
206 result << devName->second << ": ";
208 NVPairMap::const_iterator systemName(m_nvPairs.find("system"));
209 if (systemName != m_nvPairs.end()
210 && systemName->second != "none")
211 result << systemName->second << ": ";
213 result << TypeToString(GetType()) << ' ';
215 for (NVPairMap::const_iterator curVar = m_nvPairs.begin();
216 curVar != m_nvPairs.end(); curVar++) {
217 if (curVar == devName || curVar == systemName)
221 << curVar->first << "=" << curVar->second;
225 return (result.str());
231 cout << ToString() << std::flush;
235 Event::Log(int priority) const
237 syslog(priority, "%s", ToString().c_str());
240 //- Event Virtual Public Methods -----------------------------------------------
247 Event::DeepCopy() const
249 return (new Event(*this));
253 Event::Process() const
259 Event::GetTimestamp() const
261 timeval tv_timestamp;
262 struct tm tm_timestamp;
264 if (!Contains("timestamp")) {
265 throw Exception("Event contains no timestamp: %s",
266 m_eventString.c_str());
268 strptime(Value(string("timestamp")).c_str(), "%s", &tm_timestamp);
269 tv_timestamp.tv_sec = mktime(&tm_timestamp);
270 tv_timestamp.tv_usec = 0;
271 return (tv_timestamp);
275 Event::DevPath(std::string &path) const
277 char buf[SPECNAMELEN + 1];
280 if (!DevName(devName))
283 string devPath(_PATH_DEV + devName);
284 int devFd(open(devPath.c_str(), O_RDONLY));
288 /* Normalize the device name in case the DEVFS event is for a link. */
289 if (fdevname_r(devFd, buf, sizeof(buf)) == NULL) {
294 path = _PATH_DEV + devName;
302 Event::PhysicalPath(std::string &path) const
306 if (!DevPath(devPath))
309 int devFd(open(devPath.c_str(), O_RDONLY));
313 char physPath[MAXPATHLEN];
315 bool result(ioctl(devFd, DIOCGPHYSPATH, physPath) == 0);
322 //- Event Protected Methods ----------------------------------------------------
323 Event::Event(Type type, NVPairMap &map, const string &eventString)
326 m_eventString(eventString)
330 Event::Event(const Event &src)
331 : m_type(src.m_type),
332 m_nvPairs(*new NVPairMap(src.m_nvPairs)),
333 m_eventString(src.m_eventString)
338 Event::ParseEventString(Event::Type type,
339 const string &eventString,
350 * <type><device-name><unit> <pnpvars> \
351 * at <location vars> <pnpvars> \
354 * Handle all data that doesn't conform to the
355 * "name=value" format, and let the generic parser
356 * below handle the rest.
358 * Type is a single char. Skip it.
361 end = eventString.find_first_of(" \t\n", start);
362 if (end == string::npos)
363 throw ParseException(ParseException::INVALID_FORMAT,
366 nvpairs["device-name"] = eventString.substr(start, end - start);
368 start = eventString.find(" on ", end);
369 if (end == string::npos)
370 throw ParseException(ParseException::INVALID_FORMAT,
373 end = eventString.find_first_of(" \t\n", start);
374 nvpairs["parent"] = eventString.substr(start, end);
379 throw ParseException(ParseException::DISCARDED_EVENT_TYPE,
382 throw ParseException(ParseException::UNKNOWN_EVENT_TYPE,
386 /* Process common "key=value" format. */
387 for (start = 1; start < eventString.length(); start = end + 1) {
389 /* Find the '=' in the middle of the key/value pair. */
390 end = eventString.find('=', start);
391 if (end == string::npos)
395 * Find the start of the key by backing up until
396 * we hit whitespace or '!' (event type "notice").
397 * Due to the devdctl format, all key/value pair must
398 * start with one of these two characters.
400 start = eventString.find_last_of("! \t\n", end);
401 if (start == string::npos)
402 throw ParseException(ParseException::INVALID_FORMAT,
405 string key(eventString.substr(start, end - start));
408 * Walk forward from the '=' until either we exhaust
409 * the buffer or we hit whitespace.
412 if (start >= eventString.length())
413 throw ParseException(ParseException::INVALID_FORMAT,
415 end = eventString.find_first_of(" \t\n", start);
416 if (end == string::npos)
417 end = eventString.length() - 1;
418 string value(eventString.substr(start, end - start));
420 nvpairs[key] = value;
425 Event::TimestampEventString(std::string &eventString)
427 if (eventString.size() > 0) {
429 * Add a timestamp as the final field of the event if it is
430 * not already present.
432 if (eventString.find(" timestamp=") == string::npos) {
433 const size_t bufsize = 32; // Long enough for a 64-bit int
435 char timebuf[bufsize];
437 size_t eventEnd(eventString.find_last_not_of('\n') + 1);
438 if (gettimeofday(&now, NULL) != 0)
439 err(1, "gettimeofday");
440 snprintf(timebuf, bufsize, " timestamp=%" PRId64,
441 (int64_t) now.tv_sec);
442 eventString.insert(eventEnd, timebuf);
447 /*-------------------------------- DevfsEvent --------------------------------*/
448 //- DevfsEvent Static Public Methods -------------------------------------------
450 DevfsEvent::Builder(Event::Type type, NVPairMap &nvPairs,
451 const string &eventString)
453 return (new DevfsEvent(type, nvPairs, eventString));
456 //- DevfsEvent Static Protected Methods ----------------------------------------
458 DevfsEvent::IsWholeDev(const string &devName)
460 string::const_iterator i(devName.begin());
462 size_t start = devName.rfind('/');
463 if (start == string::npos) {
466 /* Just after the last '/'. */
471 /* alpha prefix followed only by digits. */
472 for (; i < devName.end() && !isdigit(*i); i++)
475 if (i == devName.end())
478 for (; i < devName.end() && isdigit(*i); i++)
481 return (i == devName.end());
484 //- DevfsEvent Virtual Public Methods ------------------------------------------
486 DevfsEvent::DeepCopy() const
488 return (new DevfsEvent(*this));
492 DevfsEvent::Process() const
497 //- DevfsEvent Public Methods --------------------------------------------------
499 DevfsEvent::IsWholeDev() const
503 return (DevName(devName) && IsDiskDev() && IsWholeDev(devName));
507 DevfsEvent::DevName(std::string &name) const
509 if (Value("subsystem") != "CDEV")
512 name = Value("cdev");
513 return (!name.empty());
516 //- DevfsEvent Protected Methods -----------------------------------------------
517 DevfsEvent::DevfsEvent(Event::Type type, NVPairMap &nvpairs,
518 const string &eventString)
519 : Event(type, nvpairs, eventString)
523 DevfsEvent::DevfsEvent(const DevfsEvent &src)
528 /*--------------------------------- GeomEvent --------------------------------*/
529 //- GeomEvent Static Public Methods --------------------------------------------
531 GeomEvent::Builder(Event::Type type, NVPairMap &nvpairs,
532 const string &eventString)
534 return (new GeomEvent(type, nvpairs, eventString));
537 //- GeomEvent Virtual Public Methods -------------------------------------------
539 GeomEvent::DeepCopy() const
541 return (new GeomEvent(*this));
545 GeomEvent::DevName(std::string &name) const
547 if (Value("subsystem") == "disk")
548 name = Value("devname");
550 name = Value("cdev");
551 return (!name.empty());
555 //- GeomEvent Protected Methods ------------------------------------------------
556 GeomEvent::GeomEvent(Event::Type type, NVPairMap &nvpairs,
557 const string &eventString)
558 : Event(type, nvpairs, eventString),
559 m_devname(Value("devname"))
563 GeomEvent::GeomEvent(const GeomEvent &src)
565 m_devname(src.m_devname)
569 /*--------------------------------- ZfsEvent ---------------------------------*/
570 //- ZfsEvent Static Public Methods ---------------------------------------------
572 ZfsEvent::Builder(Event::Type type, NVPairMap &nvpairs,
573 const string &eventString)
575 return (new ZfsEvent(type, nvpairs, eventString));
578 //- ZfsEvent Virtual Public Methods --------------------------------------------
580 ZfsEvent::DeepCopy() const
582 return (new ZfsEvent(*this));
586 ZfsEvent::DevName(std::string &name) const
591 //- ZfsEvent Protected Methods -------------------------------------------------
592 ZfsEvent::ZfsEvent(Event::Type type, NVPairMap &nvpairs,
593 const string &eventString)
594 : Event(type, nvpairs, eventString),
595 m_poolGUID(Guid(Value("pool_guid"))),
596 m_vdevGUID(Guid(Value("vdev_guid")))
600 ZfsEvent::ZfsEvent(const ZfsEvent &src)
602 m_poolGUID(src.m_poolGUID),
603 m_vdevGUID(src.m_vdevGUID)
607 } // namespace DevdCtl