Path: blob/main/sys/contrib/openzfs/cmd/zed/zed_conf.c
48380 views
// SPDX-License-Identifier: CDDL-1.01/*2* This file is part of the ZFS Event Daemon (ZED).3*4* Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).5* Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.6* Refer to the OpenZFS git commit log for authoritative copyright attribution.7*8* The contents of this file are subject to the terms of the9* Common Development and Distribution License Version 1.0 (CDDL-1.0).10* You can obtain a copy of the license from the top-level file11* "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>.12* You may not use this file except in compliance with the license.13*/1415#include <assert.h>16#include <ctype.h>17#include <dirent.h>18#include <errno.h>19#include <fcntl.h>20#include <libgen.h>21#include <limits.h>22#include <stdio.h>23#include <stdlib.h>24#include <string.h>25#include <sys/types.h>26#include <sys/stat.h>27#include <sys/uio.h>28#include <unistd.h>29#include "zed.h"30#include "zed_conf.h"31#include "zed_file.h"32#include "zed_log.h"33#include "zed_strings.h"3435/*36* Initialise the configuration with default values.37*/38void39zed_conf_init(struct zed_conf *zcp)40{41memset(zcp, 0, sizeof (*zcp));4243/* zcp->zfs_hdl opened in zed_event_init() */44/* zcp->zedlets created in zed_conf_scan_dir() */4546zcp->pid_fd = -1; /* opened in zed_conf_write_pid() */47zcp->state_fd = -1; /* opened in zed_conf_open_state() */48zcp->zevent_fd = -1; /* opened in zed_event_init() */4950zcp->max_jobs = 16;51zcp->max_zevent_buf_len = 1 << 20;5253if (!(zcp->pid_file = strdup(ZED_PID_FILE)) ||54!(zcp->zedlet_dir = strdup(ZED_ZEDLET_DIR)) ||55!(zcp->state_file = strdup(ZED_STATE_FILE)))56zed_log_die("Failed to create conf: %s", strerror(errno));57}5859/*60* Destroy the configuration [zcp].61*62* Note: zfs_hdl & zevent_fd are destroyed via zed_event_fini().63*/64void65zed_conf_destroy(struct zed_conf *zcp)66{67if (zcp->state_fd >= 0) {68if (close(zcp->state_fd) < 0)69zed_log_msg(LOG_WARNING,70"Failed to close state file \"%s\": %s",71zcp->state_file, strerror(errno));72zcp->state_fd = -1;73}74if (zcp->pid_file) {75if ((unlink(zcp->pid_file) < 0) && (errno != ENOENT))76zed_log_msg(LOG_WARNING,77"Failed to remove PID file \"%s\": %s",78zcp->pid_file, strerror(errno));79}80if (zcp->pid_fd >= 0) {81if (close(zcp->pid_fd) < 0)82zed_log_msg(LOG_WARNING,83"Failed to close PID file \"%s\": %s",84zcp->pid_file, strerror(errno));85zcp->pid_fd = -1;86}87if (zcp->pid_file) {88free(zcp->pid_file);89zcp->pid_file = NULL;90}91if (zcp->zedlet_dir) {92free(zcp->zedlet_dir);93zcp->zedlet_dir = NULL;94}95if (zcp->state_file) {96free(zcp->state_file);97zcp->state_file = NULL;98}99if (zcp->zedlets) {100zed_strings_destroy(zcp->zedlets);101zcp->zedlets = NULL;102}103}104105/*106* Display command-line help and exit.107*108* If [got_err] is 0, output to stdout and exit normally;109* otherwise, output to stderr and exit with a failure status.110*/111static void112_zed_conf_display_help(const char *prog, boolean_t got_err)113{114struct opt { const char *o, *d, *v; };115116FILE *fp = got_err ? stderr : stdout;117118struct opt *oo;119struct opt iopts[] = {120{ .o = "-h", .d = "Display help" },121{ .o = "-L", .d = "Display license information" },122{ .o = "-V", .d = "Display version information" },123{},124};125struct opt nopts[] = {126{ .o = "-v", .d = "Be verbose" },127{ .o = "-f", .d = "Force daemon to run" },128{ .o = "-F", .d = "Run daemon in the foreground" },129{ .o = "-I",130.d = "Idle daemon until kernel module is (re)loaded" },131{ .o = "-M", .d = "Lock all pages in memory" },132{ .o = "-P", .d = "$PATH for ZED to use (only used by ZTS)" },133{ .o = "-Z", .d = "Zero state file" },134{},135};136struct opt vopts[] = {137{ .o = "-d DIR", .d = "Read enabled ZEDLETs from DIR.",138.v = ZED_ZEDLET_DIR },139{ .o = "-p FILE", .d = "Write daemon's PID to FILE.",140.v = ZED_PID_FILE },141{ .o = "-s FILE", .d = "Write daemon's state to FILE.",142.v = ZED_STATE_FILE },143{ .o = "-j JOBS", .d = "Start at most JOBS at once.",144.v = "16" },145{ .o = "-b LEN", .d = "Cap kernel event buffer at LEN entries.",146.v = "1048576" },147{},148};149150fprintf(fp, "Usage: %s [OPTION]...\n", (prog ? prog : "zed"));151fprintf(fp, "\n");152for (oo = iopts; oo->o; ++oo)153fprintf(fp, " %*s %s\n", -8, oo->o, oo->d);154fprintf(fp, "\n");155for (oo = nopts; oo->o; ++oo)156fprintf(fp, " %*s %s\n", -8, oo->o, oo->d);157fprintf(fp, "\n");158for (oo = vopts; oo->o; ++oo)159fprintf(fp, " %*s %s [%s]\n", -8, oo->o, oo->d, oo->v);160fprintf(fp, "\n");161162exit(got_err ? EXIT_FAILURE : EXIT_SUCCESS);163}164165/*166* Display license information to stdout and exit.167*/168static void169_zed_conf_display_license(void)170{171printf(172"The ZFS Event Daemon (ZED) is distributed under the terms of the\n"173" Common Development and Distribution License (CDDL-1.0)\n"174" <http://opensource.org/licenses/CDDL-1.0>.\n"175"\n"176"Developed at Lawrence Livermore National Laboratory"177" (LLNL-CODE-403049).\n"178"\n");179180exit(EXIT_SUCCESS);181}182183/*184* Display version information to stdout and exit.185*/186static void187_zed_conf_display_version(void)188{189printf("%s-%s-%s\n",190ZFS_META_NAME, ZFS_META_VERSION, ZFS_META_RELEASE);191192exit(EXIT_SUCCESS);193}194195/*196* Copy the [path] string to the [resultp] ptr.197* If [path] is not an absolute path, prefix it with the current working dir.198* If [resultp] is non-null, free its existing string before assignment.199*/200static void201_zed_conf_parse_path(char **resultp, const char *path)202{203char buf[PATH_MAX];204205assert(resultp != NULL);206assert(path != NULL);207208if (*resultp)209free(*resultp);210211if (path[0] == '/') {212*resultp = strdup(path);213} else {214if (!getcwd(buf, sizeof (buf)))215zed_log_die("Failed to get current working dir: %s",216strerror(errno));217218if (strlcat(buf, "/", sizeof (buf)) >= sizeof (buf) ||219strlcat(buf, path, sizeof (buf)) >= sizeof (buf))220zed_log_die("Failed to copy path: %s",221strerror(ENAMETOOLONG));222223*resultp = strdup(buf);224}225226if (!*resultp)227zed_log_die("Failed to copy path: %s", strerror(ENOMEM));228}229230/*231* Parse the command-line options into the configuration [zcp].232*/233void234zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv)235{236const char * const opts = ":hLVd:p:P:s:vfFMZIj:b:";237int opt;238unsigned long raw;239240if (!zcp || !argv || !argv[0])241zed_log_die("Failed to parse options: Internal error");242243opterr = 0; /* suppress default getopt err msgs */244245while ((opt = getopt(argc, argv, opts)) != -1) {246switch (opt) {247case 'h':248_zed_conf_display_help(argv[0], B_FALSE);249break;250case 'L':251_zed_conf_display_license();252break;253case 'V':254_zed_conf_display_version();255break;256case 'd':257_zed_conf_parse_path(&zcp->zedlet_dir, optarg);258break;259case 'I':260zcp->do_idle = 1;261break;262case 'p':263_zed_conf_parse_path(&zcp->pid_file, optarg);264break;265case 'P':266_zed_conf_parse_path(&zcp->path, optarg);267break;268case 's':269_zed_conf_parse_path(&zcp->state_file, optarg);270break;271case 'v':272zcp->do_verbose = 1;273break;274case 'f':275zcp->do_force = 1;276break;277case 'F':278zcp->do_foreground = 1;279break;280case 'M':281zcp->do_memlock = 1;282break;283case 'Z':284zcp->do_zero = 1;285break;286case 'j':287errno = 0;288raw = strtoul(optarg, NULL, 0);289if (errno == ERANGE || raw > INT16_MAX) {290zed_log_die("%lu is too many jobs", raw);291} if (raw == 0) {292zed_log_die("0 jobs makes no sense");293} else {294zcp->max_jobs = raw;295}296break;297case 'b':298errno = 0;299raw = strtoul(optarg, NULL, 0);300if (errno == ERANGE || raw > INT32_MAX) {301zed_log_die("%lu is too large", raw);302} if (raw == 0) {303zcp->max_zevent_buf_len = INT32_MAX;304} else {305zcp->max_zevent_buf_len = raw;306}307break;308case '?':309default:310if (optopt == '?')311_zed_conf_display_help(argv[0], B_FALSE);312313fprintf(stderr, "%s: Invalid option '-%c'\n\n",314argv[0], optopt);315_zed_conf_display_help(argv[0], B_TRUE);316break;317}318}319}320321/*322* Scan the [zcp] zedlet_dir for files to exec based on the event class.323* Files must be executable by user, but not writable by group or other.324* Dotfiles are ignored.325*326* Return 0 on success with an updated set of zedlets,327* or -1 on error with errno set.328*/329int330zed_conf_scan_dir(struct zed_conf *zcp)331{332zed_strings_t *zedlets;333DIR *dirp;334struct dirent *direntp;335char pathname[PATH_MAX];336struct stat st;337int n;338339if (!zcp) {340errno = EINVAL;341zed_log_msg(LOG_ERR, "Failed to scan zedlet dir: %s",342strerror(errno));343return (-1);344}345zedlets = zed_strings_create();346if (!zedlets) {347errno = ENOMEM;348zed_log_msg(LOG_WARNING, "Failed to scan dir \"%s\": %s",349zcp->zedlet_dir, strerror(errno));350return (-1);351}352dirp = opendir(zcp->zedlet_dir);353if (!dirp) {354int errno_bak = errno;355zed_log_msg(LOG_WARNING, "Failed to open dir \"%s\": %s",356zcp->zedlet_dir, strerror(errno));357zed_strings_destroy(zedlets);358errno = errno_bak;359return (-1);360}361while ((direntp = readdir(dirp))) {362if (direntp->d_name[0] == '.')363continue;364365n = snprintf(pathname, sizeof (pathname),366"%s/%s", zcp->zedlet_dir, direntp->d_name);367if ((n < 0) || (n >= sizeof (pathname))) {368zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",369direntp->d_name, strerror(ENAMETOOLONG));370continue;371}372if (stat(pathname, &st) < 0) {373zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",374pathname, strerror(errno));375continue;376}377if (!S_ISREG(st.st_mode)) {378zed_log_msg(LOG_INFO,379"Ignoring \"%s\": not a regular file",380direntp->d_name);381continue;382}383if ((st.st_uid != 0) && !zcp->do_force) {384zed_log_msg(LOG_NOTICE,385"Ignoring \"%s\": not owned by root",386direntp->d_name);387continue;388}389if (!(st.st_mode & S_IXUSR)) {390zed_log_msg(LOG_INFO,391"Ignoring \"%s\": not executable by user",392direntp->d_name);393continue;394}395if ((st.st_mode & S_IWGRP) && !zcp->do_force) {396zed_log_msg(LOG_NOTICE,397"Ignoring \"%s\": writable by group",398direntp->d_name);399continue;400}401if ((st.st_mode & S_IWOTH) && !zcp->do_force) {402zed_log_msg(LOG_NOTICE,403"Ignoring \"%s\": writable by other",404direntp->d_name);405continue;406}407if (zed_strings_add(zedlets, NULL, direntp->d_name) < 0) {408zed_log_msg(LOG_WARNING,409"Failed to register \"%s\": %s",410direntp->d_name, strerror(errno));411continue;412}413if (zcp->do_verbose)414zed_log_msg(LOG_INFO,415"Registered zedlet \"%s\"", direntp->d_name);416}417if (closedir(dirp) < 0) {418int errno_bak = errno;419zed_log_msg(LOG_WARNING, "Failed to close dir \"%s\": %s",420zcp->zedlet_dir, strerror(errno));421zed_strings_destroy(zedlets);422errno = errno_bak;423return (-1);424}425if (zcp->zedlets)426zed_strings_destroy(zcp->zedlets);427428zcp->zedlets = zedlets;429return (0);430}431432/*433* Write the PID file specified in [zcp].434* Return 0 on success, -1 on error.435*436* This must be called after fork()ing to become a daemon (so the correct PID437* is recorded), but before daemonization is complete and the parent process438* exits (for synchronization with systemd).439*/440int441zed_conf_write_pid(struct zed_conf *zcp)442{443char buf[PATH_MAX];444int n;445char *p;446mode_t mask;447int rv;448449if (!zcp || !zcp->pid_file) {450errno = EINVAL;451zed_log_msg(LOG_ERR, "Failed to create PID file: %s",452strerror(errno));453return (-1);454}455assert(zcp->pid_fd == -1);456/*457* Create PID file directory if needed.458*/459n = strlcpy(buf, zcp->pid_file, sizeof (buf));460if (n >= sizeof (buf)) {461errno = ENAMETOOLONG;462zed_log_msg(LOG_ERR, "Failed to create PID file: %s",463strerror(errno));464goto err;465}466p = strrchr(buf, '/');467if (p)468*p = '\0';469470if ((mkdirp(buf, 0755) < 0) && (errno != EEXIST)) {471zed_log_msg(LOG_ERR, "Failed to create directory \"%s\": %s",472buf, strerror(errno));473goto err;474}475/*476* Obtain PID file lock.477*/478mask = umask(0);479umask(mask | 022);480zcp->pid_fd = open(zcp->pid_file, O_RDWR | O_CREAT | O_CLOEXEC, 0644);481umask(mask);482if (zcp->pid_fd < 0) {483zed_log_msg(LOG_ERR, "Failed to open PID file \"%s\": %s",484zcp->pid_file, strerror(errno));485goto err;486}487rv = zed_file_lock(zcp->pid_fd);488if (rv < 0) {489zed_log_msg(LOG_ERR, "Failed to lock PID file \"%s\": %s",490zcp->pid_file, strerror(errno));491goto err;492} else if (rv > 0) {493pid_t pid = zed_file_is_locked(zcp->pid_fd);494if (pid < 0) {495zed_log_msg(LOG_ERR,496"Failed to test lock on PID file \"%s\"",497zcp->pid_file);498} else if (pid > 0) {499zed_log_msg(LOG_ERR,500"Found PID %d bound to PID file \"%s\"",501pid, zcp->pid_file);502} else {503zed_log_msg(LOG_ERR,504"Inconsistent lock state on PID file \"%s\"",505zcp->pid_file);506}507goto err;508}509/*510* Write PID file.511*/512n = snprintf(buf, sizeof (buf), "%d\n", (int)getpid());513if ((n < 0) || (n >= sizeof (buf))) {514errno = ERANGE;515zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s",516zcp->pid_file, strerror(errno));517} else if (write(zcp->pid_fd, buf, n) != n) {518zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s",519zcp->pid_file, strerror(errno));520} else if (fdatasync(zcp->pid_fd) < 0) {521zed_log_msg(LOG_ERR, "Failed to sync PID file \"%s\": %s",522zcp->pid_file, strerror(errno));523} else {524return (0);525}526527err:528if (zcp->pid_fd >= 0) {529(void) close(zcp->pid_fd);530zcp->pid_fd = -1;531}532return (-1);533}534535/*536* Open and lock the [zcp] state_file.537* Return 0 on success, -1 on error.538*539* FIXME: Move state information into kernel.540*/541int542zed_conf_open_state(struct zed_conf *zcp)543{544char dirbuf[PATH_MAX];545int n;546char *p;547int rv;548549if (!zcp || !zcp->state_file) {550errno = EINVAL;551zed_log_msg(LOG_ERR, "Failed to open state file: %s",552strerror(errno));553return (-1);554}555n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf));556if (n >= sizeof (dirbuf)) {557errno = ENAMETOOLONG;558zed_log_msg(LOG_WARNING, "Failed to open state file: %s",559strerror(errno));560return (-1);561}562p = strrchr(dirbuf, '/');563if (p)564*p = '\0';565566if ((mkdirp(dirbuf, 0755) < 0) && (errno != EEXIST)) {567zed_log_msg(LOG_WARNING,568"Failed to create directory \"%s\": %s",569dirbuf, strerror(errno));570return (-1);571}572if (zcp->state_fd >= 0) {573if (close(zcp->state_fd) < 0) {574zed_log_msg(LOG_WARNING,575"Failed to close state file \"%s\": %s",576zcp->state_file, strerror(errno));577return (-1);578}579}580if (zcp->do_zero)581(void) unlink(zcp->state_file);582583zcp->state_fd = open(zcp->state_file,584O_RDWR | O_CREAT | O_CLOEXEC, 0644);585if (zcp->state_fd < 0) {586zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s",587zcp->state_file, strerror(errno));588return (-1);589}590rv = zed_file_lock(zcp->state_fd);591if (rv < 0) {592zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s",593zcp->state_file, strerror(errno));594return (-1);595}596if (rv > 0) {597pid_t pid = zed_file_is_locked(zcp->state_fd);598if (pid < 0) {599zed_log_msg(LOG_WARNING,600"Failed to test lock on state file \"%s\"",601zcp->state_file);602} else if (pid > 0) {603zed_log_msg(LOG_WARNING,604"Found PID %d bound to state file \"%s\"",605pid, zcp->state_file);606} else {607zed_log_msg(LOG_WARNING,608"Inconsistent lock state on state file \"%s\"",609zcp->state_file);610}611return (-1);612}613return (0);614}615616/*617* Read the opened [zcp] state_file to obtain the eid & etime of the last event618* processed. Write the state from the last event to the [eidp] & [etime] args619* passed by reference. Note that etime[] is an array of size 2.620* Return 0 on success, -1 on error.621*/622int623zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[])624{625ssize_t len;626struct iovec iov[3];627ssize_t n;628629if (!zcp || !eidp || !etime) {630errno = EINVAL;631zed_log_msg(LOG_ERR,632"Failed to read state file: %s", strerror(errno));633return (-1);634}635if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) {636zed_log_msg(LOG_WARNING,637"Failed to reposition state file offset: %s",638strerror(errno));639return (-1);640}641len = 0;642iov[0].iov_base = eidp;643len += iov[0].iov_len = sizeof (*eidp);644iov[1].iov_base = &etime[0];645len += iov[1].iov_len = sizeof (etime[0]);646iov[2].iov_base = &etime[1];647len += iov[2].iov_len = sizeof (etime[1]);648649n = readv(zcp->state_fd, iov, 3);650if (n == 0) {651*eidp = 0;652} else if (n < 0) {653zed_log_msg(LOG_WARNING,654"Failed to read state file \"%s\": %s",655zcp->state_file, strerror(errno));656return (-1);657} else if (n != len) {658errno = EIO;659zed_log_msg(LOG_WARNING,660"Failed to read state file \"%s\": Read %zd of %zd bytes",661zcp->state_file, n, len);662return (-1);663}664return (0);665}666667/*668* Write the [eid] & [etime] of the last processed event to the opened669* [zcp] state_file. Note that etime[] is an array of size 2.670* Return 0 on success, -1 on error.671*/672int673zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])674{675ssize_t len;676struct iovec iov[3];677ssize_t n;678679if (!zcp) {680errno = EINVAL;681zed_log_msg(LOG_ERR,682"Failed to write state file: %s", strerror(errno));683return (-1);684}685if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) {686zed_log_msg(LOG_WARNING,687"Failed to reposition state file offset: %s",688strerror(errno));689return (-1);690}691len = 0;692iov[0].iov_base = &eid;693len += iov[0].iov_len = sizeof (eid);694iov[1].iov_base = &etime[0];695len += iov[1].iov_len = sizeof (etime[0]);696iov[2].iov_base = &etime[1];697len += iov[2].iov_len = sizeof (etime[1]);698699n = writev(zcp->state_fd, iov, 3);700if (n < 0) {701zed_log_msg(LOG_WARNING,702"Failed to write state file \"%s\": %s",703zcp->state_file, strerror(errno));704return (-1);705}706if (n != len) {707errno = EIO;708zed_log_msg(LOG_WARNING,709"Failed to write state file \"%s\": Wrote %zd of %zd bytes",710zcp->state_file, n, len);711return (-1);712}713if (fdatasync(zcp->state_fd) < 0) {714zed_log_msg(LOG_WARNING,715"Failed to sync state file \"%s\": %s",716zcp->state_file, strerror(errno));717return (-1);718}719return (0);720}721722723