xref: /freebsd/cddl/usr.sbin/zfsd/zfsd_exception.cc (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3  * All rights reserved.
4  *
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.
16  *
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.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  */
32 
33 /**
34  * \file zfsd_exception
35  *
36  * Implementation of the ZfsdException class.
37  */
38 #include <sys/cdefs.h>
39 #include <sys/byteorder.h>
40 #include <sys/fs/zfs.h>
41 
42 #include <syslog.h>
43 
44 #include <string>
45 #include <list>
46 #include <sstream>
47 
48 #include <devdctl/exception.h>
49 #include <devdctl/guid.h>
50 
51 #include <libzfs.h>
52 
53 #include "vdev.h"
54 #include "zfsd_exception.h"
55 /*============================ Namespace Control =============================*/
56 using std::endl;
57 using std::string;
58 using std::stringstream;
59 
60 /*=========================== Class Implementations ==========================*/
61 /*------------------------------- ZfsdException ------------------------------*/
62 ZfsdException::ZfsdException(const char *fmt, ...)
63  : DevdCtl::Exception(),
64    m_poolConfig(NULL),
65    m_vdevConfig(NULL)
66 {
67 	va_list ap;
68 
69 	va_start(ap, fmt);
70 	FormatLog(fmt, ap);
71 	va_end(ap);
72 }
73 
74 ZfsdException::ZfsdException(zpool_handle_t *pool, const char *fmt, ...)
75  : DevdCtl::Exception(),
76    m_poolConfig(zpool_get_config(pool, NULL)),
77    m_vdevConfig(NULL)
78 {
79 	va_list ap;
80 
81 	va_start(ap, fmt);
82 	FormatLog(fmt, ap);
83 	va_end(ap);
84 }
85 
86 ZfsdException::ZfsdException(nvlist_t *poolConfig, const char *fmt, ...)
87  : DevdCtl::Exception(),
88    m_poolConfig(poolConfig),
89    m_vdevConfig(NULL)
90 {
91 	va_list ap;
92 
93 	va_start(ap, fmt);
94 	FormatLog(fmt, ap);
95 	va_end(ap);
96 }
97 
98 void
99 ZfsdException::Log() const
100 {
101 	stringstream output;
102 
103 	if (m_poolConfig != NULL) {
104 
105 		output << "Pool ";
106 
107 		const char *poolName;
108 		if (nvlist_lookup_string(m_poolConfig, ZPOOL_CONFIG_POOL_NAME,
109 				     &poolName) == 0)
110 			output << poolName;
111 		else
112 			output << "Unknown";
113 		output << ": ";
114 	}
115 
116 	if (m_vdevConfig != NULL) {
117 
118 		if (m_poolConfig != NULL) {
119 			Vdev vdev(m_poolConfig, m_vdevConfig);
120 
121 			output << "Vdev " <<  vdev.GUID() << ": ";
122 		} else {
123 			Vdev vdev(m_vdevConfig);
124 
125 			output << "Pool " <<  vdev.PoolGUID() << ": ";
126 			output << "Vdev " <<  vdev.GUID() << ": ";
127 		}
128 	}
129 
130 	output << m_log << endl;
131 	syslog(LOG_ERR, "%s", output.str().c_str());
132 }
133 
134