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