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