From 6fea88c26e5bb459c67a7e5e4e975fd6abe5050a Mon Sep 17 00:00:00 2001 From: mlelstv Date: Sun, 29 May 2016 13:35:45 +0000 Subject: [PATCH] Several improvents to iscsid - debug log is now using syslog - seperate options for log level and foreground mode - writes a pidfile so that /etc/rc.d/iscsid works Now links with libutil for pidfile(), the functions login() and logout() needed to be renamed to avoid a conflict. - drops the nothreads option - handles signals to shut down gracefully - the driver may also shut down the daemon when it terminates Currently this cannot work as the driver can only terminate when the daemon has closed the driver file handle. --- sbin/iscsid/Makefile | 4 +- sbin/iscsid/iscsid.8 | 16 ++-- sbin/iscsid/iscsid.h | 4 +- sbin/iscsid/iscsid_discover.c | 128 +++++++++++++++---------------- sbin/iscsid/iscsid_driverif.c | 69 +++++++++-------- sbin/iscsid/iscsid_globals.h | 25 +++--- sbin/iscsid/iscsid_lists.c | 28 +++---- sbin/iscsid/iscsid_main.c | 139 +++++++++++++++++++--------------- sbin/iscsid/iscsid_targets.c | 40 +++++----- 9 files changed, 232 insertions(+), 221 deletions(-) diff --git a/sbin/iscsid/Makefile b/sbin/iscsid/Makefile index 45fc7cd3ac6e..9077bd78fbb6 100644 --- a/sbin/iscsid/Makefile +++ b/sbin/iscsid/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.5 2013/08/11 06:04:38 dholland Exp $ +# $NetBSD: Makefile,v 1.6 2016/05/29 13:35:45 mlelstv Exp $ PROG= iscsid @@ -11,7 +11,7 @@ CPPFLAGS+= -D_THREAD_SAFE MAN= iscsid.8 -LDADD = -lpthread -lisns +LDADD = -lpthread -lisns -lutil INCSDIR= /usr/include diff --git a/sbin/iscsid/iscsid.8 b/sbin/iscsid/iscsid.8 index d2500d6a7f11..094ef587a0e2 100644 --- a/sbin/iscsid/iscsid.8 +++ b/sbin/iscsid/iscsid.8 @@ -1,4 +1,4 @@ -.\" $NetBSD: iscsid.8,v 1.12 2015/11/10 12:41:18 tnn Exp $ +.\" $NetBSD: iscsid.8,v 1.13 2016/05/29 13:35:45 mlelstv Exp $ .\" .\" Copyright (c) 2011 Alistair Crooks .\" All rights reserved. @@ -31,7 +31,7 @@ .Nd interface to kernel iSCSI driver .Sh SYNOPSIS .Nm -.Op Fl n +.Op Fl D .Op Fl d Ar lvl .Sh DESCRIPTION The iSCSI initiator runs as a kernel driver, and provides access @@ -60,14 +60,14 @@ or when an error occurs reading from or writing to the socket. .Pp The .Fl d -flag increases the debug level to +flag increases the log level to .Ar lvl . -Any level above 0 causes -.Nm -to remain in the foreground, and increases the amount of debug output. +At level 0 only start and stop messages are logged. The -.Fl n -flag makes the daemon single-threaded. +.Fl D +flag causes +.Nm +to remain in the foreground and to write log output to stdout. .Pp It is envisaged that user-level communication take place with .Nm diff --git a/sbin/iscsid/iscsid.h b/sbin/iscsid/iscsid.h index 8117ff75003b..26ec7777b31e 100644 --- a/sbin/iscsid/iscsid.h +++ b/sbin/iscsid/iscsid.h @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid.h,v 1.4 2015/02/21 20:33:44 joerg Exp $ */ +/* $NetBSD: iscsid.h,v 1.5 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2004,2006,2011 The NetBSD Foundation, Inc. @@ -908,8 +908,6 @@ typedef struct { Corresponding version information for driver. */ -extern int nothreads; - __END_DECLS #endif /* !_ISCSID_H_ */ diff --git a/sbin/iscsid/iscsid_discover.c b/sbin/iscsid/iscsid_discover.c index 36ca2d3f37b2..9b554cae6e56 100644 --- a/sbin/iscsid/iscsid_discover.c +++ b/sbin/iscsid/iscsid_discover.c @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_discover.c,v 1.4 2012/06/24 13:08:38 mlelstv Exp $ */ +/* $NetBSD: iscsid_discover.c,v 1.5 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -116,7 +116,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) t = isns_new_trans(isns_handle, isnsp_DevAttrQry, MY_FLAGS); if (ISNS_INVALID_TRANS == t) { - DEBOUT(("%s: get_targets iscsi_new_trans failed\n", __func__)); + DEB(10,("%s: get_targets iscsi_new_trans failed", __func__)); return ISCSID_STATUS_NO_RESOURCES; } isns_add_string(t, isnst_iSCSIName, (char *)isns->reg_iscsi_name); @@ -132,20 +132,20 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) /*tag=27: security bitmap */ retval = isns_send_trans(t, &tout, &status); - DEB(9, ("isns_send_trans called, returns %d, status %d\n", retval, status)); + DEB(9, ("isns_send_trans called, returns %d, status %d", retval, status)); if (retval) { - DEBOUT(("iSNS Attribute Query failed, rc = %d\n", retval)); + DEB(10,("iSNS Attribute Query failed, rc = %d", retval)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } /* First is target name (the one we put in), ignore */ if (isns_get_tlv(t, ISNS_TLV_FIRST, &tag, &data_len, &data_p)) { - DEBOUT(("iSNS Attribute Query returned nothing\n")); + DEB(10,("iSNS Attribute Query returned nothing")); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } if (tag != isnst_iSCSIName) { - DEBOUT(("iSNS Query returned bad type (tag = %d, length = %d)\n", + DEB(10,("iSNS Query returned bad type (tag = %d, length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; @@ -153,7 +153,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_Delimiter) { - DEBOUT(("Attr Query Missing Delimiter (tag = %d, length = %d)\n", + DEB(10,("Attr Query Missing Delimiter (tag = %d, length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; @@ -161,8 +161,8 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_iSCSIName || !data_len || data_len >= ISCSI_STRING_LENGTH) { - DEBOUT(("iSNS Query returned no or invalid name (tag = %d, " - "length = %d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid name (tag = %d, " + "length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -170,14 +170,14 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_iSCSINodeType || data_len != 4) { - DEBOUT(("iSNS Query returned no or invalid node type (tag = %d, " - "length = %d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid node type (tag = %d, " + "length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } u32 = ntohl(*((uint32_t *) data_p)); if (!(u32 & 1)) { - DEBOUT(("iSNS Query returned bad type (type=%x, should be 1)\n", u32)); + DEB(10,("iSNS Query returned bad type (type=%x, should be 1)", u32)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -185,7 +185,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag == isnst_iSCSIAlias) { if (data_len >= ISCSI_STRING_LENGTH) { - DEBOUT(("iSNS Query returned invalid alias (tag=%d, length=%d)\n", + DEB(10,("iSNS Query returned invalid alias (tag=%d, length=%d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; @@ -199,7 +199,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) if (ISNS_INVALID_TRANS == (t = isns_new_trans(isns_handle, isnsp_DevAttrQry, MY_FLAGS))) { - DEBOUT(("get_targets iscsi_new_trans failed\n")); + DEB(10,("get_targets iscsi_new_trans failed")); return ISCSID_STATUS_NO_RESOURCES; } isns_add_string(t, isnst_iSCSIName, (char *)isns->reg_iscsi_name); @@ -213,20 +213,20 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_add_tlv(t, isnst_PGTag, 0, NULL); /* 51: group tag */ retval = isns_send_trans(t, &tout, &status); - DEB(9, ("isns_send_trans called, returns %d, status %d\n", retval, status)); + DEB(9, ("isns_send_trans called, returns %d, status %d", retval, status)); if (retval) { - DEBOUT(("iSNS Attribute Query failed, rc = %d\n", retval)); + DEB(10,("iSNS Attribute Query failed, rc = %d", retval)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } /* First is target name (the one we put in), ignore */ if (isns_get_tlv(t, ISNS_TLV_FIRST, &tag, &data_len, &data_p)) { - DEBOUT(("iSNS Attribute Query returned nothing\n")); + DEB(10,("iSNS Attribute Query returned nothing")); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } if (tag != isnst_iSCSIName) { - DEBOUT(("iSNS Query2 returned bad name (tag = %d, length = %d)\n", + DEB(10,("iSNS Query2 returned bad name (tag = %d, length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; @@ -234,7 +234,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_Delimiter) { - DEBOUT(("Attr Query2 Missing Delimiter (tag = %d, length = %d)\n", + DEB(10,("Attr Query2 Missing Delimiter (tag = %d, length = %d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; @@ -243,8 +243,8 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) while (!isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p)) { if (tag != isnst_PGiSCSIName || !data_len || data_len >= ISCSI_STRING_LENGTH) { - DEBOUT(("iSNS Query2 returned no or invalid name (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS Query2 returned no or invalid name (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -252,8 +252,8 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_PGPortIPAddr || data_len != 16) { - DEBOUT(("iSNS Query returned no or invalid address (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid address (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -262,15 +262,15 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) /* Now comes the port */ isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_PGPortIPPort || data_len != 4) { - DEBOUT(("iSNS Query returned no or invalid port (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid port (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } u32 = ntohl(*((uint32_t *) data_p)); if (u32 & 0xffff0000) { - DEBOUT(("iSNS Query returned invalid port (flags=%x, " - "should be 0)\n", u32 >> 16)); + DEB(10,("iSNS Query returned invalid port (flags=%x, " + "should be 0)", u32 >> 16)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -279,8 +279,8 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) /* And each target must have a group tag */ isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p); if (tag != isnst_PGTag || (data_len && data_len != 4)) { - DEBOUT(("iSNS Query returned no or invalid group tag (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid group tag (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); return ISCSID_STATUS_ISNS_SERVER_ERROR; } @@ -292,7 +292,7 @@ get_isns_target_info(isns_t * isns, uint8_t * TargetName) /* we have everything necessary to describe the target, add it. */ - DEB(1, ("Adding <%s>, IP <%s>, Port %d, Tag %d\n", + DEB(2, ("Adding <%s>, IP <%s>, Port %d, Tag %d", name, addr.address, addr.port, addr.group_tag)); if ((targ = add_discovered_target((unsigned char *)name, &addr, PORTAL_TYPE_ISNS, @@ -335,7 +335,7 @@ deregister_isns_server(isns_t * isns) if (ISNS_INVALID_TRANS == (t = isns_new_trans(isns_handle, isnsp_DevDereg, MY_FLAGS))) { - DEBOUT(("dereg_isns_server iscsi_new_trans failed\n")); + DEB(10,("dereg_isns_server iscsi_new_trans failed")); return ISCSID_STATUS_NO_RESOURCES; } @@ -349,7 +349,7 @@ deregister_isns_server(isns_t * isns) isns_add_string(t, isnst_iSCSIName, (char *)isns->reg_iscsi_name); retval = isns_send_trans(t, &tout, &status); - DEB(9, ("DevAttrReg request returns %d, status %d\n", retval, status)); + DEB(9, ("DevAttrReg request returns %d, status %d", retval, status)); isns_free_trans(t); return ISCSID_STATUS_SUCCESS; @@ -375,7 +375,7 @@ register_isns_server(isns_t * isns) if (ISNS_INVALID_TRANS == (t = isns_new_trans(isns_handle, isnsp_DevAttrReg, MY_FLAGS))) { - DEBOUT(("iscsi_new_trans failed\n")); + DEB(10,("iscsi_new_trans failed")); return ISCSID_STATUS_NO_RESOURCES; } @@ -394,7 +394,7 @@ register_isns_server(isns_t * isns) /*tag=33 (node type = intiator) */ retval = isns_send_trans(t, &tout, &status); - DEB(9, ("DevAttrReg request returns %d, status %d\n", retval, status)); + DEB(9, ("DevAttrReg request returns %d, status %d", retval, status)); isns_free_trans(t); if (retval || status) @@ -427,7 +427,7 @@ get_registration_info(isns_t * isns) /*Get our source IP and port numbers */ n = sizeof(sa); if (getsockname(isns->sock, (struct sockaddr *)(void *)&sa, &n)) { - DEBOUT(("Getsockname returned error %d\n", errno)); + DEB(10,("Getsockname returned error %d", errno)); return ISCSID_STATUS_GENERAL_ERROR; } switch (sa.ss_family) { @@ -456,7 +456,7 @@ get_registration_info(isns_t * isns) break; default: - DEBOUT(("Getsockname returned unknown address family: %d\n", + DEB(10,("Getsockname returned unknown address family: %d", sa.ss_family)); return ISCSID_STATUS_GENERAL_ERROR; } @@ -488,7 +488,7 @@ iscsi_isns_serverconn(isns_t * isns) if (isns_handle == ISNS_INVALID_HANDLE) { if ((retval = isns_init(&isns_handle, 0)) != 0) { /*Couldn't initialize the iSNS library */ - DEBOUT(("isns_init failed with code %d\n", retval)); + DEB(10,("isns_init failed with code %d", retval)); isns_handle = ISNS_INVALID_HANDLE; return ISCSID_STATUS_GENERAL_ERROR; } @@ -507,7 +507,7 @@ iscsi_isns_serverconn(isns_t * isns) retval = getaddrinfo((char *)isns->address, port, &hints, &ai); if (retval) { - DEBOUT(("getaddrinfo failed with code %d (%s)\n", + DEB(10,("getaddrinfo failed with code %d (%s)", retval, gai_strerror(retval))); return ISCSID_STATUS_GENERAL_ERROR; } @@ -517,7 +517,7 @@ iscsi_isns_serverconn(isns_t * isns) addr->ai_protocol); if (sock == -1) { - DEBOUT(("%s: socket call FAILED!\n", __func__)); + DEB(10,("%s: socket call FAILED!", __func__)); freeaddrinfo(ai); return (uint32_t)-1; } @@ -525,19 +525,19 @@ iscsi_isns_serverconn(isns_t * isns) if (connect(sock, addr->ai_addr, addr->ai_addrlen) != -1) break; - DEB(1, ("%s: connect call FAILED!\n", __func__)); + DEB(1, ("%s: connect call FAILED!", __func__)); close(sock); sock = -1; } if (addr == NULL) { - DEBOUT(("%s: couldn't connect!\n", __func__)); + DEB(10,("%s: couldn't connect!", __func__)); freeaddrinfo(ai); return ISCSID_STATUS_GENERAL_ERROR; } if (isns_add_servercon(isns_handle, sock, addr)) { - DEBOUT(("%s: FAILED!\n", __func__)); + DEB(10,("%s: FAILED!", __func__)); close(sock); freeaddrinfo(ai); return ISCSID_STATUS_GENERAL_ERROR; @@ -581,12 +581,12 @@ update_isns_server_info(isns_t * isns) uint8_t TargetName[ISCSI_STRING_LENGTH]; - DEB(9, ("update_isns_server_info for iSNS %s\n", isns->address)); + DEB(9, ("update_isns_server_info for iSNS %s", isns->address)); if (isns->sock < 0) { if ((status = iscsi_isns_serverconn(isns)) != 0) { /*We couldn't connect to the iSNS server */ - DEB(9, ("update_isns_server_info iscsi_isns_serverconn failed\n")); + DEB(9, ("update_isns_server_info iscsi_isns_serverconn failed")); return status; } } @@ -594,7 +594,7 @@ update_isns_server_info(isns_t * isns) for (TargetName[0] = 0;;) { if (ISNS_INVALID_TRANS == (t = isns_new_trans(isns_handle, isnsp_DevGetNext, MY_FLAGS))) { - DEBOUT(("update_isns_server_info iscsi_new_trans failed\n")); + DEB(10,("update_isns_server_info iscsi_new_trans failed")); return ISCSID_STATUS_NO_RESOURCES; } @@ -609,18 +609,18 @@ update_isns_server_info(isns_t * isns) isns_add_tlv(t, isnst_iSCSINodeType, 0, NULL); if ((retval = isns_send_trans(t, &tout, &status)) != 0) { - DEBOUT(("isns_send_trans returns rc %d, status %d\n", + DEB(10,("isns_send_trans returns rc %d, status %d", retval, status)); isns_free_trans(t); break; } if (status) { - DEB(9, ("DevGetNext Status = %d\n", status)); + DEB(9, ("DevGetNext Status = %d", status)); break; } if (isns_get_tlv(t, ISNS_TLV_FIRST, &tag, &data_len, &data_p)) { - DEBOUT(("No TLV in DevGetNext response!\n")); + DEB(10,("No TLV in DevGetNext response!")); isns_free_trans(t); break; } @@ -628,8 +628,8 @@ update_isns_server_info(isns_t * isns) if (tag != isnst_iSCSIName || !data_len || data_len >= ISCSI_STRING_LENGTH) { - DEBOUT(("iSNS GetNextDev returned no or invalid name (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS GetNextDev returned no or invalid name (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); break; } @@ -637,19 +637,19 @@ update_isns_server_info(isns_t * isns) /* We must get at least the node type, and it must be a target */ if (isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p)) { - DEBOUT(("iSNS GetDevNext did not return node type\n")); + DEB(10,("iSNS GetDevNext did not return node type")); isns_free_trans(t); break; } if (tag == isnst_Delimiter && isns_get_tlv(t, ISNS_TLV_NEXT, &tag, &data_len, &data_p)) { - DEBOUT(("iSNS GetDevNext did not return node type (past delim)\n")); + DEB(10,("iSNS GetDevNext did not return node type (past delim)")); isns_free_trans(t); break; } if (tag != isnst_iSCSINodeType || data_len != 4) { - DEBOUT(("iSNS Query returned no or invalid node type (tag=%d, " - "length=%d)\n", tag, data_len)); + DEB(10,("iSNS Query returned no or invalid node type (tag=%d, " + "length=%d)", tag, data_len)); isns_free_trans(t); break; } @@ -660,7 +660,7 @@ update_isns_server_info(isns_t * isns) get_isns_target_info(isns, TargetName); } - DEB(9, ("update_isns_server_info returning SUCCESS!\n")); + DEB(9, ("update_isns_server_info returning SUCCESS!")); return ISCSID_STATUS_SUCCESS; } @@ -683,7 +683,7 @@ create_isns(iscsid_add_isns_server_req_t * req) { isns_t *isns; - DEB(9, ("Create iSNS %s\n", req->address)); + DEB(9, ("Create iSNS %s", req->address)); if ((isns = calloc(1, sizeof(*isns))) == NULL) return NULL; @@ -727,7 +727,7 @@ add_isns_server(iscsid_add_isns_server_req_t * req, iscsid_response_t ** prsp, iscsid_add_isns_server_rsp_t *res; isns_t *isns; - DEB(9, ("IN add_isns_server\n")); + DEB(9, ("IN add_isns_server")); /* * Make a response @@ -735,7 +735,7 @@ add_isns_server(iscsid_add_isns_server_req_t * req, iscsid_response_t ** prsp, rsp = make_rsp(sizeof(iscsid_add_isns_server_rsp_t), prsp, prsp_temp); if (rsp == NULL) { - DEB(9, ("OUT add_isns_server: make_rsp FAILED\n")); + DEB(9, ("OUT add_isns_server: make_rsp FAILED")); return; } @@ -748,7 +748,7 @@ add_isns_server(iscsid_add_isns_server_req_t * req, iscsid_response_t ** prsp, isns = create_isns(req); if (isns == NULL) { rsp->status = ISCSID_STATUS_NO_RESOURCES; - DEB(9, ("OUT add_isns_server: create_isns FAILED!\n")); + DEB(9, ("OUT add_isns_server: create_isns FAILED!")); return; } @@ -756,7 +756,7 @@ add_isns_server(iscsid_add_isns_server_req_t * req, iscsid_response_t ** prsp, list[ISNS_LIST].num_entries++; res->server_id = isns->entry.sid.id; - DEB(9, ("OUT add_isns_server: server_id = %d, name = %s\n", + DEB(9, ("OUT add_isns_server: server_id = %d, name = %s", isns->entry.sid.id, isns->address)); /* @@ -786,17 +786,17 @@ get_isns_server(iscsid_sym_id_t * preq, iscsid_response_t ** prsp, iscsid_get_isns_server_rsp_t *res; isns_t *isns; - DEB(9, ("IN get_isns_server\n")); + DEB(9, ("IN get_isns_server")); isns = find_isns(preq); if (isns == NULL) { rsp->status = ISCSID_STATUS_INVALID_ISNS_ID; - DEB(9, ("OUT get_isns_server: find_isns FAILED!\n")); + DEB(9, ("OUT get_isns_server: find_isns FAILED!")); return; } rsp = make_rsp(sizeof(iscsid_get_isns_server_rsp_t), prsp, prsp_temp); if (rsp == NULL) { - DEB(9, ("OUT get_isns_server: make_rsp FAILED!\n")); + DEB(9, ("OUT get_isns_server: make_rsp FAILED!")); return; } res = (iscsid_get_isns_server_rsp_t *)(void *)rsp->parameter; @@ -805,7 +805,7 @@ get_isns_server(iscsid_sym_id_t * preq, iscsid_response_t ** prsp, sizeof(res->address)); res->port = isns->port; res->server_id = isns->entry.sid; - DEB(9, ("OUT get_isns_server: id = %d, address = %s\n", + DEB(9, ("OUT get_isns_server: id = %d, address = %s", res->server_id.id, res->address)); } diff --git a/sbin/iscsid/iscsid_driverif.c b/sbin/iscsid/iscsid_driverif.c index d5595e477bd0..5f9917547fda 100644 --- a/sbin/iscsid/iscsid_driverif.c +++ b/sbin/iscsid/iscsid_driverif.c @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_driverif.c,v 1.7 2015/05/30 16:00:51 joerg Exp $ */ +/* $NetBSD: iscsid_driverif.c,v 1.8 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -76,7 +76,7 @@ set_node_name(iscsid_set_node_name_req_t * par) sizeof(snp.InitiatorAlias)); memcpy(snp.ISID, par->ISID, 6); - DEB(10, ("Setting Node Name: %s (%s)\n", + DEB(10, ("Setting Node Name: %s (%s)", snp.InitiatorName, snp.InitiatorAlias)); (void)ioctl(driver, ISCSI_SET_NODE_NAME, &snp); return snp.status; @@ -101,7 +101,7 @@ bind_socket(int sock, uint8_t * addr) struct sockaddr_in serverAddress; struct hostent *host; - DEB(8, ("Binding to <%s>\n", addr)); + DEB(8, ("Binding to <%s>", addr)); (void) memset(&serverAddress, 0x0, sizeof(serverAddress)); host = gethostbyname((char *)addr); if (host == NULL) @@ -187,7 +187,7 @@ make_connection(session_t * sess, iscsid_login_req_t * req, struct hostent *host; initiator_t *init; - DEB(9, ("Make Connection sess=%p, req=%p, res=%p, stid=%p\n", + DEB(9, ("Make Connection sess=%p, req=%p, res=%p, stid=%p", sess, req, res, stid)); (void) memset(&loginp, 0x0, sizeof(loginp)); (void) memset(&serverAddress, 0x0, sizeof(serverAddress)); @@ -245,7 +245,7 @@ make_connection(session_t * sess, iscsid_login_req_t * req, res->status = ISCSID_STATUS_INVALID_PORTAL_ID; return NULL; } - DEB(1, ("find_free_portal returns pid=%d\n", portal->entry.sid.id)); + DEB(1, ("find_free_portal returns pid=%d", portal->entry.sid.id)); } else target = portal->target; @@ -275,7 +275,7 @@ make_connection(session_t * sess, iscsid_login_req_t * req, init = select_initiator(); /* translate target address */ - DEB(8, ("Connecting to <%s>, port %d\n", addr->address, addr->port)); + DEB(8, ("Connecting to <%s>, port %d", addr->address, addr->port)); host = gethostbyname((char *)addr->address); if (host == NULL) { @@ -296,7 +296,7 @@ make_connection(session_t * sess, iscsid_login_req_t * req, res->status = ISCSID_STATUS_HOST_ERROR; return NULL; } - DEB(8, ("Gethostbyname OK, addrtype %d, len %d, addr %x\n", + DEB(8, ("Gethostbyname OK, addrtype %d, len %d, addr %x", host->h_addrtype, host->h_length, *((int *) host->h_addr_list[0]))); serverAddress.sin_family = host->h_addrtype; serverAddress.sin_port = htons((addr->port) @@ -327,13 +327,13 @@ make_connection(session_t * sess, iscsid_login_req_t * req, } } - DEB(8, ("Connecting socket\n")); + DEB(8, ("Connecting socket")); if (connect(sock, (struct sockaddr *)(void *)&serverAddress, (socklen_t)sizeof(serverAddress)) < 0) { close(sock); free(conn); res->status = ISCSID_STATUS_CONNECT_ERROR; - DEB(1, ("Connecting to socket failed (error %d), returning %d\n", + DEB(1, ("Connecting to socket failed (error %d), returning %d", errno, res->status)); return NULL; } @@ -417,7 +417,7 @@ make_connection(session_t * sess, iscsid_login_req_t * req, } else loginp.login_type = ISCSI_LOGINTYPE_DISCOVERY; - DEB(5, ("Calling Login...\n")); + DEB(5, ("Calling Login...")); ret = ioctl(driver, (sess != NULL && sess->num_connections) ? ISCSI_ADD_CONNECTION : ISCSI_LOGIN, &loginp); @@ -509,7 +509,7 @@ event_recover_connection(uint32_t sid, uint32_t cid) struct sockaddr_in serverAddress; struct hostent *host; - DEB(1, ("Event_Recover_Connection sid=%d, cid=%d\n", sid, cid)); + DEB(1, ("Event_Recover_Connection sid=%d, cid=%d", sid, cid)); (void) memset(&serverAddress, 0x0, sizeof(serverAddress)); LOCK_SESSIONS; @@ -540,15 +540,15 @@ event_recover_connection(uint32_t sid, uint32_t cid) conn->portal.addr = *addr; /* translate target address */ - DEB(1, ("Event_Recover_Connection Connecting to <%s>, port %d\n", + DEB(1, ("Event_Recover_Connection Connecting to <%s>, port %d", addr->address, addr->port)); if ((host = gethostbyname((char *)addr->address)) == NULL) { - DEB(1, ("GetHostByName failed (error %d)\n", h_errno)); + DEB(1, ("GetHostByName failed (error %d)", h_errno)); return; } if (host->h_length > (int)sizeof(serverAddress.sin_addr)) { - DEB(1, ("Host address length invalid (%d)\n", host->h_length)); + DEB(1, ("Host address length invalid (%d)", host->h_length)); return; } @@ -561,15 +561,15 @@ event_recover_connection(uint32_t sid, uint32_t cid) /* create and connect the socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { - DEB(1, ("Creating socket failed (error %d)\n", errno)); + DEB(1, ("Creating socket failed (error %d)", errno)); return; } - DEB(1, ("recover_connection: Socket = %d\n", sock)); + DEB(1, ("recover_connection: Socket = %d", sock)); if (init) { if (!bind_socket(sock, init->address)) { - DEB(1, ("Binding to interface failed (error %d)\n", errno)); + DEB(1, ("Binding to interface failed (error %d)", errno)); close(sock); return; } @@ -577,7 +577,7 @@ event_recover_connection(uint32_t sid, uint32_t cid) if (connect(sock, (struct sockaddr *)(void *)&serverAddress, (socklen_t)sizeof(serverAddress)) < 0) { - DEB(1, ("Connecting to socket failed (error %d)\n", errno)); + DEB(1, ("Connecting to socket failed (error %d)", errno)); close(sock); return; } @@ -604,7 +604,7 @@ event_recover_connection(uint32_t sid, uint32_t cid) */ void -login(iscsid_login_req_t * req, iscsid_response_t * res) +log_in(iscsid_login_req_t * req, iscsid_response_t * res) { session_t *sess; connection_t *conn; @@ -671,7 +671,7 @@ add_connection(iscsid_login_req_t * req, iscsid_response_t * res) */ uint32_t -logout(iscsid_sym_id_t * req) +log_out(iscsid_sym_id_t * req) { iscsi_logout_parameters_t logoutp; session_t *sess; @@ -689,7 +689,7 @@ logout(iscsid_sym_id_t * req) UNLOCK_SESSIONS; ret = ioctl(driver, ISCSI_LOGOUT, &logoutp); - DEB(9, ("Logout returns %d, status = %d\n", ret, logoutp.status)); + DEB(9, ("Logout returns %d, status = %d", ret, logoutp.status)); return logoutp.status; } @@ -730,7 +730,7 @@ remove_connection(iscsid_remove_connection_req_t * req) UNLOCK_SESSIONS; ret = ioctl(driver, ISCSI_REMOVE_CONNECTION, &removep); - DEB(9, ("Remove Connection returns %d, status=%d\n", ret, removep.status)); + DEB(9, ("Remove Connection returns %d, status=%d", ret, removep.status)); return removep.status; } @@ -769,7 +769,7 @@ send_targets(uint32_t stid, uint8_t **response_buffer, uint32_t *response_size) (void) memset(&logoutp, 0x0, sizeof(logoutp)); (void) memset(&res, 0x0, sizeof(res)); conn = make_connection(NULL, NULL, &res, &stid); - DEB(9, ("Make connection returns, status = %d\n", res.status)); + DEB(9, ("Make connection returns, status = %d", res.status)); if (conn == NULL) return res.status; @@ -905,26 +905,24 @@ void * /*ARGSUSED*/ event_handler(void *par) { + void (*termf)(void) = par; iscsi_wait_event_parameters_t evtp; int rc; - DEB(99, ("Event handler starts\n")); + DEB(10, ("Event handler starts")); (void) memset(&evtp, 0x0, sizeof(evtp)); evtp.event_id = event_reg.event_id; do { - if (nothreads) - rc = ioctl(driver, ISCSI_POLL_EVENT, &evtp); - else - rc = ioctl(driver, ISCSI_WAIT_EVENT, &evtp); - + rc = ioctl(driver, ISCSI_WAIT_EVENT, &evtp); if (rc != 0) { - perror("ioctl"); + DEB(10, ("event_handler ioctl failed: %s", + strerror(errno))); break; } - DEB(1, ("Got Event: kind %d, status %d, sid %d, cid %d, reason %d\n", + DEB(10, ("Got Event: kind %d, status %d, sid %d, cid %d, reason %d", evtp.event_kind, evtp.status, evtp.session_id, evtp.connection_id, evtp.reason)); @@ -943,14 +941,15 @@ event_handler(void *par) case ISCSI_RECOVER_CONNECTION: event_recover_connection(evtp.session_id, evtp.connection_id); break; - default: break; } } while (evtp.event_kind != ISCSI_DRIVER_TERMINATING); - if (nothreads && evtp.event_kind == ISCSI_DRIVER_TERMINATING) - exit_daemon(); + if (termf != NULL) + (*termf)(); + + DEB(10, ("Event handler exits")); return NULL; } @@ -981,7 +980,7 @@ verify_connection(connection_t * conn) sess->num_connections--; free(conn); } - DEB(9, ("Verify connection returns status %d\n", req.status)); + DEB(9, ("Verify connection returns status %d", req.status)); return req.status; } diff --git a/sbin/iscsid/iscsid_globals.h b/sbin/iscsid/iscsid_globals.h index efc9b55aa58c..97ba3746f281 100644 --- a/sbin/iscsid/iscsid_globals.h +++ b/sbin/iscsid/iscsid_globals.h @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_globals.h,v 1.7 2012/05/28 00:13:19 riz Exp $ */ +/* $NetBSD: iscsid_globals.h,v 1.8 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -47,6 +47,8 @@ #include #include #include +#include +#include #ifndef ISCSI_NOTHREAD #include @@ -379,20 +381,16 @@ iscsid_set_node_name_req_t node_name; /* Debugging stuff */ +extern int debug_level; -extern int debug_level; /* How much info to display */ - -#define DEBOUT(x) printf x -#define DEB(lev,x) {if (debug_level >= lev) printf x ;} +#define DEBOUT(x) iscsid_log x +#define DEB(lev,x) { if (debug_level >= lev) iscsid_log x ; } +void iscsid_log(const char *, ...); /* Session list protection shortcuts */ -#if 0 -#define LOCK_SESSIONS verify_sessions() -#define UNLOCK_SESSIONS -#endif -#define LOCK_SESSIONS if (nothreads) event_handler(NULL); else pthread_mutex_lock(&sesslist_lock) -#define UNLOCK_SESSIONS if (!nothreads) pthread_mutex_unlock(&sesslist_lock) +#define LOCK_SESSIONS pthread_mutex_lock(&sesslist_lock) +#define UNLOCK_SESSIONS pthread_mutex_unlock(&sesslist_lock) /* Check whether ID is present */ @@ -401,7 +399,6 @@ extern int debug_level; /* How much info to display */ /* iscsid_main.c */ iscsid_response_t *make_rsp(size_t, iscsid_response_t **, int *); -void exit_daemon(void) __dead; /* iscsid_lists.c */ @@ -490,10 +487,10 @@ void deregister_event_handler(void); void *event_handler(void *); uint32_t set_node_name(iscsid_set_node_name_req_t *); -void login(iscsid_login_req_t *, iscsid_response_t *); +void log_in(iscsid_login_req_t *, iscsid_response_t *); void add_connection(iscsid_login_req_t *, iscsid_response_t *); uint32_t send_targets(uint32_t, uint8_t **, uint32_t *); -uint32_t logout(iscsid_sym_id_t *); +uint32_t log_out(iscsid_sym_id_t *); uint32_t remove_connection(iscsid_remove_connection_req_t *); void get_version(iscsid_response_t **, int *); diff --git a/sbin/iscsid/iscsid_lists.c b/sbin/iscsid/iscsid_lists.c index 069e6e6a0b87..b919d1cfcf98 100644 --- a/sbin/iscsid/iscsid_lists.c +++ b/sbin/iscsid/iscsid_lists.c @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_lists.c,v 1.8 2012/12/29 08:28:20 mlelstv Exp $ */ +/* $NetBSD: iscsid_lists.c,v 1.9 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -233,7 +233,7 @@ find_TargetName(iscsid_list_kind_t lst, uint8_t * name) } /* return curr instead of t because curr==NULL if name not found */ - DEB(10, ("Find_TargetName returns %p\n", curr)); + DEB(10, ("Find_TargetName returns %p", curr)); return (target_t *)curr; } @@ -255,7 +255,7 @@ find_portal_by_addr(target_t * target, iscsi_portal_address_t * addr) TAILQ_FOREACH(curr, &list[PORTAL_LIST].list, link) { p = (void *)curr; - DEB(10, ("Find_portal_by_addr - addr %s port %d target %p\n", + DEB(10, ("Find_portal_by_addr - addr %s port %d target %p", p->addr.address, p->addr.port, p->target)); @@ -267,7 +267,7 @@ find_portal_by_addr(target_t * target, iscsi_portal_address_t * addr) } /* return curr instead of p because curr==NULL if not found */ - DEB(10, ("Find_portal_by_addr returns %p\n", curr)); + DEB(10, ("Find_portal_by_addr returns %p", curr)); return (portal_t *)curr; } @@ -295,7 +295,7 @@ find_send_target_by_addr(iscsi_portal_address_t * addr) } /* return curr instead of p because curr==NULL if not found */ - DEB(10, ("Find_send_target_by_addr returns %p\n", curr)); + DEB(10, ("Find_send_target_by_addr returns %p", curr)); return (send_target_t *)curr; } @@ -321,7 +321,7 @@ get_list(iscsid_get_list_req_t * par, iscsid_response_t ** prsp, int *prsp_temp) generic_list_t *plist; generic_entry_t *curr; - DEB(10, ("get_list, kind %d\n", par->list_kind)); + DEB(10, ("get_list, kind %d", par->list_kind)); if (par->list_kind == SESSION_LIST) LOCK_SESSIONS; @@ -380,7 +380,7 @@ search_list(iscsid_search_list_req_t * par, iscsid_response_t ** prsp, iscsid_response_t *rsp = *prsp; generic_entry_t *elem = NULL; - DEB(10, ("search_list, list_kind %d, search_kind %d\n", + DEB(10, ("search_list, list_kind %d, search_kind %d", par->list_kind, par->search_kind)); if (par->list_kind == SESSION_LIST) @@ -514,7 +514,7 @@ get_session_list(iscsid_response_t ** prsp, int *prsp_temp) connection_t *conn; int num; - DEB(10, ("get_session_list\n")); + DEB(10, ("get_session_list")); LOCK_SESSIONS; plist = &list[SESSION_LIST].list; @@ -581,7 +581,7 @@ get_connection_list(iscsid_sym_id_t *req, iscsid_response_t **prsp, connection_t *conn; int num; - DEB(10, ("get_connection_list\n")); + DEB(10, ("get_connection_list")); LOCK_SESSIONS; if ((sess = find_session(req)) == NULL) { @@ -635,7 +635,7 @@ get_connection_info(iscsid_get_connection_info_req_t * req, connection_t *conn; initiator_t *init = NULL; - DEB(10, ("get_connection_info, session %d, connection %d\n", + DEB(10, ("get_connection_info, session %d, connection %d", req->session_id.id, req->connection_id.id)); LOCK_SESSIONS; @@ -713,7 +713,7 @@ find_initiator_by_addr(uint8_t * addr) } /* return curr instead of i because if not found, curr==NULL */ - DEB(9, ("Find_initiator_by_addr returns %p\n", curr)); + DEB(9, ("Find_initiator_by_addr returns %p", curr)); return (initiator_t *)curr; } @@ -737,7 +737,7 @@ add_initiator_portal(iscsid_add_initiator_req_t *par, iscsid_response_t **prsp, iscsid_response_t *rsp = *prsp; initiator_t *init; - DEB(9, ("AddInitiatorPortal '%s' (name '%s')\n", par->address, par->name)); + DEB(9, ("AddInitiatorPortal '%s' (name '%s')", par->address, par->name)); if (find_initiator_by_addr(par->address) != NULL) { rsp->status = ISCSID_STATUS_DUPLICATE_ENTRY; @@ -754,7 +754,7 @@ add_initiator_portal(iscsid_add_initiator_req_t *par, iscsid_response_t **prsp, return; } - DEB(9, ("AddInitiatorPortal initiator_id = %d\n", initiator_id)); + DEB(9, ("AddInitiatorPortal initiator_id = %d", initiator_id)); for (initiator_id++; !initiator_id || find_initiator_id(initiator_id) != NULL;) @@ -829,7 +829,7 @@ get_initiator_portal(iscsid_sym_id_t *par, iscsid_response_t **prsp, iscsid_response_t *rsp = *prsp; initiator_t *init; - DEB(10, ("get_initiator_portal, id %d (%s)\n", par->id, par->name)); + DEB(10, ("get_initiator_portal, id %d (%s)", par->id, par->name)); if ((init = find_initiator(par)) == NULL) { rsp->status = ISCSID_STATUS_INVALID_INITIATOR_ID; diff --git a/sbin/iscsid/iscsid_main.c b/sbin/iscsid/iscsid_main.c index e11d857b0a48..90832bb0e2ae 100644 --- a/sbin/iscsid/iscsid_main.c +++ b/sbin/iscsid/iscsid_main.c @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_main.c,v 1.9 2015/05/30 16:00:51 joerg Exp $ */ +/* $NetBSD: iscsid_main.c,v 1.10 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -39,6 +39,8 @@ #include #include #include +#include +#include #define DEVICE "/dev/iscsi0" @@ -47,13 +49,14 @@ list_head_t list[NUM_DAEMON_LISTS]; /* the lists this daemon keeps */ pthread_mutex_t sesslist_lock; /* session list lock */ -pthread_t event_thread; /* event thread handle */ +pthread_t main_thread; /* main thread handle */ +pthread_t event_thread; /* event thread handle */ -int driver = -1; /* the driver's file desc */ -int client_sock; /* the client communication socket */ +int driver = -1; /* the driver's file desc */ +int client_sock; /* the client communication socket */ -int debug_level; /* How much info to display */ -int nothreads; +int debug_level; /* How much info to display */ +int debugging; /* To avoid memory fragmentation (and speed things up a bit), we use the @@ -73,7 +76,6 @@ usage(void) exit(EXIT_FAILURE); } - /* * create_node_name: * Create and set default node name. @@ -99,7 +101,7 @@ create_node_name(void) siz = ISCSI_STRING_LENGTH - 45; sysctl(mib, 2, snp.InitiatorAlias, &siz, NULL, 0); - DEB(1, ("Host Name: <%s>, Host ID: %u\n", snp.InitiatorAlias, hid)); + DEB(3, ("Host Name: <%s>, Host ID: %u", snp.InitiatorAlias, hid)); if (!snp.InitiatorAlias[0]) { printf("Warning: iSCSI Node Name not set (No Host Name)!\n"); return ISCSID_STATUS_NO_INITIATOR_NAME; @@ -165,7 +167,7 @@ init_daemon(void) list[i].num_entries = 0; } - if (!nothreads && (i = pthread_mutex_init(&sesslist_lock, NULL)) != 0) { + if ((i = pthread_mutex_init(&sesslist_lock, NULL)) != 0) { printf("Mutex init failed (%d)\n", i); close(sock); return -1; @@ -175,8 +177,7 @@ init_daemon(void) printf("Couldn't register event handler\n"); close(sock); unlink(ISCSID_SOCK_NAME); - if (!nothreads) - pthread_mutex_destroy(&sesslist_lock); + pthread_mutex_destroy(&sesslist_lock); return -1; } @@ -398,7 +399,7 @@ process_message(iscsid_request_t *req, iscsid_response_t **prsp, int *prsp_temp) rsp->status = ISCSID_STATUS_INVALID_PARAMETER; break; } - login((iscsid_login_req_t *)p, rsp); + log_in((iscsid_login_req_t *)p, rsp); break; case ISCSID_ADD_CONNECTION: @@ -414,7 +415,7 @@ process_message(iscsid_request_t *req, iscsid_response_t **prsp, int *prsp_temp) rsp->status = ISCSID_STATUS_INVALID_PARAMETER; break; } - rsp->status = logout((iscsid_sym_id_t *)p); + rsp->status = log_out((iscsid_sym_id_t *)p); break; case ISCSID_REMOVE_CONNECTION: @@ -464,13 +465,21 @@ process_message(iscsid_request_t *req, iscsid_response_t **prsp, int *prsp_temp) } } +void +iscsid_log(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vsyslog(LOG_INFO, fmt, ap); + va_end(ap); +} /* * exit_daemon: * Deregister the event handler, deregister isns servers, then exit program. */ -void +static void exit_daemon(void) { LOCK_SESSIONS; @@ -479,11 +488,19 @@ exit_daemon(void) #ifndef ISCSI_MINIMAL dereg_all_isns_servers(); #endif - - printf("iSCSI Daemon Exits\n"); exit(0); } +static void +handler_exit(void) +{ + pthread_kill(main_thread, SIGINT); +} + +static void +sighandler(int sig) +{ +} /* * main: @@ -506,79 +523,74 @@ main(int argc, char **argv) socklen_t fromlen; iscsid_request_t *req; iscsid_response_t *rsp; - struct timeval seltout = { 2, 0 }; /* 2 second poll interval */ char *p; + struct sigaction sa; - while ((c = getopt(argc, argv, "d:n")) != -1) + while ((c = getopt(argc, argv, "Dd:")) != -1) switch (c) { - case 'n': - nothreads++; + case 'D': + debugging++; break; case 'd': debug_level=(int)strtol(optarg, &p, 10); if (*p) - errx(EXIT_FAILURE, "illegal debug level -- %s", + errx(EXIT_FAILURE, "illegal log level -- %s", optarg); break; default: usage(); } + openlog("iscsid", (debugging ? LOG_PERROR : 0) | LOG_PID, LOG_DAEMON); + client_sock = init_daemon(); if (client_sock < 0) exit(1); - printf("iSCSI Daemon loaded\n"); + DEBOUT(("iSCSI daemon loaded")); - if (!debug_level) - daemon(0, 1); + if (!debugging) { + if (daemon(0, 1) < 0) + err(EXIT_FAILURE, "daemon() failed"); + pidfile(NULL); + } - if (nothreads) - setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &seltout, - sizeof(seltout)); - else { - ret = pthread_create(&event_thread, NULL, event_handler, NULL); - if (ret) { - printf("Thread creation failed (%zd)\n", ret); - close(client_sock); - unlink(ISCSID_SOCK_NAME); - deregister_event_handler(); - pthread_mutex_destroy(&sesslist_lock); - return -1; - } + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = sighandler; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + main_thread = pthread_self(); + ret = pthread_create(&event_thread, NULL, event_handler, handler_exit); + if (ret) { + printf("Thread creation failed (%zd)\n", ret); + close(client_sock); + unlink(ISCSID_SOCK_NAME); + deregister_event_handler(); + pthread_mutex_destroy(&sesslist_lock); + return -1; } /* ---------------------------------------------------------------------- */ for (;;) { + /* First, get size of request */ req = (iscsid_request_t *)(void *)req_buf; fromlen = sizeof(from); len = sizeof(iscsid_request_t); - if (nothreads) { - do { - ret = recvfrom(client_sock, req, len, MSG_PEEK | - MSG_WAITALL, (struct sockaddr *)(void *)&from, - &fromlen); - if (ret == -1) - event_handler(NULL); - } while (ret == -1 && errno == EAGAIN); - } else { - do { - ret = recvfrom(client_sock, req, len, MSG_PEEK | - MSG_WAITALL, (struct sockaddr *) &from, - &fromlen); - if (ret == -1) - event_handler(NULL); - } while (ret == -1 && errno == EAGAIN); - } + do { + ret = recvfrom(client_sock, req, len, MSG_PEEK | + MSG_WAITALL, (struct sockaddr *) &from, + &fromlen); + } while (ret == -1 && errno == EAGAIN); if ((size_t)ret != len) { - perror("Receiving from socket"); + DEBOUT(("Receiving from socket: %s",strerror(errno))); break; } - DEB(98, ("Request %d, parlen %d\n", + DEB(2, ("Request %d, parlen %d", req->request, req->parameter_length)); len += req->parameter_length; @@ -598,7 +610,7 @@ main(int argc, char **argv) ret = recvfrom(client_sock, req, len, MSG_WAITALL, (struct sockaddr *)(void *)&from, &fromlen); if ((size_t)ret != len) { - DEBOUT(("Error receiving from socket!\n")); + DEB(2, ("Error receiving from socket!")); if (req_temp) free(req); continue; @@ -611,6 +623,7 @@ main(int argc, char **argv) if (req->request == ISCSID_DAEMON_TEST) { if (req_temp) free(req); + DEB(2, ("Test message!")); continue; } /* no return path? then we can't send a reply, */ @@ -618,7 +631,7 @@ main(int argc, char **argv) if (!from.sun_path[0]) { if (req_temp) free(req); - DEBOUT(("No Return Address!\n")); + DEB(2, ("No Return Address!")); continue; } /* process the request */ @@ -626,11 +639,11 @@ main(int argc, char **argv) if (rsp == NULL) { if (req_temp) free(req); - DEBOUT(("Invalid message!\n")); + DEB(2, ("Invalid message!")); continue; } - DEB(98, ("Sending reply: status %d, len %d\n", + DEB(2, ("Sending reply: status %d, len %d", rsp->status, rsp->parameter_length)); /* send the response */ @@ -638,7 +651,7 @@ main(int argc, char **argv) ret = sendto(client_sock, rsp, len, 0, (struct sockaddr *)(void *)&from, fromlen); if (len != (size_t)ret) { - DEBOUT(("Error sending reply!\n")); + DEB(2, ("Error sending reply!")); } /* free temp buffers if we needed them */ if (req_temp) @@ -647,6 +660,10 @@ main(int argc, char **argv) free(rsp); } + pthread_join(event_thread, NULL); + + DEBOUT(("Exiting daemon")); + exit_daemon(); /* we never get here */ diff --git a/sbin/iscsid/iscsid_targets.c b/sbin/iscsid/iscsid_targets.c index 39032ffb6849..73c88100156f 100644 --- a/sbin/iscsid/iscsid_targets.c +++ b/sbin/iscsid/iscsid_targets.c @@ -1,4 +1,4 @@ -/* $NetBSD: iscsid_targets.c,v 1.5 2012/12/29 08:28:20 mlelstv Exp $ */ +/* $NetBSD: iscsid_targets.c,v 1.6 2016/05/29 13:35:45 mlelstv Exp $ */ /*- * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc. @@ -63,7 +63,7 @@ create_portal(target_t *target, iscsi_portal_address_t *addr, portal_t *portal; u_short tag = addr->group_tag; - DEB(9, ("Create Portal addr %s port %d group %d\n", + DEB(9, ("Create Portal addr %s port %d group %d", addr->address, addr->port, addr->group_tag)); if ((portal = find_portal_by_addr(target, addr)) != NULL) @@ -71,7 +71,7 @@ create_portal(target_t *target, iscsi_portal_address_t *addr, portal = calloc(1, sizeof(*portal)); if (!portal) { - DEBOUT(("Out of memory in create_portal!\n")); + DEBOUT(("Out of memory in create_portal!")); return NULL; } portal->addr = *addr; @@ -96,7 +96,7 @@ create_portal(target_t *target, iscsi_portal_address_t *addr, curr = calloc(1, sizeof(*curr)); if (!curr) { free(portal); - DEBOUT(("Out of memory in create_portal!\n")); + DEBOUT(("Out of memory in create_portal!")); return NULL; } curr->tag = tag; @@ -114,7 +114,7 @@ create_portal(target_t *target, iscsi_portal_address_t *addr, TAILQ_INSERT_TAIL(&list[PORTAL_LIST].list, &portal->entry, link); list[PORTAL_LIST].num_entries++; - DEB(9, ("create_portal returns %p\n", portal)); + DEB(9, ("create_portal returns %p", portal)); return portal; } @@ -172,10 +172,10 @@ create_target(uint8_t * name) { target_t *target; - DEB(9, ("Create Target %s\n", name)); + DEB(9, ("Create Target %s", name)); if ((target = calloc(1, sizeof(*target))) == NULL) { - DEBOUT(("Out of memory in create_target!\n")); + DEBOUT(("Out of memory in create_target!")); return NULL; } @@ -244,7 +244,7 @@ create_send_target(uint8_t * name, iscsi_portal_address_t * addr) { send_target_t *target; - DEB(9, ("Create Send Target %s\n", name)); + DEB(9, ("Create Send Target %s", name)); if ((target = calloc(1, sizeof(*target))) == NULL) return NULL; @@ -318,7 +318,7 @@ add_target(iscsid_add_target_req_t *par, iscsid_response_t **prsp, portal_t *portal; int i, num; - DEB(9, ("Add Target, name %s, num_portals %d\n", + DEB(9, ("Add Target, name %s, num_portals %d", par->TargetName, par->num_portals)); if (par->list_kind == SEND_TARGETS_LIST && par->num_portals != 1) { @@ -403,7 +403,7 @@ add_target(iscsid_add_target_req_t *par, iscsid_response_t **prsp, res->num_portals = i; } - DEB(9, ("AddTarget returns\n")); + DEB(9, ("AddTarget returns")); } @@ -431,7 +431,7 @@ add_discovered_target(uint8_t * TargetName, iscsi_portal_address_t * addr, target_t *target; portal_t *portal; - DEB(9, ("Add Discovered Target, name %s, addr %s\n", + DEB(9, ("Add Discovered Target, name %s, addr %s", TargetName, addr->address)); if ((target = find_TargetName(TARGET_LIST, TargetName)) == NULL) { @@ -528,7 +528,7 @@ get_target_info(iscsid_list_id_t *par, iscsid_response_t **prsp, int *prsp_temp) portal_t *curr = NULL; int num = 1; - DEB(10, ("get_target_info, id %d\n", par->id.id)); + DEB(10, ("get_target_info, id %d", par->id.id)); if ((target = find_target(par->list_kind, &par->id)) == NULL) { if (par->list_kind == SEND_TARGETS_LIST || @@ -587,7 +587,7 @@ add_portal(iscsid_add_portal_req_t *par, iscsid_response_t **prsp, target_t *target; portal_t *portal; - DEB(9, ("Add portal: target %d (%s), symname %s, addr %s\n", + DEB(9, ("Add portal: target %d (%s), symname %s, addr %s", par->target_id.id, par->target_id.name, par->sym_name, par->portal.address)); @@ -624,7 +624,7 @@ add_portal(iscsid_add_portal_req_t *par, iscsid_response_t **prsp, res->portal_id = portal->entry.sid; #endif - DEB(9, ("AddPortal success (id %d)\n", portal->entry.sid.id)); + DEB(9, ("AddPortal success (id %d)", portal->entry.sid.id)); } @@ -649,7 +649,7 @@ get_portal_info(iscsid_list_id_t *par, iscsid_response_t **prsp, int *prsp_temp) send_target_t *starg = NULL; int err; - DEB(10, ("get_portal_info, id %d\n", par->id.id)); + DEB(10, ("get_portal_info, id %d", par->id.id)); if (par->list_kind == SEND_TARGETS_LIST) err = ((starg = (send_target_t *)(void *)find_target(par->list_kind, @@ -699,7 +699,7 @@ remove_target(iscsid_list_id_t * par) send_target_t *starg = NULL; int err; - DEB(9, ("remove_target, id %d\n", par->id.id)); + DEB(9, ("remove_target, id %d", par->id.id)); if (par->list_kind == SEND_TARGETS_LIST) { err = ((starg = (send_target_t *)(void *)find_target(par->list_kind, @@ -850,7 +850,7 @@ refresh_send_target(uint32_t id) tp = response_buffer; while (*tp) { if (strncmp((char *)tp, "TargetName=", 11) != 0) { - DEBOUT(("Response not TargetName <%s>\n", tp)); + DEBOUT(("Response not TargetName <%s>", tp)); break; } tp += 11; @@ -872,7 +872,7 @@ refresh_send_target(uint32_t id) &addr, PORTAL_TYPE_SENDTARGET, id); } else { - DEBOUT(("Syntax error in returned target address <%s>\n", sp)); + DEBOUT(("Syntax error in returned target address <%s>", sp)); break; } } @@ -982,7 +982,7 @@ refresh_targets(iscsid_refresh_req_t * par) break; case SEND_TARGETS_LIST: - DEB(9, ("Refresh Send Targets List - num_ids = %d\n", + DEB(9, ("Refresh Send Targets List - num_ids = %d", par->num_ids)); if (par->num_ids) { /* Target ids are specified */ @@ -1004,7 +1004,7 @@ refresh_targets(iscsid_refresh_req_t * par) #ifndef ISCSI_MINIMAL case ISNS_LIST: - DEB(9, ("Refresh iSNS List - num_ids = %d\n", par->num_ids)); + DEB(9, ("Refresh iSNS List - num_ids = %d", par->num_ids)); if (par->num_ids) { /*Target ids are specified */ for (t = 0; t < par->num_ids; t++)