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