ndmpd_main.c revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Use is subject to license terms.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * BSD 3 Clause License
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Copyright (c) 2007, The Storage Networking Industry Association.
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 * - Redistributions of source code must retain the above copyright
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * notice, this list of conditions and the following disclaimer.
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 * - 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 * 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/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar/* zfs library handle & mutex */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdartypedef struct ndmpd {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar int s_shutdown_flag; /* Fields for shutdown control */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Load and initialize the plug-in module
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if ((plname = ndmpd_get_prop(NDMP_PLUGIN_PATH)) == NULL)
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 plugin_init = (ndmp_plugin_t *(*)(int))dlsym(mod_plp, "_ndmp_init");
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 plugin_fini = (void (*)(ndmp_plugin_t *))dlsym(mod_plp, "_ndmp_fini");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Set effective sets privileges to 'least' required. If fails, send
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * error messages to log file and proceed.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, PRIV_PROC_SESSION,
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 "Failed to set least required privileges to the service.");
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 syslog(LOG_ERR, "Failed to start process in background.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* If we're the parent process, exit. */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * The main NDMP daemon function
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Parameters:
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * argc (input) - the argument count
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * argv (input) - command line options
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_DAEMON);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Check for existing ndmpd door server (make sure ndmpd is not already
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* ndmpd is already running, exit. */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* load ENVs */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "%s SMF properties initialization failed.", argv[0]);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Global zone check */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Trusted Solaris check */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Trusted Solaris not supported.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar switch (c) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * close any open file descriptors which are greater
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * than STDERR_FILENO
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* set up signal handler */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) sigdelset(&set, SIGABRT); /* always unblocked for ASSERT() */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_DAEMON);
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar if (mod_init() != 0) {
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Failed to load the plugin module.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* libzfs init */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Failed to initialize ZFS library.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* initialize and start the door server */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar syslog(LOG_ERR, "Can not start ndmpd door server.");
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Prior to this point, we are single-threaded. We will be
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * multi-threaded from this point on.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar (void) pthread_create(NULL, NULL, (funct_t)ndmpd_main,
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar /* Refresh SMF properties */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar "Service properties initialization "
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Typically SIGINT or SIGTERM.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar * Enable libumem debugging by default on DEBUG builds.
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return ("default,verbose"); /* $UMEM_DEBUG setting */
2654012f83cec5dc15b61dfe3e4a4915f186e7a6Reza Sabdar return ("fail,contents"); /* $UMEM_LOGGING setting */