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 /* 22*4246c8e9SJack 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> 29*4246c8e9SJack 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 */ 56fcf3ce44SJohn Forte 57fcf3ce44SJohn Forte #if !defined(SMF_EXIT_ERR_OTHER) 58fcf3ce44SJohn Forte #define SMF_EXIT_ERR_OTHER -1 59fcf3ce44SJohn Forte #endif 60fcf3ce44SJohn Forte 61fcf3ce44SJohn Forte /* 62fcf3ce44SJohn Forte * Global Variables related to the synchronization of the child process 63fcf3ce44SJohn Forte * -------------------------------------------------------------------- 64fcf3ce44SJohn Forte */ 65fcf3ce44SJohn Forte static pid_t iscsi_child_pid; 66fcf3ce44SJohn Forte static sem_t iscsi_child_sem; 67fcf3ce44SJohn Forte static int iscsi_child_door_handle; 68fcf3ce44SJohn Forte static int iscsi_child_smf_exit_code; 69fcf3ce44SJohn Forte 70fcf3ce44SJohn Forte /* 71fcf3ce44SJohn Forte * Global Variables related to the door accessed by the kernel 72fcf3ce44SJohn Forte * ----------------------------------------------------------- 73fcf3ce44SJohn Forte */ 74fcf3ce44SJohn Forte static int iscsi_dev_handle; 75fcf3ce44SJohn Forte static int iscsi_kernel_door_handle; 76fcf3ce44SJohn Forte 77fcf3ce44SJohn Forte /* 78fcf3ce44SJohn Forte * Prototypes of Functions the body of which is defined farther down 79fcf3ce44SJohn Forte * in this file. 80fcf3ce44SJohn Forte * ----------------------------------------------------------------- 81fcf3ce44SJohn Forte */ 82fcf3ce44SJohn Forte static void call_child_door(int value); 83fcf3ce44SJohn Forte static void sigchld_handler(int sig); 84fcf3ce44SJohn Forte static boolean_t discovery_event_wait(int did); 85*4246c8e9SJack Meng static void signone(int, siginfo_t *, void *); 86fcf3ce44SJohn Forte 87fcf3ce44SJohn Forte static 88fcf3ce44SJohn Forte void 89fcf3ce44SJohn Forte iscsi_child_door( 90fcf3ce44SJohn Forte void *cookie, 91fcf3ce44SJohn Forte char *args, 92fcf3ce44SJohn Forte size_t alen, 93fcf3ce44SJohn Forte door_desc_t *ddp, 94fcf3ce44SJohn Forte uint_t ndid 95fcf3ce44SJohn Forte ); 96fcf3ce44SJohn Forte 97fcf3ce44SJohn Forte static 98fcf3ce44SJohn Forte void 99fcf3ce44SJohn Forte iscsi_kernel_door( 100fcf3ce44SJohn Forte void *cookie, 101fcf3ce44SJohn Forte char *args, 102fcf3ce44SJohn Forte size_t alen, 103fcf3ce44SJohn Forte door_desc_t *ddp, 104fcf3ce44SJohn Forte uint_t ndid 105fcf3ce44SJohn Forte ); 106fcf3ce44SJohn Forte 107fcf3ce44SJohn Forte static 108fcf3ce44SJohn Forte iscsi_door_cnf_t * 109fcf3ce44SJohn Forte _getipnodebyname_req( 110fcf3ce44SJohn Forte getipnodebyname_req_t *req, 111fcf3ce44SJohn Forte int req_len, 112fcf3ce44SJohn Forte size_t *pcnf_len 113fcf3ce44SJohn Forte ); 114fcf3ce44SJohn Forte 115fcf3ce44SJohn Forte /* 116fcf3ce44SJohn Forte * main -- Entry point of the iSCSI door server daemon 117fcf3ce44SJohn Forte * 118fcf3ce44SJohn Forte * This function forks, waits for the child process feedback and exits. 119fcf3ce44SJohn Forte */ 120fcf3ce44SJohn Forte /* ARGSUSED */ 121fcf3ce44SJohn Forte int 122fcf3ce44SJohn Forte main( 123fcf3ce44SJohn Forte int argc, 124fcf3ce44SJohn Forte char *argv[] 125fcf3ce44SJohn Forte ) 126fcf3ce44SJohn Forte { 127fcf3ce44SJohn Forte int i; 128*4246c8e9SJack Meng int sig; 129*4246c8e9SJack Meng sigset_t sigs, allsigs; 130*4246c8e9SJack Meng struct sigaction act; 131fcf3ce44SJohn Forte 132fcf3ce44SJohn Forte /* 133fcf3ce44SJohn Forte * Get the locale set up before calling any other routines 134fcf3ce44SJohn Forte * with messages to ouput. 135fcf3ce44SJohn Forte */ 136fcf3ce44SJohn Forte (void) setlocale(LC_ALL, ""); 137fcf3ce44SJohn Forte openlog("ISCSI_DOOR_DAEMON_SYSLOG_PP", LOG_PID, LOG_DAEMON); 138fcf3ce44SJohn Forte 139fcf3ce44SJohn Forte /* The child semaphore is created. */ 140fcf3ce44SJohn Forte if (sem_init(&iscsi_child_sem, 0, 0) == -1) { 141fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 142fcf3ce44SJohn Forte } 143fcf3ce44SJohn Forte 144fcf3ce44SJohn Forte /* The door for the child is created. */ 145fcf3ce44SJohn Forte iscsi_child_door_handle = door_create(iscsi_child_door, NULL, 0); 146fcf3ce44SJohn Forte if (iscsi_child_door_handle == -1) { 147fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 148fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 149fcf3ce44SJohn Forte } 150fcf3ce44SJohn Forte 151fcf3ce44SJohn Forte /* A signal handler is set for SIGCHLD. */ 152fcf3ce44SJohn Forte (void) signal(SIGCHLD, sigchld_handler); 153fcf3ce44SJohn Forte 154fcf3ce44SJohn Forte /* 155fcf3ce44SJohn Forte * Here begins the daemonizing code 156fcf3ce44SJohn Forte * -------------------------------- 157fcf3ce44SJohn Forte */ 158fcf3ce44SJohn Forte iscsi_child_pid = fork(); 159fcf3ce44SJohn Forte if (iscsi_child_pid < 0) { 160fcf3ce44SJohn Forte /* The fork failed. */ 161fcf3ce44SJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("Cannot fork")); 162fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 163fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 164fcf3ce44SJohn Forte } 165fcf3ce44SJohn Forte 166fcf3ce44SJohn Forte if (iscsi_child_pid) { 167fcf3ce44SJohn Forte /* 168fcf3ce44SJohn Forte * The parent exits after the child has provided feedback. This 169fcf3ce44SJohn Forte * waiting phase is to meet one of greenline's requirements. 170fcf3ce44SJohn Forte * We shouldn't return till we are sure the service is ready to 171fcf3ce44SJohn Forte * be provided. 172fcf3ce44SJohn Forte */ 173fcf3ce44SJohn Forte (void) sem_wait(&iscsi_child_sem); 174fcf3ce44SJohn Forte (void) sem_destroy(&iscsi_child_sem); 175fcf3ce44SJohn Forte exit(iscsi_child_smf_exit_code); 176fcf3ce44SJohn Forte } 177fcf3ce44SJohn Forte 178fcf3ce44SJohn Forte /* 179fcf3ce44SJohn Forte * stdout and stderr are redirected to "/dev/null". 180fcf3ce44SJohn Forte */ 181fcf3ce44SJohn Forte i = open("/dev/null", O_RDWR); 182fcf3ce44SJohn Forte (void) dup2(i, 1); 183fcf3ce44SJohn Forte (void) dup2(i, 2); 184fcf3ce44SJohn Forte 185fcf3ce44SJohn Forte /* 186fcf3ce44SJohn Forte * Here ends the daemonizing code 187fcf3ce44SJohn Forte * ------------------------------ 188fcf3ce44SJohn Forte */ 189fcf3ce44SJohn Forte 190fcf3ce44SJohn Forte /* 191*4246c8e9SJack Meng * Block out all signals 192fcf3ce44SJohn Forte */ 193*4246c8e9SJack Meng (void) sigfillset(&allsigs); 194*4246c8e9SJack Meng (void) pthread_sigmask(SIG_BLOCK, &allsigs, NULL); 195fcf3ce44SJohn Forte 196fcf3ce44SJohn Forte /* setup the door handle */ 197fcf3ce44SJohn Forte iscsi_kernel_door_handle = door_create(iscsi_kernel_door, NULL, 0); 198fcf3ce44SJohn Forte if (iscsi_kernel_door_handle == -1) { 199fcf3ce44SJohn Forte perror(gettext("door_create failed")); 200fcf3ce44SJohn Forte syslog(LOG_DAEMON | LOG_ERR, gettext("door_create failed")); 201fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 202fcf3ce44SJohn Forte } 203fcf3ce44SJohn Forte 204fcf3ce44SJohn Forte /* 205fcf3ce44SJohn Forte * The iSCSI driver is opened. 206fcf3ce44SJohn Forte */ 207fcf3ce44SJohn Forte iscsi_dev_handle = open(ISCSI_DRIVER_DEVCTL, O_RDWR); 208fcf3ce44SJohn Forte if (iscsi_dev_handle == -1) { 209fcf3ce44SJohn Forte /* The driver couldn't be opened. */ 210fcf3ce44SJohn Forte perror(gettext("iscsi device open failed")); 211fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 212fcf3ce44SJohn Forte } 213fcf3ce44SJohn Forte 214fcf3ce44SJohn Forte if (ioctl( 215fcf3ce44SJohn Forte iscsi_dev_handle, 216*4246c8e9SJack Meng ISCSI_SMF_ONLINE, 217fcf3ce44SJohn Forte &iscsi_kernel_door_handle) == -1) { 218fcf3ce44SJohn Forte (void) close(iscsi_dev_handle); 219*4246c8e9SJack Meng perror(gettext("ioctl: enable iscsi initiator")); 220fcf3ce44SJohn Forte exit(SMF_EXIT_ERR_OTHER); 221fcf3ce44SJohn Forte } 222fcf3ce44SJohn Forte 223*4246c8e9SJack Meng /* 224*4246c8e9SJack Meng * Keep the dev open, so to keep iscsi module from unloaded. 225*4246c8e9SJack Meng * This is crutial to guarantee the consistency of the 226*4246c8e9SJack Meng * door_handle and service state in kernel. 227*4246c8e9SJack Meng */ 228*4246c8e9SJack Meng 229fcf3ce44SJohn Forte /* We have to wait for the discovery process to finish. */ 230fcf3ce44SJohn Forte (void) discovery_event_wait(iscsi_dev_handle); 231fcf3ce44SJohn Forte 232*4246c8e9SJack Meng /* We let the parent know that everything is ok. */ 233fcf3ce44SJohn Forte call_child_door(SMF_EXIT_OK); 234*4246c8e9SJack Meng 235*4246c8e9SJack Meng /* now set up signals we care about */ 236*4246c8e9SJack Meng 237*4246c8e9SJack Meng (void) sigemptyset(&sigs); 238*4246c8e9SJack Meng (void) sigaddset(&sigs, SIGTERM); 239*4246c8e9SJack Meng (void) sigaddset(&sigs, SIGINT); 240*4246c8e9SJack Meng (void) sigaddset(&sigs, SIGQUIT); 241*4246c8e9SJack Meng 242*4246c8e9SJack Meng /* make sure signals to be enqueued */ 243*4246c8e9SJack Meng act.sa_flags = SA_SIGINFO; 244*4246c8e9SJack Meng act.sa_sigaction = signone; 245*4246c8e9SJack Meng 246*4246c8e9SJack Meng (void) sigaction(SIGTERM, &act, NULL); 247*4246c8e9SJack Meng (void) sigaction(SIGINT, &act, NULL); 248*4246c8e9SJack Meng (void) sigaction(SIGQUIT, &act, NULL); 249*4246c8e9SJack Meng 250*4246c8e9SJack Meng /* wait and process signals */ 251fcf3ce44SJohn Forte for (;;) { 252*4246c8e9SJack Meng sig = sigwait(&sigs); 253*4246c8e9SJack Meng if (sig < 0) 254*4246c8e9SJack Meng continue; 255*4246c8e9SJack Meng switch (sig) { 256*4246c8e9SJack Meng case SIGQUIT: 257*4246c8e9SJack Meng case SIGINT: 258*4246c8e9SJack Meng case SIGTERM: 259*4246c8e9SJack Meng if (ioctl( 260*4246c8e9SJack Meng iscsi_dev_handle, 261*4246c8e9SJack Meng ISCSI_SMF_OFFLINE, 262*4246c8e9SJack Meng NULL) == -1) { 263*4246c8e9SJack Meng perror(gettext("ioctl: disable" 264*4246c8e9SJack Meng " iscsi initiator")); 265*4246c8e9SJack Meng /* 266*4246c8e9SJack Meng * Keep running if unable 267*4246c8e9SJack Meng * to stop 268*4246c8e9SJack Meng */ 269*4246c8e9SJack Meng break; 270*4246c8e9SJack Meng } 271*4246c8e9SJack Meng (void) close(iscsi_dev_handle); 272*4246c8e9SJack Meng return (0); 273*4246c8e9SJack Meng break; 274*4246c8e9SJack Meng default: 275*4246c8e9SJack Meng break; 276*4246c8e9SJack Meng } 277fcf3ce44SJohn Forte } 278fcf3ce44SJohn Forte } 279fcf3ce44SJohn Forte 280fcf3ce44SJohn Forte /* 281fcf3ce44SJohn Forte * sigchld_handler -- SIGCHLD Handler 282fcf3ce44SJohn Forte * 283fcf3ce44SJohn Forte */ 284fcf3ce44SJohn Forte /* ARGSUSED */ 285fcf3ce44SJohn Forte static 286fcf3ce44SJohn Forte void 287fcf3ce44SJohn Forte sigchld_handler( 288fcf3ce44SJohn Forte int sig 289fcf3ce44SJohn Forte ) 290fcf3ce44SJohn Forte { 291fcf3ce44SJohn Forte int status; 292fcf3ce44SJohn Forte pid_t ret_pid; 293fcf3ce44SJohn Forte 294fcf3ce44SJohn Forte /* This is the default code. */ 295fcf3ce44SJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER; 296fcf3ce44SJohn Forte 297fcf3ce44SJohn Forte ret_pid = waitpid(iscsi_child_pid, &status, WNOHANG); 298fcf3ce44SJohn Forte 299fcf3ce44SJohn Forte if (ret_pid == iscsi_child_pid) { 300fcf3ce44SJohn Forte if (WIFEXITED(status)) { 301fcf3ce44SJohn Forte iscsi_child_smf_exit_code = WEXITSTATUS(status); 302fcf3ce44SJohn Forte } 303fcf3ce44SJohn Forte } 304fcf3ce44SJohn Forte (void) sem_post(&iscsi_child_sem); 305fcf3ce44SJohn Forte } 306fcf3ce44SJohn Forte 307fcf3ce44SJohn Forte /* 308fcf3ce44SJohn Forte * iscsi_child_door -- Child process door entry point 309fcf3ce44SJohn Forte * 310fcf3ce44SJohn Forte * This function is executed when a driver calls door_ki_upcall(). 311fcf3ce44SJohn Forte */ 312fcf3ce44SJohn Forte /* ARGSUSED */ 313fcf3ce44SJohn Forte static 314fcf3ce44SJohn Forte void 315fcf3ce44SJohn Forte iscsi_child_door( 316fcf3ce44SJohn Forte void *cookie, 317fcf3ce44SJohn Forte char *args, 318fcf3ce44SJohn Forte size_t alen, 319fcf3ce44SJohn Forte door_desc_t *ddp, 320fcf3ce44SJohn Forte uint_t ndid 321fcf3ce44SJohn Forte ) 322fcf3ce44SJohn Forte { 323fcf3ce44SJohn Forte int *ptr = (int *)args; 324fcf3ce44SJohn Forte 325fcf3ce44SJohn Forte iscsi_child_smf_exit_code = SMF_EXIT_ERR_OTHER; 326fcf3ce44SJohn Forte 327fcf3ce44SJohn Forte if (alen >= sizeof (iscsi_child_smf_exit_code)) { 328fcf3ce44SJohn Forte iscsi_child_smf_exit_code = *ptr; 329fcf3ce44SJohn Forte } 330fcf3ce44SJohn Forte (void) sem_post(&iscsi_child_sem); 331fcf3ce44SJohn Forte (void) door_return(NULL, 0, NULL, 0); 332fcf3ce44SJohn Forte } 333fcf3ce44SJohn Forte 334fcf3ce44SJohn Forte /* 335fcf3ce44SJohn Forte * iscsi_kernel_door -- Kernel door entry point 336fcf3ce44SJohn Forte * 337fcf3ce44SJohn Forte * This function is executed when a driver calls door_ki_upcall(). 338fcf3ce44SJohn Forte */ 339fcf3ce44SJohn Forte /* ARGSUSED */ 340fcf3ce44SJohn Forte static 341fcf3ce44SJohn Forte void 342fcf3ce44SJohn Forte iscsi_kernel_door( 343fcf3ce44SJohn Forte void *cookie, 344fcf3ce44SJohn Forte char *args, 345fcf3ce44SJohn Forte size_t alen, 346fcf3ce44SJohn Forte door_desc_t *ddp, 347fcf3ce44SJohn Forte uint_t ndid 348fcf3ce44SJohn Forte ) 349fcf3ce44SJohn Forte { 350fcf3ce44SJohn Forte iscsi_door_msg_hdr_t err_ind; 351fcf3ce44SJohn Forte iscsi_door_req_t *req; 352fcf3ce44SJohn Forte iscsi_door_cnf_t *cnf; 353fcf3ce44SJohn Forte size_t cnf_len; 354fcf3ce44SJohn Forte char *err_txt; 355fcf3ce44SJohn Forte int err_code; 356fcf3ce44SJohn Forte 357fcf3ce44SJohn Forte /* Local variables pre-initialization */ 358fcf3ce44SJohn Forte err_ind.signature = ISCSI_DOOR_REQ_SIGNATURE; 359fcf3ce44SJohn Forte err_ind.version = ISCSI_DOOR_REQ_VERSION_1; 360fcf3ce44SJohn Forte err_ind.opcode = ISCSI_DOOR_ERROR_IND; 361fcf3ce44SJohn Forte 362fcf3ce44SJohn Forte req = (iscsi_door_req_t *)args; 363fcf3ce44SJohn Forte cnf = (iscsi_door_cnf_t *)&err_ind; 364fcf3ce44SJohn Forte cnf_len = sizeof (err_ind); 365fcf3ce44SJohn Forte 366fcf3ce44SJohn Forte /* 367fcf3ce44SJohn Forte * The validity of the request is checked before going any farther. 368fcf3ce44SJohn Forte */ 369fcf3ce44SJohn Forte if (req == NULL) { 370fcf3ce44SJohn Forte /* 371fcf3ce44SJohn Forte * A request has to be passed. 372fcf3ce44SJohn Forte */ 373fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 374fcf3ce44SJohn Forte } else if (alen < sizeof (iscsi_door_msg_hdr_t)) { 375fcf3ce44SJohn Forte /* 376fcf3ce44SJohn Forte * The buffer containing the request must be at least as big 377fcf3ce44SJohn Forte * as message header. 378fcf3ce44SJohn Forte */ 379fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_LENGTH; 380fcf3ce44SJohn Forte } else if (req->hdr.signature != ISCSI_DOOR_REQ_SIGNATURE) { 381fcf3ce44SJohn Forte /* 382fcf3ce44SJohn Forte * The request must be correctly signed. 383fcf3ce44SJohn Forte */ 384fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 385fcf3ce44SJohn Forte } else if (req->hdr.version != ISCSI_DOOR_REQ_VERSION_1) { 386fcf3ce44SJohn Forte /* 387fcf3ce44SJohn Forte * The version of the request must be supported by the server. 388fcf3ce44SJohn Forte */ 389fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_VERSION; 390fcf3ce44SJohn Forte } else { 391fcf3ce44SJohn Forte /* 392fcf3ce44SJohn Forte * The request is treated according to the opcode. 393fcf3ce44SJohn Forte */ 394fcf3ce44SJohn Forte switch (req->hdr.opcode) { 395fcf3ce44SJohn Forte 396fcf3ce44SJohn Forte case ISCSI_DOOR_GETIPNODEBYNAME_REQ: 397fcf3ce44SJohn Forte cnf = _getipnodebyname_req( 398fcf3ce44SJohn Forte &req->ginbn_req, 399fcf3ce44SJohn Forte alen, 400fcf3ce44SJohn Forte &cnf_len); 401fcf3ce44SJohn Forte break; 402fcf3ce44SJohn Forte default: 403fcf3ce44SJohn Forte err_ind.status = ISCSI_DOOR_STATUS_REQ_INVALID; 404fcf3ce44SJohn Forte break; 405fcf3ce44SJohn Forte } 406fcf3ce44SJohn Forte } 407fcf3ce44SJohn Forte err_code = door_return((char *)cnf, cnf_len, NULL, 0); 408fcf3ce44SJohn Forte 409fcf3ce44SJohn Forte switch (err_code) { 410fcf3ce44SJohn Forte case E2BIG: 411fcf3ce44SJohn Forte err_txt = "E2BIG"; 412fcf3ce44SJohn Forte break; 413fcf3ce44SJohn Forte case EFAULT: 414fcf3ce44SJohn Forte err_txt = "EFAULT"; 415fcf3ce44SJohn Forte break; 416fcf3ce44SJohn Forte case EINVAL: 417fcf3ce44SJohn Forte err_txt = "EINVAL"; 418fcf3ce44SJohn Forte break; 419fcf3ce44SJohn Forte case EMFILE: 420fcf3ce44SJohn Forte err_txt = "EMFILE"; 421fcf3ce44SJohn Forte break; 422fcf3ce44SJohn Forte default: 423fcf3ce44SJohn Forte err_txt = "?"; 424fcf3ce44SJohn Forte break; 425fcf3ce44SJohn Forte } 426fcf3ce44SJohn Forte (void) fprintf(stderr, "door_return error(%s,%d)", err_txt, err_code); 427fcf3ce44SJohn Forte syslog( 428fcf3ce44SJohn Forte LOG_DAEMON | LOG_ERR, 429fcf3ce44SJohn Forte gettext("!door_return error(%s,%d)"), 430fcf3ce44SJohn Forte err_txt, 431fcf3ce44SJohn Forte err_code); 432fcf3ce44SJohn Forte } 433fcf3ce44SJohn Forte 434fcf3ce44SJohn Forte /* 435fcf3ce44SJohn Forte * _getipnodebyname_req 436fcf3ce44SJohn Forte * 437fcf3ce44SJohn Forte * This function executes the request ISCSI_DOOR_GETIPNODEBYNAME_REQ. It 438fcf3ce44SJohn Forte * calls getipnodebyname() but doesn't return all the information. The 439fcf3ce44SJohn Forte * confirmation structure only contains one IP address of the list returned 440fcf3ce44SJohn Forte * by getipnodebyname(). 441fcf3ce44SJohn Forte */ 442fcf3ce44SJohn Forte static 443fcf3ce44SJohn Forte iscsi_door_cnf_t * 444fcf3ce44SJohn Forte _getipnodebyname_req( 445fcf3ce44SJohn Forte getipnodebyname_req_t *req, 446fcf3ce44SJohn Forte int req_len, 447fcf3ce44SJohn Forte size_t *pcnf_len 448fcf3ce44SJohn Forte ) { 449fcf3ce44SJohn Forte getipnodebyname_cnf_t *cnf = (getipnodebyname_cnf_t *)req; 450fcf3ce44SJohn Forte size_t cnf_len; 451fcf3ce44SJohn Forte struct hostent *hptr; 452fcf3ce44SJohn Forte char *name; 453fcf3ce44SJohn Forte 454fcf3ce44SJohn Forte /* The opcode is changed immediately. */ 455fcf3ce44SJohn Forte cnf->hdr.opcode = ISCSI_DOOR_GETIPNODEBYNAME_CNF; 456fcf3ce44SJohn Forte 457fcf3ce44SJohn Forte /* The size of the request is checked against the minimum required. */ 458fcf3ce44SJohn Forte if (req_len < sizeof (getipnodebyname_cnf_t)) { 459fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT; 460fcf3ce44SJohn Forte *pcnf_len = req_len; 461fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 462fcf3ce44SJohn Forte } 463fcf3ce44SJohn Forte 464fcf3ce44SJohn Forte name = (char *)req + req->name_offset; 465fcf3ce44SJohn Forte 466fcf3ce44SJohn Forte /* 467fcf3ce44SJohn Forte * The pointer to the name has to stay inside the request but 468fcf3ce44SJohn Forte * after the header. 469fcf3ce44SJohn Forte */ 470fcf3ce44SJohn Forte if ((name < ((char *)req + sizeof (getipnodebyname_req_t))) || 471fcf3ce44SJohn Forte ((name + req->name_length) > ((char *)req + req_len))) { 472fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_REQ_FORMAT; 473fcf3ce44SJohn Forte *pcnf_len = req_len; 474fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 475fcf3ce44SJohn Forte } 476fcf3ce44SJohn Forte 477fcf3ce44SJohn Forte /* The library function is called. */ 478fcf3ce44SJohn Forte hptr = getipnodebyname( 479fcf3ce44SJohn Forte name, 480fcf3ce44SJohn Forte (int)req->af, 481fcf3ce44SJohn Forte (int)req->flags, 482fcf3ce44SJohn Forte (int *)&cnf->error_num); 483fcf3ce44SJohn Forte 484fcf3ce44SJohn Forte if (hptr) { 485fcf3ce44SJohn Forte /* 486fcf3ce44SJohn Forte * The call was successful. Now starts the painful work of 487fcf3ce44SJohn Forte * parsing the data. However, for version 1 we will only 488fcf3ce44SJohn Forte * return the first address. 489fcf3ce44SJohn Forte */ 490fcf3ce44SJohn Forte cnf_len = sizeof (getipnodebyname_cnf_t); 491fcf3ce44SJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t); 492fcf3ce44SJohn Forte cnf->h_alias_list_length = 0; 493fcf3ce44SJohn Forte cnf->h_alias_list_offset = 0; 494fcf3ce44SJohn Forte cnf->h_name_len = 0; 495fcf3ce44SJohn Forte cnf->h_name_offset = 0; 496fcf3ce44SJohn Forte 497fcf3ce44SJohn Forte cnf->h_addrlen = (uint32_t)hptr->h_length; 498fcf3ce44SJohn Forte cnf->h_addrtype = (uint32_t)hptr->h_addrtype; 499fcf3ce44SJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t); 500fcf3ce44SJohn Forte 501fcf3ce44SJohn Forte if (*hptr->h_addr_list != NULL) { 502fcf3ce44SJohn Forte (void) memcpy( 503fcf3ce44SJohn Forte ((char *)cnf + sizeof (getipnodebyname_cnf_t)), 504fcf3ce44SJohn Forte *hptr->h_addr_list, 505fcf3ce44SJohn Forte hptr->h_length); 506fcf3ce44SJohn Forte cnf->h_addr_list_length = 1; 507fcf3ce44SJohn Forte cnf->h_size_needed += cnf->h_addrlen; 508fcf3ce44SJohn Forte cnf_len += hptr->h_length; 509fcf3ce44SJohn Forte } else { 510fcf3ce44SJohn Forte cnf->h_addr_list_length = 0; 511fcf3ce44SJohn Forte cnf->h_size_needed += hptr->h_length; 512fcf3ce44SJohn Forte } 513fcf3ce44SJohn Forte *pcnf_len = cnf_len; 514fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS; 515fcf3ce44SJohn Forte freehostent(hptr); 516fcf3ce44SJohn Forte } else { 517fcf3ce44SJohn Forte cnf->hdr.status = ISCSI_DOOR_STATUS_SUCCESS; 518fcf3ce44SJohn Forte cnf->h_addrlen = 0; 519fcf3ce44SJohn Forte cnf->h_addrtype = 0; 520fcf3ce44SJohn Forte cnf->h_addr_list_offset = sizeof (getipnodebyname_cnf_t); 521fcf3ce44SJohn Forte cnf->h_addr_list_length = 0; 522fcf3ce44SJohn Forte cnf->h_name_offset = sizeof (getipnodebyname_cnf_t); 523fcf3ce44SJohn Forte cnf->h_name_len = 0; 524fcf3ce44SJohn Forte cnf->h_alias_list_offset = sizeof (getipnodebyname_cnf_t); 525fcf3ce44SJohn Forte cnf->h_alias_list_length = 0; 526fcf3ce44SJohn Forte cnf->h_size_needed = sizeof (getipnodebyname_cnf_t); 527fcf3ce44SJohn Forte *pcnf_len = sizeof (getipnodebyname_cnf_t); 528fcf3ce44SJohn Forte } 529fcf3ce44SJohn Forte return ((iscsi_door_cnf_t *)cnf); 530fcf3ce44SJohn Forte } 531fcf3ce44SJohn Forte 532fcf3ce44SJohn Forte /* 533fcf3ce44SJohn Forte * call_child_door -- This function calls the child door with the value 534fcf3ce44SJohn Forte * provided by the caller. 535fcf3ce44SJohn Forte * 536fcf3ce44SJohn Forte */ 537fcf3ce44SJohn Forte static 538fcf3ce44SJohn Forte void 539fcf3ce44SJohn Forte call_child_door( 540fcf3ce44SJohn Forte int value 541fcf3ce44SJohn Forte ) 542fcf3ce44SJohn Forte { 543fcf3ce44SJohn Forte door_arg_t door_arg; 544fcf3ce44SJohn Forte 545fcf3ce44SJohn Forte (void) memset(&door_arg, 0, sizeof (door_arg)); 546fcf3ce44SJohn Forte door_arg.data_ptr = (char *)&value; 547fcf3ce44SJohn Forte door_arg.data_size = sizeof (value); 548fcf3ce44SJohn Forte (void) door_call(iscsi_child_door_handle, &door_arg); 549fcf3ce44SJohn Forte } 550fcf3ce44SJohn Forte 551fcf3ce44SJohn Forte /* 552fcf3ce44SJohn Forte * get_luns_count -- 553fcf3ce44SJohn Forte */ 554fcf3ce44SJohn Forte static 555fcf3ce44SJohn Forte uint32_t 556fcf3ce44SJohn Forte get_luns_count( 557fcf3ce44SJohn Forte int did 558fcf3ce44SJohn Forte ) 559fcf3ce44SJohn Forte { 560fcf3ce44SJohn Forte iscsi_lun_list_t *lun_list; 561fcf3ce44SJohn Forte iscsi_lun_list_t *tmp; 562fcf3ce44SJohn Forte size_t len; 563fcf3ce44SJohn Forte uint32_t lun_count; 564fcf3ce44SJohn Forte 565fcf3ce44SJohn Forte lun_list = (iscsi_lun_list_t *)malloc(sizeof (*lun_list)); 566fcf3ce44SJohn Forte 567fcf3ce44SJohn Forte (void) memset(lun_list, 0, sizeof (*lun_list)); 568fcf3ce44SJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION; 569fcf3ce44SJohn Forte lun_list->ll_in_cnt = 1; 570fcf3ce44SJohn Forte lun_list->ll_all_tgts = B_TRUE; 571fcf3ce44SJohn Forte 572fcf3ce44SJohn Forte for (;;) { 573fcf3ce44SJohn Forte 574fcf3ce44SJohn Forte if (ioctl( 575fcf3ce44SJohn Forte did, 576fcf3ce44SJohn Forte ISCSI_LUN_OID_LIST_GET, 577fcf3ce44SJohn Forte lun_list) == -1) { 578fcf3ce44SJohn Forte free(lun_list); 579fcf3ce44SJohn Forte /* The Ioctl didn't go well. */ 580fcf3ce44SJohn Forte return (0); 581fcf3ce44SJohn Forte } 582fcf3ce44SJohn Forte if (lun_list->ll_in_cnt >= lun_list->ll_out_cnt) { 583fcf3ce44SJohn Forte /* We got it all. */ 584fcf3ce44SJohn Forte break; 585fcf3ce44SJohn Forte } 586fcf3ce44SJohn Forte /* 587fcf3ce44SJohn Forte * We didn't get all the targets. Let's build a new Ioctl with 588fcf3ce44SJohn Forte * a new size. 589fcf3ce44SJohn Forte */ 590fcf3ce44SJohn Forte tmp = lun_list; 591fcf3ce44SJohn Forte len = tmp->ll_out_cnt * sizeof (tmp->ll_luns); 592fcf3ce44SJohn Forte len += sizeof (*tmp) - sizeof (tmp->ll_luns); 593fcf3ce44SJohn Forte lun_list = (iscsi_lun_list_t *)malloc(len); 594fcf3ce44SJohn Forte if (lun_list == NULL) { 595fcf3ce44SJohn Forte /* No resources. */ 596fcf3ce44SJohn Forte free(tmp); 597fcf3ce44SJohn Forte return (0); 598fcf3ce44SJohn Forte } 599fcf3ce44SJohn Forte (void) memset(lun_list, 0, len); 600fcf3ce44SJohn Forte lun_list->ll_vers = ISCSI_INTERFACE_VERSION; 601fcf3ce44SJohn Forte lun_list->ll_in_cnt = tmp->ll_out_cnt; 602fcf3ce44SJohn Forte lun_list->ll_all_tgts = B_TRUE; 603fcf3ce44SJohn Forte free(tmp); 604fcf3ce44SJohn Forte } 605fcf3ce44SJohn Forte lun_count = lun_list->ll_out_cnt; 606fcf3ce44SJohn Forte free(lun_list); 607fcf3ce44SJohn Forte return (lun_count); 608fcf3ce44SJohn Forte } 609fcf3ce44SJohn Forte 610fcf3ce44SJohn Forte /* 611fcf3ce44SJohn Forte * discovery_event_wait -- Waits for the discovery process to finish. 612fcf3ce44SJohn Forte * 613fcf3ce44SJohn Forte */ 614fcf3ce44SJohn Forte static 615fcf3ce44SJohn Forte boolean_t 616fcf3ce44SJohn Forte discovery_event_wait( 617fcf3ce44SJohn Forte int did 618fcf3ce44SJohn Forte ) 619fcf3ce44SJohn Forte { 620fcf3ce44SJohn Forte boolean_t rc; 621fcf3ce44SJohn Forte uint32_t lun_count; 622fcf3ce44SJohn Forte uint32_t lun_timer; 623fcf3ce44SJohn Forte uint32_t tmp; 624fcf3ce44SJohn Forte iSCSIDiscoveryMethod_t discovery_flags; 625fcf3ce44SJohn Forte iSCSIDiscoveryMethod_t discovery_all; 626fcf3ce44SJohn Forte 627fcf3ce44SJohn Forte rc = B_FALSE; 628fcf3ce44SJohn Forte lun_count = 0; 629fcf3ce44SJohn Forte lun_timer = 0; 630fcf3ce44SJohn Forte discovery_flags = 0; 631fcf3ce44SJohn Forte discovery_all = iSCSIDiscoveryMethodStatic | 632fcf3ce44SJohn Forte iSCSIDiscoveryMethodSLP | 633fcf3ce44SJohn Forte iSCSIDiscoveryMethodISNS | 634fcf3ce44SJohn Forte iSCSIDiscoveryMethodSendTargets; 635fcf3ce44SJohn Forte 636fcf3ce44SJohn Forte for (;;) { 637fcf3ce44SJohn Forte 638fcf3ce44SJohn Forte /* The status discovery flags are read. */ 639fcf3ce44SJohn Forte if (ioctl( 640fcf3ce44SJohn Forte did, 641fcf3ce44SJohn Forte ISCSI_DISCOVERY_EVENTS, 642fcf3ce44SJohn Forte &discovery_flags) == -1) { 643fcf3ce44SJohn Forte /* IO problem */ 644fcf3ce44SJohn Forte break; 645fcf3ce44SJohn Forte } 646fcf3ce44SJohn Forte 647fcf3ce44SJohn Forte if (discovery_flags == discovery_all) { 648fcf3ce44SJohn Forte /* Discovery over */ 649fcf3ce44SJohn Forte rc = B_TRUE; 650fcf3ce44SJohn Forte break; 651fcf3ce44SJohn Forte } 652fcf3ce44SJohn Forte 653fcf3ce44SJohn Forte if (lun_timer >= ISCSI_DISCOVERY_POLL_DELAY2) { 654fcf3ce44SJohn Forte /* Let's check if the driver is making progress. */ 655fcf3ce44SJohn Forte tmp = get_luns_count(did); 656fcf3ce44SJohn Forte if (tmp <= lun_count) { 657fcf3ce44SJohn Forte /* No progress */ 658fcf3ce44SJohn Forte break; 659fcf3ce44SJohn Forte } 660fcf3ce44SJohn Forte lun_count = tmp; 661fcf3ce44SJohn Forte lun_timer = 0; 662fcf3ce44SJohn Forte } 663fcf3ce44SJohn Forte (void) sleep(ISCSI_DISCOVERY_POLL_DELAY1); 664fcf3ce44SJohn Forte lun_timer += ISCSI_DISCOVERY_POLL_DELAY1; 665fcf3ce44SJohn Forte } 666fcf3ce44SJohn Forte return (rc); 667fcf3ce44SJohn Forte } 668*4246c8e9SJack Meng 669*4246c8e9SJack Meng /*ARGSUSED*/ 670*4246c8e9SJack Meng static void 671*4246c8e9SJack Meng signone(int sig, siginfo_t *sip, void *utp) 672*4246c8e9SJack Meng { 673*4246c8e9SJack Meng } 674