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/fs/zfs.h>
40 
41 #include <syslog.h>
42 
43 #include <string>
44 #include <list>
45 #include <sstream>
46 
47 #include <devdctl/exception.h>
48 #include <devdctl/guid.h>
49 
50 #include <libzfs.h>
51 
52 #include "vdev.h"
53 #include "zfsd_exception.h"
54 
55 __FBSDID("$FreeBSD$");
56 /*============================ Namespace Control =============================*/
57 using std::endl;
58 using std::string;
59 using std::stringstream;
60 
61 /*=========================== Class Implementations ==========================*/
62 /*------------------------------- ZfsdException ------------------------------*/
63 ZfsdException::ZfsdException(const char *fmt, ...)
64  : DevdCtl::Exception(),
65    m_poolConfig(NULL),
66    m_vdevConfig(NULL)
67 {
68 	va_list ap;
69 
70 	va_start(ap, fmt);
71 	FormatLog(fmt, ap);
72 	va_end(ap);
73 }
74 
75 ZfsdException::ZfsdException(zpool_handle_t *pool, const char *fmt, ...)
76  : DevdCtl::Exception(),
77    m_poolConfig(zpool_get_config(pool, NULL)),
78    m_vdevConfig(NULL)
79 {
80 	va_list ap;
81 
82 	va_start(ap, fmt);
83 	FormatLog(fmt, ap);
84 	va_end(ap);
85 }
86 
87 ZfsdException::ZfsdException(nvlist_t *poolConfig, const char *fmt, ...)
88  : DevdCtl::Exception(),
89    m_poolConfig(poolConfig),
90    m_vdevConfig(NULL)
91 {
92 	va_list ap;
93 
94 	va_start(ap, fmt);
95 	FormatLog(fmt, ap);
96 	va_end(ap);
97 }
98 
99 void
100 ZfsdException::Log() const
101 {
102 	stringstream output;
103 
104 	if (m_poolConfig != NULL) {
105 
106 		output << "Pool ";
107 
108 		char *poolName;
109 		if (nvlist_lookup_string(m_poolConfig, ZPOOL_CONFIG_POOL_NAME,
110 				     &poolName) == 0)
111 			output << poolName;
112 		else
113 			output << "Unknown";
114 		output << ": ";
115 	}
116 
117 	if (m_vdevConfig != NULL) {
118 
119 		if (m_poolConfig != NULL) {
120 			Vdev vdev(m_poolConfig, m_vdevConfig);
121 
122 			output << "Vdev " <<  vdev.GUID() << ": ";
123 		} else {
124 			Vdev vdev(m_vdevConfig);
125 
126 			output << "Pool " <<  vdev.PoolGUID() << ": ";
127 			output << "Vdev " <<  vdev.GUID() << ": ";
128 		}
129 	}
130 
131 	output << m_log << endl;
132 	syslog(LOG_ERR, "%s", output.str().c_str());
133 }
134 
135