1fcf3ce44SJohn Forte /* 2fcf3ce44SJohn Forte * CDDL HEADER START 3fcf3ce44SJohn Forte * 4fcf3ce44SJohn Forte * The contents of this file are subject to the terms of the 5fcf3ce44SJohn Forte * Common Development and Distribution License (the "License"). 6fcf3ce44SJohn Forte * You may not use this file except in compliance with the License. 7fcf3ce44SJohn Forte * 8fcf3ce44SJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fcf3ce44SJohn Forte * or http://www.opensolaris.org/os/licensing. 10fcf3ce44SJohn Forte * See the License for the specific language governing permissions 11fcf3ce44SJohn Forte * and limitations under the License. 12fcf3ce44SJohn Forte * 13fcf3ce44SJohn Forte * When distributing Covered Code, include this CDDL HEADER in each 14fcf3ce44SJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fcf3ce44SJohn Forte * If applicable, add the following below this CDDL HEADER, with the 16fcf3ce44SJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying 17fcf3ce44SJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner] 18fcf3ce44SJohn Forte * 19fcf3ce44SJohn Forte * CDDL HEADER END 20fcf3ce44SJohn Forte */ 21fcf3ce44SJohn Forte /* 224246c8e9SJack Meng * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fcf3ce44SJohn Forte * Use is subject to license terms. 24fcf3ce44SJohn Forte */ 25fcf3ce44SJohn Forte 26fcf3ce44SJohn Forte #include <sys/types.h> 27fcf3ce44SJohn Forte #include <sys/stat.h> 28fcf3ce44SJohn Forte #include <sys/socket.h> 294246c8e9SJack Meng #include <signal.h> 30fcf3ce44SJohn Forte #include <locale.h> 31fcf3ce44SJohn Forte #include <syslog.h> 32fcf3ce44SJohn Forte #include <netdb.h> 33fcf3ce44SJohn Forte #include <stdlib.h> 34fcf3ce44SJohn Forte #include <string.h> 35fcf3ce44SJohn Forte #include <fcntl.h> 36fcf3ce44SJohn Forte #include <unistd.h> 37fcf3ce44SJohn Forte #include <stdio.h> 38fcf3ce44SJohn Forte #include <errno.h> 39fcf3ce44SJohn Forte #include <door.h> 40fcf3ce44SJohn Forte #include <meta.h> 41fcf3ce44SJohn Forte #include <libsysevent.h> 42fcf3ce44SJohn Forte #include <wait.h> 43fcf3ce44SJohn Forte #include <semaphore.h> 44fcf3ce44SJohn Forte #include <libscf.h> 45fcf3ce44SJohn Forte 46fcf3ce44SJohn Forte #include <sys/scsi/adapters/iscsi_door.h> 47fcf3ce44SJohn Forte #include <sys/scsi/adapters/iscsi_if.h> 48fcf3ce44SJohn Forte 49fcf3ce44SJohn Forte /* 50fcf3ce44SJohn Forte * Local Defines 51fcf3ce44SJohn Forte * ------------- 52fcf3ce44SJohn Forte */ 53fcf3ce44SJohn Forte #define ISCSI_DOOR_DAEMON_SYSLOG_PP "iscsid" 54fcf3ce44SJohn Forte #define ISCSI_DISCOVERY_POLL_DELAY1 1 /* Seconds */ 55fcf3ce44SJohn Forte #define ISCSI_DISCOVERY_POLL_DELAY2 60 /* Seconds */ 56*aefdd131Sbing zhao - Sun Microsystems - Beijing China #define ISCSI_SMF_OFFLINE_DELAY 10 /* Seconds */ 57*aefdd131Sbing zhao - Sun Microsystems - Beijing China #define ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES 60 58fcf3ce44SJohn Forte 59fcf3ce44SJohn Forte #if !defined(SMF_EXIT_ERR_OTHER) 60fcf3ce44SJohn Forte #define SMF_EXIT_ERR_OTHER -1 61fcf3ce44SJohn Forte #endif 62fcf3ce44SJohn Forte 63fcf3ce44SJohn Forte /* 64fcf3ce44SJohn Forte * Global Variables related to the synchronization of the child process 65fcf3ce44SJohn Forte * -------------------------------------------------------------------- 66fcf3ce44SJohn Forte */ 67fcf3ce44SJohn Forte static pid_t iscsi_child_pid; 68fcf3ce44SJohn Forte static sem_t iscsi_child_sem; 69fcf3ce44SJohn Forte static int iscsi_child_door_handle; 70fcf3ce44SJohn Forte static int iscsi_child_smf_exit_code; 71fcf3ce44SJohn Forte 72fcf3ce44SJohn Forte /* 73fcf3ce44SJohn Forte * Global Variables related to the door accessed by the kernel 74fcf3ce44SJohn Forte * ----------------------------------------------------------- 75fcf3ce44SJohn Forte */ 76fcf3ce44SJohn Forte static int iscsi_dev_handle; 77fcf3ce44SJohn Forte static int iscsi_kernel_door_handle; 78fcf3ce44SJohn Forte 79fcf3ce44SJohn Forte /* 80fcf3ce44SJohn Forte * Prototypes of Functions the body of which is defined farther down 81fcf3ce44SJohn Forte * in this file. 82fcf3ce44SJohn Forte * ----------------------------------------------------------------- 83fcf3ce44SJohn Forte */ 84fcf3ce44SJohn Forte static void call_child_door(int value); 85fcf3ce44SJohn Forte static void sigchld_handler(int sig); 86fcf3ce44SJohn Forte static boolean_t discovery_event_wait(int did); 874246c8e9SJack Meng static void signone(int, siginfo_t *, void *); 88fcf3ce44SJohn Forte 89fcf3ce44SJohn Forte static 90fcf3ce44SJohn Forte void 91fcf3ce44SJohn Forte iscsi_child_door( 92fcf3ce44SJohn Forte void *cookie, 93fcf3ce44SJohn Forte char *args, 94fcf3ce44SJohn Forte size_t alen, 95fcf3ce44SJohn Forte door_desc_t *ddp, 96fcf3ce44SJohn Forte uint_t ndid 97fcf3ce44SJohn Forte ); 98fcf3ce44SJohn Forte 99fcf3ce44SJohn Forte static 100fcf3ce44SJohn Forte void 101fcf3ce44SJohn Forte iscsi_kernel_door( 102fcf3ce44SJohn Forte void *cookie, 103fcf3ce44SJohn Forte char *args, 104fcf3ce44SJohn Forte size_t alen, 105fcf3ce44SJohn Forte door_desc_t *ddp, 106fcf3ce44SJohn Forte uint_t ndid 107fcf3ce44SJohn Forte ); 108fcf3ce44SJohn Forte 109fcf3ce44SJohn Forte static 110fcf3ce44SJohn Forte iscsi_door_cnf_t * 111fcf3ce44SJohn Forte _getipnodebyname_req( 112fcf3ce44SJohn Forte getipnodebyname_req_t *req, 113fcf3ce44SJohn Forte int req_len, 114fcf3ce44SJohn Forte size_t *pcnf_len 115fcf3ce44SJohn Forte ); 116fcf3ce44SJohn Forte 117fcf3ce44SJohn Forte /* 118fcf3ce44SJohn Forte * main -- Entry point of the iSCSI door server daemon 119fcf3ce44SJohn Forte * 120fcf3ce44SJohn Forte * This function forks, waits for the child process feedback and exits. 121fcf3ce44SJohn Forte */ 122fcf3ce44SJohn Forte /* ARGSUSED */ 123fcf3ce44SJohn Forte int 124fcf3ce44SJohn Forte main( 125fcf3ce44SJohn Forte int argc, 126fcf3ce44SJohn Forte char *argv[] 127fcf3ce44SJohn Forte ) 128fcf3ce44SJohn Forte { 129fcf3ce44SJohn Forte int i; 1304246c8e9SJack Meng int sig; 131*aefdd131Sbing zhao - Sun Microsystems - Beijing China int ret = -1; 132*aefdd131Sbing zhao - Sun Microsystems - Beijing China int retry = 0; 1334246c8e9SJack Meng sigset_t sigs, allsigs; 1344246c8e9SJack Meng struct sigaction act; 135fcf3ce44SJohn Forte 136fcf3ce44SJohn Forte /* 137fcf3ce44SJohn Forte * Get the locale set up before calling any other routines 138fcf3ce44SJohn Forte * with messages to ouput. 139fcf3ce44SJohn Forte */ 140fcf3ce44SJohn Forte (void) setlocale(LC_ALL, ""); 141fcf3ce44SJohn Forte openlog("ISCSI_DOOR_DAEMON_SYSLOG_PP", LOG_PID, LOG_DAEMON); 142fcf3ce44SJohn Forte 143fcf3ce44SJohn Forte /* The child semaphore is created. */ 144fcf3ce44SJohn Forte if (sem_init(&iscsi_child_sem, 0, 0) == -1) { 145fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 146fcf3ce44SJohn Forte } 147fcf3ce44SJohn Forte 148fcf3ce44SJohn Forte /* The door for the child is created. */ 149fcf3ce44SJohn Forte iscsi_child_door_handle = door_create(iscsi_child_door, NULL, 0); 150fcf3ce44SJohn Forte if (iscsi_child_door_handle == -1) { 151fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 152fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 153fcf3ce44SJohn Forte } 154fcf3ce44SJohn Forte 155fcf3ce44SJohn Forte /* A signal handler is set for SIGCHLD. */ 156fcf3ce44SJohn Forte (void) signal(SIGCHLD, sigchld_handler); 157fcf3ce44SJohn Forte 158fcf3ce44SJohn Forte /* 159fcf3ce44SJohn Forte * Here begins the daemonizing code 160fcf3ce44SJohn Forte * -------------------------------- 161fcf3ce44SJohn Forte */ 162fcf3ce44SJohn Forte iscsi_child_pid = fork(); 163fcf3ce44SJohn Forte if (iscsi_child_pid < 0) { 164fcf3ce44SJohn Forte /* The fork failed. */ 165fcf3ce44SJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("Cannot fork")); 166fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 167fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 168fcf3ce44SJohn Forte } 169fcf3ce44SJohn Forte 170fcf3ce44SJohn Forte if (iscsi_child_pid) { 171fcf3ce44SJohn Forte /* 172fcf3ce44SJohn Forte * The parent exits after the child has provided feedback. This 173fcf3ce44SJohn Forte * waiting phase is to meet one of greenline's requirements. 174fcf3ce44SJohn Forte * We shouldn't return till we are sure the service is ready to 175fcf3ce44SJohn Forte * be provided. 176fcf3ce44SJohn Forte */ 177fcf3ce44SJohn Forte (void) sem_wait(&iscsi_child_sem); 178fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 179fcf3ce44SJohn Forte exit(iscsi_child_smf_exit_code); 180fcf3ce44SJohn Forte } 181fcf3ce44SJohn Forte 182fcf3ce44SJohn Forte /* 183fcf3ce44SJohn Forte * stdout and stderr are redirected to "/dev/null". 184fcf3ce44SJohn Forte */ 185fcf3ce44SJohn Forte i = open("/dev/null", O_RDWR); 186fcf3ce44SJohn Forte (void) dup2(i, 1); 187fcf3ce44SJohn Forte (void) dup2(i, 2); 188fcf3ce44SJohn Forte 189fcf3ce44SJohn Forte /* 190fcf3ce44SJohn Forte * Here ends the daemonizing code 191fcf3ce44SJohn Forte * ------------------------------ 192fcf3ce44SJohn Forte */ 193fcf3ce44SJohn Forte 194fcf3ce44SJohn Forte /* 1954246c8e9SJack Meng * Block out all signals 196fcf3ce44SJohn Forte */ 1974246c8e9SJack Meng (void) sigfillset(&allsigs); 1984246c8e9SJack Meng (void) pthread_sigmask(SIG_BLOCK, &allsigs, NULL); 199fcf3ce44SJohn Forte 200fcf3ce44SJohn Forte /* setup the door handle */ 201fcf3ce44SJohn Forte iscsi_kernel_door_handle = door_create(iscsi_kernel_door, NULL, 0); 202fcf3ce44SJohn Forte if (iscsi_kernel_door_handle == -1) { 203fcf3ce44SJohn Forte perror(gettext("door_create failed")); 204fcf3ce44SJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("door_create failed")); 205fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 206fcf3ce44SJohn Forte } 207fcf3ce44SJohn Forte 208fcf3ce44SJohn Forte /* 209fcf3ce44SJohn Forte * The iSCSI driver is opened. 210fcf3ce44SJohn Forte */ 211fcf3ce44SJohn Forte iscsi_dev_handle = open(ISCSI_DRIVER_DEVCTL, O_RDWR); 212fcf3ce44SJohn Forte if (iscsi_dev_handle == -1) { 213fcf3ce44SJohn Forte /* The driver couldn't be opened. */ 214fcf3ce44SJohn Forte perror(gettext("iscsi device open failed")); 215fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 216fcf3ce44SJohn Forte } 217fcf3ce44SJohn Forte 218fcf3ce44SJohn Forte if (ioctl( 219fcf3ce44SJohn Forte iscsi_dev_handle, 2204246c8e9SJack Meng ISCSI_SMF_ONLINE, 221fcf3ce44SJohn Forte &iscsi_kernel_door_handle) == -1) { 222fcf3ce44SJohn Forte (void) close(iscsi_dev_handle); 2234246c8e9SJack Meng perror(gettext("ioctl: enable iscsi initiator")); 224fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 225fcf3ce44SJohn Forte } 226fcf3ce44SJohn Forte 2274246c8e9SJack Meng /* 2284246c8e9SJack Meng * Keep the dev open, so to keep iscsi module from unloaded. 2294246c8e9SJack Meng * This is crutial to guarantee the consistency of the 2304246c8e9SJack Meng * door_handle and service state in kernel. 2314246c8e9SJack Meng */ 2324246c8e9SJack Meng 233fcf3ce44SJohn Forte /* We have to wait for the discovery process to finish. */ 234fcf3ce44SJohn Forte (void) discovery_event_wait(iscsi_dev_handle); 235fcf3ce44SJohn Forte 2364246c8e9SJack Meng /* We let the parent know that everything is ok. */ 237fcf3ce44SJohn Forte call_child_door(SMF_EXIT_OK); 2384246c8e9SJack Meng 2394246c8e9SJack Meng /* now set up signals we care about */ 2404246c8e9SJack Meng 2414246c8e9SJack Meng (void) sigemptyset(&sigs); 2424246c8e9SJack Meng (void) sigaddset(&sigs, SIGTERM); 2434246c8e9SJack Meng (void) sigaddset(&sigs, SIGINT); 2444246c8e9SJack Meng (void) sigaddset(&sigs, SIGQUIT); 2454246c8e9SJack Meng 2464246c8e9SJack Meng /* make sure signals to be enqueued */ 2474246c8e9SJack Meng act.sa_flags = SA_SIGINFO; 2484246c8e9SJack Meng act.sa_sigaction = signone; 2494246c8e9SJack Meng 2504246c8e9SJack Meng (void) sigaction(SIGTERM, &act, NULL); 2514246c8e9SJack Meng (void) sigaction(SIGINT, &act, NULL); 2524246c8e9SJack Meng (void) sigaction(SIGQUIT, &act, NULL); 2534246c8e9SJack Meng 2544246c8e9SJack Meng /* wait and process signals */ 255fcf3ce44SJohn Forte for (;;) { 2564246c8e9SJack Meng sig = sigwait(&sigs); 2574246c8e9SJack Meng if (sig < 0) 2584246c8e9SJack Meng continue; 2594246c8e9SJack Meng switch (sig) { 2604246c8e9SJack Meng case SIGQUIT: 2614246c8e9SJack Meng case SIGINT: 2624246c8e9SJack Meng case SIGTERM: 263*aefdd131Sbing zhao - Sun Microsystems - Beijing China do { 264*aefdd131Sbing zhao - Sun Microsystems - Beijing China ret = ioctl(iscsi_dev_handle, 265*aefdd131Sbing zhao - Sun Microsystems - Beijing China ISCSI_SMF_OFFLINE, NULL); 266*aefdd131Sbing zhao - Sun Microsystems - Beijing China if (ret == -1) { 2674246c8e9SJack Meng perror(gettext("ioctl: disable" 2684246c8e9SJack Meng " iscsi initiator")); 2694246c8e9SJack Meng /* 270*aefdd131Sbing zhao - Sun Microsystems - Beijing China * Keep retrying if unable 2714246c8e9SJack Meng * to stop 2724246c8e9SJack Meng */ 273*aefdd131Sbing zhao - Sun Microsystems - Beijing China (void) sleep(ISCSI_SMF_OFFLINE_DELAY); 274*aefdd131Sbing zhao - Sun Microsystems - Beijing China retry++; 2754246c8e9SJack Meng } 276*aefdd131Sbing zhao - Sun Microsystems - Beijing China } while (ret == -1 && 277*aefdd131Sbing zhao - Sun Microsystems - Beijing China retry < ISCSI_SMF_OFFLINE_MAX_RETRY_TIMES); 2784246c8e9SJack Meng (void) close(iscsi_dev_handle); 2794246c8e9SJack Meng return (0); 2804246c8e9SJack Meng break; 2814246c8e9SJack Meng default: 2824246c8e9SJack Meng break; 2834246c8e9SJack Meng } 284fcf3ce44SJohn Forte } 285fcf3ce44SJohn Forte } 286fcf3ce44SJohn Forte 287fcf3ce44SJohn Forte /* 288fcf3ce44SJohn Forte * sigchld_handler -- SIGCHLD Handler 289fcf3ce44SJohn Forte * 290fcf3ce44SJohn Forte */ 291fcf3ce44SJohn Forte /* ARGSUSED */ 292fcf3ce44SJohn Forte static 293fcf3ce44SJohn Forte void 294fcf3ce44SJohn Forte sigchld_handler( 295fcf3ce44SJohn Forte int sig 296fcf3ce44SJohn Forte ) 297fcf3ce44SJohn Forte { 298fcf3ce44SJohn Forte int status; 299fcf3ce44SJohn Forte pid_t ret_pid; 300fcf3ce44SJohn Forte 301fcf3ce44SJohn Forte /* This is the default code. */ 302fcf3ce44SJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER; 303fcf3ce44SJohn Forte 304fcf3ce44SJohn Forte ret_pid = waitpid(iscsi_child_pid, &status, WNOHANG); 305fcf3ce44SJohn Forte 306fcf3ce44SJohn Forte if (ret_pid == iscsi_child_pid) { 307fcf3ce44SJohn Forte if (WIFEXITED(status)) { 308fcf3ce44SJohn Forte iscsi_child_smf_exit_code = WEXITSTATUS(status); 309fcf3ce44SJohn Forte } 310fcf3ce44SJohn Forte } 311fcf3ce44SJohn Forte (void) sem_post(&iscsi_child_sem); 312fcf3ce44SJohn Forte } 313fcf3ce44SJohn Forte 314fcf3ce44SJohn Forte /* 315fcf3ce44SJohn Forte * iscsi_child_door -- Child process door entry point 316fcf3ce44SJohn Forte * 317fcf3ce44SJohn Forte * This function is executed when a driver calls door_ki_upcall(). 318fcf3ce44SJohn Forte */ 319fcf3ce44SJohn Forte /* ARGSUSED */ 320fcf3ce44SJohn Forte static 321fcf3ce44SJohn Forte void 322fcf3ce44SJohn Forte iscsi_child_door( 323fcf3ce44SJohn Forte void *cookie, 324fcf3ce44SJohn Forte char *args, 325fcf3ce44SJohn Forte size_t alen, 326fcf3ce44SJohn Forte door_desc_t *ddp, 327fcf3ce44SJohn Forte uint_t ndid 328fcf3ce44SJohn Forte ) 329fcf3ce44SJohn Forte { 330fcf3ce44SJohn Forte int *ptr = (int *)args; 331fcf3ce44SJohn Forte 332fcf3ce44SJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER; 333fcf3ce44SJohn Forte 334fcf3ce44SJohn Forte if (alen >= sizeof (iscsi_child_smf_exit_code)) { 335fcf3ce44SJohn Forte iscsi_child_smf_exit_code = *ptr; 336fcf3ce44SJohn Forte } 337fcf3ce44SJohn Forte (void) sem_post(&iscsi_child_sem); 338fcf3ce44SJohn Forte (void) door_return(NULL, 0, NULL, 0); 339fcf3ce44SJohn Forte } 340fcf3ce44SJohn Forte 341fcf3ce44SJohn Forte /* 342fcf3ce44SJohn Forte * iscsi_kernel_door -- Kernel door entry point 343fcf3ce44SJohn Forte * 344fcf3ce44SJohn Forte * This function is executed when a driver calls door_ki_upcall(). 345fcf3ce44SJohn Forte */ 346fcf3ce44SJohn Forte /* ARGSUSED */ 347fcf3ce44SJohn Forte static 348fcf3ce44SJohn Forte void 349fcf3ce44SJohn Forte iscsi_kernel_door( 350fcf3ce44SJohn Forte void *cookie, 351fcf3ce44SJohn Forte char *args, 352fcf3ce44SJohn Forte size_t alen, 353fcf3ce44SJohn Forte door_desc_t *ddp, 354fcf3ce44SJohn Forte uint_t ndid 355fcf3ce44SJohn Forte ) 356fcf3ce44SJohn Forte { 357fcf3ce44SJohn Forte iscsi_door_msg_hdr_t err_ind; 358fcf3ce44SJohn Forte iscsi_door_req_t *req; 359fcf3ce44SJohn Forte iscsi_door_cnf_t *cnf; 360fcf3ce44SJohn Forte size_t cnf_len; 361fcf3ce44SJohn Forte char *err_txt; 362fcf3ce44SJohn Forte int err_code; 363fcf3ce44SJohn Forte 364fcf3ce44SJohn Forte /* Local variables pre-initialization */ 365fcf3ce44SJohn Forte err_ind.signature = ISCSI_DOOR_REQ_SIGNATURE; 366fcf3ce44SJohn Forte err_ind.version = ISCSI_DOOR_REQ_VERSION_1; 367fcf3ce44SJohn Forte err_ind.opcode = ISCSI_DOOR_ERROR_IND; 368fcf3ce44SJohn Forte 369fcf3ce44SJohn Forte req = (iscsi_door_req_t *)args; 370fcf3ce44SJohn Forte cnf = (iscsi_door_cnf_t *)&err_ind; 371fcf3ce44SJohn Forte cnf_len = sizeof (err_ind); 372fcf3ce44SJohn Forte 373fcf3ce44SJohn Forte /* 374fcf3ce44SJohn Forte * The validity of the request is checked before going any farther. 375fcf3ce44SJohn Forte */ 376fcf3ce44SJohn Forte if (req == NULL) { 377fcf3ce44SJohn Forte /* 378fcf3ce44SJohn Forte * A request has to be passed. 379fcf3ce44SJohn Forte */ 380fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 381fcf3ce44SJohn Forte } else if (alen < sizeof (iscsi_door_msg_hdr_t)) { 382fcf3ce44SJohn Forte /* 383fcf3ce44SJohn Forte * The buffer containing the request must be at least as big 384fcf3ce44SJohn Forte * as message header. 385fcf3ce44SJohn Forte */ 386fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_LENGTH; 387fcf3ce44SJohn Forte } else if (req->hdr.signature != ISCSI_DOOR_REQ_SIGNATURE) { 388fcf3ce44SJohn Forte /* 389fcf3ce44SJohn Forte * The request must be correctly signed. 390fcf3ce44SJohn Forte */ 391fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 392fcf3ce44SJohn Forte } else if (req->hdr.version != ISCSI_DOOR_REQ_VERSION_1) { 393fcf3ce44SJohn Forte /* 394fcf3ce44SJohn Forte * The version of the request must be supported by the server. 395fcf3ce44SJohn Forte */ 396fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_VERSION; 397fcf3ce44SJohn Forte } else { 398fcf3ce44SJohn Forte /* 399fcf3ce44SJohn Forte * The request is treated according to the opcode. 400fcf3ce44SJohn Forte */ 401fcf3ce44SJohn Forte switch (req->hdr.opcode) { 402fcf3ce44SJohn Forte 403fcf3ce44SJohn Forte case ISCSI_DOOR_GETIPNODEBYNAME_REQ: 404fcf3ce44SJohn Forte cnf = _getipnodebyname_req( 405fcf3ce44SJohn Forte &req->ginbn_req, 406fcf3ce44SJohn Forte alen, 407fcf3ce44SJohn Forte &cnf_len); 408fcf3ce44SJohn Forte break; 409fcf3ce44SJohn Forte default: 410fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 411fcf3ce44SJohn Forte break; 412fcf3ce44SJohn Forte } 413fcf3ce44SJohn Forte } 414fcf3ce44SJohn Forte err_code = door_return((char *)cnf, cnf_len, NULL, 0); 415fcf3ce44SJohn Forte 416fcf3ce44SJohn Forte switch (err_code) { 417fcf3ce44SJohn Forte case E2BIG: 418fcf3ce44SJohn Forte err_txt = "E2BIG"; 419fcf3ce44SJohn Forte break; 420fcf3ce44SJohn Forte case EFAULT: 421fcf3ce44SJohn Forte err_txt = "EFAULT"; 422fcf3ce44SJohn Forte break; 423fcf3ce44SJohn Forte case EINVAL: 424fcf3ce44SJohn Forte err_txt = "EINVAL"; 425fcf3ce44SJohn Forte break; 426fcf3ce44SJohn Forte case EMFILE: 427fcf3ce44SJohn Forte err_txt = "EMFILE"; 428fcf3ce44SJohn Forte break; 429fcf3ce44SJohn Forte default: 430fcf3ce44SJohn Forte err_txt = "?"; 431fcf3ce44SJohn Forte break; 432fcf3ce44SJohn Forte } 433fcf3ce44SJohn Forte (void) fprintf(stderr, "door_return error(%s,%d)", err_txt, err_code); 434fcf3ce44SJohn Forte syslog( 435fcf3ce44SJohn Forte LOG_DAEMON | LOG_ERR, 436fcf3ce44SJohn Forte gettext("!door_return error(%s,%d)"), 437fcf3ce44SJohn Forte err_txt, 438fcf3ce44SJohn Forte err_code); 439fcf3ce44SJohn Forte } 440fcf3ce44SJohn Forte 441fcf3ce44SJohn Forte /* 442fcf3ce44SJohn Forte * _getipnodebyname_req 443fcf3ce44SJohn Forte * 444fcf3ce44SJohn Forte * This function executes the request ISCSI_DOOR_GETIPNODEBYNAME_REQ. It 445fcf3ce44SJohn Forte * calls getipnodebyname() but doesn't return all the information. The 446fcf3ce44SJohn Forte * confirmation structure only contains one IP address of the list returned 447fcf3ce44SJohn Forte * by getipnodebyname(). 448fcf3ce44SJohn Forte */ 449fcf3ce44SJohn Forte static 450fcf3ce44SJohn Forte iscsi_door_cnf_t * 451fcf3ce44SJohn Forte _getipnodebyname_req( 452fcf3ce44SJohn Forte getipnodebyname_req_t *req, 453fcf3ce44SJohn Forte int req_len, 454fcf3ce44SJohn Forte size_t *pcnf_len 455fcf3ce44SJohn Forte ) { 456fcf3ce44SJohn Forte getipnodebyname_cnf_t *cnf = (getipnodebyname_cnf_t *)req; 457fcf3ce44SJohn Forte size_t cnf_len; 458fcf3ce44SJohn Forte struct hostent *hptr; 459fcf3ce44SJohn Forte char *name; 460fcf3ce44SJohn Forte 461fcf3ce44SJohn Forte /* The opcode is changed immediately. */ 462fcf3ce44SJohn Forte cnf->hdr.opcode = ISCSI_DOOR_GETIPNODEBYNAME_CNF; 463fcf3ce44SJohn Forte 464fcf3ce44SJohn Forte /* The size of the request is checked against the minimum required. */ 465fcf3ce44SJohn Forte if (req_len < sizeof (getipnodebyname_cnf_t)) { 466fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT; 467fcf3ce44SJohn Forte *pcnf_len = req_len; 468fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 469fcf3ce44SJohn Forte } 470fcf3ce44SJohn Forte 471fcf3ce44SJohn Forte name = (char *)req + req->name_offset; 472fcf3ce44SJohn Forte 473fcf3ce44SJohn Forte /* 474fcf3ce44SJohn Forte * The pointer to the name has to stay inside the request but 475fcf3ce44SJohn Forte * after the header. 476fcf3ce44SJohn Forte */ 477fcf3ce44SJohn Forte if ((name < ((char *)req + sizeof (getipnodebyname_req_t))) || 478fcf3ce44SJohn Forte ((name + req->name_length) > ((char *)req + req_len))) { 479fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT; 480fcf3ce44SJohn Forte *pcnf_len = req_len; 481fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 482fcf3ce44SJohn Forte } 483fcf3ce44SJohn Forte 484fcf3ce44SJohn Forte /* The library function is called. */ 485fcf3ce44SJohn Forte hptr = getipnodebyname( 486fcf3ce44SJohn Forte name, 487fcf3ce44SJohn Forte (int)req->af, 488fcf3ce44SJohn Forte (int)req->flags, 489fcf3ce44SJohn Forte (int *)&cnf->error_num); 490fcf3ce44SJohn Forte 491fcf3ce44SJohn Forte if (hptr) { 492fcf3ce44SJohn Forte /* 493fcf3ce44SJohn Forte * The call was successful. Now starts the painful work of 494fcf3ce44SJohn Forte * parsing the data. However, for version 1 we will only 495fcf3ce44SJohn Forte * return the first address. 496fcf3ce44SJohn Forte */ 497fcf3ce44SJohn Forte cnf_len = sizeof (getipnodebyname_cnf_t); 498fcf3ce44SJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t); 499fcf3ce44SJohn Forte cnf->h_alias_list_length = 0; 500fcf3ce44SJohn Forte cnf->h_alias_list_offset = 0; 501fcf3ce44SJohn Forte cnf->h_name_len = 0; 502fcf3ce44SJohn Forte cnf->h_name_offset = 0; 503fcf3ce44SJohn Forte 504fcf3ce44SJohn Forte cnf->h_addrlen = (uint32_t)hptr->h_length; 505fcf3ce44SJohn Forte cnf->h_addrtype = (uint32_t)hptr->h_addrtype; 506fcf3ce44SJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t); 507fcf3ce44SJohn Forte 508fcf3ce44SJohn Forte if (*hptr->h_addr_list != NULL) { 509fcf3ce44SJohn Forte (void) memcpy( 510fcf3ce44SJohn Forte ((char *)cnf + sizeof (getipnodebyname_cnf_t)), 511fcf3ce44SJohn Forte *hptr->h_addr_list, 512fcf3ce44SJohn Forte hptr->h_length); 513fcf3ce44SJohn Forte cnf->h_addr_list_length = 1; 514fcf3ce44SJohn Forte cnf->h_size_needed += cnf->h_addrlen; 515fcf3ce44SJohn Forte cnf_len += hptr->h_length; 516fcf3ce44SJohn Forte } else { 517fcf3ce44SJohn Forte cnf->h_addr_list_length = 0; 518fcf3ce44SJohn Forte cnf->h_size_needed += hptr->h_length; 519fcf3ce44SJohn Forte } 520fcf3ce44SJohn Forte *pcnf_len = cnf_len; 521fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS; 522fcf3ce44SJohn Forte freehostent(hptr); 523fcf3ce44SJohn Forte } else { 524fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS; 525fcf3ce44SJohn Forte cnf->h_addrlen = 0; 526fcf3ce44SJohn Forte cnf->h_addrtype = 0; 527fcf3ce44SJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t); 528fcf3ce44SJohn Forte cnf->h_addr_list_length = 0; 529fcf3ce44SJohn Forte cnf->h_name_offset = sizeof (getipnodebyname_cnf_t); 530fcf3ce44SJohn Forte cnf->h_name_len = 0; 531fcf3ce44SJohn Forte cnf->h_alias_list_offset = sizeof (getipnodebyname_cnf_t); 532fcf3ce44SJohn Forte cnf->h_alias_list_length = 0; 533fcf3ce44SJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t); 534fcf3ce44SJohn Forte *pcnf_len = sizeof (getipnodebyname_cnf_t); 535fcf3ce44SJohn Forte } 536fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 537fcf3ce44SJohn Forte } 538fcf3ce44SJohn Forte 539fcf3ce44SJohn Forte /* 540fcf3ce44SJohn Forte * call_child_door -- This function calls the child door with the value 541fcf3ce44SJohn Forte * provided by the caller. 542fcf3ce44SJohn Forte * 543fcf3ce44SJohn Forte */ 544fcf3ce44SJohn Forte static 545fcf3ce44SJohn Forte void 546fcf3ce44SJohn Forte call_child_door( 547fcf3ce44SJohn Forte int value 548fcf3ce44SJohn Forte ) 549fcf3ce44SJohn Forte { 550fcf3ce44SJohn Forte door_arg_t door_arg; 551fcf3ce44SJohn Forte 552fcf3ce44SJohn Forte (void) memset(&door_arg, 0, sizeof (door_arg)); 553fcf3ce44SJohn Forte door_arg.data_ptr = (char *)&value; 554fcf3ce44SJohn Forte door_arg.data_size = sizeof (value); 555fcf3ce44SJohn Forte (void) door_call(iscsi_child_door_handle, &door_arg); 556fcf3ce44SJohn Forte } 557fcf3ce44SJohn Forte 558fcf3ce44SJohn Forte /* 559fcf3ce44SJohn Forte * get_luns_count -- 560fcf3ce44SJohn Forte */ 561fcf3ce44SJohn Forte static 562fcf3ce44SJohn Forte uint32_t 563fcf3ce44SJohn Forte get_luns_count( 564fcf3ce44SJohn Forte int did 565fcf3ce44SJohn Forte ) 566fcf3ce44SJohn Forte { 567fcf3ce44SJohn Forte iscsi_lun_list_t *lun_list; 568fcf3ce44SJohn Forte iscsi_lun_list_t *tmp; 569fcf3ce44SJohn Forte size_t len; 570fcf3ce44SJohn Forte uint32_t lun_count; 571fcf3ce44SJohn Forte 572fcf3ce44SJohn Forte lun_list = (iscsi_lun_list_t *)malloc(sizeof (*lun_list)); 573fcf3ce44SJohn Forte 574fcf3ce44SJohn Forte (void) memset(lun_list, 0, sizeof (*lun_list)); 575fcf3ce44SJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION; 576fcf3ce44SJohn Forte lun_list->ll_in_cnt = 1; 577fcf3ce44SJohn Forte lun_list->ll_all_tgts = B_TRUE; 578fcf3ce44SJohn Forte 579fcf3ce44SJohn Forte for (;;) { 580fcf3ce44SJohn Forte 581fcf3ce44SJohn Forte if (ioctl( 582fcf3ce44SJohn Forte did, 583fcf3ce44SJohn Forte ISCSI_LUN_OID_LIST_GET, 584fcf3ce44SJohn Forte lun_list) == -1) { 585fcf3ce44SJohn Forte free(lun_list); 586fcf3ce44SJohn Forte /* The Ioctl didn't go well. */ 587fcf3ce44SJohn Forte return (0); 588fcf3ce44SJohn Forte } 589fcf3ce44SJohn Forte if (lun_list->ll_in_cnt >= lun_list->ll_out_cnt) { 590fcf3ce44SJohn Forte /* We got it all. */ 591fcf3ce44SJohn Forte break; 592fcf3ce44SJohn Forte } 593fcf3ce44SJohn Forte /* 594fcf3ce44SJohn Forte * We didn't get all the targets. Let's build a new Ioctl with 595fcf3ce44SJohn Forte * a new size. 596fcf3ce44SJohn Forte */ 597fcf3ce44SJohn Forte tmp = lun_list; 598fcf3ce44SJohn Forte len = tmp->ll_out_cnt * sizeof (tmp->ll_luns); 599fcf3ce44SJohn Forte len += sizeof (*tmp) - sizeof (tmp->ll_luns); 600fcf3ce44SJohn Forte lun_list = (iscsi_lun_list_t *)malloc(len); 601fcf3ce44SJohn Forte if (lun_list == NULL) { 602fcf3ce44SJohn Forte /* No resources. */ 603fcf3ce44SJohn Forte free(tmp); 604fcf3ce44SJohn Forte return (0); 605fcf3ce44SJohn Forte } 606fcf3ce44SJohn Forte (void) memset(lun_list, 0, len); 607fcf3ce44SJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION; 608fcf3ce44SJohn Forte lun_list->ll_in_cnt = tmp->ll_out_cnt; 609fcf3ce44SJohn Forte lun_list->ll_all_tgts = B_TRUE; 610fcf3ce44SJohn Forte free(tmp); 611fcf3ce44SJohn Forte } 612fcf3ce44SJohn Forte lun_count = lun_list->ll_out_cnt; 613fcf3ce44SJohn Forte free(lun_list); 614fcf3ce44SJohn Forte return (lun_count); 615fcf3ce44SJohn Forte } 616fcf3ce44SJohn Forte 617fcf3ce44SJohn Forte /* 618fcf3ce44SJohn Forte * discovery_event_wait -- Waits for the discovery process to finish. 619fcf3ce44SJohn Forte * 620fcf3ce44SJohn Forte */ 621fcf3ce44SJohn Forte static 622fcf3ce44SJohn Forte boolean_t 623fcf3ce44SJohn Forte discovery_event_wait( 624fcf3ce44SJohn Forte int did 625fcf3ce44SJohn Forte ) 626fcf3ce44SJohn Forte { 627fcf3ce44SJohn Forte boolean_t rc; 628fcf3ce44SJohn Forte uint32_t lun_count; 629fcf3ce44SJohn Forte uint32_t lun_timer; 630fcf3ce44SJohn Forte uint32_t tmp; 631fcf3ce44SJohn Forte iSCSIDiscoveryMethod_t discovery_flags; 632fcf3ce44SJohn Forte iSCSIDiscoveryMethod_t discovery_all; 633fcf3ce44SJohn Forte 634fcf3ce44SJohn Forte rc = B_FALSE; 635fcf3ce44SJohn Forte lun_count = 0; 636fcf3ce44SJohn Forte lun_timer = 0; 637fcf3ce44SJohn Forte discovery_flags = 0; 638fcf3ce44SJohn Forte discovery_all = iSCSIDiscoveryMethodStatic | 639fcf3ce44SJohn Forte iSCSIDiscoveryMethodSLP | 640fcf3ce44SJohn Forte iSCSIDiscoveryMethodISNS | 641fcf3ce44SJohn Forte iSCSIDiscoveryMethodSendTargets; 642fcf3ce44SJohn Forte 643fcf3ce44SJohn Forte for (;;) { 644fcf3ce44SJohn Forte 645fcf3ce44SJohn Forte /* The status discovery flags are read. */ 646fcf3ce44SJohn Forte if (ioctl( 647fcf3ce44SJohn Forte did, 648fcf3ce44SJohn Forte ISCSI_DISCOVERY_EVENTS, 649fcf3ce44SJohn Forte &discovery_flags) == -1) { 650fcf3ce44SJohn Forte /* IO problem */ 651fcf3ce44SJohn Forte break; 652fcf3ce44SJohn Forte } 653fcf3ce44SJohn Forte 654fcf3ce44SJohn Forte if (discovery_flags == discovery_all) { 655fcf3ce44SJohn Forte /* Discovery over */ 656fcf3ce44SJohn Forte rc = B_TRUE; 657fcf3ce44SJohn Forte break; 658fcf3ce44SJohn Forte } 659fcf3ce44SJohn Forte 660fcf3ce44SJohn Forte if (lun_timer >= ISCSI_DISCOVERY_POLL_DELAY2) { 661fcf3ce44SJohn Forte /* Let's check if the driver is making progress. */ 662fcf3ce44SJohn Forte tmp = get_luns_count(did); 663fcf3ce44SJohn Forte if (tmp <= lun_count) { 664fcf3ce44SJohn Forte /* No progress */ 665fcf3ce44SJohn Forte break; 666fcf3ce44SJohn Forte } 667fcf3ce44SJohn Forte lun_count = tmp; 668fcf3ce44SJohn Forte lun_timer = 0; 669fcf3ce44SJohn Forte } 670fcf3ce44SJohn Forte (void) sleep(ISCSI_DISCOVERY_POLL_DELAY1); 671fcf3ce44SJohn Forte lun_timer += ISCSI_DISCOVERY_POLL_DELAY1; 672fcf3ce44SJohn Forte } 673fcf3ce44SJohn Forte return (rc); 674fcf3ce44SJohn Forte } 6754246c8e9SJack Meng 6764246c8e9SJack Meng /*ARGSUSED*/ 6774246c8e9SJack Meng static void 6784246c8e9SJack Meng signone(int sig, siginfo_t *sip, void *utp) 6794246c8e9SJack Meng { 6804246c8e9SJack Meng } 681