Print this page
6659 nvlist_free(NULL) is a no-op
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c
+++ new/usr/src/cmd/fm/modules/common/syslog-msgs/syslog.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 #include <sys/fm/protocol.h>
27 27 #include <sys/strlog.h>
28 28 #include <sys/log.h>
29 29 #include <libscf.h>
30 30
31 31 #include <fm/fmd_api.h>
32 32 #include <fm/fmd_msg.h>
33 33
34 34 #include <stropts.h>
35 35 #include <strings.h>
36 36 #include <syslog.h>
37 37 #include <alloca.h>
38 38 #include <unistd.h>
39 39 #include <stdlib.h>
40 40 #include <errno.h>
41 41 #include <fcntl.h>
42 42
43 43 static struct stats {
44 44 fmd_stat_t bad_vers;
45 45 fmd_stat_t bad_code;
46 46 fmd_stat_t log_err;
47 47 fmd_stat_t msg_err;
48 48 fmd_stat_t no_msg;
49 49 } syslog_stats = {
50 50 { "bad_vers", FMD_TYPE_UINT64, "event version is missing or invalid" },
51 51 { "bad_code", FMD_TYPE_UINT64, "event code has no dictionary name" },
52 52 { "log_err", FMD_TYPE_UINT64, "failed to log message to log(7D)" },
53 53 { "msg_err", FMD_TYPE_UINT64, "failed to log message to sysmsg(7D)" },
54 54 { "no_msg", FMD_TYPE_UINT64, "message logging suppressed" }
55 55 };
56 56
57 57 static const struct facility {
58 58 const char *fac_name;
59 59 int fac_value;
60 60 } syslog_facs[] = {
61 61 { "LOG_DAEMON", LOG_DAEMON },
62 62 { "LOG_LOCAL0", LOG_LOCAL0 },
63 63 { "LOG_LOCAL1", LOG_LOCAL1 },
64 64 { "LOG_LOCAL2", LOG_LOCAL2 },
65 65 { "LOG_LOCAL3", LOG_LOCAL3 },
66 66 { "LOG_LOCAL4", LOG_LOCAL4 },
67 67 { "LOG_LOCAL5", LOG_LOCAL5 },
68 68 { "LOG_LOCAL6", LOG_LOCAL6 },
69 69 { "LOG_LOCAL7", LOG_LOCAL7 },
70 70 { NULL, 0 }
71 71 };
72 72
73 73 static fmd_msg_hdl_t *syslog_msghdl; /* handle for libfmd_msg calls */
74 74 static int syslog_msgall; /* set to message all faults */
75 75 static log_ctl_t syslog_ctl; /* log(7D) meta-data for each msg */
76 76 static int syslog_logfd = -1; /* log(7D) file descriptor */
77 77 static int syslog_msgfd = -1; /* sysmsg(7D) file descriptor */
78 78 static int syslog_file; /* log to syslog_logfd */
79 79 static int syslog_cons; /* log to syslog_msgfd */
80 80 static const char SYSLOG_POINTER[] = "syslog-msgs-pointer";
81 81
82 82 /*
83 83 * Ideally we would just use syslog(3C) for outputting our messages, but our
84 84 * messaging standard defines a nice multi-line format and syslogd(1M) is very
85 85 * inflexible and stupid when it comes to multi-line messages. It pulls data
86 86 * out of log(7D) and splits it up by \n, printing each line to the console
87 87 * with its usual prefix of date and sender; it uses the same behavior for the
88 88 * messages file as well. Further, syslog(3C) provides no CE_CONT equivalent
89 89 * for userland callers (which at least works around repeated file prefixing).
90 90 * So with a multi-line message format, your file and console end up like this:
91 91 *
92 92 * Dec 02 18:08:40 hostname this is my nicely formatted
93 93 * Dec 02 18:08:40 hostname message designed for 80 cols
94 94 * ...
95 95 *
96 96 * To resolve these issues, we use our own syslog_emit() wrapper to emit
97 97 * messages and some knowledge of how the Solaris log drivers work. We first
98 98 * construct an enlarged format string containing the appropriate msgid(1).
99 99 * We then format the caller's message using the provided format and buffer.
100 100 * We send this message to log(7D) using putmsg() with SL_CONSOLE | SL_LOGONLY
101 101 * set in the log_ctl_t. The log driver allows us to set SL_LOGONLY when we
102 102 * construct messages ourself, indicating that syslogd should only emit the
103 103 * message to /var/adm/messages and any remote hosts, and skip the console.
104 104 * Then we emit the message a second time, without the special prefix, to the
105 105 * sysmsg(7D) device, which handles console redirection and also permits us
106 106 * to output any characters we like to the console, including \n and \r.
107 107 */
108 108 static void
109 109 syslog_emit(fmd_hdl_t *hdl, const char *msg)
110 110 {
111 111 struct strbuf ctl, dat;
112 112 uint32_t msgid;
113 113
114 114 char *buf;
115 115 size_t buflen;
116 116
117 117 const char *format = "fmd: [ID %u FACILITY_AND_PRIORITY] %s";
118 118 STRLOG_MAKE_MSGID(format, msgid);
119 119
120 120 buflen = snprintf(NULL, 0, format, msgid, msg);
121 121 buf = alloca(buflen + 1);
122 122 (void) snprintf(buf, buflen + 1, format, msgid, msg);
123 123
124 124 ctl.buf = (void *)&syslog_ctl;
125 125 ctl.len = sizeof (syslog_ctl);
126 126
127 127 dat.buf = buf;
128 128 dat.len = buflen + 1;
129 129
130 130 /*
131 131 * The underlying log driver won't accept messages longer than
132 132 * LOG_MAXPS bytes. Therefore, messages which exceed this limit will
133 133 * be truncated and appended with a pointer to the full message.
134 134 */
135 135 if (dat.len > LOG_MAXPS) {
136 136 char *syslog_pointer, *p;
137 137 size_t plen;
138 138
139 139 if ((syslog_pointer = fmd_msg_gettext_id(syslog_msghdl, NULL,
140 140 SYSLOG_POINTER)) == NULL) {
141 141 /*
142 142 * This shouldn't happen, but if it does we'll just
143 143 * truncate the message.
144 144 */
145 145 buf[LOG_MAXPS - 1] = '\0';
146 146 dat.len = LOG_MAXPS;
147 147 } else {
148 148 plen = strlen(syslog_pointer) + 1;
149 149 buf[LOG_MAXPS - plen] = '\0';
150 150 /*
151 151 * If possible, the pointer is appended after a newline
152 152 */
153 153 if ((p = strrchr(buf, '\n')) == NULL)
154 154 p = &buf[LOG_MAXPS - plen];
155 155
156 156 (void) strcpy(p, syslog_pointer);
157 157 free(syslog_pointer);
158 158 dat.len = strlen(buf) + 1;
159 159 }
160 160 }
161 161 if (syslog_file && putmsg(syslog_logfd, &ctl, &dat, 0) != 0) {
162 162 fmd_hdl_debug(hdl, "putmsg failed: %s\n", strerror(errno));
163 163 syslog_stats.log_err.fmds_value.ui64++;
164 164 }
165 165
166 166 dat.buf = strchr(buf, ']');
167 167 dat.len -= (size_t)(dat.buf - buf);
168 168
169 169 dat.buf[0] = '\r'; /* overwrite ']' with carriage return */
170 170 dat.buf[1] = '\n'; /* overwrite ' ' with newline */
171 171
172 172 if (syslog_cons && write(syslog_msgfd, dat.buf, dat.len) != dat.len) {
173 173 fmd_hdl_debug(hdl, "write failed: %s\n", strerror(errno));
↓ open down ↓ |
173 lines elided |
↑ open up ↑ |
174 174 syslog_stats.msg_err.fmds_value.ui64++;
175 175 }
176 176 }
177 177
178 178 static void
179 179 free_notify_prefs(fmd_hdl_t *hdl, nvlist_t **prefs, uint_t nprefs)
180 180 {
181 181 int i;
182 182
183 183 for (i = 0; i < nprefs; i++) {
184 - if (prefs[i])
185 - nvlist_free(prefs[i]);
184 + nvlist_free(prefs[i]);
186 185 }
187 186
188 187 fmd_hdl_free(hdl, prefs, sizeof (nvlist_t *) * nprefs);
189 188 }
190 189
191 190 static int
192 191 get_notify_prefs(fmd_hdl_t *hdl, nvlist_t *ev_nvl, nvlist_t ***pref_nvl,
193 192 uint_t *nprefs)
194 193 {
195 194 nvlist_t *top_nvl, **np_nvlarr, *mech_nvl;
196 195 nvlist_t **tmparr;
197 196 int ret, i;
198 197 uint_t nelem, nslelem;
199 198
200 199 if ((ret = smf_notify_get_params(&top_nvl, ev_nvl)) != SCF_SUCCESS) {
201 200 ret = scf_error();
202 201 if (ret != SCF_ERROR_NOT_FOUND) {
203 202 fmd_hdl_debug(hdl, "Error looking up notification "
204 203 "preferences (%s)", scf_strerror(ret));
205 204 return (ret);
206 205 }
207 206 return (ret);
208 207 }
209 208
210 209 if (nvlist_lookup_nvlist_array(top_nvl, SCF_NOTIFY_PARAMS, &np_nvlarr,
211 210 &nelem) != 0) {
212 211 fmd_hdl_debug(hdl, "Malformed preference nvlist\n");
213 212 ret = SCF_ERROR_INVALID_ARGUMENT;
214 213 goto pref_done;
215 214 }
216 215
217 216 tmparr = fmd_hdl_alloc(hdl, nelem * sizeof (nvlist_t *), FMD_SLEEP);
218 217 nslelem = 0;
219 218
220 219 for (i = 0; i < nelem; i++) {
221 220 if (nvlist_lookup_nvlist(np_nvlarr[i], "syslog", &mech_nvl)
222 221 == 0)
223 222 tmparr[nslelem++] = fmd_nvl_dup(hdl, mech_nvl,
224 223 FMD_SLEEP);
225 224 }
226 225
227 226 if (nslelem != 0) {
228 227 size_t sz = nslelem * sizeof (nvlist_t *);
229 228
230 229 *pref_nvl = fmd_hdl_zalloc(hdl, sz, FMD_SLEEP);
231 230 *nprefs = nslelem;
232 231 bcopy(tmparr, *pref_nvl, sz);
233 232 ret = 0;
234 233 } else {
235 234 *pref_nvl = NULL;
236 235 *nprefs = 0;
237 236 ret = SCF_ERROR_NOT_FOUND;
238 237 }
239 238
240 239 fmd_hdl_free(hdl, tmparr, nelem * sizeof (nvlist_t *));
241 240 pref_done:
242 241 nvlist_free(top_nvl);
243 242 return (ret);
244 243 }
245 244
246 245 /*ARGSUSED*/
247 246 static void
248 247 syslog_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
249 248 {
250 249 uint8_t version;
251 250 boolean_t domsg, *active;
252 251 char *msg;
253 252 nvlist_t **prefs;
254 253 uint_t nprefs, nelems;
255 254 int ret;
256 255
257 256 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
258 257 version > FM_SUSPECT_VERSION) {
259 258 fmd_hdl_debug(hdl, "invalid event version: %u\n", version);
260 259 syslog_stats.bad_vers.fmds_value.ui64++;
261 260 return; /* invalid event version */
262 261 }
263 262
264 263 if (!syslog_msgall && nvlist_lookup_boolean_value(nvl,
265 264 FM_SUSPECT_MESSAGE, &domsg) == 0 && !domsg) {
266 265 fmd_hdl_debug(hdl, "%s requested no message\n", class);
267 266 syslog_stats.no_msg.fmds_value.ui64++;
268 267 return; /* event is not to be messaged */
269 268 }
270 269
271 270 ret = get_notify_prefs(hdl, nvl, &prefs, &nprefs);
272 271 if (ret == SCF_ERROR_NOT_FOUND) {
273 272 /*
274 273 * No syslog notification preferences specified for this type of
275 274 * event, so we're done
276 275 */
277 276 fmd_hdl_debug(hdl, "No syslog notification preferences "
278 277 "configured for class %s\n", class);
279 278 syslog_stats.no_msg.fmds_value.ui64++;
280 279 return;
281 280 } else if (ret != 0 || nvlist_lookup_boolean_array(prefs[0], "active",
282 281 &active, &nelems)) {
283 282 fmd_hdl_debug(hdl, "Failed to retrieve notification "
284 283 "preferences for class %s\n", class);
285 284 if (ret == 0)
286 285 free_notify_prefs(hdl, prefs, nprefs);
287 286 return;
288 287 } else if (!active[0]) {
289 288 fmd_hdl_debug(hdl, "Syslog notifications disabled for "
290 289 "class %s\n", class);
291 290 syslog_stats.no_msg.fmds_value.ui64++;
292 291 free_notify_prefs(hdl, prefs, nprefs);
293 292 return;
294 293 }
295 294 free_notify_prefs(hdl, prefs, nprefs);
296 295
297 296 if ((msg = fmd_msg_gettext_nv(syslog_msghdl, NULL, nvl)) == NULL) {
298 297 fmd_hdl_debug(hdl, "failed to format message");
299 298 syslog_stats.bad_code.fmds_value.ui64++;
300 299 return; /* libfmd_msg error */
301 300 }
302 301
303 302 syslog_ctl.pri &= LOG_FACMASK;
304 303 if (strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
305 304 strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
306 305 strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
307 306 strcmp(class, FM_LIST_UPDATED_CLASS) == 0)
308 307 syslog_ctl.pri |= LOG_NOTICE;
309 308 else
310 309 syslog_ctl.pri |= LOG_ERR;
311 310
312 311 syslog_emit(hdl, msg);
313 312 free(msg);
314 313 }
315 314
316 315 static const fmd_prop_t fmd_props[] = {
317 316 { "console", FMD_TYPE_BOOL, "true" },
318 317 { "facility", FMD_TYPE_STRING, "LOG_DAEMON" },
319 318 { "gmt", FMD_TYPE_BOOL, "false" },
320 319 { "syslogd", FMD_TYPE_BOOL, "true" },
321 320 { "url", FMD_TYPE_STRING, "http://illumos.org/msg/" },
322 321 { "message_all", FMD_TYPE_BOOL, "false" },
323 322 { NULL, 0, NULL }
324 323 };
325 324
326 325 static const fmd_hdl_ops_t fmd_ops = {
327 326 syslog_recv, /* fmdo_recv */
328 327 NULL, /* fmdo_timeout */
329 328 NULL, /* fmdo_close */
330 329 NULL, /* fmdo_stats */
331 330 NULL, /* fmdo_gc */
332 331 };
333 332
334 333 static const fmd_hdl_info_t fmd_info = {
335 334 "Syslog Messaging Agent", "1.1", &fmd_ops, fmd_props
336 335 };
337 336
338 337 void
339 338 _fmd_init(fmd_hdl_t *hdl)
340 339 {
341 340 const struct facility *fp;
342 341 char *facname, *tz, *rootdir, *urlbase;
343 342
344 343 if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
345 344 return; /* invalid data in configuration file */
346 345
347 346 (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (syslog_stats) /
348 347 sizeof (fmd_stat_t), (fmd_stat_t *)&syslog_stats);
349 348
350 349 if ((syslog_logfd = open("/dev/conslog", O_WRONLY | O_NOCTTY)) == -1)
351 350 fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/conslog");
352 351
353 352 if ((syslog_msgfd = open("/dev/sysmsg", O_WRONLY | O_NOCTTY)) == -1)
354 353 fmd_hdl_abort(hdl, "syslog-msgs failed to open /dev/sysmsg");
355 354
356 355 /*
357 356 * If the "gmt" property is set to true, force our EVENT-TIME to be
358 357 * reported in GMT time; otherwise we use localtime. tzset() affects
359 358 * the results of subsequent calls to strftime(3C) above.
360 359 */
361 360 if (fmd_prop_get_int32(hdl, "gmt") == FMD_B_TRUE &&
362 361 ((tz = getenv("TZ")) == NULL || strcmp(tz, "GMT") != 0)) {
363 362 (void) putenv(fmd_hdl_strdup(hdl, "TZ=GMT", FMD_SLEEP));
364 363 tzset(); /* reload env */
365 364 }
366 365
367 366 /*
368 367 * Look up the value of the "facility" property and use it to determine
369 368 * what syslog LOG_* facility value we use to fill in our log_ctl_t.
370 369 * The details of our logging method are described above syslog_emit().
371 370 */
372 371 facname = fmd_prop_get_string(hdl, "facility");
373 372
374 373 for (fp = syslog_facs; fp->fac_name != NULL; fp++) {
375 374 if (strcmp(fp->fac_name, facname) == 0)
376 375 break;
377 376 }
378 377
379 378 if (fp->fac_name == NULL)
380 379 fmd_hdl_abort(hdl, "invalid 'facility' setting: %s\n", facname);
381 380
382 381 fmd_prop_free_string(hdl, facname);
383 382 syslog_ctl.pri = fp->fac_value;
384 383 syslog_ctl.flags = SL_CONSOLE | SL_LOGONLY;
385 384
386 385 /*
387 386 * Cache any properties we use every time we receive an event and
388 387 * subscribe to list.suspect events regardless of the .conf file.
389 388 */
390 389 syslog_file = fmd_prop_get_int32(hdl, "syslogd");
391 390 syslog_cons = fmd_prop_get_int32(hdl, "console");
392 391 syslog_msgall = fmd_prop_get_int32(hdl, "message_all");
393 392
394 393 rootdir = fmd_prop_get_string(hdl, "fmd.rootdir");
395 394 syslog_msghdl = fmd_msg_init(rootdir, FMD_MSG_VERSION);
396 395 fmd_prop_free_string(hdl, rootdir);
397 396
398 397 if (syslog_msghdl == NULL)
399 398 fmd_hdl_abort(hdl, "failed to initialize libfmd_msg");
400 399
401 400 urlbase = fmd_prop_get_string(hdl, "url");
402 401 (void) fmd_msg_url_set(syslog_msghdl, urlbase);
403 402 fmd_prop_free_string(hdl, urlbase);
404 403
405 404 /*
406 405 * We subscribe to all FM events and then consult the notification
407 406 * preferences in the serice configuration repo to determine whether
408 407 * or not to emit a console message.
409 408 */
410 409 fmd_hdl_subscribe(hdl, FM_LIST_SUSPECT_CLASS);
411 410 fmd_hdl_subscribe(hdl, FM_LIST_REPAIRED_CLASS);
412 411 fmd_hdl_subscribe(hdl, FM_LIST_RESOLVED_CLASS);
413 412 fmd_hdl_subscribe(hdl, FM_LIST_ISOLATED_CLASS);
414 413 fmd_hdl_subscribe(hdl, FM_LIST_UPDATED_CLASS);
415 414 }
416 415
417 416 /*ARGSUSED*/
418 417 void
419 418 _fmd_fini(fmd_hdl_t *hdl)
420 419 {
421 420 fmd_msg_fini(syslog_msghdl);
422 421 (void) close(syslog_logfd);
423 422 (void) close(syslog_msgfd);
424 423 }
↓ open down ↓ |
229 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX