2 * This file is part of the ZFS Event Daemon (ZED).
4 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
6 * Refer to the ZoL git commit log for authoritative copyright attribution.
8 * The contents of this file are subject to the terms of the
9 * Common Development and Distribution License Version 1.0 (CDDL-1.0).
10 * You can obtain a copy of the license from the top-level file
11 * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>.
12 * You may not use this file except in compliance with the license.
28 #include "zed_strings.h"
30 #define ZEVENT_FILENO 3
33 * Create an environment string array for passing to execve() using the
34 * NAME=VALUE strings in container [zsp].
35 * Return a newly-allocated environment, or NULL on error.
38 _zed_exec_create_env(zed_strings_t *zsp)
49 num_ptrs = zed_strings_count(zsp) + 1;
50 buflen = num_ptrs * sizeof (char *);
51 for (q = zed_strings_first(zsp); q; q = zed_strings_next(zsp))
52 buflen += strlen(q) + 1;
54 buf = calloc(1, buflen);
59 p = buf + (num_ptrs * sizeof (char *));
61 for (q = zed_strings_first(zsp); q; q = zed_strings_next(zsp)) {
69 assert(buf + buflen == p);
70 return ((char **)buf);
74 * Fork a child process to handle event [eid]. The program [prog]
75 * in directory [dir] is executed with the environment [env].
77 * The file descriptor [zfd] is the zevent_fd used to track the
78 * current cursor location within the zevent nvlist.
81 _zed_exec_fork_child(uint64_t eid, const char *dir, const char *prog,
96 n = snprintf(path, sizeof (path), "%s/%s", dir, prog);
97 if ((n < 0) || (n >= sizeof (path))) {
98 zed_log_msg(LOG_WARNING,
99 "Failed to fork \"%s\" for eid=%llu: %s",
100 prog, eid, strerror(ENAMETOOLONG));
105 zed_log_msg(LOG_WARNING,
106 "Failed to fork \"%s\" for eid=%llu: %s",
107 prog, eid, strerror(errno));
109 } else if (pid == 0) {
111 if ((fd = open("/dev/null", O_RDWR)) != -1) {
112 (void) dup2(fd, STDIN_FILENO);
113 (void) dup2(fd, STDOUT_FILENO);
114 (void) dup2(fd, STDERR_FILENO);
116 (void) dup2(zfd, ZEVENT_FILENO);
117 zed_file_close_from(ZEVENT_FILENO + 1);
118 execle(path, prog, NULL, env);
124 zed_log_msg(LOG_INFO, "Invoking \"%s\" eid=%llu pid=%d",
127 /* FIXME: Timeout rogue child processes with sigalarm? */
130 * Wait for child process using WNOHANG to limit
131 * the time spent waiting to 10 seconds (10,000ms).
133 for (n = 0; n < 1000; n++) {
134 wpid = waitpid(pid, &status, WNOHANG);
135 if (wpid == (pid_t)-1) {
138 zed_log_msg(LOG_WARNING,
139 "Failed to wait for \"%s\" eid=%llu pid=%d",
142 } else if (wpid == 0) {
145 /* child still running */
147 t.tv_nsec = 10000000; /* 10ms */
148 (void) nanosleep(&t, NULL);
152 if (WIFEXITED(status)) {
153 zed_log_msg(LOG_INFO,
154 "Finished \"%s\" eid=%llu pid=%d exit=%d",
155 prog, eid, pid, WEXITSTATUS(status));
156 } else if (WIFSIGNALED(status)) {
157 zed_log_msg(LOG_INFO,
158 "Finished \"%s\" eid=%llu pid=%d sig=%d/%s",
159 prog, eid, pid, WTERMSIG(status),
160 strsignal(WTERMSIG(status)));
162 zed_log_msg(LOG_INFO,
163 "Finished \"%s\" eid=%llu pid=%d status=0x%X",
164 prog, eid, (unsigned int) status);
170 * kill child process after 10 seconds
173 zed_log_msg(LOG_WARNING, "Killing hung \"%s\" pid=%d",
175 (void) kill(pid, SIGKILL);
180 * Process the event [eid] by synchronously invoking all zedlets with a
181 * matching class prefix.
183 * Each executable in [zedlets] from the directory [dir] is matched against
184 * the event's [class], [subclass], and the "all" class (which matches
185 * all events). Every zedlet with a matching class prefix is invoked.
186 * The NAME=VALUE strings in [envs] will be passed to the zedlet as
187 * environment variables.
189 * The file descriptor [zfd] is the zevent_fd used to track the
190 * current cursor location within the zevent nvlist.
192 * Return 0 on success, -1 on error.
195 zed_exec_process(uint64_t eid, const char *class, const char *subclass,
196 const char *dir, zed_strings_t *zedlets, zed_strings_t *envs, int zfd)
198 const char *class_strings[4];
199 const char *allclass = "all";
205 if (!dir || !zedlets || !envs || zfd < 0)
221 e = _zed_exec_create_env(envs);
223 for (z = zed_strings_first(zedlets); z; z = zed_strings_next(zedlets)) {
224 for (csp = class_strings; *csp; csp++) {
226 if ((strncmp(z, *csp, n) == 0) && !isalpha(z[n]))
227 _zed_exec_fork_child(eid, dir, z, e, zfd);