Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*-
 * Copyright (c) 2011, 2012, 2013, 2016 Spectra Logic Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
 *
 * $FreeBSD$
 */

/**
 * \file devdctl_event.h
 *
 * \brief Class hierarchy used to express events received via
 *        the devdctl API.
 */

#ifndef _DEVDCTL_EVENT_H_
#define	_DEVDCTL_EVENT_H_

/*============================ Namespace Control =============================*/
namespace DevdCtl
{

/*=========================== Forward Declarations ===========================*/
class EventFactory;

/*============================= Class Definitions ============================*/
/*-------------------------------- NVPairMap ---------------------------------*/
/**
 * NVPairMap is a specialization of the standard map STL container.
 */
typedef std::map<std::string, std::string> NVPairMap;

/*----------------------------------- Event ----------------------------------*/
/**
 * \brief Container for the name => value pairs that comprise the content of
 *        a device control event.
 *
 * All name => value data for events can be accessed via the Contains()
 * and Value() methods.  name => value pairs for data not explicitly
 * received as a name => value pair are synthesized during parsing.  For
 * example, ATTACH and DETACH events have "device-name" and "parent"
 * name => value pairs added.
 */
class Event
{
	friend class EventFactory;

public:
	/** Event type */
	enum Type {
		/** Generic event notification. */
		NOTIFY  = '!',

		/** A driver was not found for this device. */
		NOMATCH = '?',

		/** A bus device instance has been added. */
		ATTACH  = '+',

		/** A bus device instance has been removed. */
		DETACH  = '-'
	};

	/**
	 * Factory method type to construct an Event given
	 * the type of event and an NVPairMap populated from
	 * the event string received from devd.
	 */
	typedef Event* (BuildMethod)(Type, NVPairMap &, const std::string &);

	/** Generic Event object factory. */
	static BuildMethod Builder;

	static Event *CreateEvent(const EventFactory &factory,
				  const std::string &eventString);

	/**
	 * Returns the devname, if any, associated with the event
	 *
	 * \param name	Devname, returned by reference
	 * \return	True iff the event contained a devname
	 */
	virtual bool DevName(std::string &name)	const;

	/**
	 * Returns the absolute pathname of the device associated with this
	 * event.
	 *
	 * \param name	Devname, returned by reference
	 * \return	True iff the event contained a devname
	 */
	bool DevPath(std::string &path)		const;

	/**
	 * Returns true iff this event refers to a disk device
	 */
	bool IsDiskDev()			const;

	/** Returns the physical path of the device, if any
	 *
	 * \param path	Physical path, returned by reference
	 * \return	True iff the event contains a device with a physical
	 * 		path
	 */
	bool PhysicalPath(std::string &path)	const;

	/**
	 * Provide a user friendly string representation of an
	 * event type.
	 *
	 * \param type  The type of event to map to a string.
	 *
	 * \return  A user friendly string representing the input type.
	 */
	static const char  *TypeToString(Type type);

	/**
	 * Determine the availability of a name => value pair by name.
	 *
	 * \param name  The key name to search for in this event instance.
	 *
	 * \return  true if the specified key is available in this
	 *          event, otherwise false.
	 */
	bool Contains(const std::string &name)		 const;

	/**
	 * \param key  The name of the key for which to retrieve its
	 *             associated value.
	 *
	 * \return  A const reference to the string representing the
	 *          value associated with key.
	 *
	 * \note  For key's with no registered value, the empty string
	 *        is returned.
	 */
	const std::string &Value(const std::string &key) const;

	/**
	 * Get the type of this event instance.
	 *
	 * \return  The type of this event instance.
	 */
	Type GetType()					 const;

	/**
	 * Get the original DevdCtl event string for this event.
	 *
	 * \return  The DevdCtl event string.
	 */
	const std::string &GetEventString()		 const;

	/**
	 * Convert the event instance into a string suitable for
	 * printing to the console or emitting to syslog.
	 *
	 * \return  A string of formatted event data.
	 */
	std::string ToString()				 const;

	/**
	 * Pretty-print this event instance to cout.
	 */
	void Print()					 const;

	/**
	 * Pretty-print this event instance to syslog.
	 *
	 * \param priority  The logging priority/facility.
	 *                  See syslog(3).
	 */
	void Log(int priority)				 const;

	/**
	 * Create and return a fully independent clone
	 * of this event.
	 */
	virtual Event *DeepCopy()			 const;

	/** Destructor */
	virtual ~Event();

	/**
	 * Interpret and perform any actions necessary to
	 * consume the event.
	 *
	 * \return True if this event should be queued for later reevaluation
	 */
	virtual bool Process()				 const;

	/**
	 * Get the time that the event was created
	 */
	timeval GetTimestamp()				 const;

	/**
	 * Add a timestamp to the event string, if one does not already exist
	 * TODO: make this an instance method that operates on the std::map
	 * instead of the string.  We must fix zfsd's CaseFile serialization
	 * routines first, so that they don't need the raw event string.
	 *
	 * \param[in,out] eventString The devd event string to modify
	 */
	static void TimestampEventString(std::string &eventString);

	/**
	 * Access all parsed key => value pairs.
	 */
	const NVPairMap &GetMap()			 const;

protected:
	/** Table entries used to map a type to a user friendly string. */
	struct EventTypeRecord
	{
		Type         m_type;
		const char  *m_typeName;
	};

	/**
	 * Constructor
	 *
	 * \param type  The type of event to create.
	 */
	Event(Type type, NVPairMap &map, const std::string &eventString);

	/** Deep copy constructor. */
	Event(const Event &src);

	/** Always empty string returned when NVPairMap lookups fail. */
	static const std::string    s_theEmptyString;

	/** Unsorted table of event types. */
	static EventTypeRecord      s_typeTable[];

	/** The type of this event. */
	const Type                  m_type;

	/**
	 * Event attribute storage.
	 *
	 * \note Although stored by reference (since m_nvPairs can
	 *       never be NULL), the NVPairMap referenced by this field
	 *       is dynamically allocated and owned by this event object.
	 *       m_nvPairs must be deleted at event destruction.
	 */
	NVPairMap                  &m_nvPairs;

	/**
	 * The unaltered event string, as received from devd, used to
	 * create this event object.
	 */
	std::string                 m_eventString;

private:
	/**
	 * Ingest event data from the supplied string.
	 *
	 * \param[in] eventString  The string of devd event data to parse.
	 * \param[out] nvpairs     Returns the parsed data
	 */
	static void ParseEventString(Type type, const std::string &eventString,
				     NVPairMap &nvpairs);
};

inline Event::Type
Event::GetType() const
{
	return (m_type);
}

inline const std::string &
Event::GetEventString() const
{
	return (m_eventString);
}

inline const NVPairMap &
Event::GetMap()	const
{
	return (m_nvPairs);
}

/*--------------------------------- EventList --------------------------------*/
/**
 * EventList is a specialization of the standard list STL container.
 */
typedef std::list<Event *> EventList;

/*-------------------------------- DevfsEvent --------------------------------*/
class DevfsEvent : public Event
{
public:
	/** Specialized Event object factory for Devfs events. */
	static BuildMethod Builder;

	virtual Event *DeepCopy()		const;

	/**
	 * Interpret and perform any actions necessary to
	 * consume the event.
	 * \return True if this event should be queued for later reevaluation
	 */
	virtual bool Process()			const;

	bool IsWholeDev()			const;
	virtual bool DevName(std::string &name)	const;

protected:
	/**
	 * Given the device name of a disk, determine if the device
	 * represents the whole device, not just a partition.
	 *
	 * \param devName  Device name of disk device to test.
	 *
	 * \return  True if the device name represents the whole device.
	 *          Otherwise false.
	 */
	static bool IsWholeDev(const std::string &devName);

	/** DeepCopy Constructor. */
	DevfsEvent(const DevfsEvent &src);

	/** Constructor */
	DevfsEvent(Type, NVPairMap &, const std::string &);
};

/*--------------------------------- GeomEvent --------------------------------*/
class GeomEvent : public Event
{
public:
	/** Specialized Event object factory for GEOM events. */
	static BuildMethod Builder;

	virtual Event *DeepCopy()	const;

	virtual bool DevName(std::string &name)	const;

	const std::string &DeviceName()	const;

protected:
	/** Constructor */
	GeomEvent(Type, NVPairMap &, const std::string &);

	/** Deep copy constructor. */
	GeomEvent(const GeomEvent &src);

	std::string m_devname;
};

/*--------------------------------- ZfsEvent ---------------------------------*/
class ZfsEvent : public Event
{
public:
	/** Specialized Event object factory for ZFS events. */
	static BuildMethod Builder;

	virtual Event *DeepCopy()	const;

	virtual bool DevName(std::string &name)	const;

	const std::string &PoolName()	const;
	Guid		   PoolGUID()	const;
	Guid		   VdevGUID()	const;

protected:
	/** Constructor */
	ZfsEvent(Type, NVPairMap &, const std::string &);

	/** Deep copy constructor. */
	ZfsEvent(const ZfsEvent &src);

	Guid	m_poolGUID;
	Guid	m_vdevGUID;
};

//- ZfsEvent Inline Public Methods --------------------------------------------
inline const std::string&
ZfsEvent::PoolName() const
{
	/* The pool name is reported as the subsystem of ZFS events. */
	return (Value("subsystem"));
}

inline Guid
ZfsEvent::PoolGUID() const
{
	return (m_poolGUID);
}

inline Guid
ZfsEvent::VdevGUID() const
{
	return (m_vdevGUID);
}

} // namespace DevdCtl
#endif /*_DEVDCTL_EVENT_H_ */