ndmpd_main.c revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Use is subject to license terms.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * BSD 3 Clause License
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Copyright (c) 2007, The Storage Networking Industry Association.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Redistribution and use in source and binary forms, with or without
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * modification, are permitted provided that the following conditions
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * are met:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * - Redistributions of source code must retain the above copyright
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * notice, this list of conditions and the following disclaimer.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * - Redistributions in binary form must reproduce the above copyright
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * notice, this list of conditions and the following disclaimer in
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * the documentation and/or other materials provided with the
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * distribution.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * - Neither the name of The Storage Networking Industry Association (SNIA)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * nor the names of its contributors may be used to endorse or promote
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * products derived from this software without specific prior written
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * permission.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * POSSIBILITY OF SUCH DAMAGE.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <errno.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <signal.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <libgen.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <libscf.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <libintl.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <sys/wait.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <zone.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <tsol/label.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include <dlfcn.h>
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include "ndmpd.h"
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#include "ndmpd_common.h"
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/* zfs library handle & mutex */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarlibzfs_handle_t *zlibh;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarmutex_t zlib_mtx;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarvoid *mod_plp;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic void ndmpd_sig_handler(int sig);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdartypedef struct ndmpd {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar int s_shutdown_flag; /* Fields for shutdown control */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar int s_sigval;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar} ndmpd_t;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarndmpd_t ndmpd;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Load and initialize the plug-in module
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic int
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarmod_init()
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar char *plname;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmp_plugin_t *(*plugin_init)(int);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmp_pl = NULL;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((plname = ndmpd_get_prop(NDMP_PLUGIN_PATH)) == NULL)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((mod_plp = dlopen(plname, RTLD_LOCAL | RTLD_NOW)) == NULL) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Error loading the plug-in %s", plname);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar plugin_init = (ndmp_plugin_t *(*)(int))dlsym(mod_plp, "_ndmp_init");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (plugin_init == NULL) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) dlclose(mod_plp);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((ndmp_pl = plugin_init(NDMP_PLUGIN_VERSION)) == NULL) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Error loading the plug-in %s", plname);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (-1);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Unload
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic void
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarmod_fini()
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmp_pl == NULL)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar void (*plugin_fini)(ndmp_plugin_t *);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar plugin_fini = (void (*)(ndmp_plugin_t *))dlsym(mod_plp, "_ndmp_fini");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (plugin_fini == NULL) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) dlclose(mod_plp);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar plugin_fini(ndmp_pl);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) dlclose(mod_plp);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic void
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdardaemonize_init(char *arg)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar sigset_t set, oset;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar pid_t pid;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Set effective sets privileges to 'least' required. If fails, send
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * error messages to log file and proceed.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (priv_set(PRIV_SET, PRIV_EFFECTIVE,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, PRIV_PROC_SESSION,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_PROC_FORK, PRIV_PROC_EXEC,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_PROC_AUDIT, PRIV_PROC_SETID, PRIV_PROC_OWNER, PRIV_FILE_CHOWN,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_FILE_CHOWN_SELF, PRIV_FILE_DAC_READ, PRIV_FILE_DAC_SEARCH,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_FILE_DAC_WRITE, PRIV_FILE_OWNER, PRIV_FILE_SETID,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_SYS_LINKDIR, PRIV_SYS_DEVICES, PRIV_SYS_MOUNT, PRIV_SYS_CONFIG,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar NULL))
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "Failed to set least required privileges to the service.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Block all signals prior to the fork and leave them blocked in the
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * parent so we don't get in a situation where the parent gets SIGINT
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * and returns non-zero exit status and the child is actually running.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * In the child, restore the signal mask once we've done our setsid().
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigfillset(&set);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGABRT);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigprocmask(SIG_BLOCK, &set, &oset);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((pid = fork()) == -1) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar openlog(arg, LOG_PID | LOG_NDELAY, LOG_DAEMON);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Failed to start process in background.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* If we're the parent process, exit. */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (pid != 0) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar _exit(0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) setsid();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigprocmask(SIG_SETMASK, &oset, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) chdir("/");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) umask(0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic void
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdardaemonize_fini(void)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar int fd;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((fd = open("/dev/null", O_RDWR)) >= 0) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) close(fd);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * main
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * The main NDMP daemon function
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Parameters:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * argc (input) - the argument count
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * argv (input) - command line options
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Returns:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * 0
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarint
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarmain(int argc, char *argv[])
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar char c;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar struct sigaction act;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar sigset_t set;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar void *arg = 0;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_DAEMON);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Check for existing ndmpd door server (make sure ndmpd is not already
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * running)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmp_door_check()) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* ndmpd is already running, exit. */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (0);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* load ENVs */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmpd_load_prop()) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "%s SMF properties initialization failed.", argv[0]);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Global zone check */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (getzoneid() != GLOBAL_ZONEID) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Local zone not supported.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_FATAL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Trusted Solaris check */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (is_system_labeled()) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Trusted Solaris not supported.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_FATAL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar opterr = 0;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar while ((c = getopt(argc, argv, ":d")) != -1) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar switch (c) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar case 'd':
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) set_debug_level(TRUE);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar break;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar default:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "%s: Invalid option -%c.",
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar argv[0], optopt);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Usage: %s [-d]", argv[0]);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar closelog();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * close any open file descriptors which are greater
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * than STDERR_FILENO
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar closefrom(STDERR_FILENO + 1);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* set up signal handler */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigfillset(&set);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGABRT); /* always unblocked for ASSERT() */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigfillset(&act.sa_mask);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar act.sa_handler = ndmpd_sig_handler;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar act.sa_flags = 0;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigaction(SIGTERM, &act, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigaction(SIGHUP, &act, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigaction(SIGINT, &act, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigaction(SIGUSR1, &act, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGTERM);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGHUP);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGINT);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGUSR1);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) daemonize_init(argv[0]);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_DAEMON);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) mutex_init(&log_lock, 0, NULL);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (mod_init() != 0) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Failed to load the plugin module.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* libzfs init */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((zlibh = libzfs_init()) == NULL) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Failed to initialize ZFS library.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* initialize and start the door server */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmp_door_init()) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Can not start ndmpd door server.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar exit(SMF_EXIT_ERR_CONFIG);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) tlm_init();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Prior to this point, we are single-threaded. We will be
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * multi-threaded from this point on.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) pthread_create(NULL, NULL, (funct_t)ndmpd_main,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void *)&arg);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar while (!ndmpd.s_shutdown_flag) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigsuspend(&set);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar switch (ndmpd.s_sigval) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar case 0:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar break;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar case SIGHUP:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Refresh SMF properties */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmpd_load_prop())
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "Service properties initialization "
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "failed.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar break;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar default:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Typically SIGINT or SIGTERM.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmpd.s_shutdown_flag = 1;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar break;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmpd.s_sigval = 0;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar }
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) mutex_destroy(&log_lock);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar libzfs_fini(zlibh);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar mod_fini();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmp_door_fini();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar daemonize_fini();
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar free(ndmp_log_path);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return (SMF_EXIT_OK);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarstatic void
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarndmpd_sig_handler(int sig)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (ndmpd.s_sigval == 0)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar ndmpd.s_sigval = sig;
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/*
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Enable libumem debugging by default on DEBUG builds.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#ifdef DEBUG
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarconst char *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar_umem_debug_init(void)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return ("default,verbose"); /* $UMEM_DEBUG setting */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdarconst char *
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar_umem_logging_init(void)
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar{
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return ("fail,contents"); /* $UMEM_LOGGING setting */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar}
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar#endif