xref: /freebsd/sys/contrib/openzfs/cmd/zed/zed_event.c (revision 716fd348e01c5f2ba125f878a634a753436c2994)
1eda14cbcSMatt Macy /*
2180f8225SMatt Macy  * This file is part of the ZFS Event Daemon (ZED).
3180f8225SMatt Macy  *
4eda14cbcSMatt Macy  * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5eda14cbcSMatt Macy  * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
616038816SMartin Matuska  * Refer to the OpenZFS git commit log for authoritative copyright attribution.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
9eda14cbcSMatt Macy  * Common Development and Distribution License Version 1.0 (CDDL-1.0).
10eda14cbcSMatt Macy  * You can obtain a copy of the license from the top-level file
11eda14cbcSMatt Macy  * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>.
12eda14cbcSMatt Macy  * You may not use this file except in compliance with the license.
13eda14cbcSMatt Macy  */
14eda14cbcSMatt Macy 
15eda14cbcSMatt Macy #include <ctype.h>
16eda14cbcSMatt Macy #include <errno.h>
17eda14cbcSMatt Macy #include <fcntl.h>
1816038816SMartin Matuska #include <libzfs_core.h>
19eda14cbcSMatt Macy #include <paths.h>
20eda14cbcSMatt Macy #include <stdarg.h>
21eda14cbcSMatt Macy #include <stdio.h>
22eda14cbcSMatt Macy #include <stdlib.h>
23eda14cbcSMatt Macy #include <string.h>
24eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
25eda14cbcSMatt Macy #include <time.h>
26eda14cbcSMatt Macy #include <unistd.h>
27eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
28eda14cbcSMatt Macy #include "zed.h"
29eda14cbcSMatt Macy #include "zed_conf.h"
30eda14cbcSMatt Macy #include "zed_disk_event.h"
31eda14cbcSMatt Macy #include "zed_event.h"
32eda14cbcSMatt Macy #include "zed_exec.h"
33eda14cbcSMatt Macy #include "zed_file.h"
34eda14cbcSMatt Macy #include "zed_log.h"
35eda14cbcSMatt Macy #include "zed_strings.h"
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy #include "agents/zfs_agents.h"
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy #define	MAXBUF	4096
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy /*
42eda14cbcSMatt Macy  * Open the libzfs interface.
43eda14cbcSMatt Macy  */
44eda14cbcSMatt Macy int
45eda14cbcSMatt Macy zed_event_init(struct zed_conf *zcp)
46eda14cbcSMatt Macy {
47eda14cbcSMatt Macy 	if (!zcp)
48eda14cbcSMatt Macy 		zed_log_die("Failed zed_event_init: %s", strerror(EINVAL));
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy 	zcp->zfs_hdl = libzfs_init();
51eda14cbcSMatt Macy 	if (!zcp->zfs_hdl) {
52eda14cbcSMatt Macy 		if (zcp->do_idle)
53eda14cbcSMatt Macy 			return (-1);
54eda14cbcSMatt Macy 		zed_log_die("Failed to initialize libzfs");
55eda14cbcSMatt Macy 	}
56eda14cbcSMatt Macy 
5716038816SMartin Matuska 	zcp->zevent_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
58eda14cbcSMatt Macy 	if (zcp->zevent_fd < 0) {
59eda14cbcSMatt Macy 		if (zcp->do_idle)
60eda14cbcSMatt Macy 			return (-1);
61eda14cbcSMatt Macy 		zed_log_die("Failed to open \"%s\": %s",
62eda14cbcSMatt Macy 		    ZFS_DEV, strerror(errno));
63eda14cbcSMatt Macy 	}
64eda14cbcSMatt Macy 
65eda14cbcSMatt Macy 	zfs_agent_init(zcp->zfs_hdl);
66eda14cbcSMatt Macy 
67eda14cbcSMatt Macy 	if (zed_disk_event_init() != 0) {
68eda14cbcSMatt Macy 		if (zcp->do_idle)
69eda14cbcSMatt Macy 			return (-1);
70eda14cbcSMatt Macy 		zed_log_die("Failed to initialize disk events");
71eda14cbcSMatt Macy 	}
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy 	return (0);
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy /*
77eda14cbcSMatt Macy  * Close the libzfs interface.
78eda14cbcSMatt Macy  */
79eda14cbcSMatt Macy void
80eda14cbcSMatt Macy zed_event_fini(struct zed_conf *zcp)
81eda14cbcSMatt Macy {
82eda14cbcSMatt Macy 	if (!zcp)
83eda14cbcSMatt Macy 		zed_log_die("Failed zed_event_fini: %s", strerror(EINVAL));
84eda14cbcSMatt Macy 
85eda14cbcSMatt Macy 	zed_disk_event_fini();
86eda14cbcSMatt Macy 	zfs_agent_fini();
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy 	if (zcp->zevent_fd >= 0) {
89eda14cbcSMatt Macy 		if (close(zcp->zevent_fd) < 0)
90eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING, "Failed to close \"%s\": %s",
91eda14cbcSMatt Macy 			    ZFS_DEV, strerror(errno));
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 		zcp->zevent_fd = -1;
94eda14cbcSMatt Macy 	}
95eda14cbcSMatt Macy 	if (zcp->zfs_hdl) {
96eda14cbcSMatt Macy 		libzfs_fini(zcp->zfs_hdl);
97eda14cbcSMatt Macy 		zcp->zfs_hdl = NULL;
98eda14cbcSMatt Macy 	}
9916038816SMartin Matuska 
10016038816SMartin Matuska 	zed_exec_fini();
10116038816SMartin Matuska }
10216038816SMartin Matuska 
10316038816SMartin Matuska static void
10416038816SMartin Matuska _bump_event_queue_length(void)
10516038816SMartin Matuska {
10616038816SMartin Matuska 	int zzlm = -1, wr;
10716038816SMartin Matuska 	char qlen_buf[12] = {0}; /* parameter is int => max "-2147483647\n" */
10816038816SMartin Matuska 	long int qlen;
10916038816SMartin Matuska 
11016038816SMartin Matuska 	zzlm = open("/sys/module/zfs/parameters/zfs_zevent_len_max", O_RDWR);
11116038816SMartin Matuska 	if (zzlm < 0)
11216038816SMartin Matuska 		goto done;
11316038816SMartin Matuska 
11416038816SMartin Matuska 	if (read(zzlm, qlen_buf, sizeof (qlen_buf)) < 0)
11516038816SMartin Matuska 		goto done;
11616038816SMartin Matuska 	qlen_buf[sizeof (qlen_buf) - 1] = '\0';
11716038816SMartin Matuska 
11816038816SMartin Matuska 	errno = 0;
11916038816SMartin Matuska 	qlen = strtol(qlen_buf, NULL, 10);
12016038816SMartin Matuska 	if (errno == ERANGE)
12116038816SMartin Matuska 		goto done;
12216038816SMartin Matuska 
12316038816SMartin Matuska 	if (qlen <= 0)
12416038816SMartin Matuska 		qlen = 512; /* default zfs_zevent_len_max value */
12516038816SMartin Matuska 	else
12616038816SMartin Matuska 		qlen *= 2;
12716038816SMartin Matuska 
12816038816SMartin Matuska 	if (qlen > INT_MAX)
12916038816SMartin Matuska 		qlen = INT_MAX;
13016038816SMartin Matuska 	wr = snprintf(qlen_buf, sizeof (qlen_buf), "%ld", qlen);
13116038816SMartin Matuska 
13216038816SMartin Matuska 	if (pwrite(zzlm, qlen_buf, wr, 0) < 0)
13316038816SMartin Matuska 		goto done;
13416038816SMartin Matuska 
13516038816SMartin Matuska 	zed_log_msg(LOG_WARNING, "Bumping queue length to %ld", qlen);
13616038816SMartin Matuska 
13716038816SMartin Matuska done:
13816038816SMartin Matuska 	if (zzlm > -1)
13916038816SMartin Matuska 		(void) close(zzlm);
140eda14cbcSMatt Macy }
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy /*
143eda14cbcSMatt Macy  * Seek to the event specified by [saved_eid] and [saved_etime].
144eda14cbcSMatt Macy  * This protects against processing a given event more than once.
145eda14cbcSMatt Macy  * Return 0 upon a successful seek to the specified event, or -1 otherwise.
146eda14cbcSMatt Macy  *
147eda14cbcSMatt Macy  * A zevent is considered to be uniquely specified by its (eid,time) tuple.
148eda14cbcSMatt Macy  * The unsigned 64b eid is set to 1 when the kernel module is loaded, and
149eda14cbcSMatt Macy  * incremented by 1 for each new event.  Since the state file can persist
150eda14cbcSMatt Macy  * across a kernel module reload, the time must be checked to ensure a match.
151eda14cbcSMatt Macy  */
152eda14cbcSMatt Macy int
153eda14cbcSMatt Macy zed_event_seek(struct zed_conf *zcp, uint64_t saved_eid, int64_t saved_etime[])
154eda14cbcSMatt Macy {
155eda14cbcSMatt Macy 	uint64_t eid;
156eda14cbcSMatt Macy 	int found;
157eda14cbcSMatt Macy 	nvlist_t *nvl;
158eda14cbcSMatt Macy 	int n_dropped;
159eda14cbcSMatt Macy 	int64_t *etime;
160eda14cbcSMatt Macy 	uint_t nelem;
161eda14cbcSMatt Macy 	int rv;
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	if (!zcp) {
164eda14cbcSMatt Macy 		errno = EINVAL;
165eda14cbcSMatt Macy 		zed_log_msg(LOG_ERR, "Failed to seek zevent: %s",
166eda14cbcSMatt Macy 		    strerror(errno));
167eda14cbcSMatt Macy 		return (-1);
168eda14cbcSMatt Macy 	}
169eda14cbcSMatt Macy 	eid = 0;
170eda14cbcSMatt Macy 	found = 0;
171eda14cbcSMatt Macy 	while ((eid < saved_eid) && !found) {
172eda14cbcSMatt Macy 		rv = zpool_events_next(zcp->zfs_hdl, &nvl, &n_dropped,
173eda14cbcSMatt Macy 		    ZEVENT_NONBLOCK, zcp->zevent_fd);
174eda14cbcSMatt Macy 
175eda14cbcSMatt Macy 		if ((rv != 0) || !nvl)
176eda14cbcSMatt Macy 			break;
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 		if (n_dropped > 0) {
179eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING, "Missed %d events", n_dropped);
18016038816SMartin Matuska 			_bump_event_queue_length();
181eda14cbcSMatt Macy 		}
182eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(nvl, "eid", &eid) != 0) {
183eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING, "Failed to lookup zevent eid");
184eda14cbcSMatt Macy 		} else if (nvlist_lookup_int64_array(nvl, "time",
185eda14cbcSMatt Macy 		    &etime, &nelem) != 0) {
186eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING,
187eda14cbcSMatt Macy 			    "Failed to lookup zevent time (eid=%llu)", eid);
188eda14cbcSMatt Macy 		} else if (nelem != 2) {
189eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING,
190eda14cbcSMatt Macy 			    "Failed to lookup zevent time (eid=%llu, nelem=%u)",
191eda14cbcSMatt Macy 			    eid, nelem);
192eda14cbcSMatt Macy 		} else if ((eid != saved_eid) ||
193eda14cbcSMatt Macy 		    (etime[0] != saved_etime[0]) ||
194eda14cbcSMatt Macy 		    (etime[1] != saved_etime[1])) {
195eda14cbcSMatt Macy 			/* no-op */
196eda14cbcSMatt Macy 		} else {
197eda14cbcSMatt Macy 			found = 1;
198eda14cbcSMatt Macy 		}
199eda14cbcSMatt Macy 		free(nvl);
200eda14cbcSMatt Macy 	}
201eda14cbcSMatt Macy 	if (!found && (saved_eid > 0)) {
202eda14cbcSMatt Macy 		if (zpool_events_seek(zcp->zfs_hdl, ZEVENT_SEEK_START,
203eda14cbcSMatt Macy 		    zcp->zevent_fd) < 0)
204eda14cbcSMatt Macy 			zed_log_msg(LOG_WARNING, "Failed to seek to eid=0");
205eda14cbcSMatt Macy 		else
206eda14cbcSMatt Macy 			eid = 0;
207eda14cbcSMatt Macy 	}
208eda14cbcSMatt Macy 	zed_log_msg(LOG_NOTICE, "Processing events since eid=%llu", eid);
209eda14cbcSMatt Macy 	return (found ? 0 : -1);
210eda14cbcSMatt Macy }
211eda14cbcSMatt Macy 
212eda14cbcSMatt Macy /*
213eda14cbcSMatt Macy  * Return non-zero if nvpair [name] should be formatted in hex; o/w, return 0.
214eda14cbcSMatt Macy  */
215eda14cbcSMatt Macy static int
216eda14cbcSMatt Macy _zed_event_value_is_hex(const char *name)
217eda14cbcSMatt Macy {
218eda14cbcSMatt Macy 	const char *hex_suffix[] = {
219eda14cbcSMatt Macy 		"_guid",
220eda14cbcSMatt Macy 		"_guids",
221eda14cbcSMatt Macy 		NULL
222eda14cbcSMatt Macy 	};
223eda14cbcSMatt Macy 	const char **pp;
224eda14cbcSMatt Macy 	char *p;
225eda14cbcSMatt Macy 
226eda14cbcSMatt Macy 	if (!name)
227eda14cbcSMatt Macy 		return (0);
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	for (pp = hex_suffix; *pp; pp++) {
230eda14cbcSMatt Macy 		p = strstr(name, *pp);
231eda14cbcSMatt Macy 		if (p && strlen(p) == strlen(*pp))
232eda14cbcSMatt Macy 			return (1);
233eda14cbcSMatt Macy 	}
234eda14cbcSMatt Macy 	return (0);
235eda14cbcSMatt Macy }
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy /*
238eda14cbcSMatt Macy  * Add an environment variable for [eid] to the container [zsp].
239eda14cbcSMatt Macy  *
240eda14cbcSMatt Macy  * The variable name is the concatenation of [prefix] and [name] converted to
241eda14cbcSMatt Macy  * uppercase with non-alphanumeric characters converted to underscores;
242eda14cbcSMatt Macy  * [prefix] is optional, and [name] must begin with an alphabetic character.
243eda14cbcSMatt Macy  * If the converted variable name already exists within the container [zsp],
244eda14cbcSMatt Macy  * its existing value will be replaced with the new value.
245eda14cbcSMatt Macy  *
246eda14cbcSMatt Macy  * The variable value is specified by the format string [fmt].
247eda14cbcSMatt Macy  *
248eda14cbcSMatt Macy  * Returns 0 on success, and -1 on error (with errno set).
249eda14cbcSMatt Macy  *
250eda14cbcSMatt Macy  * All environment variables in [zsp] should be added through this function.
251eda14cbcSMatt Macy  */
25216038816SMartin Matuska static __attribute__((format(printf, 5, 6))) int
253eda14cbcSMatt Macy _zed_event_add_var(uint64_t eid, zed_strings_t *zsp,
254eda14cbcSMatt Macy     const char *prefix, const char *name, const char *fmt, ...)
255eda14cbcSMatt Macy {
256eda14cbcSMatt Macy 	char keybuf[MAXBUF];
257eda14cbcSMatt Macy 	char valbuf[MAXBUF];
258eda14cbcSMatt Macy 	char *dstp;
259eda14cbcSMatt Macy 	const char *srcp;
260eda14cbcSMatt Macy 	const char *lastp;
261eda14cbcSMatt Macy 	int n;
262eda14cbcSMatt Macy 	int buflen;
263eda14cbcSMatt Macy 	va_list vargs;
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 	assert(zsp != NULL);
266eda14cbcSMatt Macy 	assert(fmt != NULL);
267eda14cbcSMatt Macy 
268eda14cbcSMatt Macy 	if (!name) {
269eda14cbcSMatt Macy 		errno = EINVAL;
270eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
271eda14cbcSMatt Macy 		    "Failed to add variable for eid=%llu: Name is empty", eid);
272eda14cbcSMatt Macy 		return (-1);
273eda14cbcSMatt Macy 	} else if (!isalpha(name[0])) {
274eda14cbcSMatt Macy 		errno = EINVAL;
275eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
276eda14cbcSMatt Macy 		    "Failed to add variable for eid=%llu: "
277eda14cbcSMatt Macy 		    "Name \"%s\" is invalid", eid, name);
278eda14cbcSMatt Macy 		return (-1);
279eda14cbcSMatt Macy 	}
280eda14cbcSMatt Macy 	/*
281eda14cbcSMatt Macy 	 * Construct the string key by converting PREFIX (if present) and NAME.
282eda14cbcSMatt Macy 	 */
283eda14cbcSMatt Macy 	dstp = keybuf;
284eda14cbcSMatt Macy 	lastp = keybuf + sizeof (keybuf);
285eda14cbcSMatt Macy 	if (prefix) {
286eda14cbcSMatt Macy 		for (srcp = prefix; *srcp && (dstp < lastp); srcp++)
287eda14cbcSMatt Macy 			*dstp++ = isalnum(*srcp) ? toupper(*srcp) : '_';
288eda14cbcSMatt Macy 	}
289eda14cbcSMatt Macy 	for (srcp = name; *srcp && (dstp < lastp); srcp++)
290eda14cbcSMatt Macy 		*dstp++ = isalnum(*srcp) ? toupper(*srcp) : '_';
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	if (dstp == lastp) {
293eda14cbcSMatt Macy 		errno = ENAMETOOLONG;
294eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
295eda14cbcSMatt Macy 		    "Failed to add variable for eid=%llu: Name too long", eid);
296eda14cbcSMatt Macy 		return (-1);
297eda14cbcSMatt Macy 	}
298eda14cbcSMatt Macy 	*dstp = '\0';
299eda14cbcSMatt Macy 	/*
300eda14cbcSMatt Macy 	 * Construct the string specified by "[PREFIX][NAME]=[FMT]".
301eda14cbcSMatt Macy 	 */
302eda14cbcSMatt Macy 	dstp = valbuf;
303eda14cbcSMatt Macy 	buflen = sizeof (valbuf);
304eda14cbcSMatt Macy 	n = strlcpy(dstp, keybuf, buflen);
305eda14cbcSMatt Macy 	if (n >= sizeof (valbuf)) {
306eda14cbcSMatt Macy 		errno = EMSGSIZE;
307eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s for eid=%llu: %s",
308eda14cbcSMatt Macy 		    keybuf, eid, "Exceeded buffer size");
309eda14cbcSMatt Macy 		return (-1);
310eda14cbcSMatt Macy 	}
311eda14cbcSMatt Macy 	dstp += n;
312eda14cbcSMatt Macy 	buflen -= n;
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy 	*dstp++ = '=';
315eda14cbcSMatt Macy 	buflen--;
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 	if (buflen <= 0) {
318eda14cbcSMatt Macy 		errno = EMSGSIZE;
319eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s for eid=%llu: %s",
320eda14cbcSMatt Macy 		    keybuf, eid, "Exceeded buffer size");
321eda14cbcSMatt Macy 		return (-1);
322eda14cbcSMatt Macy 	}
323eda14cbcSMatt Macy 
324eda14cbcSMatt Macy 	va_start(vargs, fmt);
325eda14cbcSMatt Macy 	n = vsnprintf(dstp, buflen, fmt, vargs);
326eda14cbcSMatt Macy 	va_end(vargs);
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	if ((n < 0) || (n >= buflen)) {
329eda14cbcSMatt Macy 		errno = EMSGSIZE;
330eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s for eid=%llu: %s",
331eda14cbcSMatt Macy 		    keybuf, eid, "Exceeded buffer size");
332eda14cbcSMatt Macy 		return (-1);
333eda14cbcSMatt Macy 	} else if (zed_strings_add(zsp, keybuf, valbuf) < 0) {
334eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s for eid=%llu: %s",
335eda14cbcSMatt Macy 		    keybuf, eid, strerror(errno));
336eda14cbcSMatt Macy 		return (-1);
337eda14cbcSMatt Macy 	}
338eda14cbcSMatt Macy 	return (0);
339eda14cbcSMatt Macy }
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy static int
342eda14cbcSMatt Macy _zed_event_add_array_err(uint64_t eid, const char *name)
343eda14cbcSMatt Macy {
344eda14cbcSMatt Macy 	errno = EMSGSIZE;
345eda14cbcSMatt Macy 	zed_log_msg(LOG_WARNING,
346eda14cbcSMatt Macy 	    "Failed to convert nvpair \"%s\" for eid=%llu: "
347eda14cbcSMatt Macy 	    "Exceeded buffer size", name, eid);
348eda14cbcSMatt Macy 	return (-1);
349eda14cbcSMatt Macy }
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy static int
352eda14cbcSMatt Macy _zed_event_add_int8_array(uint64_t eid, zed_strings_t *zsp,
353eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
354eda14cbcSMatt Macy {
355eda14cbcSMatt Macy 	char buf[MAXBUF];
356eda14cbcSMatt Macy 	int buflen = sizeof (buf);
357eda14cbcSMatt Macy 	const char *name;
358eda14cbcSMatt Macy 	int8_t *i8p;
359eda14cbcSMatt Macy 	uint_t nelem;
360eda14cbcSMatt Macy 	uint_t i;
361eda14cbcSMatt Macy 	char *p;
362eda14cbcSMatt Macy 	int n;
363eda14cbcSMatt Macy 
364eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_INT8_ARRAY));
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy 	name = nvpair_name(nvp);
367eda14cbcSMatt Macy 	(void) nvpair_value_int8_array(nvp, &i8p, &nelem);
368eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
369eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%d ", i8p[i]);
370eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
371eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
372eda14cbcSMatt Macy 		p += n;
373eda14cbcSMatt Macy 		buflen -= n;
374eda14cbcSMatt Macy 	}
375eda14cbcSMatt Macy 	if (nelem > 0)
376eda14cbcSMatt Macy 		*--p = '\0';
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
379eda14cbcSMatt Macy }
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy static int
382eda14cbcSMatt Macy _zed_event_add_uint8_array(uint64_t eid, zed_strings_t *zsp,
383eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
384eda14cbcSMatt Macy {
385eda14cbcSMatt Macy 	char buf[MAXBUF];
386eda14cbcSMatt Macy 	int buflen = sizeof (buf);
387eda14cbcSMatt Macy 	const char *name;
388eda14cbcSMatt Macy 	uint8_t *u8p;
389eda14cbcSMatt Macy 	uint_t nelem;
390eda14cbcSMatt Macy 	uint_t i;
391eda14cbcSMatt Macy 	char *p;
392eda14cbcSMatt Macy 	int n;
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_UINT8_ARRAY));
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 	name = nvpair_name(nvp);
397eda14cbcSMatt Macy 	(void) nvpair_value_uint8_array(nvp, &u8p, &nelem);
398eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
399eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%u ", u8p[i]);
400eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
401eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
402eda14cbcSMatt Macy 		p += n;
403eda14cbcSMatt Macy 		buflen -= n;
404eda14cbcSMatt Macy 	}
405eda14cbcSMatt Macy 	if (nelem > 0)
406eda14cbcSMatt Macy 		*--p = '\0';
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
409eda14cbcSMatt Macy }
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy static int
412eda14cbcSMatt Macy _zed_event_add_int16_array(uint64_t eid, zed_strings_t *zsp,
413eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
414eda14cbcSMatt Macy {
415eda14cbcSMatt Macy 	char buf[MAXBUF];
416eda14cbcSMatt Macy 	int buflen = sizeof (buf);
417eda14cbcSMatt Macy 	const char *name;
418eda14cbcSMatt Macy 	int16_t *i16p;
419eda14cbcSMatt Macy 	uint_t nelem;
420eda14cbcSMatt Macy 	uint_t i;
421eda14cbcSMatt Macy 	char *p;
422eda14cbcSMatt Macy 	int n;
423eda14cbcSMatt Macy 
424eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_INT16_ARRAY));
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 	name = nvpair_name(nvp);
427eda14cbcSMatt Macy 	(void) nvpair_value_int16_array(nvp, &i16p, &nelem);
428eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
429eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%d ", i16p[i]);
430eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
431eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
432eda14cbcSMatt Macy 		p += n;
433eda14cbcSMatt Macy 		buflen -= n;
434eda14cbcSMatt Macy 	}
435eda14cbcSMatt Macy 	if (nelem > 0)
436eda14cbcSMatt Macy 		*--p = '\0';
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
439eda14cbcSMatt Macy }
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy static int
442eda14cbcSMatt Macy _zed_event_add_uint16_array(uint64_t eid, zed_strings_t *zsp,
443eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
444eda14cbcSMatt Macy {
445eda14cbcSMatt Macy 	char buf[MAXBUF];
446eda14cbcSMatt Macy 	int buflen = sizeof (buf);
447eda14cbcSMatt Macy 	const char *name;
448eda14cbcSMatt Macy 	uint16_t *u16p;
449eda14cbcSMatt Macy 	uint_t nelem;
450eda14cbcSMatt Macy 	uint_t i;
451eda14cbcSMatt Macy 	char *p;
452eda14cbcSMatt Macy 	int n;
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_UINT16_ARRAY));
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy 	name = nvpair_name(nvp);
457eda14cbcSMatt Macy 	(void) nvpair_value_uint16_array(nvp, &u16p, &nelem);
458eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
459eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%u ", u16p[i]);
460eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
461eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
462eda14cbcSMatt Macy 		p += n;
463eda14cbcSMatt Macy 		buflen -= n;
464eda14cbcSMatt Macy 	}
465eda14cbcSMatt Macy 	if (nelem > 0)
466eda14cbcSMatt Macy 		*--p = '\0';
467eda14cbcSMatt Macy 
468eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
469eda14cbcSMatt Macy }
470eda14cbcSMatt Macy 
471eda14cbcSMatt Macy static int
472eda14cbcSMatt Macy _zed_event_add_int32_array(uint64_t eid, zed_strings_t *zsp,
473eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
474eda14cbcSMatt Macy {
475eda14cbcSMatt Macy 	char buf[MAXBUF];
476eda14cbcSMatt Macy 	int buflen = sizeof (buf);
477eda14cbcSMatt Macy 	const char *name;
478eda14cbcSMatt Macy 	int32_t *i32p;
479eda14cbcSMatt Macy 	uint_t nelem;
480eda14cbcSMatt Macy 	uint_t i;
481eda14cbcSMatt Macy 	char *p;
482eda14cbcSMatt Macy 	int n;
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_INT32_ARRAY));
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	name = nvpair_name(nvp);
487eda14cbcSMatt Macy 	(void) nvpair_value_int32_array(nvp, &i32p, &nelem);
488eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
489eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%d ", i32p[i]);
490eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
491eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
492eda14cbcSMatt Macy 		p += n;
493eda14cbcSMatt Macy 		buflen -= n;
494eda14cbcSMatt Macy 	}
495eda14cbcSMatt Macy 	if (nelem > 0)
496eda14cbcSMatt Macy 		*--p = '\0';
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
499eda14cbcSMatt Macy }
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy static int
502eda14cbcSMatt Macy _zed_event_add_uint32_array(uint64_t eid, zed_strings_t *zsp,
503eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
504eda14cbcSMatt Macy {
505eda14cbcSMatt Macy 	char buf[MAXBUF];
506eda14cbcSMatt Macy 	int buflen = sizeof (buf);
507eda14cbcSMatt Macy 	const char *name;
508eda14cbcSMatt Macy 	uint32_t *u32p;
509eda14cbcSMatt Macy 	uint_t nelem;
510eda14cbcSMatt Macy 	uint_t i;
511eda14cbcSMatt Macy 	char *p;
512eda14cbcSMatt Macy 	int n;
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY));
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 	name = nvpair_name(nvp);
517eda14cbcSMatt Macy 	(void) nvpair_value_uint32_array(nvp, &u32p, &nelem);
518eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
519eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%u ", u32p[i]);
520eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
521eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
522eda14cbcSMatt Macy 		p += n;
523eda14cbcSMatt Macy 		buflen -= n;
524eda14cbcSMatt Macy 	}
525eda14cbcSMatt Macy 	if (nelem > 0)
526eda14cbcSMatt Macy 		*--p = '\0';
527eda14cbcSMatt Macy 
528eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
529eda14cbcSMatt Macy }
530eda14cbcSMatt Macy 
531eda14cbcSMatt Macy static int
532eda14cbcSMatt Macy _zed_event_add_int64_array(uint64_t eid, zed_strings_t *zsp,
533eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
534eda14cbcSMatt Macy {
535eda14cbcSMatt Macy 	char buf[MAXBUF];
536eda14cbcSMatt Macy 	int buflen = sizeof (buf);
537eda14cbcSMatt Macy 	const char *name;
538eda14cbcSMatt Macy 	int64_t *i64p;
539eda14cbcSMatt Macy 	uint_t nelem;
540eda14cbcSMatt Macy 	uint_t i;
541eda14cbcSMatt Macy 	char *p;
542eda14cbcSMatt Macy 	int n;
543eda14cbcSMatt Macy 
544eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_INT64_ARRAY));
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 	name = nvpair_name(nvp);
547eda14cbcSMatt Macy 	(void) nvpair_value_int64_array(nvp, &i64p, &nelem);
548eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
549eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%lld ", (u_longlong_t)i64p[i]);
550eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
551eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
552eda14cbcSMatt Macy 		p += n;
553eda14cbcSMatt Macy 		buflen -= n;
554eda14cbcSMatt Macy 	}
555eda14cbcSMatt Macy 	if (nelem > 0)
556eda14cbcSMatt Macy 		*--p = '\0';
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
559eda14cbcSMatt Macy }
560eda14cbcSMatt Macy 
561eda14cbcSMatt Macy static int
562eda14cbcSMatt Macy _zed_event_add_uint64_array(uint64_t eid, zed_strings_t *zsp,
563eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
564eda14cbcSMatt Macy {
565eda14cbcSMatt Macy 	char buf[MAXBUF];
566eda14cbcSMatt Macy 	int buflen = sizeof (buf);
567eda14cbcSMatt Macy 	const char *name;
568eda14cbcSMatt Macy 	const char *fmt;
569eda14cbcSMatt Macy 	uint64_t *u64p;
570eda14cbcSMatt Macy 	uint_t nelem;
571eda14cbcSMatt Macy 	uint_t i;
572eda14cbcSMatt Macy 	char *p;
573eda14cbcSMatt Macy 	int n;
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_UINT64_ARRAY));
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy 	name = nvpair_name(nvp);
578eda14cbcSMatt Macy 	fmt = _zed_event_value_is_hex(name) ? "0x%.16llX " : "%llu ";
579eda14cbcSMatt Macy 	(void) nvpair_value_uint64_array(nvp, &u64p, &nelem);
580eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
581eda14cbcSMatt Macy 		n = snprintf(p, buflen, fmt, (u_longlong_t)u64p[i]);
582eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
583eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
584eda14cbcSMatt Macy 		p += n;
585eda14cbcSMatt Macy 		buflen -= n;
586eda14cbcSMatt Macy 	}
587eda14cbcSMatt Macy 	if (nelem > 0)
588eda14cbcSMatt Macy 		*--p = '\0';
589eda14cbcSMatt Macy 
590eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
591eda14cbcSMatt Macy }
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy static int
594eda14cbcSMatt Macy _zed_event_add_string_array(uint64_t eid, zed_strings_t *zsp,
595eda14cbcSMatt Macy     const char *prefix, nvpair_t *nvp)
596eda14cbcSMatt Macy {
597eda14cbcSMatt Macy 	char buf[MAXBUF];
598eda14cbcSMatt Macy 	int buflen = sizeof (buf);
599eda14cbcSMatt Macy 	const char *name;
600eda14cbcSMatt Macy 	char **strp;
601eda14cbcSMatt Macy 	uint_t nelem;
602eda14cbcSMatt Macy 	uint_t i;
603eda14cbcSMatt Macy 	char *p;
604eda14cbcSMatt Macy 	int n;
605eda14cbcSMatt Macy 
606eda14cbcSMatt Macy 	assert((nvp != NULL) && (nvpair_type(nvp) == DATA_TYPE_STRING_ARRAY));
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy 	name = nvpair_name(nvp);
609eda14cbcSMatt Macy 	(void) nvpair_value_string_array(nvp, &strp, &nelem);
610eda14cbcSMatt Macy 	for (i = 0, p = buf; (i < nelem) && (buflen > 0); i++) {
611eda14cbcSMatt Macy 		n = snprintf(p, buflen, "%s ", strp[i] ? strp[i] : "<NULL>");
612eda14cbcSMatt Macy 		if ((n < 0) || (n >= buflen))
613eda14cbcSMatt Macy 			return (_zed_event_add_array_err(eid, name));
614eda14cbcSMatt Macy 		p += n;
615eda14cbcSMatt Macy 		buflen -= n;
616eda14cbcSMatt Macy 	}
617eda14cbcSMatt Macy 	if (nelem > 0)
618eda14cbcSMatt Macy 		*--p = '\0';
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 	return (_zed_event_add_var(eid, zsp, prefix, name, "%s", buf));
621eda14cbcSMatt Macy }
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy /*
624eda14cbcSMatt Macy  * Convert the nvpair [nvp] to a string which is added to the environment
625eda14cbcSMatt Macy  * of the child process.
626eda14cbcSMatt Macy  * Return 0 on success, -1 on error.
627eda14cbcSMatt Macy  */
628eda14cbcSMatt Macy static void
629eda14cbcSMatt Macy _zed_event_add_nvpair(uint64_t eid, zed_strings_t *zsp, nvpair_t *nvp)
630eda14cbcSMatt Macy {
631eda14cbcSMatt Macy 	const char *name;
632eda14cbcSMatt Macy 	data_type_t type;
633eda14cbcSMatt Macy 	const char *prefix = ZEVENT_VAR_PREFIX;
634eda14cbcSMatt Macy 	boolean_t b;
635eda14cbcSMatt Macy 	double d;
636eda14cbcSMatt Macy 	uint8_t i8;
637eda14cbcSMatt Macy 	uint16_t i16;
638eda14cbcSMatt Macy 	uint32_t i32;
639eda14cbcSMatt Macy 	uint64_t i64;
640eda14cbcSMatt Macy 	char *str;
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 	assert(zsp != NULL);
643eda14cbcSMatt Macy 	assert(nvp != NULL);
644eda14cbcSMatt Macy 
645eda14cbcSMatt Macy 	name = nvpair_name(nvp);
646eda14cbcSMatt Macy 	type = nvpair_type(nvp);
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy 	switch (type) {
649eda14cbcSMatt Macy 	case DATA_TYPE_BOOLEAN:
650eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%s", "1");
651eda14cbcSMatt Macy 		break;
652eda14cbcSMatt Macy 	case DATA_TYPE_BOOLEAN_VALUE:
653eda14cbcSMatt Macy 		(void) nvpair_value_boolean_value(nvp, &b);
654eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%s", b ? "1" : "0");
655eda14cbcSMatt Macy 		break;
656eda14cbcSMatt Macy 	case DATA_TYPE_BYTE:
657eda14cbcSMatt Macy 		(void) nvpair_value_byte(nvp, &i8);
658eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%d", i8);
659eda14cbcSMatt Macy 		break;
660eda14cbcSMatt Macy 	case DATA_TYPE_INT8:
661eda14cbcSMatt Macy 		(void) nvpair_value_int8(nvp, (int8_t *)&i8);
662eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%d", i8);
663eda14cbcSMatt Macy 		break;
664eda14cbcSMatt Macy 	case DATA_TYPE_UINT8:
665eda14cbcSMatt Macy 		(void) nvpair_value_uint8(nvp, &i8);
666eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%u", i8);
667eda14cbcSMatt Macy 		break;
668eda14cbcSMatt Macy 	case DATA_TYPE_INT16:
669eda14cbcSMatt Macy 		(void) nvpair_value_int16(nvp, (int16_t *)&i16);
670eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%d", i16);
671eda14cbcSMatt Macy 		break;
672eda14cbcSMatt Macy 	case DATA_TYPE_UINT16:
673eda14cbcSMatt Macy 		(void) nvpair_value_uint16(nvp, &i16);
674eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%u", i16);
675eda14cbcSMatt Macy 		break;
676eda14cbcSMatt Macy 	case DATA_TYPE_INT32:
677eda14cbcSMatt Macy 		(void) nvpair_value_int32(nvp, (int32_t *)&i32);
678eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%d", i32);
679eda14cbcSMatt Macy 		break;
680eda14cbcSMatt Macy 	case DATA_TYPE_UINT32:
681eda14cbcSMatt Macy 		(void) nvpair_value_uint32(nvp, &i32);
682eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%u", i32);
683eda14cbcSMatt Macy 		break;
684eda14cbcSMatt Macy 	case DATA_TYPE_INT64:
685eda14cbcSMatt Macy 		(void) nvpair_value_int64(nvp, (int64_t *)&i64);
686eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name,
687eda14cbcSMatt Macy 		    "%lld", (longlong_t)i64);
688eda14cbcSMatt Macy 		break;
689eda14cbcSMatt Macy 	case DATA_TYPE_UINT64:
690eda14cbcSMatt Macy 		(void) nvpair_value_uint64(nvp, &i64);
691eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name,
692eda14cbcSMatt Macy 		    (_zed_event_value_is_hex(name) ? "0x%.16llX" : "%llu"),
693eda14cbcSMatt Macy 		    (u_longlong_t)i64);
694eda14cbcSMatt Macy 		/*
695eda14cbcSMatt Macy 		 * shadow readable strings for vdev state pairs
696eda14cbcSMatt Macy 		 */
697eda14cbcSMatt Macy 		if (strcmp(name, FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
698eda14cbcSMatt Macy 		    strcmp(name, FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
699eda14cbcSMatt Macy 			char alt[32];
700eda14cbcSMatt Macy 
701eda14cbcSMatt Macy 			(void) snprintf(alt, sizeof (alt), "%s_str", name);
702eda14cbcSMatt Macy 			_zed_event_add_var(eid, zsp, prefix, alt, "%s",
703eda14cbcSMatt Macy 			    zpool_state_to_name(i64, VDEV_AUX_NONE));
704eda14cbcSMatt Macy 		} else
705eda14cbcSMatt Macy 		/*
706eda14cbcSMatt Macy 		 * shadow readable strings for pool state
707eda14cbcSMatt Macy 		 */
708eda14cbcSMatt Macy 		if (strcmp(name, FM_EREPORT_PAYLOAD_ZFS_POOL_STATE) == 0) {
709eda14cbcSMatt Macy 			char alt[32];
710eda14cbcSMatt Macy 
711eda14cbcSMatt Macy 			(void) snprintf(alt, sizeof (alt), "%s_str", name);
712eda14cbcSMatt Macy 			_zed_event_add_var(eid, zsp, prefix, alt, "%s",
713eda14cbcSMatt Macy 			    zpool_pool_state_to_name(i64));
714eda14cbcSMatt Macy 		}
715eda14cbcSMatt Macy 		break;
716eda14cbcSMatt Macy 	case DATA_TYPE_DOUBLE:
717eda14cbcSMatt Macy 		(void) nvpair_value_double(nvp, &d);
718eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name, "%g", d);
719eda14cbcSMatt Macy 		break;
720eda14cbcSMatt Macy 	case DATA_TYPE_HRTIME:
721eda14cbcSMatt Macy 		(void) nvpair_value_hrtime(nvp, (hrtime_t *)&i64);
722eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name,
723eda14cbcSMatt Macy 		    "%llu", (u_longlong_t)i64);
724eda14cbcSMatt Macy 		break;
725eda14cbcSMatt Macy 	case DATA_TYPE_STRING:
726eda14cbcSMatt Macy 		(void) nvpair_value_string(nvp, &str);
727eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, prefix, name,
728eda14cbcSMatt Macy 		    "%s", (str ? str : "<NULL>"));
729eda14cbcSMatt Macy 		break;
730eda14cbcSMatt Macy 	case DATA_TYPE_INT8_ARRAY:
731eda14cbcSMatt Macy 		_zed_event_add_int8_array(eid, zsp, prefix, nvp);
732eda14cbcSMatt Macy 		break;
733eda14cbcSMatt Macy 	case DATA_TYPE_UINT8_ARRAY:
734eda14cbcSMatt Macy 		_zed_event_add_uint8_array(eid, zsp, prefix, nvp);
735eda14cbcSMatt Macy 		break;
736eda14cbcSMatt Macy 	case DATA_TYPE_INT16_ARRAY:
737eda14cbcSMatt Macy 		_zed_event_add_int16_array(eid, zsp, prefix, nvp);
738eda14cbcSMatt Macy 		break;
739eda14cbcSMatt Macy 	case DATA_TYPE_UINT16_ARRAY:
740eda14cbcSMatt Macy 		_zed_event_add_uint16_array(eid, zsp, prefix, nvp);
741eda14cbcSMatt Macy 		break;
742eda14cbcSMatt Macy 	case DATA_TYPE_INT32_ARRAY:
743eda14cbcSMatt Macy 		_zed_event_add_int32_array(eid, zsp, prefix, nvp);
744eda14cbcSMatt Macy 		break;
745eda14cbcSMatt Macy 	case DATA_TYPE_UINT32_ARRAY:
746eda14cbcSMatt Macy 		_zed_event_add_uint32_array(eid, zsp, prefix, nvp);
747eda14cbcSMatt Macy 		break;
748eda14cbcSMatt Macy 	case DATA_TYPE_INT64_ARRAY:
749eda14cbcSMatt Macy 		_zed_event_add_int64_array(eid, zsp, prefix, nvp);
750eda14cbcSMatt Macy 		break;
751eda14cbcSMatt Macy 	case DATA_TYPE_UINT64_ARRAY:
752eda14cbcSMatt Macy 		_zed_event_add_uint64_array(eid, zsp, prefix, nvp);
753eda14cbcSMatt Macy 		break;
754eda14cbcSMatt Macy 	case DATA_TYPE_STRING_ARRAY:
755eda14cbcSMatt Macy 		_zed_event_add_string_array(eid, zsp, prefix, nvp);
756eda14cbcSMatt Macy 		break;
75716038816SMartin Matuska 	case DATA_TYPE_NVLIST:
75816038816SMartin Matuska 	case DATA_TYPE_BOOLEAN_ARRAY:
75916038816SMartin Matuska 	case DATA_TYPE_BYTE_ARRAY:
760eda14cbcSMatt Macy 	case DATA_TYPE_NVLIST_ARRAY:
76116038816SMartin Matuska 		_zed_event_add_var(eid, zsp, prefix, name, "_NOT_IMPLEMENTED_");
762eda14cbcSMatt Macy 		break;
763eda14cbcSMatt Macy 	default:
764eda14cbcSMatt Macy 		errno = EINVAL;
765eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
766eda14cbcSMatt Macy 		    "Failed to convert nvpair \"%s\" for eid=%llu: "
767eda14cbcSMatt Macy 		    "Unrecognized type=%u", name, eid, (unsigned int) type);
768eda14cbcSMatt Macy 		break;
769eda14cbcSMatt Macy 	}
770eda14cbcSMatt Macy }
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy /*
773eda14cbcSMatt Macy  * Restrict various environment variables to safe and sane values
774eda14cbcSMatt Macy  * when constructing the environment for the child process, unless
775eda14cbcSMatt Macy  * we're running with a custom $PATH (like under the ZFS test suite).
776eda14cbcSMatt Macy  *
777eda14cbcSMatt Macy  * Reference: Secure Programming Cookbook by Viega & Messier, Section 1.1.
778eda14cbcSMatt Macy  */
779eda14cbcSMatt Macy static void
780eda14cbcSMatt Macy _zed_event_add_env_restrict(uint64_t eid, zed_strings_t *zsp,
781eda14cbcSMatt Macy     const char *path)
782eda14cbcSMatt Macy {
783eda14cbcSMatt Macy 	const char *env_restrict[][2] = {
784eda14cbcSMatt Macy 		{ "IFS",		" \t\n" },
785eda14cbcSMatt Macy 		{ "PATH",		_PATH_STDPATH },
786eda14cbcSMatt Macy 		{ "ZDB",		SBINDIR "/zdb" },
787eda14cbcSMatt Macy 		{ "ZED",		SBINDIR "/zed" },
788eda14cbcSMatt Macy 		{ "ZFS",		SBINDIR "/zfs" },
789eda14cbcSMatt Macy 		{ "ZINJECT",		SBINDIR "/zinject" },
790eda14cbcSMatt Macy 		{ "ZPOOL",		SBINDIR "/zpool" },
791eda14cbcSMatt Macy 		{ "ZFS_ALIAS",		ZFS_META_ALIAS },
792eda14cbcSMatt Macy 		{ "ZFS_VERSION",	ZFS_META_VERSION },
793eda14cbcSMatt Macy 		{ "ZFS_RELEASE",	ZFS_META_RELEASE },
794eda14cbcSMatt Macy 		{ NULL,			NULL }
795eda14cbcSMatt Macy 	};
796eda14cbcSMatt Macy 
797eda14cbcSMatt Macy 	/*
798eda14cbcSMatt Macy 	 * If we have a custom $PATH, use the default ZFS binary locations
799eda14cbcSMatt Macy 	 * instead of the hard-coded ones.
800eda14cbcSMatt Macy 	 */
801eda14cbcSMatt Macy 	const char *env_path[][2] = {
802eda14cbcSMatt Macy 		{ "IFS",		" \t\n" },
803eda14cbcSMatt Macy 		{ "PATH",		NULL }, /* $PATH copied in later on */
804eda14cbcSMatt Macy 		{ "ZDB",		"zdb" },
805eda14cbcSMatt Macy 		{ "ZED",		"zed" },
806eda14cbcSMatt Macy 		{ "ZFS",		"zfs" },
807eda14cbcSMatt Macy 		{ "ZINJECT",		"zinject" },
808eda14cbcSMatt Macy 		{ "ZPOOL",		"zpool" },
809eda14cbcSMatt Macy 		{ "ZFS_ALIAS",		ZFS_META_ALIAS },
810eda14cbcSMatt Macy 		{ "ZFS_VERSION",	ZFS_META_VERSION },
811eda14cbcSMatt Macy 		{ "ZFS_RELEASE",	ZFS_META_RELEASE },
812eda14cbcSMatt Macy 		{ NULL,			NULL }
813eda14cbcSMatt Macy 	};
814eda14cbcSMatt Macy 	const char *(*pa)[2];
815eda14cbcSMatt Macy 
816eda14cbcSMatt Macy 	assert(zsp != NULL);
817eda14cbcSMatt Macy 
818eda14cbcSMatt Macy 	pa = path != NULL ? env_path : env_restrict;
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	for (; *(*pa); pa++) {
821eda14cbcSMatt Macy 		/* Use our custom $PATH if we have one */
822eda14cbcSMatt Macy 		if (path != NULL && strcmp((*pa)[0], "PATH") == 0)
823eda14cbcSMatt Macy 			(*pa)[1] = path;
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, NULL, (*pa)[0], "%s", (*pa)[1]);
826eda14cbcSMatt Macy 	}
827eda14cbcSMatt Macy }
828eda14cbcSMatt Macy 
829eda14cbcSMatt Macy /*
830eda14cbcSMatt Macy  * Preserve specified variables from the parent environment
831eda14cbcSMatt Macy  * when constructing the environment for the child process.
832eda14cbcSMatt Macy  *
833eda14cbcSMatt Macy  * Reference: Secure Programming Cookbook by Viega & Messier, Section 1.1.
834eda14cbcSMatt Macy  */
835eda14cbcSMatt Macy static void
836eda14cbcSMatt Macy _zed_event_add_env_preserve(uint64_t eid, zed_strings_t *zsp)
837eda14cbcSMatt Macy {
838eda14cbcSMatt Macy 	const char *env_preserve[] = {
839eda14cbcSMatt Macy 		"TZ",
840eda14cbcSMatt Macy 		NULL
841eda14cbcSMatt Macy 	};
842eda14cbcSMatt Macy 	const char **keyp;
843eda14cbcSMatt Macy 	const char *val;
844eda14cbcSMatt Macy 
845eda14cbcSMatt Macy 	assert(zsp != NULL);
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy 	for (keyp = env_preserve; *keyp; keyp++) {
848eda14cbcSMatt Macy 		if ((val = getenv(*keyp)))
849eda14cbcSMatt Macy 			_zed_event_add_var(eid, zsp, NULL, *keyp, "%s", val);
850eda14cbcSMatt Macy 	}
851eda14cbcSMatt Macy }
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy /*
854eda14cbcSMatt Macy  * Compute the "subclass" by removing the first 3 components of [class]
855eda14cbcSMatt Macy  * (which will always be of the form "*.fs.zfs").  Return a pointer inside
856eda14cbcSMatt Macy  * the string [class], or NULL if insufficient components exist.
857eda14cbcSMatt Macy  */
858eda14cbcSMatt Macy static const char *
859eda14cbcSMatt Macy _zed_event_get_subclass(const char *class)
860eda14cbcSMatt Macy {
861eda14cbcSMatt Macy 	const char *p;
862eda14cbcSMatt Macy 	int i;
863eda14cbcSMatt Macy 
864eda14cbcSMatt Macy 	if (!class)
865eda14cbcSMatt Macy 		return (NULL);
866eda14cbcSMatt Macy 
867eda14cbcSMatt Macy 	p = class;
868eda14cbcSMatt Macy 	for (i = 0; i < 3; i++) {
869eda14cbcSMatt Macy 		p = strchr(p, '.');
870eda14cbcSMatt Macy 		if (!p)
871eda14cbcSMatt Macy 			break;
872eda14cbcSMatt Macy 		p++;
873eda14cbcSMatt Macy 	}
874eda14cbcSMatt Macy 	return (p);
875eda14cbcSMatt Macy }
876eda14cbcSMatt Macy 
877eda14cbcSMatt Macy /*
878eda14cbcSMatt Macy  * Convert the zevent time from a 2-element array of 64b integers
879eda14cbcSMatt Macy  * into a more convenient form:
880eda14cbcSMatt Macy  * - TIME_SECS is the second component of the time.
881eda14cbcSMatt Macy  * - TIME_NSECS is the nanosecond component of the time.
882eda14cbcSMatt Macy  * - TIME_STRING is an almost-RFC3339-compliant string representation.
883eda14cbcSMatt Macy  */
884eda14cbcSMatt Macy static void
885eda14cbcSMatt Macy _zed_event_add_time_strings(uint64_t eid, zed_strings_t *zsp, int64_t etime[])
886eda14cbcSMatt Macy {
887*716fd348SMartin Matuska 	struct tm stp;
888eda14cbcSMatt Macy 	char buf[32];
889eda14cbcSMatt Macy 
890eda14cbcSMatt Macy 	assert(zsp != NULL);
891eda14cbcSMatt Macy 	assert(etime != NULL);
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy 	_zed_event_add_var(eid, zsp, ZEVENT_VAR_PREFIX, "TIME_SECS",
894*716fd348SMartin Matuska 	    "%" PRId64, etime[0]);
895eda14cbcSMatt Macy 	_zed_event_add_var(eid, zsp, ZEVENT_VAR_PREFIX, "TIME_NSECS",
896*716fd348SMartin Matuska 	    "%" PRId64, etime[1]);
897eda14cbcSMatt Macy 
898*716fd348SMartin Matuska 	if (!localtime_r((const time_t *) &etime[0], &stp)) {
899eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s%s for eid=%llu: %s",
900eda14cbcSMatt Macy 		    ZEVENT_VAR_PREFIX, "TIME_STRING", eid, "localtime error");
901*716fd348SMartin Matuska 	} else if (!strftime(buf, sizeof (buf), "%Y-%m-%d %H:%M:%S%z", &stp)) {
902eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to add %s%s for eid=%llu: %s",
903eda14cbcSMatt Macy 		    ZEVENT_VAR_PREFIX, "TIME_STRING", eid, "strftime error");
904eda14cbcSMatt Macy 	} else {
905eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, ZEVENT_VAR_PREFIX, "TIME_STRING",
906eda14cbcSMatt Macy 		    "%s", buf);
907eda14cbcSMatt Macy 	}
908eda14cbcSMatt Macy }
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy /*
911eda14cbcSMatt Macy  * Service the next zevent, blocking until one is available.
912eda14cbcSMatt Macy  */
913eda14cbcSMatt Macy int
914eda14cbcSMatt Macy zed_event_service(struct zed_conf *zcp)
915eda14cbcSMatt Macy {
916eda14cbcSMatt Macy 	nvlist_t *nvl;
917eda14cbcSMatt Macy 	nvpair_t *nvp;
918eda14cbcSMatt Macy 	int n_dropped;
919eda14cbcSMatt Macy 	zed_strings_t *zsp;
920eda14cbcSMatt Macy 	uint64_t eid;
921eda14cbcSMatt Macy 	int64_t *etime;
922eda14cbcSMatt Macy 	uint_t nelem;
923eda14cbcSMatt Macy 	char *class;
924eda14cbcSMatt Macy 	const char *subclass;
925eda14cbcSMatt Macy 	int rv;
926eda14cbcSMatt Macy 
927eda14cbcSMatt Macy 	if (!zcp) {
928eda14cbcSMatt Macy 		errno = EINVAL;
929eda14cbcSMatt Macy 		zed_log_msg(LOG_ERR, "Failed to service zevent: %s",
930eda14cbcSMatt Macy 		    strerror(errno));
931eda14cbcSMatt Macy 		return (EINVAL);
932eda14cbcSMatt Macy 	}
933eda14cbcSMatt Macy 	rv = zpool_events_next(zcp->zfs_hdl, &nvl, &n_dropped, ZEVENT_NONE,
934eda14cbcSMatt Macy 	    zcp->zevent_fd);
935eda14cbcSMatt Macy 
936eda14cbcSMatt Macy 	if ((rv != 0) || !nvl)
937eda14cbcSMatt Macy 		return (errno);
938eda14cbcSMatt Macy 
939eda14cbcSMatt Macy 	if (n_dropped > 0) {
940eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Missed %d events", n_dropped);
94116038816SMartin Matuska 		_bump_event_queue_length();
942eda14cbcSMatt Macy 	}
943eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(nvl, "eid", &eid) != 0) {
944eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING, "Failed to lookup zevent eid");
945eda14cbcSMatt Macy 	} else if (nvlist_lookup_int64_array(
946eda14cbcSMatt Macy 	    nvl, "time", &etime, &nelem) != 0) {
947eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
948eda14cbcSMatt Macy 		    "Failed to lookup zevent time (eid=%llu)", eid);
949eda14cbcSMatt Macy 	} else if (nelem != 2) {
950eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
951eda14cbcSMatt Macy 		    "Failed to lookup zevent time (eid=%llu, nelem=%u)",
952eda14cbcSMatt Macy 		    eid, nelem);
953eda14cbcSMatt Macy 	} else if (nvlist_lookup_string(nvl, "class", &class) != 0) {
954eda14cbcSMatt Macy 		zed_log_msg(LOG_WARNING,
955eda14cbcSMatt Macy 		    "Failed to lookup zevent class (eid=%llu)", eid);
956eda14cbcSMatt Macy 	} else {
957eda14cbcSMatt Macy 		/* let internal modules see this event first */
958eda14cbcSMatt Macy 		zfs_agent_post_event(class, NULL, nvl);
959eda14cbcSMatt Macy 
960eda14cbcSMatt Macy 		zsp = zed_strings_create();
961eda14cbcSMatt Macy 
962eda14cbcSMatt Macy 		nvp = NULL;
963eda14cbcSMatt Macy 		while ((nvp = nvlist_next_nvpair(nvl, nvp)))
964eda14cbcSMatt Macy 			_zed_event_add_nvpair(eid, zsp, nvp);
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy 		_zed_event_add_env_restrict(eid, zsp, zcp->path);
967eda14cbcSMatt Macy 		_zed_event_add_env_preserve(eid, zsp);
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, ZED_VAR_PREFIX, "PID",
970eda14cbcSMatt Macy 		    "%d", (int)getpid());
971eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, ZED_VAR_PREFIX, "ZEDLET_DIR",
972eda14cbcSMatt Macy 		    "%s", zcp->zedlet_dir);
973eda14cbcSMatt Macy 		subclass = _zed_event_get_subclass(class);
974eda14cbcSMatt Macy 		_zed_event_add_var(eid, zsp, ZEVENT_VAR_PREFIX, "SUBCLASS",
975eda14cbcSMatt Macy 		    "%s", (subclass ? subclass : class));
976eda14cbcSMatt Macy 
977eda14cbcSMatt Macy 		_zed_event_add_time_strings(eid, zsp, etime);
978eda14cbcSMatt Macy 
97916038816SMartin Matuska 		zed_exec_process(eid, class, subclass, zcp, zsp);
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 		zed_conf_write_state(zcp, eid, etime);
982eda14cbcSMatt Macy 
983eda14cbcSMatt Macy 		zed_strings_destroy(zsp);
984eda14cbcSMatt Macy 	}
985eda14cbcSMatt Macy 	nvlist_free(nvl);
986eda14cbcSMatt Macy 	return (0);
987eda14cbcSMatt Macy }
988