f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER START
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The contents of this file are subject to the terms of the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Common Development and Distribution License (the "License").
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * You may not use this file except in compliance with the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * See the License for the specific language governing permissions
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * and limitations under the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * When distributing Covered Code, include this CDDL HEADER in each
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * If applicable, add the following below this CDDL HEADER, with the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * fields enclosed by brackets "[]" replaced with your own identifying
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * information: Portions Copyright [yyyy] [name of copyright owner]
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER END
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Simple-minded raw event publication from user context. See extensive
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * comments in libfmevent.h. These interfaces remain Project Private -
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * they have to evolve before rollout to Public levels.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Events are dispatched synchronously using the GPEC sysevent mechanism.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The caller context must therefore be one in which a sysevent_evc_publish
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * (and possibly sysevent_evc_bind if not already bound) is safe. We will
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * also allocate and manipulate nvlists.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Since we use GPEC, which has no least privilege awareness, these interfaces
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * will only work for would-be producers running as root.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * There is no event rate throttling applied, so we rely on producers
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * to throttle themselves. A future refinement should apply mandatory
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * but tuneable throttling on a per-producer basis. In this first version
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * the only throttle is the publication event queue depth - we'll drop
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * events when the queue is full.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * We can publish over four channels, for privileged/non-privileged and
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * high/low priority. Since only privileged producers will work now
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * (see above) we hardcode priv == B_TRUE and so only two channels are
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * actually used, separating higher and lower value streams from privileged
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * producers.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbystatic struct {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby evchan_t *binding; /* GPEC binding, once bound */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const uint32_t flags; /* flags to use in binding */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby { FMEV_CHAN_USER_PRIV_LV, NULL, EVCH_HOLD_PEND_INDEF },
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby { FMEV_CHAN_USER_PRIV_HV, NULL, EVCH_HOLD_PEND_INDEF}
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#define CHANIDX(priv, pri) (2 * ((priv) != 0) + (pri == FMEV_HIPRI))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#define CHAN_NAME(priv, pri) (chaninfo[CHANIDX(priv, pri)].name)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#define CHAN_BINDING(priv, pri) (chaninfo[CHANIDX(priv, pri)].binding)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#define CHAN_FLAGS(priv, pri) (chaninfo[CHANIDX(priv, pri)].flags)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Called after fork in the new child. We clear the cached event
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * channel bindings which are only valid in the process that created
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby for (i = 0; i < sizeof (chaninfo) / sizeof chaninfo[0]; i++)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) pthread_atfork(NULL, NULL, clear_bindings);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (sysevent_evc_bind(CHAN_NAME(priv, pri), &evc,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby strnlen(ruleset, FMEV_MAX_RULESET_LEN) == FMEV_MAX_RULESET_LEN)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (strnlen(class, FMEV_PUB_MAXCLASSLEN) == FMEV_PUB_MAXCLASSLEN)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (strnlen(subclass, FMEV_PUB_MAXSUBCLASSLEN) ==
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (pri == FMEV_LOPRI || pri == FMEV_HIPRI ?
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby static const char *pristr[] = { "low", "high" };
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyvrfy(const char **rulesetp, const char **classp, const char **subclassp,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (rulesetp && (rc = vrfy_ruleset(*rulesetp)) != FMEV_OK)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (classp && (rc = vrfy_class(*classp)) != FMEV_OK ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby subclassp && (rc = vrfy_subclass(*subclassp)) != FMEV_OK ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyva2nvl(nvlist_t **nvlp, va_list ap, uint_t ntuples)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((name = va_arg(ap, char *)) == NULL || name == FMEV_ARG_TERM)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby while (name != NULL && name != FMEV_ARG_TERM && processed <= ntuples) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby break; /* terminate on first error */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (name != FMEV_ARG_TERM || processed != ntuples) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbydo_publish(const char *file, const char *func, int64_t line,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *ruleset, const char *class, const char *subclass,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby fmev_pri_t pri, nvlist_t *nvl, uint_t ntuples, va_list ap)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Enforce NV_UNIQUE_NAME
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((nvlist_nvflag(nvl) & NV_UNIQUE_NAME) != NV_UNIQUE_NAME)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby } else if (ntuples != 0) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Even if the caller has no tuples to publish (just an event
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * class and subclass), we are going to add some detector
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * information so we need some nvlist.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (nvlist_alloc(&tmpnvl, NV_UNIQUE_NAME, 0) != 0)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Add detector information
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (file && nvlist_add_string(pub, "__fmev_file", file) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby func && nvlist_add_string(pub, "__fmev_func", func) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby line != -1 && nvlist_add_int64(pub, "__fmev_line", line) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby nvlist_add_int32(pub, "__fmev_pid", getpid()) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby nvlist_add_string(pub, "__fmev_execname", getexecname()) != 0) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * We abuse the GPEC publication arguments as follows:
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * GPEC argument Our usage
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * -------------------- -----------------
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * const char *class Raw class
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * const char *subclass Raw subclass
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * const char *vendor Ruleset name
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * const char *pub_name Unused
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * nvlist_t *attr_list Event attributes
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby rc = (sysevent_evc_publish(evc, class, subclass, ruleset, "",
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby pub, EVCH_NOSLEEP) == 0) ? FMEV_SUCCESS : FMEVERR_TRANSPORT;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby /* Free a passed in nvlist iff success */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *file, const char *func, int64_t line,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *ruleset, const char *class, const char *subclass,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((rc = vrfy(&ruleset, &class, &subclass, &pri)) != FMEV_OK)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby pri, attr, 0, NULL)); /* any attr freed iff success */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *file, const char *func, int64_t line,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *ruleset, const char *class, const char *subclass,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((rc = vrfy(&ruleset, &class, &subclass, &pri)) != FMEV_OK)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby_fmev_publish(const char *class, const char *subclass, fmev_pri_t pri,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((rc = vrfy(NULL, &class, &subclass, &pri)) != FMEV_OK)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby_fmev_rspublish(const char *ruleset, const char *class, const char *subclass,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((rc = vrfy(&ruleset, &class, &subclass, &pri)) != FMEV_OK)