1 /* 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 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. 13 */ 14 15 #include <assert.h> 16 #include <ctype.h> 17 #include <errno.h> 18 #include <fcntl.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <stddef.h> 22 #include <sys/avl.h> 23 #include <sys/resource.h> 24 #include <sys/stat.h> 25 #include <sys/wait.h> 26 #include <time.h> 27 #include <unistd.h> 28 #include <pthread.h> 29 #include <signal.h> 30 31 #include "zed_exec.h" 32 #include "zed_log.h" 33 #include "zed_strings.h" 34 35 #define ZEVENT_FILENO 3 36 37 struct launched_process_node { 38 avl_node_t node; 39 pid_t pid; 40 uint64_t eid; 41 char *name; 42 }; 43 44 static int 45 _launched_process_node_compare(const void *x1, const void *x2) 46 { 47 pid_t p1; 48 pid_t p2; 49 50 assert(x1 != NULL); 51 assert(x2 != NULL); 52 53 p1 = ((const struct launched_process_node *) x1)->pid; 54 p2 = ((const struct launched_process_node *) x2)->pid; 55 56 if (p1 < p2) 57 return (-1); 58 else if (p1 == p2) 59 return (0); 60 else 61 return (1); 62 } 63 64 static pthread_t _reap_children_tid = (pthread_t)-1; 65 static volatile boolean_t _reap_children_stop; 66 static avl_tree_t _launched_processes; 67 static pthread_mutex_t _launched_processes_lock = PTHREAD_MUTEX_INITIALIZER; 68 static int16_t _launched_processes_limit; 69 70 /* 71 * Create an environment string array for passing to execve() using the 72 * NAME=VALUE strings in container [zsp]. 73 * Return a newly-allocated environment, or NULL on error. 74 */ 75 static char ** 76 _zed_exec_create_env(zed_strings_t *zsp) 77 { 78 int num_ptrs; 79 int buflen; 80 char *buf; 81 char **pp; 82 char *p; 83 const char *q; 84 int i; 85 int len; 86 87 num_ptrs = zed_strings_count(zsp) + 1; 88 buflen = num_ptrs * sizeof (char *); 89 for (q = zed_strings_first(zsp); q; q = zed_strings_next(zsp)) 90 buflen += strlen(q) + 1; 91 92 buf = calloc(1, buflen); 93 if (!buf) 94 return (NULL); 95 96 pp = (char **)buf; 97 p = buf + (num_ptrs * sizeof (char *)); 98 i = 0; 99 for (q = zed_strings_first(zsp); q; q = zed_strings_next(zsp)) { 100 pp[i] = p; 101 len = strlen(q) + 1; 102 memcpy(p, q, len); 103 p += len; 104 i++; 105 } 106 pp[i] = NULL; 107 assert(buf + buflen == p); 108 return ((char **)buf); 109 } 110 111 /* 112 * Fork a child process to handle event [eid]. The program [prog] 113 * in directory [dir] is executed with the environment [env]. 114 * 115 * The file descriptor [zfd] is the zevent_fd used to track the 116 * current cursor location within the zevent nvlist. 117 */ 118 static void 119 _zed_exec_fork_child(uint64_t eid, const char *dir, const char *prog, 120 char *env[], int zfd, boolean_t in_foreground) 121 { 122 char path[PATH_MAX]; 123 int n; 124 pid_t pid; 125 int fd; 126 struct launched_process_node *node; 127 sigset_t mask; 128 struct timespec launch_timeout = 129 { .tv_sec = 0, .tv_nsec = 200 * 1000 * 1000, }; 130 131 assert(dir != NULL); 132 assert(prog != NULL); 133 assert(env != NULL); 134 assert(zfd >= 0); 135 136 while (__atomic_load_n(&_launched_processes_limit, 137 __ATOMIC_SEQ_CST) <= 0) 138 (void) nanosleep(&launch_timeout, NULL); 139 140 n = snprintf(path, sizeof (path), "%s/%s", dir, prog); 141 if ((n < 0) || (n >= sizeof (path))) { 142 zed_log_msg(LOG_WARNING, 143 "Failed to fork \"%s\" for eid=%llu: %s", 144 prog, eid, strerror(ENAMETOOLONG)); 145 return; 146 } 147 (void) pthread_mutex_lock(&_launched_processes_lock); 148 pid = fork(); 149 if (pid < 0) { 150 (void) pthread_mutex_unlock(&_launched_processes_lock); 151 zed_log_msg(LOG_WARNING, 152 "Failed to fork \"%s\" for eid=%llu: %s", 153 prog, eid, strerror(errno)); 154 return; 155 } else if (pid == 0) { 156 (void) sigemptyset(&mask); 157 (void) sigprocmask(SIG_SETMASK, &mask, NULL); 158 159 (void) umask(022); 160 if (in_foreground && /* we're already devnulled if daemonised */ 161 (fd = open("/dev/null", O_RDWR | O_CLOEXEC)) != -1) { 162 (void) dup2(fd, STDIN_FILENO); 163 (void) dup2(fd, STDOUT_FILENO); 164 (void) dup2(fd, STDERR_FILENO); 165 } 166 (void) dup2(zfd, ZEVENT_FILENO); 167 execle(path, prog, NULL, env); 168 _exit(127); 169 } 170 171 /* parent process */ 172 173 node = calloc(1, sizeof (*node)); 174 if (node) { 175 node->pid = pid; 176 node->eid = eid; 177 node->name = strdup(prog); 178 179 avl_add(&_launched_processes, node); 180 } 181 (void) pthread_mutex_unlock(&_launched_processes_lock); 182 183 __atomic_sub_fetch(&_launched_processes_limit, 1, __ATOMIC_SEQ_CST); 184 zed_log_msg(LOG_INFO, "Invoking \"%s\" eid=%llu pid=%d", 185 prog, eid, pid); 186 } 187 188 static void 189 _nop(int sig) 190 {} 191 192 static void * 193 _reap_children(void *arg) 194 { 195 struct launched_process_node node, *pnode; 196 pid_t pid; 197 int status; 198 struct rusage usage; 199 struct sigaction sa = {}; 200 201 (void) sigfillset(&sa.sa_mask); 202 (void) sigdelset(&sa.sa_mask, SIGCHLD); 203 (void) pthread_sigmask(SIG_SETMASK, &sa.sa_mask, NULL); 204 205 (void) sigemptyset(&sa.sa_mask); 206 sa.sa_handler = _nop; 207 sa.sa_flags = SA_NOCLDSTOP; 208 (void) sigaction(SIGCHLD, &sa, NULL); 209 210 for (_reap_children_stop = B_FALSE; !_reap_children_stop; ) { 211 (void) pthread_mutex_lock(&_launched_processes_lock); 212 pid = wait4(0, &status, WNOHANG, &usage); 213 214 if (pid == 0 || pid == (pid_t)-1) { 215 (void) pthread_mutex_unlock(&_launched_processes_lock); 216 if (pid == 0 || errno == ECHILD) 217 pause(); 218 else if (errno != EINTR) 219 zed_log_msg(LOG_WARNING, 220 "Failed to wait for children: %s", 221 strerror(errno)); 222 } else { 223 memset(&node, 0, sizeof (node)); 224 node.pid = pid; 225 pnode = avl_find(&_launched_processes, &node, NULL); 226 if (pnode) { 227 memcpy(&node, pnode, sizeof (node)); 228 229 avl_remove(&_launched_processes, pnode); 230 free(pnode); 231 } 232 (void) pthread_mutex_unlock(&_launched_processes_lock); 233 __atomic_add_fetch(&_launched_processes_limit, 1, 234 __ATOMIC_SEQ_CST); 235 236 usage.ru_utime.tv_sec += usage.ru_stime.tv_sec; 237 usage.ru_utime.tv_usec += usage.ru_stime.tv_usec; 238 usage.ru_utime.tv_sec += 239 usage.ru_utime.tv_usec / (1000 * 1000); 240 usage.ru_utime.tv_usec %= 1000 * 1000; 241 242 if (WIFEXITED(status)) { 243 zed_log_msg(LOG_INFO, 244 "Finished \"%s\" eid=%llu pid=%d " 245 "time=%llu.%06us exit=%d", 246 node.name, node.eid, pid, 247 (unsigned long long) usage.ru_utime.tv_sec, 248 (unsigned int) usage.ru_utime.tv_usec, 249 WEXITSTATUS(status)); 250 } else if (WIFSIGNALED(status)) { 251 zed_log_msg(LOG_INFO, 252 "Finished \"%s\" eid=%llu pid=%d " 253 "time=%llu.%06us sig=%d/%s", 254 node.name, node.eid, pid, 255 (unsigned long long) usage.ru_utime.tv_sec, 256 (unsigned int) usage.ru_utime.tv_usec, 257 WTERMSIG(status), 258 strsignal(WTERMSIG(status))); 259 } else { 260 zed_log_msg(LOG_INFO, 261 "Finished \"%s\" eid=%llu pid=%d " 262 "time=%llu.%06us status=0x%X", 263 node.name, node.eid, 264 (unsigned long long) usage.ru_utime.tv_sec, 265 (unsigned int) usage.ru_utime.tv_usec, 266 (unsigned int) status); 267 } 268 269 free(node.name); 270 } 271 } 272 273 return (NULL); 274 } 275 276 void 277 zed_exec_fini(void) 278 { 279 struct launched_process_node *node; 280 void *ck = NULL; 281 282 if (_reap_children_tid == (pthread_t)-1) 283 return; 284 285 _reap_children_stop = B_TRUE; 286 (void) pthread_kill(_reap_children_tid, SIGCHLD); 287 (void) pthread_join(_reap_children_tid, NULL); 288 289 while ((node = avl_destroy_nodes(&_launched_processes, &ck)) != NULL) { 290 free(node->name); 291 free(node); 292 } 293 avl_destroy(&_launched_processes); 294 295 (void) pthread_mutex_destroy(&_launched_processes_lock); 296 (void) pthread_mutex_init(&_launched_processes_lock, NULL); 297 298 _reap_children_tid = (pthread_t)-1; 299 } 300 301 /* 302 * Process the event [eid] by synchronously invoking all zedlets with a 303 * matching class prefix. 304 * 305 * Each executable in [zcp->zedlets] from the directory [zcp->zedlet_dir] 306 * is matched against the event's [class], [subclass], and the "all" class 307 * (which matches all events). 308 * Every zedlet with a matching class prefix is invoked. 309 * The NAME=VALUE strings in [envs] will be passed to the zedlet as 310 * environment variables. 311 * 312 * The file descriptor [zcp->zevent_fd] is the zevent_fd used to track the 313 * current cursor location within the zevent nvlist. 314 * 315 * Return 0 on success, -1 on error. 316 */ 317 int 318 zed_exec_process(uint64_t eid, const char *class, const char *subclass, 319 struct zed_conf *zcp, zed_strings_t *envs) 320 { 321 const char *class_strings[4]; 322 const char *allclass = "all"; 323 const char **csp; 324 const char *z; 325 char **e; 326 int n; 327 328 if (!zcp->zedlet_dir || !zcp->zedlets || !envs || zcp->zevent_fd < 0) 329 return (-1); 330 331 if (_reap_children_tid == (pthread_t)-1) { 332 _launched_processes_limit = zcp->max_jobs; 333 334 if (pthread_create(&_reap_children_tid, NULL, 335 _reap_children, NULL) != 0) 336 return (-1); 337 pthread_setname_np(_reap_children_tid, "reap ZEDLETs"); 338 339 avl_create(&_launched_processes, _launched_process_node_compare, 340 sizeof (struct launched_process_node), 341 offsetof(struct launched_process_node, node)); 342 } 343 344 csp = class_strings; 345 346 if (class) 347 *csp++ = class; 348 349 if (subclass) 350 *csp++ = subclass; 351 352 if (allclass) 353 *csp++ = allclass; 354 355 *csp = NULL; 356 357 e = _zed_exec_create_env(envs); 358 359 for (z = zed_strings_first(zcp->zedlets); z; 360 z = zed_strings_next(zcp->zedlets)) { 361 for (csp = class_strings; *csp; csp++) { 362 n = strlen(*csp); 363 if ((strncmp(z, *csp, n) == 0) && !isalpha(z[n])) 364 _zed_exec_fork_child(eid, zcp->zedlet_dir, 365 z, e, zcp->zevent_fd, zcp->do_foreground); 366 } 367 } 368 free(e); 369 return (0); 370 } 371