4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <sys/param.h>
36 #include <sys/sysevent/eventdefs.h>
37 #include <sys/sysevent/dr.h>
39 #include <libnvpair.h>
49 * How dpritab is used:
50 * dpritab[dlvl_t value] = corresponding syslog priority
52 * Be careful of some priorities (facility + severity) that get "lost" by
53 * default since they have no syslog.conf entries such as daemon.info and
54 * daemon.debug; see syslog(3C) and syslog.conf(4) for more info
56 int dpritab
[] = {LOG_INFO
, LOG_WARNING
, LOG_NOTICE
, LOG_NOTICE
};
57 int dpritab_len
= sizeof (dpritab
) / sizeof (dpritab
[0]);
60 * the following affects pcidr_set_logopt() which plugins should use to set
61 * these logging options received from the handler
63 dlvl_t dlvl
= MIN_DLVL
; /* verbosity */
64 char *prg
= ""; /* program name */
65 FILE *dfp
= NULL
; /* file to output messages to */
66 int dsys
= 1; /* flag controlling output to syslog */
70 pcidr_malloc(size_t size
)
80 for (i
= 0; i
< PCIDR_MALLOC_CNT
; i
++) {
81 assert(errno
== EAGAIN
);
84 (void) usleep(PCIDR_MALLOC_TIME
);
93 /* exit() in case assertions are disabled (NDEBUG defined) */
100 dprint(dlvl_t lvl
, char *fmt
, ...)
106 if (dlvl
< lvl
|| (dsys
== 0 && dfp
== NULL
))
111 buflen
= vsnprintf(NULL
, 0, fmt
, ap
);
116 buf
= (char *)pcidr_malloc(sizeof (char) * buflen
);
120 rv
= vsnprintf(buf
, buflen
, fmt
, ap
);
129 syslog(dpritab
[lvl
], "%s", buf
);
132 (void) fprintf(dfp
, "%s", buf
);
139 pcidr_set_logopt(pcidr_logopt_t
*logopt
)
149 * if <name> is recognized, function will return its type through <typep> and
150 * return 0; else function will return non-zero
153 pcidr_name2type(char *name
, data_type_t
*typep
)
156 if (strcmp(name
, ATTRNM_CLASS
) == 0 ||
157 strcmp(name
, ATTRNM_SUBCLASS
) == 0 ||
158 strcmp(name
, ATTRNM_PUB_NAME
) == 0 ||
159 strcmp(name
, DR_REQ_TYPE
) == 0 ||
160 strcmp(name
, DR_AP_ID
) == 0) {
161 *typep
= DATA_TYPE_STRING
;
170 pcidr_print_attrlist(dlvl_t lvl
, nvlist_t
*attrlistp
, char *prestr
)
172 char *fn
= "pcidr_print_attrlist";
182 while ((nvpairp
= nvlist_next_nvpair(attrlistp
, nvpairp
)) != NULL
) {
183 type
= nvpair_type(nvpairp
);
184 name
= nvpair_name(nvpairp
);
187 case DATA_TYPE_STRING
:
188 rv
= nvpair_value_string(nvpairp
, &valstr
);
190 dprint(lvl
, "%s: nvpair_value_string() "
191 "failed: name = %s, rv = %d\n",
197 dprint(lvl
, "%s: unsupported type: name = %s, "
198 "type = 0x%x\n", fn
, name
, (int)type
);
201 dprint(lvl
, "%s%s = %s\n", prestr
, name
, valstr
);
207 * if one of the args matches <valstr>, return 0; else return non-zero
208 * args list must be NULL terminated;
209 * if args list is empty, this will return 0 if <valstr> is NOT empty
212 pcidr_check_string(char *valstr
, ...)
218 assert(valstr
!= NULL
);
220 va_start(ap
, valstr
);
221 if (va_arg(ap
, char *) == NULL
) {
222 if (valstr
[0] != '\0')
227 va_start(ap
, valstr
);
228 while ((argstr
= va_arg(ap
, char *)) != NULL
) {
229 if (strcmp(argstr
, valstr
) == 0) {
241 * dr attribute values that the default plugin checks for;
242 * other plugins may also use this if they support a superset of these
244 * returns 0 if valid, else non-zero
247 pcidr_check_attrs(pcidr_attrs_t
*drp
)
249 char *fn
= "pcidr_check_attrs";
255 if (pcidr_check_string(val
, EC_DR
, NULL
) != 0) {
256 dprint(DDEBUG
, "%s: attribute \"%s\" has invalid value = %s\n",
261 name
= ATTRNM_SUBCLASS
;
263 if (pcidr_check_string(val
, ESC_DR_REQ
, NULL
) != 0) {
264 dprint(DDEBUG
, "%s: attribute \"%s\" has invalid value = %s\n",
269 name
= ATTRNM_PUB_NAME
;
271 if (pcidr_check_string(val
, NULL
) != 0) {
272 dprint(DDEBUG
, "%s: attribute \"%s\" is empty\n",
278 val
= drp
->dr_req_type
;
279 if (pcidr_check_string(val
, DR_REQ_INCOMING_RES
, DR_REQ_OUTGOING_RES
,
281 dprint(DDEBUG
, "%s: attribute \"%s\" has invalid value = %s\n",
288 if (pcidr_check_string(drp
->dr_ap_id
, NULL
) != 0) {
289 dprint(DDEBUG
, "%s: attribute \"%s\" is empty\n",
299 * get dr attributes from <listp> for the default plugin and returns
300 * them through <drp>;
301 * returns 0 on success
304 pcidr_get_attrs(nvlist_t
*attrlistp
, pcidr_attrs_t
*drp
)
306 char *fn
= "pcidr_get_attrs";
311 r
= nvlist_lookup_string(attrlistp
, name
, &drp
->class);
313 dprint(DDEBUG
, "%s: nvlist_lookup_string() failed for "
314 "attribute \"%s\": rv = %d\n", fn
, name
, r
);
318 name
= ATTRNM_SUBCLASS
;
319 r
= nvlist_lookup_string(attrlistp
, name
, &drp
->subclass
);
321 dprint(DDEBUG
, "%s: nvlist_lookup_string() failed for "
322 "attribute \"%s\": rv = %d\n", fn
, name
, r
);
326 name
= ATTRNM_PUB_NAME
;
327 r
= nvlist_lookup_string(attrlistp
, name
, &drp
->pub_name
);
329 dprint(DDEBUG
, "%s: nvlist_lookup_string() failed for "
330 "attribute \"%s\": rv = %d\n", fn
, name
, r
);
335 r
= nvlist_lookup_string(attrlistp
, name
, &drp
->dr_req_type
);
337 dprint(DDEBUG
, "%s: nvlist_lookup_string() failed for "
338 "attribute \"%s\": rv = %d\n", fn
, name
, r
);
343 r
= nvlist_lookup_string(attrlistp
, name
, &drp
->dr_ap_id
);
345 dprint(DDEBUG
, "%s: nvlist_lookup_string() failed for "
346 "attribute \"%s\": rv = %d\n", fn
, name
, r
);