indent(1): Attempt to preserve some consistent style.
[freebsd-src.git] / lib / libdevdctl / event.h
blob74ee3f7fd8722891bcfe6b768bfd8af9b21c93bb
1 /*-
2 * Copyright (c) 2011, 2012, 2013, 2016 Spectra Logic Corporation
3 * All rights reserved.
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.
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.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
32 * $FreeBSD$
35 /**
36 * \file devdctl_event.h
38 * \brief Class hierarchy used to express events received via
39 * the devdctl API.
42 #ifndef _DEVDCTL_EVENT_H_
43 #define _DEVDCTL_EVENT_H_
45 /*============================ Namespace Control =============================*/
46 namespace DevdCtl
49 /*=========================== Forward Declarations ===========================*/
50 class EventFactory;
52 /*============================= Class Definitions ============================*/
53 /*-------------------------------- NVPairMap ---------------------------------*/
54 /**
55 * NVPairMap is a specialization of the standard map STL container.
57 typedef std::map<std::string, std::string> NVPairMap;
59 /*----------------------------------- Event ----------------------------------*/
60 /**
61 * \brief Container for the name => value pairs that comprise the content of
62 * a device control event.
64 * All name => value data for events can be accessed via the Contains()
65 * and Value() methods. name => value pairs for data not explicitly
66 * received as a name => value pair are synthesized during parsing. For
67 * example, ATTACH and DETACH events have "device-name" and "parent"
68 * name => value pairs added.
70 class Event
72 friend class EventFactory;
74 public:
75 /** Event type */
76 enum Type {
77 /** Generic event notification. */
78 NOTIFY = '!',
80 /** A driver was not found for this device. */
81 NOMATCH = '?',
83 /** A bus device instance has been added. */
84 ATTACH = '+',
86 /** A bus device instance has been removed. */
87 DETACH = '-'
90 /**
91 * Factory method type to construct an Event given
92 * the type of event and an NVPairMap populated from
93 * the event string received from devd.
95 typedef Event* (BuildMethod)(Type, NVPairMap &, const std::string &);
97 /** Generic Event object factory. */
98 static BuildMethod Builder;
100 static Event *CreateEvent(const EventFactory &factory,
101 const std::string &eventString);
104 * Returns the devname, if any, associated with the event
106 * \param name Devname, returned by reference
107 * \return True iff the event contained a devname
109 virtual bool DevName(std::string &name) const;
112 * Returns the absolute pathname of the device associated with this
113 * event.
115 * \param name Devname, returned by reference
116 * \return True iff the event contained a devname
118 bool DevPath(std::string &path) const;
121 * Returns true iff this event refers to a disk device
123 bool IsDiskDev() const;
125 /** Returns the physical path of the device, if any
127 * \param path Physical path, returned by reference
128 * \return True iff the event contains a device with a physical
129 * path
131 bool PhysicalPath(std::string &path) const;
134 * Provide a user friendly string representation of an
135 * event type.
137 * \param type The type of event to map to a string.
139 * \return A user friendly string representing the input type.
141 static const char *TypeToString(Type type);
144 * Determine the availability of a name => value pair by name.
146 * \param name The key name to search for in this event instance.
148 * \return true if the specified key is available in this
149 * event, otherwise false.
151 bool Contains(const std::string &name) const;
154 * \param key The name of the key for which to retrieve its
155 * associated value.
157 * \return A const reference to the string representing the
158 * value associated with key.
160 * \note For key's with no registered value, the empty string
161 * is returned.
163 const std::string &Value(const std::string &key) const;
166 * Get the type of this event instance.
168 * \return The type of this event instance.
170 Type GetType() const;
173 * Get the original DevdCtl event string for this event.
175 * \return The DevdCtl event string.
177 const std::string &GetEventString() const;
180 * Convert the event instance into a string suitable for
181 * printing to the console or emitting to syslog.
183 * \return A string of formatted event data.
185 std::string ToString() const;
188 * Pretty-print this event instance to cout.
190 void Print() const;
193 * Pretty-print this event instance to syslog.
195 * \param priority The logging priority/facility.
196 * See syslog(3).
198 void Log(int priority) const;
201 * Create and return a fully independent clone
202 * of this event.
204 virtual Event *DeepCopy() const;
206 /** Destructor */
207 virtual ~Event();
210 * Interpret and perform any actions necessary to
211 * consume the event.
213 * \return True if this event should be queued for later reevaluation
215 virtual bool Process() const;
218 * Get the time that the event was created
220 timeval GetTimestamp() const;
223 * Add a timestamp to the event string, if one does not already exist
224 * TODO: make this an instance method that operates on the std::map
225 * instead of the string. We must fix zfsd's CaseFile serialization
226 * routines first, so that they don't need the raw event string.
228 * \param[in,out] eventString The devd event string to modify
230 static void TimestampEventString(std::string &eventString);
233 * Access all parsed key => value pairs.
235 const NVPairMap &GetMap() const;
237 protected:
238 /** Table entries used to map a type to a user friendly string. */
239 struct EventTypeRecord
241 Type m_type;
242 const char *m_typeName;
246 * Constructor
248 * \param type The type of event to create.
250 Event(Type type, NVPairMap &map, const std::string &eventString);
252 /** Deep copy constructor. */
253 Event(const Event &src);
255 /** Always empty string returned when NVPairMap lookups fail. */
256 static const std::string s_theEmptyString;
258 /** Unsorted table of event types. */
259 static EventTypeRecord s_typeTable[];
261 /** The type of this event. */
262 const Type m_type;
265 * Event attribute storage.
267 * \note Although stored by reference (since m_nvPairs can
268 * never be NULL), the NVPairMap referenced by this field
269 * is dynamically allocated and owned by this event object.
270 * m_nvPairs must be deleted at event destruction.
272 NVPairMap &m_nvPairs;
275 * The unaltered event string, as received from devd, used to
276 * create this event object.
278 std::string m_eventString;
280 private:
282 * Ingest event data from the supplied string.
284 * \param[in] eventString The string of devd event data to parse.
285 * \param[out] nvpairs Returns the parsed data
287 static void ParseEventString(Type type, const std::string &eventString,
288 NVPairMap &nvpairs);
291 inline Event::Type
292 Event::GetType() const
294 return (m_type);
297 inline const std::string &
298 Event::GetEventString() const
300 return (m_eventString);
303 inline const NVPairMap &
304 Event::GetMap() const
306 return (m_nvPairs);
309 /*--------------------------------- EventList --------------------------------*/
311 * EventList is a specialization of the standard list STL container.
313 typedef std::list<Event *> EventList;
315 /*-------------------------------- DevfsEvent --------------------------------*/
316 class DevfsEvent : public Event
318 public:
319 /** Specialized Event object factory for Devfs events. */
320 static BuildMethod Builder;
322 virtual Event *DeepCopy() const;
325 * Interpret and perform any actions necessary to
326 * consume the event.
327 * \return True if this event should be queued for later reevaluation
329 virtual bool Process() const;
331 bool IsWholeDev() const;
332 virtual bool DevName(std::string &name) const;
334 protected:
336 * Given the device name of a disk, determine if the device
337 * represents the whole device, not just a partition.
339 * \param devName Device name of disk device to test.
341 * \return True if the device name represents the whole device.
342 * Otherwise false.
344 static bool IsWholeDev(const std::string &devName);
346 /** DeepCopy Constructor. */
347 DevfsEvent(const DevfsEvent &src);
349 /** Constructor */
350 DevfsEvent(Type, NVPairMap &, const std::string &);
353 /*--------------------------------- GeomEvent --------------------------------*/
354 class GeomEvent : public Event
356 public:
357 /** Specialized Event object factory for GEOM events. */
358 static BuildMethod Builder;
360 virtual Event *DeepCopy() const;
362 virtual bool DevName(std::string &name) const;
364 const std::string &DeviceName() const;
366 protected:
367 /** Constructor */
368 GeomEvent(Type, NVPairMap &, const std::string &);
370 /** Deep copy constructor. */
371 GeomEvent(const GeomEvent &src);
373 std::string m_devname;
376 /*--------------------------------- ZfsEvent ---------------------------------*/
377 class ZfsEvent : public Event
379 public:
380 /** Specialized Event object factory for ZFS events. */
381 static BuildMethod Builder;
383 virtual Event *DeepCopy() const;
385 virtual bool DevName(std::string &name) const;
387 const std::string &PoolName() const;
388 Guid PoolGUID() const;
389 Guid VdevGUID() const;
391 protected:
392 /** Constructor */
393 ZfsEvent(Type, NVPairMap &, const std::string &);
395 /** Deep copy constructor. */
396 ZfsEvent(const ZfsEvent &src);
398 Guid m_poolGUID;
399 Guid m_vdevGUID;
402 //- ZfsEvent Inline Public Methods --------------------------------------------
403 inline const std::string&
404 ZfsEvent::PoolName() const
406 /* The pool name is reported as the subsystem of ZFS events. */
407 return (Value("subsystem"));
410 inline Guid
411 ZfsEvent::PoolGUID() const
413 return (m_poolGUID);
416 inline Guid
417 ZfsEvent::VdevGUID() const
419 return (m_vdevGUID);
422 } // namespace DevdCtl
423 #endif /*_DEVDCTL_EVENT_H_ */