fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
08dcd69c259e91563982b9e7715366f99fee816eJack Meng * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
33f5ff17089e3a43e6e730bf80384c233123dbd9Milan Jurik * Copyright 2012 Milan Jurik. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/stat.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/socket.h>
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng#include <signal.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <locale.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <syslog.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netdb.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdio.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <door.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <meta.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <libsysevent.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <wait.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <semaphore.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <libscf.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/scsi/adapters/iscsi_door.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/scsi/adapters/iscsi_if.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Local Defines
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define ISCSI_DOOR_DAEMON_SYSLOG_PP "iscsid"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define ISCSI_DISCOVERY_POLL_DELAY1 1 /* Seconds */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define ISCSI_DISCOVERY_POLL_DELAY2 60 /* Seconds */
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China#define ISCSI_SMF_OFFLINE_DELAY 10 /* Seconds */
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China#define ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES 60
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if !defined(SMF_EXIT_ERR_OTHER)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define SMF_EXIT_ERR_OTHER -1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Global Variables related to the synchronization of the child process
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * --------------------------------------------------------------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic pid_t iscsi_child_pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic sem_t iscsi_child_sem;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int iscsi_child_door_handle;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int iscsi_child_smf_exit_code;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Global Variables related to the door accessed by the kernel
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -----------------------------------------------------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int iscsi_dev_handle;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int iscsi_kernel_door_handle;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Prototypes of Functions the body of which is defined farther down
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in this file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -----------------------------------------------------------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void call_child_door(int value);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void sigchld_handler(int sig);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic boolean_t discovery_event_wait(int did);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Mengstatic void signone(int, siginfo_t *, void *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_child_door(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *cookie,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *args,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t alen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_desc_t *ddp,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint_t ndid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_kernel_door(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *cookie,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *args,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t alen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_desc_t *ddp,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint_t ndid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_door_cnf_t *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_getipnodebyname_req(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getipnodebyname_req_t *req,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int req_len,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t *pcnf_len
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * main -- Entry point of the iSCSI door server daemon
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function forks, waits for the child process feedback and exits.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortemain(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int argc,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *argv[]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng int i;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng int sig;
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China int ret = -1;
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China int retry = 0;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng sigset_t sigs, allsigs;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng struct sigaction act;
08dcd69c259e91563982b9e7715366f99fee816eJack Meng uint32_t rval;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Get the locale set up before calling any other routines
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * with messages to ouput.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) setlocale(LC_ALL, "");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openlog("ISCSI_DOOR_DAEMON_SYSLOG_PP", LOG_PID, LOG_DAEMON);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The child semaphore is created. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sem_init(&iscsi_child_sem, 0, 0) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The door for the child is created. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_door_handle = door_create(iscsi_child_door, NULL, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (iscsi_child_door_handle == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_destroy(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* A signal handler is set for SIGCHLD. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) signal(SIGCHLD, sigchld_handler);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Here begins the daemonizing code
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * --------------------------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_pid = fork();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (iscsi_child_pid < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The fork failed. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("Cannot fork"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_destroy(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (iscsi_child_pid) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The parent exits after the child has provided feedback. This
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * waiting phase is to meet one of greenline's requirements.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We shouldn't return till we are sure the service is ready to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * be provided.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_wait(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_destroy(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(iscsi_child_smf_exit_code);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * stdout and stderr are redirected to "/dev/null".
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i = open("/dev/null", O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) dup2(i, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) dup2(i, 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Here ends the daemonizing code
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ------------------------------
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng * Block out all signals
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigfillset(&allsigs);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) pthread_sigmask(SIG_BLOCK, &allsigs, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* setup the door handle */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_kernel_door_handle = door_create(iscsi_kernel_door, NULL, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (iscsi_kernel_door_handle == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(gettext("door_create failed"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("door_create failed"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The iSCSI driver is opened.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_dev_handle = open(ISCSI_DRIVER_DEVCTL, O_RDWR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (iscsi_dev_handle == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The driver couldn't be opened. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(gettext("iscsi device open failed"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_dev_handle,
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng ISCSI_SMF_ONLINE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &iscsi_kernel_door_handle) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(iscsi_dev_handle);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng perror(gettext("ioctl: enable iscsi initiator"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(SMF_EXIT_ERR_OTHER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng /*
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng * Keep the dev open, so to keep iscsi module from unloaded.
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng * This is crutial to guarantee the consistency of the
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng * door_handle and service state in kernel.
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng */
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* We have to wait for the discovery process to finish. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) discovery_event_wait(iscsi_dev_handle);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng /* We let the parent know that everything is ok. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte call_child_door(SMF_EXIT_OK);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng /* now set up signals we care about */
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigemptyset(&sigs);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaddset(&sigs, SIGTERM);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaddset(&sigs, SIGINT);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaddset(&sigs, SIGQUIT);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng /* make sure signals to be enqueued */
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng act.sa_flags = SA_SIGINFO;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng act.sa_sigaction = signone;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaction(SIGTERM, &act, NULL);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaction(SIGINT, &act, NULL);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) sigaction(SIGQUIT, &act, NULL);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng /* wait and process signals */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng sig = sigwait(&sigs);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng if (sig < 0)
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng continue;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng switch (sig) {
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng case SIGQUIT:
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng case SIGINT:
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng case SIGTERM:
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China do {
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China ret = ioctl(iscsi_dev_handle,
08dcd69c259e91563982b9e7715366f99fee816eJack Meng ISCSI_SMF_OFFLINE, &rval);
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China if (ret == -1) {
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China /*
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China * Keep retrying if unable
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China * to stop
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China */
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China (void) sleep(ISCSI_SMF_OFFLINE_DELAY);
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China retry++;
aefdd1313598221872b3e520302b40e1874f2dc7bing zhao - Sun Microsystems - Beijing China }
08dcd69c259e91563982b9e7715366f99fee816eJack Meng } while ((ret == -1) &&
08dcd69c259e91563982b9e7715366f99fee816eJack Meng (retry < ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES));
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng (void) close(iscsi_dev_handle);
08dcd69c259e91563982b9e7715366f99fee816eJack Meng if (rval == B_FALSE) {
08dcd69c259e91563982b9e7715366f99fee816eJack Meng syslog(LOG_DAEMON, gettext("iSCSI initiator"
08dcd69c259e91563982b9e7715366f99fee816eJack Meng " service exited with sessions left."));
08dcd69c259e91563982b9e7715366f99fee816eJack Meng }
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng return (0);
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng default:
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng break;
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * sigchld_handler -- SIGCHLD Handler
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesigchld_handler(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int sig
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int status;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t ret_pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* This is the default code. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret_pid = waitpid(iscsi_child_pid, &status, WNOHANG);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret_pid == iscsi_child_pid) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (WIFEXITED(status)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_smf_exit_code = WEXITSTATUS(status);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_post(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * iscsi_child_door -- Child process door entry point
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function is executed when a driver calls door_ki_upcall().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_child_door(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *cookie,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *args,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t alen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_desc_t *ddp,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint_t ndid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int *ptr = (int *)args;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alen >= sizeof (iscsi_child_smf_exit_code)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_child_smf_exit_code = *ptr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sem_post(&iscsi_child_sem);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) door_return(NULL, 0, NULL, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * iscsi_kernel_door -- Kernel door entry point
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function is executed when a driver calls door_ki_upcall().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_kernel_door(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *cookie,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *args,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t alen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_desc_t *ddp,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint_t ndid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_door_msg_hdr_t err_ind;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_door_req_t *req;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_door_cnf_t *cnf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t cnf_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *err_txt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int err_code;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Local variables pre-initialization */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.signature = ISCSI_DOOR_REQ_SIGNATURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.version = ISCSI_DOOR_REQ_VERSION_1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.opcode = ISCSI_DOOR_ERROR_IND;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte req = (iscsi_door_req_t *)args;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf = (iscsi_door_cnf_t *)&err_ind;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf_len = sizeof (err_ind);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The validity of the request is checked before going any farther.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (req == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A request has to be passed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (alen < sizeof (iscsi_door_msg_hdr_t)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The buffer containing the request must be at least as big
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * as message header.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_LENGTH;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (req->hdr.signature != ISCSI_DOOR_REQ_SIGNATURE) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The request must be correctly signed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (req->hdr.version != ISCSI_DOOR_REQ_VERSION_1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The version of the request must be supported by the server.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_VERSION;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The request is treated according to the opcode.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (req->hdr.opcode) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case ISCSI_DOOR_GETIPNODEBYNAME_REQ:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf = _getipnodebyname_req(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &req->ginbn_req,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte alen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &cnf_len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_code = door_return((char *)cnf, cnf_len, NULL, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (err_code) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case E2BIG:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt = "E2BIG";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case EFAULT:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt = "EFAULT";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case EINVAL:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt = "EINVAL";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case EMFILE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt = "EMFILE";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt = "?";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "door_return error(%s,%d)", err_txt, err_code);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte syslog(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LOG_DAEMON | LOG_ERR,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("!door_return error(%s,%d)"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_txt,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err_code);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _getipnodebyname_req
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function executes the request ISCSI_DOOR_GETIPNODEBYNAME_REQ. It
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * calls getipnodebyname() but doesn't return all the information. The
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * confirmation structure only contains one IP address of the list returned
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * by getipnodebyname().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteiscsi_door_cnf_t *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_getipnodebyname_req(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getipnodebyname_req_t *req,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int req_len,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t *pcnf_len
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getipnodebyname_cnf_t *cnf = (getipnodebyname_cnf_t *)req;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t cnf_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct hostent *hptr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *name;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The opcode is changed immediately. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->hdr.opcode = ISCSI_DOOR_GETIPNODEBYNAME_CNF;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The size of the request is checked against the minimum required. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (req_len < sizeof (getipnodebyname_cnf_t)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *pcnf_len = req_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((iscsi_door_cnf_t *)cnf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte name = (char *)req + req->name_offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The pointer to the name has to stay inside the request but
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * after the header.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((name < ((char *)req + sizeof (getipnodebyname_req_t))) ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((name + req->name_length) > ((char *)req + req_len))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *pcnf_len = req_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((iscsi_door_cnf_t *)cnf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The library function is called. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hptr = getipnodebyname(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte name,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (int)req->af,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (int)req->flags,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (int *)&cnf->error_num);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (hptr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The call was successful. Now starts the painful work of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * parsing the data. However, for version 1 we will only
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return the first address.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf_len = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_alias_list_length = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_alias_list_offset = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_name_len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_name_offset = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addrlen = (uint32_t)hptr->h_length;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addrtype = (uint32_t)hptr->h_addrtype;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*hptr->h_addr_list != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((char *)cnf + sizeof (getipnodebyname_cnf_t)),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *hptr->h_addr_list,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hptr->h_length);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addr_list_length = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_size_needed += cnf->h_addrlen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf_len += hptr->h_length;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addr_list_length = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_size_needed += hptr->h_length;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *pcnf_len = cnf_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte freehostent(hptr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addrlen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addrtype = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_addr_list_length = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_name_offset = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_name_len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_alias_list_offset = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_alias_list_length = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *pcnf_len = sizeof (getipnodebyname_cnf_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((iscsi_door_cnf_t *)cnf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * call_child_door -- This function calls the child door with the value
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * provided by the caller.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecall_child_door(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int value
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_arg_t door_arg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memset(&door_arg, 0, sizeof (door_arg));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_arg.data_ptr = (char *)&value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte door_arg.data_size = sizeof (value);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) door_call(iscsi_child_door_handle, &door_arg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * get_luns_count --
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteuint32_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteget_luns_count(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int did
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_lun_list_t *lun_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iscsi_lun_list_t *tmp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t lun_count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list = (iscsi_lun_list_t *)malloc(sizeof (*lun_list));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memset(lun_list, 0, sizeof (*lun_list));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_in_cnt = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_all_tgts = B_TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte did,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ISCSI_LUN_OID_LIST_GET,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(lun_list);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The Ioctl didn't go well. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lun_list->ll_in_cnt >= lun_list->ll_out_cnt) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* We got it all. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We didn't get all the targets. Let's build a new Ioctl with
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a new size.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = lun_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = tmp->ll_out_cnt * sizeof (tmp->ll_luns);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len += sizeof (*tmp) - sizeof (tmp->ll_luns);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list = (iscsi_lun_list_t *)malloc(len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lun_list == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* No resources. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memset(lun_list, 0, len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_in_cnt = tmp->ll_out_cnt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_list->ll_all_tgts = B_TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_count = lun_list->ll_out_cnt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(lun_list);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (lun_count);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * discovery_event_wait -- Waits for the discovery process to finish.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteboolean_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortediscovery_event_wait(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int did
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte boolean_t rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t lun_count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t lun_timer;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t tmp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iSCSIDiscoveryMethod_t discovery_flags;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iSCSIDiscoveryMethod_t discovery_all;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = B_FALSE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_count = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_timer = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte discovery_flags = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte discovery_all = iSCSIDiscoveryMethodStatic |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iSCSIDiscoveryMethodSLP |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iSCSIDiscoveryMethodISNS |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte iSCSIDiscoveryMethodSendTargets;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* The status discovery flags are read. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte did,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ISCSI_DISCOVERY_EVENTS,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &discovery_flags) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* IO problem */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (discovery_flags == discovery_all) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Discovery over */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = B_TRUE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lun_timer >= ISCSI_DISCOVERY_POLL_DELAY2) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Let's check if the driver is making progress. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = get_luns_count(did);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tmp <= lun_count) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* No progress */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_count = tmp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_timer = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sleep(ISCSI_DISCOVERY_POLL_DELAY1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lun_timer += ISCSI_DISCOVERY_POLL_DELAY1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (rc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng/*ARGSUSED*/
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Mengstatic void
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Mengsignone(int sig, siginfo_t *sip, void *utp)
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng{
4246c8e92ef9ad6ada2b992b7af02832ff071bf7Jack Meng}