Home | History | Annotate | Line # | Download | only in iscsictl
iscsic_main.c revision 1.3
      1  1.3  christos /*	$NetBSD: iscsic_main.c,v 1.3 2011/10/30 18:40:06 christos Exp $	*/
      2  1.1       agc 
      3  1.1       agc /*-
      4  1.1       agc  * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
      5  1.1       agc  * All rights reserved.
      6  1.1       agc  *
      7  1.1       agc  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       agc  * by Wasabi Systems, Inc.
      9  1.1       agc  *
     10  1.1       agc  * Redistribution and use in source and binary forms, with or without
     11  1.1       agc  * modification, are permitted provided that the following conditions
     12  1.1       agc  * are met:
     13  1.1       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.1       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.1       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       agc  *    documentation and/or other materials provided with the distribution.
     18  1.1       agc  *
     19  1.1       agc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1       agc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1       agc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1       agc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1       agc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1       agc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1       agc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1       agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1       agc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1       agc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1       agc  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1       agc  */
     31  1.1       agc #include <sys/types.h>
     32  1.1       agc #include <sys/param.h>
     33  1.1       agc #include <sys/socket.h>
     34  1.1       agc #include <sys/un.h>
     35  1.1       agc 
     36  1.1       agc #include "iscsic_globals.h"
     37  1.1       agc #include "iscsi_test.h"
     38  1.1       agc 
     39  1.1       agc #include <err.h>
     40  1.1       agc #include <errno.h>
     41  1.1       agc #include <fcntl.h>
     42  1.1       agc #include <stdarg.h>
     43  1.1       agc 
     44  1.1       agc #define DEVICE    "/dev/iscsi0"
     45  1.1       agc 
     46  1.1       agc #define ISCSICTL_VERSION	"20110805"
     47  1.1       agc 
     48  1.1       agc /*
     49  1.1       agc  *-------- Table of commands and the associated handler function -------------
     50  1.1       agc */
     51  1.1       agc 
     52  1.1       agc static command_t cmds[] = {
     53  1.1       agc 	{"version", get_version},
     54  1.1       agc 	{"add_target", add_target},
     55  1.1       agc 	{"add_portal", add_portal},
     56  1.1       agc 	{"remove_target", remove_target},
     57  1.1       agc 	{"slp_find_targets", slp_find_targets},
     58  1.1       agc 	{"refresh_targets", refresh_targets},
     59  1.1       agc 	{"list_targets", list_targets},
     60  1.1       agc 	{"add_send_target", add_send_target},
     61  1.1       agc 	{"remove_send_target", remove_send_target},
     62  1.1       agc 	{"list_send_targets", list_send_targets},
     63  1.1       agc 	{"add_isns_server", add_isns_server},
     64  1.1       agc 	{"remove_isns_server", remove_isns_server},
     65  1.1       agc 	{"find_isns_servers", find_isns_servers},
     66  1.1       agc 	{"list_isns_servers", list_isns_servers},
     67  1.1       agc 	{"refresh_isns", refresh_isns},
     68  1.1       agc 	{"login", login},
     69  1.1       agc 	{"logout", logout},
     70  1.1       agc 	{"add_connection", add_connection},
     71  1.1       agc 	{"remove_connection", remove_connection},
     72  1.1       agc 	{"inquiry", inquiry},
     73  1.1       agc 	{"read_capacity", read_capacity},
     74  1.1       agc 	{"report_luns", report_luns},
     75  1.1       agc 	{"test_unit_ready", test_unit_ready},
     76  1.1       agc 	{"add_initiator", add_initiator},
     77  1.1       agc 	{"remove_initiator", remove_initiator},
     78  1.1       agc 	{"list_initiators", list_initiators},
     79  1.1       agc 	{"list_sessions", list_sessions},
     80  1.1       agc 	{"set_node_name", set_node_name},
     81  1.1       agc #ifdef ISCSI_DEBUG
     82  1.1       agc 	{"kill_daemon", kill_daemon},
     83  1.1       agc #endif
     84  1.1       agc #ifdef ISCSI_TEST_MODE
     85  1.1       agc 	{"test", test},
     86  1.1       agc #endif
     87  1.1       agc 	{NULL, NULL}
     88  1.1       agc };
     89  1.1       agc 
     90  1.1       agc 
     91  1.1       agc /*
     92  1.1       agc  *-------- Table of error codes and the associated message text -------------
     93  1.1       agc */
     94  1.1       agc 
     95  1.1       agc typedef struct {
     96  1.1       agc 	unsigned	 code;
     97  1.1       agc 	const char	*str;
     98  1.1       agc } status_msg_t;
     99  1.1       agc 
    100  1.1       agc static status_msg_t status_msg[] = {
    101  1.1       agc 	{ISCSI_STATUS_LIST_EMPTY, "The list is empty"},
    102  1.1       agc 	{ISCSI_STATUS_DUPLICATE_NAME, "The specified name is not unique"},
    103  1.1       agc 	{ISCSI_STATUS_GENERAL_ERROR, "A non-specific error occurred"},
    104  1.1       agc 	{ISCSI_STATUS_LOGIN_FAILED, "The login failed"},
    105  1.1       agc 	{ISCSI_STATUS_CONNECTION_FAILED, "The attempt to establish a connection failed"},
    106  1.1       agc 	{ISCSI_STATUS_AUTHENTICATION_FAILED, "Authentication negotiation failed"},
    107  1.1       agc 	{ISCSI_STATUS_NO_RESOURCES, "Could not allocate resources (e.g. memory)"},
    108  1.1       agc 	{ISCSI_STATUS_MAXED_CONNECTIONS, "Maximum number of connections exceeded"},
    109  1.1       agc 	{ISCSI_STATUS_INVALID_SESSION_ID, "Session ID not found"},
    110  1.1       agc 	{ISCSI_STATUS_INVALID_CONNECTION_ID, "Connection ID not found"},
    111  1.1       agc 	{ISCSI_STATUS_INVALID_SOCKET, "Specified socket is invalid"},
    112  1.1       agc 	{ISCSI_STATUS_NOTIMPL, "Feature not implemented"},
    113  1.1       agc 	{ISCSI_STATUS_CHECK_CONDITION, "Target reported CHECK CONDITION"},
    114  1.1       agc 	{ISCSI_STATUS_TARGET_BUSY, "Target reported BUSY"},
    115  1.1       agc 	{ISCSI_STATUS_TARGET_ERROR, "Target reported other error"},
    116  1.1       agc 	{ISCSI_STATUS_TARGET_FAILURE, "Command Response was Target Failure"},
    117  1.1       agc 	{ISCSI_STATUS_TARGET_DROP, "Target dropped connection"},
    118  1.1       agc 	{ISCSI_STATUS_SOCKET_ERROR, "Communication failure"},
    119  1.1       agc 	{ISCSI_STATUS_PARAMETER_MISSING, "A required ioctl parameter is missing"},
    120  1.1       agc 	{ISCSI_STATUS_PARAMETER_INVALID, "A parameter is malformed (string too long etc.)"},
    121  1.1       agc 	{ISCSI_STATUS_MAP_FAILED, "Mapping the LUNs failed"},
    122  1.1       agc 	{ISCSI_STATUS_NO_INITIATOR_NAME, "Initiator name not set"},
    123  1.1       agc 	{ISCSI_STATUS_NEGOTIATION_ERROR, "Negotiation failure (invalid key or value)"},
    124  1.1       agc 	{ISCSI_STATUS_TIMEOUT, "Command timed out (at iSCSI level)"},
    125  1.1       agc 	{ISCSI_STATUS_PROTOCOL_ERROR, "Internal Error (Protocol error reject)"},
    126  1.1       agc 	{ISCSI_STATUS_PDU_ERROR, "Internal Error (Invalid PDU field reject)"},
    127  1.1       agc 	{ISCSI_STATUS_CMD_NOT_SUPPORTED, "Target does not support iSCSI command"},
    128  1.1       agc 	{ISCSI_STATUS_DRIVER_UNLOAD, "Driver is unloading"},
    129  1.1       agc 	{ISCSI_STATUS_LOGOUT, "Session was logged out"},
    130  1.1       agc 	{ISCSI_STATUS_PDUS_LOST, "Excessive PDU loss"},
    131  1.1       agc 	{ISCSI_STATUS_INVALID_EVENT_ID, "Invalid Event ID"},
    132  1.1       agc 	{ISCSI_STATUS_EVENT_DEREGISTERED, "Wait for event cancelled by deregistration"},
    133  1.1       agc 	{ISCSI_STATUS_EVENT_WAITING, "Already waiting for event"},
    134  1.1       agc 	{ISCSI_STATUS_TASK_NOT_FOUND, "Task Management: task not found"},
    135  1.1       agc 	{ISCSI_STATUS_LUN_NOT_FOUND, "Task Management: LUN not found"},
    136  1.1       agc 	{ISCSI_STATUS_TASK_ALLEGIANT, "Task Management: Task still allegiant"},
    137  1.1       agc 	{ISCSI_STATUS_CANT_REASSIGN, "Task Management: Task reassignment not supported"},
    138  1.1       agc 	{ISCSI_STATUS_FUNCTION_UNSUPPORTED, "Task Management: Function unsupported"},
    139  1.1       agc 	{ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED, "Task Management: Function not authorized"},
    140  1.1       agc 	{ISCSI_STATUS_FUNCTION_REJECTED, "Task Management: Function rejected"},
    141  1.1       agc 	{ISCSI_STATUS_UNKNOWN_REASON, "Task Management: Unknown reason code"},
    142  1.1       agc 	{ISCSI_STATUS_DUPLICATE_ID, "Duplicate ID"},
    143  1.1       agc 	{ISCSI_STATUS_INVALID_ID, "ID not found"},
    144  1.1       agc 	{ISCSI_STATUS_TARGET_LOGOUT, "Target requested logout"},
    145  1.1       agc 	{ISCSI_STATUS_LOGOUT_CID_NOT_FOUND, "Logout error: Connection ID not found"},
    146  1.1       agc 	{ISCSI_STATUS_LOGOUT_RECOVERY_NS, "Logout error: Recovery not supported"},
    147  1.1       agc 	{ISCSI_STATUS_LOGOUT_ERROR, "Logout error: Unknown reason"},
    148  1.1       agc 
    149  1.1       agc 	{ISCSID_STATUS_LIST_EMPTY, "The list is empty"},
    150  1.1       agc 	{ISCSID_STATUS_DUPLICATE_NAME, "The specified name is not unique"},
    151  1.1       agc 	{ISCSID_STATUS_GENERAL_ERROR, "A non-specific error occurred"},
    152  1.1       agc 	{ISCSID_STATUS_CONNECT_ERROR, "Failed to connect to target"},
    153  1.1       agc 	{ISCSID_STATUS_NO_RESOURCES, "Could not allocate resources (e.g. memory)"},
    154  1.1       agc 	{ISCSID_STATUS_INVALID_SESSION_ID, "Session ID not found"},
    155  1.1       agc 	{ISCSID_STATUS_INVALID_CONNECTION_ID, "Connection ID not found"},
    156  1.1       agc 	{ISCSID_STATUS_NOTIMPL, "Feature not implemented"},
    157  1.1       agc 	{ISCSID_STATUS_SOCKET_ERROR, "Failed to create socket"},
    158  1.1       agc 	{ISCSID_STATUS_PARAMETER_MISSING, "A required parameter is missing"},
    159  1.1       agc 	{ISCSID_STATUS_PARAMETER_INVALID, "Request parameter is invalid"},
    160  1.1       agc 	{ISCSID_STATUS_NO_INITIATOR_NAME, "Initiator name was not set"},
    161  1.1       agc 	{ISCSID_STATUS_TIMEOUT, "Request timed out"},
    162  1.1       agc 	{ISCSID_STATUS_DRIVER_NOT_LOADED, "iSCSI Driver not loaded"},
    163  1.1       agc 	{ISCSID_STATUS_INVALID_REQUEST, "Unknown request code"},
    164  1.1       agc 	{ISCSID_STATUS_INVALID_PORTAL_ID, "Portal ID not found"},
    165  1.1       agc 	{ISCSID_STATUS_INVALID_TARGET_ID, "Target ID not found"},
    166  1.1       agc 	{ISCSID_STATUS_NOT_FOUND, "Requested item not found"},
    167  1.1       agc 	{ISCSID_STATUS_HOST_NOT_FOUND, "Target address not found"},
    168  1.1       agc 	{ISCSID_STATUS_HOST_TRY_AGAIN, "Target address retrieval failed, try again later"},
    169  1.1       agc 	{ISCSID_STATUS_HOST_ERROR, "Target address invalid"},
    170  1.1       agc 	{ISCSID_STATUS_NO_TARGETS_FOUND, "No targets found"},
    171  1.1       agc 	{ISCSID_STATUS_INVALID_ISNS_ID, "iSNS ID not found"},
    172  1.1       agc 	{ISCSID_STATUS_ISNS_ERROR, "Problem connecting to iSNS"},
    173  1.1       agc 	{ISCSID_STATUS_ISNS_SERVER_ERROR, "iSNS server returned garbage"},
    174  1.1       agc 	{ISCSID_STATUS_DUPLICATE_ENTRY, "The entry already exists"},
    175  1.1       agc 	{ISCSID_STATUS_INVALID_INITIATOR_ID, "Initiator ID not found"},
    176  1.1       agc 	{ISCSID_STATUS_INITIATOR_BIND_ERROR, "Bind to initiator portal failed"},
    177  1.1       agc 
    178  1.1       agc #ifdef ISCSI_TEST_MODE
    179  1.1       agc 	{ISCSI_STATUS_TEST_INACTIVE, "Test not assigned to connection"},
    180  1.1       agc 	{ISCSI_STATUS_TEST_CANCELED, "Test was cancelled"},
    181  1.1       agc 	{ISCSI_STATUS_TEST_CONNECTION_CLOSED, "Connection closed"},
    182  1.1       agc 	{ISCSI_STATUS_TEST_MODIFICATION_SKIPPED, "Modification skipped due to bad offset"},
    183  1.1       agc 	{ISCSI_STATUS_TEST_HEADER_CRC_ERROR, "Received PDU had bad header CRC"},
    184  1.1       agc 	{ISCSI_STATUS_TEST_DATA_CRC_ERROR, "Received PDU had bad data CRC"},
    185  1.1       agc 	{ISCSI_STATUS_TEST_DATA_READ_ERROR, "Error while receiving PDU"},
    186  1.1       agc #endif
    187  1.1       agc 
    188  1.1       agc 	{0, NULL}
    189  1.1       agc };
    190  1.1       agc 
    191  1.1       agc /* -------------------------------------------------------------------------- */
    192  1.1       agc /* local variables */
    193  1.1       agc 
    194  1.1       agc static struct sockaddr_un	daemon_name;	/* daemon socket name */
    195  1.1       agc 
    196  1.1       agc static char		 	sockdir[MAXPATHLEN]; /* where myname lives */
    197  1.1       agc static struct sockaddr_un	myname;		/* my socket name */
    198  1.1       agc static int			sock;		/* the socket */
    199  1.1       agc 
    200  1.1       agc static char *cmdname;			/* pointer to command name for error msgs */
    201  1.1       agc 
    202  1.1       agc /* global variables */
    203  1.1       agc 
    204  1.1       agc uint8_t buf[BUF_SIZE];			/* buffer for daemon comm and driver I/O */
    205  1.1       agc 
    206  1.1       agc int driver;						/* driver handle */
    207  1.1       agc 
    208  1.1       agc #ifdef ISCSI_DEBUG
    209  1.1       agc int debug_level = ISCSI_DEBUG;			/* How much info to display */
    210  1.1       agc #endif
    211  1.1       agc 
    212  1.1       agc /* -------------------------------------------------------------------------- */
    213  1.1       agc 
    214  1.1       agc #define progname  getprogname()
    215  1.1       agc 
    216  1.1       agc 
    217  1.1       agc /*
    218  1.1       agc  * bye:
    219  1.1       agc  *    Cleanup and exit. Does not return.
    220  1.1       agc */
    221  1.1       agc 
    222  1.1       agc static void
    223  1.1       agc bye(void)
    224  1.1       agc {
    225  1.1       agc 	close(sock);
    226  1.1       agc 	(void) unlink(myname.sun_path);
    227  1.1       agc 	(void) rmdir(sockdir);
    228  1.1       agc 	exit(EXIT_FAILURE);
    229  1.1       agc }
    230  1.1       agc 
    231  1.1       agc 
    232  1.1       agc /*
    233  1.1       agc  * arg_error:
    234  1.1       agc  *    Display error message about an invalid argument, exit.
    235  1.1       agc  *
    236  1.1       agc  *    Parameters:
    237  1.1       agc  *       argp        Argument value
    238  1.1       agc  *       fmt         Error message
    239  1.1       agc  *       ...         additional output arguments
    240  1.1       agc  *
    241  1.1       agc  *    Does not return.
    242  1.1       agc */
    243  1.1       agc 
    244  1.1       agc void
    245  1.1       agc arg_error(char *argp, const char *fmt, ...)
    246  1.1       agc {
    247  1.1       agc 	va_list args;
    248  1.1       agc 	char	lbuf[BUF_SIZE];
    249  1.1       agc 
    250  1.1       agc 	va_start(args, fmt);
    251  1.1       agc 	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
    252  1.1       agc 	fprintf(stderr, "%s: %s: Invalid option at or near '%s': %s\n",
    253  1.1       agc 		progname, cmdname, argp, lbuf);
    254  1.1       agc 	bye();
    255  1.1       agc }
    256  1.1       agc 
    257  1.1       agc 
    258  1.1       agc /*
    259  1.1       agc  * arg_missing:
    260  1.1       agc  *    Display error message about a missing argument, exit.
    261  1.1       agc  *
    262  1.1       agc  *    Parameters:
    263  1.1       agc  *       arg      Argument name
    264  1.1       agc  *
    265  1.1       agc  *    Does not return.
    266  1.1       agc  */
    267  1.1       agc 
    268  1.1       agc void
    269  1.1       agc arg_missing(const char *arg)
    270  1.1       agc {
    271  1.1       agc 	warnx("%s: Missing argument: %s", cmdname, arg);
    272  1.1       agc 	bye();
    273  1.1       agc }
    274  1.1       agc 
    275  1.1       agc 
    276  1.1       agc /*
    277  1.1       agc  * io_error:
    278  1.1       agc  *    Display error message about an I/O error (includes system error code)
    279  1.1       agc  *
    280  1.1       agc  *    Parameters:
    281  1.1       agc  *       fmt         format string
    282  1.1       agc  *       ...         additional output arguments
    283  1.1       agc  *
    284  1.1       agc  *    Does not return.
    285  1.1       agc  */
    286  1.1       agc 
    287  1.1       agc void
    288  1.1       agc io_error(const char *fmt, ...)
    289  1.1       agc {
    290  1.1       agc 	va_list args;
    291  1.1       agc 	char	lbuf[BUF_SIZE];
    292  1.1       agc 
    293  1.1       agc 	va_start(args, fmt);
    294  1.1       agc 	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
    295  1.1       agc 	fprintf(stderr, "%s: %s: %s: %s\n",
    296  1.1       agc 		progname, cmdname, lbuf, strerror(errno));
    297  1.1       agc 	bye();
    298  1.1       agc }
    299  1.1       agc 
    300  1.1       agc 
    301  1.1       agc /*
    302  1.1       agc  * gen_error:
    303  1.1       agc  *    Display general error message.
    304  1.1       agc  *
    305  1.1       agc  *    Parameters:
    306  1.1       agc  *       fmt         format string
    307  1.1       agc  *       ...         additional output arguments
    308  1.1       agc  *
    309  1.1       agc  *    Does not return.
    310  1.1       agc  */
    311  1.1       agc 
    312  1.1       agc void
    313  1.1       agc gen_error(const char *fmt, ...)
    314  1.1       agc {
    315  1.1       agc 	va_list args;
    316  1.1       agc 	char	lbuf[BUF_SIZE];
    317  1.1       agc 
    318  1.1       agc 	va_start(args, fmt);
    319  1.1       agc 	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
    320  1.1       agc 	fprintf(stderr, "%s: %s: %s\n", progname, cmdname, lbuf);
    321  1.1       agc 	bye();
    322  1.1       agc }
    323  1.1       agc 
    324  1.1       agc 
    325  1.1       agc /*
    326  1.1       agc  * check_extra_args:
    327  1.1       agc  *    Display error message & exit if there is an unrecognized argument.
    328  1.1       agc  *
    329  1.1       agc  *    Parameters:
    330  1.1       agc  *       argc     Argument count
    331  1.1       agc  *       argv     Argument value array.
    332  1.1       agc  *
    333  1.1       agc  *    Does not return if an extra arg is found.
    334  1.1       agc  */
    335  1.1       agc 
    336  1.1       agc void
    337  1.1       agc check_extra_args(int argc, char **argv)
    338  1.1       agc {
    339  1.1       agc 	int i;
    340  1.1       agc 
    341  1.1       agc 	for (i = 0; i < argc; i++)
    342  1.1       agc 		if (argv[i] != NULL) {
    343  1.1       agc 			warnx("%s: Unrecognized argument '%s'", cmdname, argv[i]);
    344  1.1       agc 			bye();
    345  1.1       agc 		}
    346  1.1       agc }
    347  1.1       agc 
    348  1.1       agc 
    349  1.1       agc /*
    350  1.1       agc  * status_error:
    351  1.1       agc  *    Display error message for status returned by daemon or driver, exit.
    352  1.1       agc  *
    353  1.1       agc  *    Parameters:
    354  1.1       agc  *          n     Status code.
    355  1.1       agc  *
    356  1.1       agc  *    Does not return.
    357  1.1       agc  */
    358  1.1       agc 
    359  1.1       agc void
    360  1.1       agc status_error(unsigned n)
    361  1.1       agc {
    362  1.1       agc 	int i;
    363  1.1       agc 
    364  1.1       agc 	for (i = 0; status_msg[i].code; i++)
    365  1.1       agc 		if (status_msg[i].code == n)
    366  1.1       agc 			break;
    367  1.1       agc 
    368  1.1       agc 	if (status_msg[i].code)
    369  1.1       agc 		warnx("%s: %s", cmdname, status_msg[i].str);
    370  1.1       agc 	else
    371  1.1       agc 		warnx("%s: Undefined error code %d", cmdname, n);
    372  1.1       agc 
    373  1.1       agc 	bye();
    374  1.1       agc }
    375  1.1       agc 
    376  1.1       agc 
    377  1.1       agc /*
    378  1.1       agc  * status_error_slist:
    379  1.1       agc  *    Display error message for status returned by daemon or driver, but
    380  1.1       agc  *	  replace a "list is empty" code by an "ID not found" code, exit.
    381  1.1       agc  *
    382  1.1       agc  *    Parameters:
    383  1.1       agc  *          n     Status code.
    384  1.1       agc  *
    385  1.1       agc  *    Does not return.
    386  1.1       agc  */
    387  1.1       agc 
    388  1.1       agc void
    389  1.1       agc status_error_slist(unsigned n)
    390  1.1       agc {
    391  1.1       agc 	if (n == ISCSI_STATUS_LIST_EMPTY || n == ISCSID_STATUS_LIST_EMPTY)
    392  1.1       agc 		n = ISCSI_STATUS_INVALID_ID;
    393  1.1       agc 	status_error (n);
    394  1.1       agc }
    395  1.1       agc 
    396  1.1       agc 
    397  1.1       agc /*
    398  1.1       agc  * get_response:
    399  1.1       agc  *    Read the response from the daemon.
    400  1.1       agc  *
    401  1.1       agc  *    Parameters:
    402  1.1       agc  *          temp  If TRUE, the response is dynamically allocated (so it is not
    403  1.1       agc  *                overwritten by further requests or responses).
    404  1.1       agc  *
    405  1.1       agc  *    Returns:
    406  1.1       agc  *          Pointer to the response.
    407  1.1       agc  *
    408  1.1       agc  *    Notes:
    409  1.1       agc  *       This routine allocates an extra integer to mark whether the returned
    410  1.1       agc  *       buffer is dynamic or static. This marker is one int before the
    411  1.1       agc  *       returned pointer.
    412  1.1       agc  */
    413  1.1       agc 
    414  1.1       agc iscsid_response_t *
    415  1.1       agc get_response(int temp)
    416  1.1       agc {
    417  1.3  christos 	ssize_t ret;
    418  1.3  christos 	size_t len;
    419  1.1       agc 	iscsid_response_t *rsp;
    420  1.1       agc 	int *pbuf;
    421  1.1       agc 
    422  1.3  christos 	pbuf = (int *)(void *)buf;
    423  1.3  christos 	rsp = (iscsid_response_t *)(void *)&pbuf[1];
    424  1.1       agc 	*pbuf = 0;
    425  1.1       agc 
    426  1.1       agc 	/* get size of response */
    427  1.1       agc 	len = sizeof(iscsid_response_t);
    428  1.1       agc 	ret = recv(sock, rsp, len, MSG_PEEK | MSG_WAITALL);
    429  1.3  christos 	if ((size_t)ret != len)
    430  1.1       agc 		io_error("Receiving daemon data");
    431  1.1       agc 
    432  1.1       agc 	DEB(9, ("Status %d, parlen %d\n", rsp->status, rsp->parameter_length));
    433  1.1       agc 
    434  1.1       agc 	len += rsp->parameter_length;
    435  1.1       agc 
    436  1.1       agc 	/*
    437  1.1       agc 	   if a temp buffer has been requested, or if the response is too large
    438  1.1       agc 	   to fit into the static buffer, alloc a temp buffer.
    439  1.1       agc 	 */
    440  1.1       agc 
    441  1.1       agc 	temp = temp || (len > (int)(sizeof(buf) - sizeof(int)));
    442  1.1       agc 
    443  1.1       agc 	if (temp) {
    444  1.1       agc 		if (NULL == (pbuf = (int *) malloc(len + sizeof(int))))
    445  1.2  christos 			gen_error("Can't allocate response buffer (%zu bytes)",
    446  1.1       agc 				len + sizeof(int));
    447  1.1       agc 
    448  1.3  christos 		rsp = (iscsid_response_t *)(void *)&pbuf[1];
    449  1.1       agc 		*pbuf = 1;
    450  1.1       agc 	}
    451  1.1       agc 	/* get the complete response */
    452  1.1       agc 
    453  1.1       agc 	ret = recv(sock, rsp, len, MSG_WAITALL);
    454  1.3  christos 	if ((size_t)ret != len)
    455  1.1       agc 		io_error("Receiving daemon data");
    456  1.1       agc 
    457  1.1       agc 	return rsp;
    458  1.1       agc }
    459  1.1       agc 
    460  1.1       agc 
    461  1.1       agc /*
    462  1.1       agc  * free_response:
    463  1.1       agc  *    If the response buffer was dynamically allocated, free it.
    464  1.1       agc  *
    465  1.1       agc  *    Parameters:
    466  1.1       agc  *          rsp   The response buffer.
    467  1.1       agc  *                The dynamic allocation marker is the int preceding
    468  1.1       agc  *                this address.
    469  1.1       agc  */
    470  1.1       agc 
    471  1.1       agc void
    472  1.1       agc free_response(iscsid_response_t * rsp)
    473  1.1       agc {
    474  1.1       agc 	int *pbuf;
    475  1.1       agc 
    476  1.3  christos 	pbuf = ((int *)(void *)rsp) - 1;
    477  1.1       agc 	if (*pbuf)
    478  1.1       agc 		free(pbuf);
    479  1.1       agc }
    480  1.1       agc 
    481  1.1       agc 
    482  1.1       agc /*
    483  1.1       agc  * send_request:
    484  1.1       agc  *    Send a request to the daemon.
    485  1.1       agc  *
    486  1.1       agc  *    Parameters:
    487  1.1       agc  *          request  The request code.
    488  1.1       agc  *          par_len  The parameter length.
    489  1.1       agc  *          par      The parameter.
    490  1.1       agc  */
    491  1.1       agc 
    492  1.1       agc void
    493  1.3  christos send_request(unsigned request, size_t par_len, void *par)
    494  1.1       agc {
    495  1.1       agc 	iscsid_request_t *req;
    496  1.3  christos 	size_t len;
    497  1.3  christos 	ssize_t ret;
    498  1.3  christos 	int req_temp;
    499  1.1       agc 
    500  1.1       agc 	len = sizeof(iscsid_request_t) + par_len;
    501  1.1       agc 
    502  1.1       agc 	/* alloc buffer if static one is too small to hold request */
    503  1.3  christos 	req_temp = len > sizeof(buf);
    504  1.1       agc 
    505  1.1       agc 	if (req_temp) {
    506  1.1       agc 		req = malloc(len);
    507  1.1       agc 		if (req == NULL)
    508  1.3  christos 			gen_error("Out of memory allocating %zu bytes\n", len);
    509  1.1       agc 	} else
    510  1.3  christos 		req = (iscsid_request_t *)(void *)buf;
    511  1.1       agc 
    512  1.1       agc 	/* setup request */
    513  1.1       agc 	req->request = request;
    514  1.3  christos 	req->parameter_length = (uint32_t)par_len;
    515  1.1       agc 	if (par_len)
    516  1.1       agc 		memcpy(req->parameter, par, par_len);
    517  1.1       agc 
    518  1.1       agc 	/* and send it out */
    519  1.3  christos 	ret = sendto(sock, req, len, 0, (struct sockaddr *)(void *)&daemon_name,
    520  1.3  christos 				 (socklen_t)sizeof(struct sockaddr_un));
    521  1.3  christos 	if ((size_t)ret != len) {
    522  1.1       agc 		io_error("Sending daemon message");
    523  1.1       agc 	}
    524  1.1       agc 	if (req_temp)
    525  1.1       agc 		free(req);
    526  1.1       agc }
    527  1.1       agc 
    528  1.1       agc 
    529  1.1       agc /*
    530  1.1       agc  * main:
    531  1.1       agc  *    check command, init driver handle and socket, dispatch command.
    532  1.1       agc  *
    533  1.1       agc  *    Parameter:  argc, argv - passed on to commands with offset of 2, so
    534  1.1       agc  *                argv [0] is first argument after command verb.
    535  1.1       agc  *
    536  1.1       agc  *    Returns:
    537  1.1       agc  *          Whatever the command handler returns, which is currently always 0.
    538  1.1       agc  */
    539  1.1       agc 
    540  1.1       agc int
    541  1.1       agc main(int argc, char **argv)
    542  1.1       agc {
    543  1.1       agc 	command_t	*c;
    544  1.1       agc 	int		 res;
    545  1.1       agc 	int		 i;
    546  1.1       agc 
    547  1.1       agc 	(void) snprintf(sockdir, sizeof(sockdir), "/tmp/iscsictl.XXXXXX");
    548  1.1       agc 	while ((i = getopt(argc, argv, "d:")) != -1) {
    549  1.1       agc 		switch(i) {
    550  1.1       agc 		case 'd':
    551  1.1       agc 			(void) snprintf(sockdir, sizeof(sockdir), "%s", optarg);
    552  1.1       agc 			break;
    553  1.1       agc 		default:
    554  1.1       agc 			break;
    555  1.1       agc 		}
    556  1.1       agc 	}
    557  1.1       agc 	if (argc - optind  < 1) {
    558  1.1       agc 		errx(EXIT_FAILURE, "Usage: %s <command> <options>, see manual for details.",
    559  1.1       agc 			progname);
    560  1.1       agc 	}
    561  1.1       agc 
    562  1.1       agc 	cmdname = argv[optind];
    563  1.1       agc 
    564  1.1       agc 	for (c = cmds; c->cmd != NULL; c++) {
    565  1.1       agc 		if (strcmp(c->cmd, cmdname) == 0) {
    566  1.1       agc 			break;
    567  1.1       agc 		}
    568  1.1       agc 	}
    569  1.1       agc 	if (c->cmd == NULL) {
    570  1.1       agc 		errx(EXIT_FAILURE, "Unknown command: '%s'", cmdname);
    571  1.1       agc 	}
    572  1.1       agc 	if ((driver = open(DEVICE, O_RDONLY)) < 0) {
    573  1.1       agc 		warn("Opening " DEVICE);
    574  1.1       agc #ifndef ISCSI_DEBUG
    575  1.1       agc 		/* DEBUG ONLY: Allow CLI to operate w/o driver */
    576  1.1       agc 		return EXIT_FAILURE;
    577  1.1       agc #endif
    578  1.1       agc 	}
    579  1.1       agc 
    580  1.1       agc 	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
    581  1.1       agc 	if (sock < 0)
    582  1.1       agc 		err(EXIT_FAILURE, "opening datagram socket");
    583  1.1       agc 
    584  1.1       agc 	/* bind socket to unique name */
    585  1.1       agc 	if (mkdtemp(sockdir) == NULL) {
    586  1.1       agc 		errx(EXIT_FAILURE, "can't create iscsictl dir '%s'", sockdir);
    587  1.1       agc 	}
    588  1.1       agc 	myname.sun_family = AF_UNIX;
    589  1.1       agc 	(void) snprintf(myname.sun_path, sizeof(myname.sun_path), "%s/socket", sockdir);
    590  1.3  christos 	if (bind(sock, (struct sockaddr *)(void *)&myname,
    591  1.3  christos 	    (socklen_t)sizeof(struct sockaddr_un)) < 0) {
    592  1.1       agc 		io_error("Binding name to datagram socket");
    593  1.1       agc 	}
    594  1.1       agc 	daemon_name.sun_family = AF_UNIX;
    595  1.1       agc 	strlcpy(daemon_name.sun_path, ISCSID_SOCK_NAME,
    596  1.1       agc 		sizeof(daemon_name.sun_path));
    597  1.1       agc 
    598  1.1       agc 	/* dispatch command */
    599  1.1       agc 	res = (*c->proc)(optind + 1, &argv[optind + 1]);
    600  1.1       agc 
    601  1.1       agc 	/* cleanup */
    602  1.1       agc 	close(sock);
    603  1.1       agc 	unlink(myname.sun_path);
    604  1.1       agc 	rmdir(sockdir);
    605  1.1       agc 
    606  1.1       agc 	return res;
    607  1.1       agc }
    608