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)
34 * \file devdctl_event.h
36 * \brief Class hierarchy used to express events received via
40 #ifndef _DEVDCTL_EVENT_H_
41 #define _DEVDCTL_EVENT_H_
43 /*============================ Namespace Control =============================*/
47 /*=========================== Forward Declarations ===========================*/
50 /*============================= Class Definitions ============================*/
51 /*-------------------------------- NVPairMap ---------------------------------*/
53 * NVPairMap is a specialization of the standard map STL container.
55 typedef std::map<std::string, std::string> NVPairMap;
57 /*----------------------------------- Event ----------------------------------*/
59 * \brief Container for the name => value pairs that comprise the content of
60 * a device control event.
62 * All name => value data for events can be accessed via the Contains()
63 * and Value() methods. name => value pairs for data not explicitly
64 * received as a name => value pair are synthesized during parsing. For
65 * example, ATTACH and DETACH events have "device-name" and "parent"
66 * name => value pairs added.
70 friend class EventFactory;
75 /** Generic event notification. */
78 /** A driver was not found for this device. */
81 /** A bus device instance has been added. */
84 /** A bus device instance has been removed. */
89 * Factory method type to construct an Event given
90 * the type of event and an NVPairMap populated from
91 * the event string received from devd.
93 typedef Event* (BuildMethod)(Type, NVPairMap &, const std::string &);
95 /** Generic Event object factory. */
96 static BuildMethod Builder;
98 static Event *CreateEvent(const EventFactory &factory,
99 const std::string &eventString);
102 * Returns the devname, if any, associated with the event
104 * \param name Devname, returned by reference
105 * \return True iff the event contained a devname
107 virtual bool DevName(std::string &name) const;
110 * Returns the absolute pathname of the device associated with this
113 * \param name Devname, returned by reference
114 * \return True iff the event contained a devname
116 bool DevPath(std::string &path) const;
119 * Returns true iff this event refers to a disk device
121 bool IsDiskDev() const;
123 /** Returns the physical path of the device, if any
125 * \param path Physical path, returned by reference
126 * \return True iff the event contains a device with a physical
129 bool PhysicalPath(std::string &path) const;
132 * Provide a user friendly string representation of an
135 * \param type The type of event to map to a string.
137 * \return A user friendly string representing the input type.
139 static const char *TypeToString(Type type);
142 * Determine the availability of a name => value pair by name.
144 * \param name The key name to search for in this event instance.
146 * \return true if the specified key is available in this
147 * event, otherwise false.
149 bool Contains(const std::string &name) const;
152 * \param key The name of the key for which to retrieve its
155 * \return A const reference to the string representing the
156 * value associated with key.
158 * \note For key's with no registered value, the empty string
161 const std::string &Value(const std::string &key) const;
164 * Get the type of this event instance.
166 * \return The type of this event instance.
168 Type GetType() const;
171 * Get the original DevdCtl event string for this event.
173 * \return The DevdCtl event string.
175 const std::string &GetEventString() const;
178 * Convert the event instance into a string suitable for
179 * printing to the console or emitting to syslog.
181 * \return A string of formatted event data.
183 std::string ToString() const;
186 * Pretty-print this event instance to cout.
191 * Pretty-print this event instance to syslog.
193 * \param priority The logging priority/facility.
196 void Log(int priority) const;
199 * Create and return a fully independent clone
202 virtual Event *DeepCopy() const;
208 * Interpret and perform any actions necessary to
211 * \return True if this event should be queued for later reevaluation
213 virtual bool Process() const;
216 * Get the time that the event was created
218 timeval GetTimestamp() const;
221 * Add a timestamp to the event string, if one does not already exist
222 * TODO: make this an instance method that operates on the std::map
223 * instead of the string. We must fix zfsd's CaseFile serialization
224 * routines first, so that they don't need the raw event string.
226 * \param[in,out] eventString The devd event string to modify
228 static void TimestampEventString(std::string &eventString);
231 * Access all parsed key => value pairs.
233 const NVPairMap &GetMap() const;
236 /** Table entries used to map a type to a user friendly string. */
237 struct EventTypeRecord
240 const char *m_typeName;
246 * \param type The type of event to create.
248 Event(Type type, NVPairMap &map, const std::string &eventString);
250 /** Deep copy constructor. */
251 Event(const Event &src);
253 /** Always empty string returned when NVPairMap lookups fail. */
254 static const std::string s_theEmptyString;
256 /** Unsorted table of event types. */
257 static EventTypeRecord s_typeTable[];
259 /** The type of this event. */
263 * Event attribute storage.
265 * \note Although stored by reference (since m_nvPairs can
266 * never be NULL), the NVPairMap referenced by this field
267 * is dynamically allocated and owned by this event object.
268 * m_nvPairs must be deleted at event destruction.
270 NVPairMap &m_nvPairs;
273 * The unaltered event string, as received from devd, used to
274 * create this event object.
276 std::string m_eventString;
280 * Ingest event data from the supplied string.
282 * \param[in] eventString The string of devd event data to parse.
283 * \param[out] nvpairs Returns the parsed data
285 static void ParseEventString(Type type, const std::string &eventString,
290 Event::GetType() const
295 inline const std::string &
296 Event::GetEventString() const
298 return (m_eventString);
301 inline const NVPairMap &
302 Event::GetMap() const
307 /*--------------------------------- EventList --------------------------------*/
309 * EventList is a specialization of the standard list STL container.
311 typedef std::list<Event *> EventList;
313 /*-------------------------------- DevfsEvent --------------------------------*/
314 class DevfsEvent : public Event
317 /** Specialized Event object factory for Devfs events. */
318 static BuildMethod Builder;
320 virtual Event *DeepCopy() const;
323 * Interpret and perform any actions necessary to
325 * \return True if this event should be queued for later reevaluation
327 virtual bool Process() const;
329 bool IsWholeDev() const;
330 virtual bool DevName(std::string &name) const;
334 * Given the device name of a disk, determine if the device
335 * represents the whole device, not just a partition.
337 * \param devName Device name of disk device to test.
339 * \return True if the device name represents the whole device.
342 static bool IsWholeDev(const std::string &devName);
344 /** DeepCopy Constructor. */
345 DevfsEvent(const DevfsEvent &src);
348 DevfsEvent(Type, NVPairMap &, const std::string &);
351 /*--------------------------------- GeomEvent --------------------------------*/
352 class GeomEvent : public Event
355 /** Specialized Event object factory for GEOM events. */
356 static BuildMethod Builder;
358 virtual Event *DeepCopy() const;
360 virtual bool DevName(std::string &name) const;
362 const std::string &DeviceName() const;
366 GeomEvent(Type, NVPairMap &, const std::string &);
368 /** Deep copy constructor. */
369 GeomEvent(const GeomEvent &src);
371 std::string m_devname;
374 /*--------------------------------- ZfsEvent ---------------------------------*/
375 class ZfsEvent : public Event
378 /** Specialized Event object factory for ZFS events. */
379 static BuildMethod Builder;
381 virtual Event *DeepCopy() const;
383 virtual bool DevName(std::string &name) const;
385 const std::string &PoolName() const;
386 Guid PoolGUID() const;
387 Guid VdevGUID() const;
391 ZfsEvent(Type, NVPairMap &, const std::string &);
393 /** Deep copy constructor. */
394 ZfsEvent(const ZfsEvent &src);
400 //- ZfsEvent Inline Public Methods --------------------------------------------
401 inline const std::string&
402 ZfsEvent::PoolName() const
404 /* The pool name is reported as the subsystem of ZFS events. */
405 return (Value("subsystem"));
409 ZfsEvent::PoolGUID() const
415 ZfsEvent::VdevGUID() const
420 } // namespace DevdCtl
421 #endif /*_DEVDCTL_EVENT_H_ */