1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * This file is part of the ZFS Event Daemon (ZED) 3*eda14cbcSMatt Macy * for ZFS on Linux (ZoL) <http://zfsonlinux.org/>. 4*eda14cbcSMatt Macy * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049). 5*eda14cbcSMatt Macy * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC. 6*eda14cbcSMatt Macy * Refer to the ZoL git commit log for authoritative copyright attribution. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 9*eda14cbcSMatt Macy * Common Development and Distribution License Version 1.0 (CDDL-1.0). 10*eda14cbcSMatt Macy * You can obtain a copy of the license from the top-level file 11*eda14cbcSMatt Macy * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>. 12*eda14cbcSMatt Macy * You may not use this file except in compliance with the license. 13*eda14cbcSMatt Macy */ 14*eda14cbcSMatt Macy 15*eda14cbcSMatt Macy #include <errno.h> 16*eda14cbcSMatt Macy #include <fcntl.h> 17*eda14cbcSMatt Macy #include <signal.h> 18*eda14cbcSMatt Macy #include <stdio.h> 19*eda14cbcSMatt Macy #include <stdlib.h> 20*eda14cbcSMatt Macy #include <string.h> 21*eda14cbcSMatt Macy #include <sys/mman.h> 22*eda14cbcSMatt Macy #include <sys/stat.h> 23*eda14cbcSMatt Macy #include <unistd.h> 24*eda14cbcSMatt Macy #include "zed.h" 25*eda14cbcSMatt Macy #include "zed_conf.h" 26*eda14cbcSMatt Macy #include "zed_event.h" 27*eda14cbcSMatt Macy #include "zed_file.h" 28*eda14cbcSMatt Macy #include "zed_log.h" 29*eda14cbcSMatt Macy 30*eda14cbcSMatt Macy static volatile sig_atomic_t _got_exit = 0; 31*eda14cbcSMatt Macy static volatile sig_atomic_t _got_hup = 0; 32*eda14cbcSMatt Macy 33*eda14cbcSMatt Macy /* 34*eda14cbcSMatt Macy * Signal handler for SIGINT & SIGTERM. 35*eda14cbcSMatt Macy */ 36*eda14cbcSMatt Macy static void 37*eda14cbcSMatt Macy _exit_handler(int signum) 38*eda14cbcSMatt Macy { 39*eda14cbcSMatt Macy _got_exit = 1; 40*eda14cbcSMatt Macy } 41*eda14cbcSMatt Macy 42*eda14cbcSMatt Macy /* 43*eda14cbcSMatt Macy * Signal handler for SIGHUP. 44*eda14cbcSMatt Macy */ 45*eda14cbcSMatt Macy static void 46*eda14cbcSMatt Macy _hup_handler(int signum) 47*eda14cbcSMatt Macy { 48*eda14cbcSMatt Macy _got_hup = 1; 49*eda14cbcSMatt Macy } 50*eda14cbcSMatt Macy 51*eda14cbcSMatt Macy /* 52*eda14cbcSMatt Macy * Register signal handlers. 53*eda14cbcSMatt Macy */ 54*eda14cbcSMatt Macy static void 55*eda14cbcSMatt Macy _setup_sig_handlers(void) 56*eda14cbcSMatt Macy { 57*eda14cbcSMatt Macy struct sigaction sa; 58*eda14cbcSMatt Macy 59*eda14cbcSMatt Macy if (sigemptyset(&sa.sa_mask) < 0) 60*eda14cbcSMatt Macy zed_log_die("Failed to initialize sigset"); 61*eda14cbcSMatt Macy 62*eda14cbcSMatt Macy sa.sa_flags = SA_RESTART; 63*eda14cbcSMatt Macy sa.sa_handler = SIG_IGN; 64*eda14cbcSMatt Macy 65*eda14cbcSMatt Macy if (sigaction(SIGPIPE, &sa, NULL) < 0) 66*eda14cbcSMatt Macy zed_log_die("Failed to ignore SIGPIPE"); 67*eda14cbcSMatt Macy 68*eda14cbcSMatt Macy sa.sa_handler = _exit_handler; 69*eda14cbcSMatt Macy if (sigaction(SIGINT, &sa, NULL) < 0) 70*eda14cbcSMatt Macy zed_log_die("Failed to register SIGINT handler"); 71*eda14cbcSMatt Macy 72*eda14cbcSMatt Macy if (sigaction(SIGTERM, &sa, NULL) < 0) 73*eda14cbcSMatt Macy zed_log_die("Failed to register SIGTERM handler"); 74*eda14cbcSMatt Macy 75*eda14cbcSMatt Macy sa.sa_handler = _hup_handler; 76*eda14cbcSMatt Macy if (sigaction(SIGHUP, &sa, NULL) < 0) 77*eda14cbcSMatt Macy zed_log_die("Failed to register SIGHUP handler"); 78*eda14cbcSMatt Macy } 79*eda14cbcSMatt Macy 80*eda14cbcSMatt Macy /* 81*eda14cbcSMatt Macy * Lock all current and future pages in the virtual memory address space. 82*eda14cbcSMatt Macy * Access to locked pages will never be delayed by a page fault. 83*eda14cbcSMatt Macy * 84*eda14cbcSMatt Macy * EAGAIN is tested up to max_tries in case this is a transient error. 85*eda14cbcSMatt Macy * 86*eda14cbcSMatt Macy * Note that memory locks are not inherited by a child created via fork() 87*eda14cbcSMatt Macy * and are automatically removed during an execve(). As such, this must 88*eda14cbcSMatt Macy * be called after the daemon fork()s (when running in the background). 89*eda14cbcSMatt Macy */ 90*eda14cbcSMatt Macy static void 91*eda14cbcSMatt Macy _lock_memory(void) 92*eda14cbcSMatt Macy { 93*eda14cbcSMatt Macy #if HAVE_MLOCKALL 94*eda14cbcSMatt Macy int i = 0; 95*eda14cbcSMatt Macy const int max_tries = 10; 96*eda14cbcSMatt Macy 97*eda14cbcSMatt Macy for (i = 0; i < max_tries; i++) { 98*eda14cbcSMatt Macy if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) { 99*eda14cbcSMatt Macy zed_log_msg(LOG_INFO, "Locked all pages in memory"); 100*eda14cbcSMatt Macy return; 101*eda14cbcSMatt Macy } 102*eda14cbcSMatt Macy if (errno != EAGAIN) 103*eda14cbcSMatt Macy break; 104*eda14cbcSMatt Macy } 105*eda14cbcSMatt Macy zed_log_die("Failed to lock memory pages: %s", strerror(errno)); 106*eda14cbcSMatt Macy 107*eda14cbcSMatt Macy #else /* HAVE_MLOCKALL */ 108*eda14cbcSMatt Macy zed_log_die("Failed to lock memory pages: mlockall() not supported"); 109*eda14cbcSMatt Macy #endif /* HAVE_MLOCKALL */ 110*eda14cbcSMatt Macy } 111*eda14cbcSMatt Macy 112*eda14cbcSMatt Macy /* 113*eda14cbcSMatt Macy * Start daemonization of the process including the double fork(). 114*eda14cbcSMatt Macy * 115*eda14cbcSMatt Macy * The parent process will block here until _finish_daemonize() is called 116*eda14cbcSMatt Macy * (in the grandchild process), at which point the parent process will exit. 117*eda14cbcSMatt Macy * This prevents the parent process from exiting until initialization is 118*eda14cbcSMatt Macy * complete. 119*eda14cbcSMatt Macy */ 120*eda14cbcSMatt Macy static void 121*eda14cbcSMatt Macy _start_daemonize(void) 122*eda14cbcSMatt Macy { 123*eda14cbcSMatt Macy pid_t pid; 124*eda14cbcSMatt Macy struct sigaction sa; 125*eda14cbcSMatt Macy 126*eda14cbcSMatt Macy /* Create pipe for communicating with child during daemonization. */ 127*eda14cbcSMatt Macy zed_log_pipe_open(); 128*eda14cbcSMatt Macy 129*eda14cbcSMatt Macy /* Background process and ensure child is not process group leader. */ 130*eda14cbcSMatt Macy pid = fork(); 131*eda14cbcSMatt Macy if (pid < 0) { 132*eda14cbcSMatt Macy zed_log_die("Failed to create child process: %s", 133*eda14cbcSMatt Macy strerror(errno)); 134*eda14cbcSMatt Macy } else if (pid > 0) { 135*eda14cbcSMatt Macy 136*eda14cbcSMatt Macy /* Close writes since parent will only read from pipe. */ 137*eda14cbcSMatt Macy zed_log_pipe_close_writes(); 138*eda14cbcSMatt Macy 139*eda14cbcSMatt Macy /* Wait for notification that daemonization is complete. */ 140*eda14cbcSMatt Macy zed_log_pipe_wait(); 141*eda14cbcSMatt Macy 142*eda14cbcSMatt Macy zed_log_pipe_close_reads(); 143*eda14cbcSMatt Macy _exit(EXIT_SUCCESS); 144*eda14cbcSMatt Macy } 145*eda14cbcSMatt Macy 146*eda14cbcSMatt Macy /* Close reads since child will only write to pipe. */ 147*eda14cbcSMatt Macy zed_log_pipe_close_reads(); 148*eda14cbcSMatt Macy 149*eda14cbcSMatt Macy /* Create independent session and detach from terminal. */ 150*eda14cbcSMatt Macy if (setsid() < 0) 151*eda14cbcSMatt Macy zed_log_die("Failed to create new session: %s", 152*eda14cbcSMatt Macy strerror(errno)); 153*eda14cbcSMatt Macy 154*eda14cbcSMatt Macy /* Prevent child from terminating on HUP when session leader exits. */ 155*eda14cbcSMatt Macy if (sigemptyset(&sa.sa_mask) < 0) 156*eda14cbcSMatt Macy zed_log_die("Failed to initialize sigset"); 157*eda14cbcSMatt Macy 158*eda14cbcSMatt Macy sa.sa_flags = 0; 159*eda14cbcSMatt Macy sa.sa_handler = SIG_IGN; 160*eda14cbcSMatt Macy 161*eda14cbcSMatt Macy if (sigaction(SIGHUP, &sa, NULL) < 0) 162*eda14cbcSMatt Macy zed_log_die("Failed to ignore SIGHUP"); 163*eda14cbcSMatt Macy 164*eda14cbcSMatt Macy /* Ensure process cannot re-acquire terminal. */ 165*eda14cbcSMatt Macy pid = fork(); 166*eda14cbcSMatt Macy if (pid < 0) { 167*eda14cbcSMatt Macy zed_log_die("Failed to create grandchild process: %s", 168*eda14cbcSMatt Macy strerror(errno)); 169*eda14cbcSMatt Macy } else if (pid > 0) { 170*eda14cbcSMatt Macy _exit(EXIT_SUCCESS); 171*eda14cbcSMatt Macy } 172*eda14cbcSMatt Macy } 173*eda14cbcSMatt Macy 174*eda14cbcSMatt Macy /* 175*eda14cbcSMatt Macy * Finish daemonization of the process by closing stdin/stdout/stderr. 176*eda14cbcSMatt Macy * 177*eda14cbcSMatt Macy * This must be called at the end of initialization after all external 178*eda14cbcSMatt Macy * communication channels are established and accessible. 179*eda14cbcSMatt Macy */ 180*eda14cbcSMatt Macy static void 181*eda14cbcSMatt Macy _finish_daemonize(void) 182*eda14cbcSMatt Macy { 183*eda14cbcSMatt Macy int devnull; 184*eda14cbcSMatt Macy 185*eda14cbcSMatt Macy /* Preserve fd 0/1/2, but discard data to/from stdin/stdout/stderr. */ 186*eda14cbcSMatt Macy devnull = open("/dev/null", O_RDWR); 187*eda14cbcSMatt Macy if (devnull < 0) 188*eda14cbcSMatt Macy zed_log_die("Failed to open /dev/null: %s", strerror(errno)); 189*eda14cbcSMatt Macy 190*eda14cbcSMatt Macy if (dup2(devnull, STDIN_FILENO) < 0) 191*eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stdin: %s", 192*eda14cbcSMatt Macy strerror(errno)); 193*eda14cbcSMatt Macy 194*eda14cbcSMatt Macy if (dup2(devnull, STDOUT_FILENO) < 0) 195*eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stdout: %s", 196*eda14cbcSMatt Macy strerror(errno)); 197*eda14cbcSMatt Macy 198*eda14cbcSMatt Macy if (dup2(devnull, STDERR_FILENO) < 0) 199*eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stderr: %s", 200*eda14cbcSMatt Macy strerror(errno)); 201*eda14cbcSMatt Macy 202*eda14cbcSMatt Macy if ((devnull > STDERR_FILENO) && (close(devnull) < 0)) 203*eda14cbcSMatt Macy zed_log_die("Failed to close /dev/null: %s", strerror(errno)); 204*eda14cbcSMatt Macy 205*eda14cbcSMatt Macy /* Notify parent that daemonization is complete. */ 206*eda14cbcSMatt Macy zed_log_pipe_close_writes(); 207*eda14cbcSMatt Macy } 208*eda14cbcSMatt Macy 209*eda14cbcSMatt Macy /* 210*eda14cbcSMatt Macy * ZFS Event Daemon (ZED). 211*eda14cbcSMatt Macy */ 212*eda14cbcSMatt Macy int 213*eda14cbcSMatt Macy main(int argc, char *argv[]) 214*eda14cbcSMatt Macy { 215*eda14cbcSMatt Macy struct zed_conf *zcp; 216*eda14cbcSMatt Macy uint64_t saved_eid; 217*eda14cbcSMatt Macy int64_t saved_etime[2]; 218*eda14cbcSMatt Macy 219*eda14cbcSMatt Macy zed_log_init(argv[0]); 220*eda14cbcSMatt Macy zed_log_stderr_open(LOG_NOTICE); 221*eda14cbcSMatt Macy zcp = zed_conf_create(); 222*eda14cbcSMatt Macy zed_conf_parse_opts(zcp, argc, argv); 223*eda14cbcSMatt Macy if (zcp->do_verbose) 224*eda14cbcSMatt Macy zed_log_stderr_open(LOG_INFO); 225*eda14cbcSMatt Macy 226*eda14cbcSMatt Macy if (geteuid() != 0) 227*eda14cbcSMatt Macy zed_log_die("Must be run as root"); 228*eda14cbcSMatt Macy 229*eda14cbcSMatt Macy zed_conf_parse_file(zcp); 230*eda14cbcSMatt Macy 231*eda14cbcSMatt Macy zed_file_close_from(STDERR_FILENO + 1); 232*eda14cbcSMatt Macy 233*eda14cbcSMatt Macy (void) umask(0); 234*eda14cbcSMatt Macy 235*eda14cbcSMatt Macy if (chdir("/") < 0) 236*eda14cbcSMatt Macy zed_log_die("Failed to change to root directory"); 237*eda14cbcSMatt Macy 238*eda14cbcSMatt Macy if (zed_conf_scan_dir(zcp) < 0) 239*eda14cbcSMatt Macy exit(EXIT_FAILURE); 240*eda14cbcSMatt Macy 241*eda14cbcSMatt Macy if (!zcp->do_foreground) { 242*eda14cbcSMatt Macy _start_daemonize(); 243*eda14cbcSMatt Macy zed_log_syslog_open(LOG_DAEMON); 244*eda14cbcSMatt Macy } 245*eda14cbcSMatt Macy _setup_sig_handlers(); 246*eda14cbcSMatt Macy 247*eda14cbcSMatt Macy if (zcp->do_memlock) 248*eda14cbcSMatt Macy _lock_memory(); 249*eda14cbcSMatt Macy 250*eda14cbcSMatt Macy if ((zed_conf_write_pid(zcp) < 0) && (!zcp->do_force)) 251*eda14cbcSMatt Macy exit(EXIT_FAILURE); 252*eda14cbcSMatt Macy 253*eda14cbcSMatt Macy if (!zcp->do_foreground) 254*eda14cbcSMatt Macy _finish_daemonize(); 255*eda14cbcSMatt Macy 256*eda14cbcSMatt Macy zed_log_msg(LOG_NOTICE, 257*eda14cbcSMatt Macy "ZFS Event Daemon %s-%s (PID %d)", 258*eda14cbcSMatt Macy ZFS_META_VERSION, ZFS_META_RELEASE, (int)getpid()); 259*eda14cbcSMatt Macy 260*eda14cbcSMatt Macy if (zed_conf_open_state(zcp) < 0) 261*eda14cbcSMatt Macy exit(EXIT_FAILURE); 262*eda14cbcSMatt Macy 263*eda14cbcSMatt Macy if (zed_conf_read_state(zcp, &saved_eid, saved_etime) < 0) 264*eda14cbcSMatt Macy exit(EXIT_FAILURE); 265*eda14cbcSMatt Macy 266*eda14cbcSMatt Macy idle: 267*eda14cbcSMatt Macy /* 268*eda14cbcSMatt Macy * If -I is specified, attempt to open /dev/zfs repeatedly until 269*eda14cbcSMatt Macy * successful. 270*eda14cbcSMatt Macy */ 271*eda14cbcSMatt Macy do { 272*eda14cbcSMatt Macy if (!zed_event_init(zcp)) 273*eda14cbcSMatt Macy break; 274*eda14cbcSMatt Macy /* Wait for some time and try again. tunable? */ 275*eda14cbcSMatt Macy sleep(30); 276*eda14cbcSMatt Macy } while (!_got_exit && zcp->do_idle); 277*eda14cbcSMatt Macy 278*eda14cbcSMatt Macy if (_got_exit) 279*eda14cbcSMatt Macy goto out; 280*eda14cbcSMatt Macy 281*eda14cbcSMatt Macy zed_event_seek(zcp, saved_eid, saved_etime); 282*eda14cbcSMatt Macy 283*eda14cbcSMatt Macy while (!_got_exit) { 284*eda14cbcSMatt Macy int rv; 285*eda14cbcSMatt Macy if (_got_hup) { 286*eda14cbcSMatt Macy _got_hup = 0; 287*eda14cbcSMatt Macy (void) zed_conf_scan_dir(zcp); 288*eda14cbcSMatt Macy } 289*eda14cbcSMatt Macy rv = zed_event_service(zcp); 290*eda14cbcSMatt Macy 291*eda14cbcSMatt Macy /* ENODEV: When kernel module is unloaded (osx) */ 292*eda14cbcSMatt Macy if (rv == ENODEV) 293*eda14cbcSMatt Macy break; 294*eda14cbcSMatt Macy } 295*eda14cbcSMatt Macy 296*eda14cbcSMatt Macy zed_log_msg(LOG_NOTICE, "Exiting"); 297*eda14cbcSMatt Macy zed_event_fini(zcp); 298*eda14cbcSMatt Macy 299*eda14cbcSMatt Macy if (zcp->do_idle && !_got_exit) 300*eda14cbcSMatt Macy goto idle; 301*eda14cbcSMatt Macy 302*eda14cbcSMatt Macy out: 303*eda14cbcSMatt Macy zed_conf_destroy(zcp); 304*eda14cbcSMatt Macy zed_log_fini(); 305*eda14cbcSMatt Macy exit(EXIT_SUCCESS); 306*eda14cbcSMatt Macy } 307