4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
40 #include <fm/fmd_api.h>
43 #include "disk_monitor.h"
45 extern log_class_t g_verbose
;
47 static pthread_mutex_t log_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 verror(const char *fmt
, va_list ap
)
54 dm_assert(pthread_mutex_lock(&log_mutex
) == 0);
55 fmd_hdl_vdebug(g_fm_hdl
, fmt
, ap
);
57 if (fmt
[strlen(fmt
) - 1] != '\n')
58 fmd_hdl_debug(g_fm_hdl
, ": %s\n", strerror(error
));
60 dm_assert(pthread_mutex_unlock(&log_mutex
) == 0);
64 vwarn_e(const char *fmt
, va_list ap
)
68 dm_assert(pthread_mutex_lock(&log_mutex
) == 0);
69 fmd_hdl_debug(g_fm_hdl
, "WARNING: ");
70 fmd_hdl_vdebug(g_fm_hdl
, fmt
, ap
);
72 if (fmt
[strlen(fmt
) - 1] != '\n')
73 fmd_hdl_debug(g_fm_hdl
, ": %s\n", strerror(error
));
75 dm_assert(pthread_mutex_unlock(&log_mutex
) == 0);
79 vwarn(const char *fmt
, va_list ap
)
81 dm_assert(pthread_mutex_lock(&log_mutex
) == 0);
82 fmd_hdl_debug(g_fm_hdl
, "WARNING: ");
83 fmd_hdl_vdebug(g_fm_hdl
, fmt
, ap
);
84 dm_assert(pthread_mutex_unlock(&log_mutex
) == 0);
88 vcont(log_class_t cl
, const char *fmt
, va_list ap
)
92 if ((g_verbose
& cl
) != cl
)
95 dm_assert(pthread_mutex_lock(&log_mutex
) == 0);
96 fmd_hdl_vdebug(g_fm_hdl
, fmt
, ap
);
98 if (fmt
[strlen(fmt
) - 1] != '\n')
99 fmd_hdl_debug(g_fm_hdl
, ": %s\n", strerror(error
));
101 dm_assert(pthread_mutex_unlock(&log_mutex
) == 0);
105 log_msg(log_class_t cl
, const char *fmt
, ...)
109 if ((g_verbose
& cl
) != cl
)
112 dm_assert(pthread_mutex_lock(&log_mutex
) == 0);
114 fmd_hdl_vdebug(g_fm_hdl
, fmt
, ap
);
116 dm_assert(pthread_mutex_unlock(&log_mutex
) == 0);
121 log_err(const char *fmt
, ...)
125 if ((g_verbose
& MM_ERR
) != MM_ERR
)
135 log_warn(const char *fmt
, ...)
139 if ((g_verbose
& MM_WARN
) != MM_WARN
)
149 log_warn_e(const char *fmt
, ...)
153 if ((g_verbose
& MM_WARN
) != MM_WARN
)
162 dfree(void *p
, size_t sz
)
164 fmd_hdl_free(g_fm_hdl
, p
, sz
);
170 fmd_hdl_strfree(g_fm_hdl
, s
);
176 return (fmd_hdl_alloc(g_fm_hdl
, sz
, FMD_SLEEP
));
182 return (fmd_hdl_zalloc(g_fm_hdl
, sz
, FMD_SLEEP
));
187 dstrdup(const char *s
)
189 return (fmd_hdl_strdup(g_fm_hdl
, s
, FMD_SLEEP
));
193 queue_add(qu_t
*qp
, void *data
)
196 (struct q_node
*)qp
->nalloc(sizeof (struct q_node
));
197 struct q_node
*nodep
;
201 dm_assert(pthread_mutex_lock(&qp
->mutex
) == 0);
203 if (qp
->nodep
== NULL
)
208 while (nodep
->next
!= NULL
)
214 /* If the queue was empty, we need to wake people up */
215 if (qp
->boe
&& qp
->nodep
== qnp
)
216 dm_assert(pthread_cond_broadcast(&qp
->cvar
) == 0);
217 dm_assert(pthread_mutex_unlock(&qp
->mutex
) == 0);
221 queue_remove(qu_t
*qp
)
224 struct q_node
*nextnode
;
226 dm_assert(pthread_mutex_lock(&qp
->mutex
) == 0);
228 /* Wait while the queue is empty */
229 while (qp
->boe
&& qp
->nodep
== NULL
) {
230 (void) pthread_cond_wait(&qp
->cvar
, &qp
->mutex
);
234 * If Block-On-Empty is false, the queue may be empty
236 if (qp
->nodep
!= NULL
) {
237 rv
= qp
->nodep
->data
;
238 nextnode
= qp
->nodep
->next
;
239 qp
->nfree(qp
->nodep
, sizeof (struct q_node
));
240 qp
->nodep
= nextnode
;
243 dm_assert(pthread_mutex_unlock(&qp
->mutex
) == 0);
248 new_queue(boolean_t block_on_empty
, void *(*nodealloc
)(size_t),
249 void (*nodefree
)(void *, size_t), void (*data_deallocator
)(void *))
251 qu_t
*newqp
= (qu_t
*)dmalloc(sizeof (qu_t
));
253 newqp
->boe
= block_on_empty
;
254 newqp
->nalloc
= nodealloc
;
255 newqp
->nfree
= nodefree
;
256 newqp
->data_dealloc
= data_deallocator
;
257 dm_assert(pthread_mutex_init(&newqp
->mutex
, NULL
) == 0);
258 dm_assert(pthread_cond_init(&newqp
->cvar
, NULL
) == 0);
265 queue_free(qu_t
**qpp
)
270 dm_assert(pthread_mutex_destroy(&qp
->mutex
) == 0);
271 dm_assert(pthread_cond_destroy(&qp
->cvar
) == 0);
275 while ((item
= queue_remove(qp
)) != NULL
) {
276 qp
->data_dealloc(item
);
279 dm_assert(qp
->nodep
== NULL
);
281 dfree(qp
, sizeof (qu_t
));
286 _dm_assert(const char *assertion
, const char *file
, int line
, const char *func
)
289 * No newline is appended to the assertion message so that
290 * errno can be translated for us by fmd_hdl_abort().
293 fmd_hdl_abort(g_fm_hdl
, "Assertion failed: "
294 "%s, file: %s, line: %d, function: %s", assertion
, file
,
297 fmd_hdl_abort(g_fm_hdl
, "Assertion failed: "
298 "%s, file: %s, line: %d", assertion
, file
, line
);