SHA256
1
0
forked from pool/open-iscsi
open-iscsi/open-iscsi-check-active-sessions-before-delete

112 lines
2.8 KiB
Plaintext

diff --git a/usr/iscsiadm.c b/usr/iscsiadm.c
index 07922ca..873607b 100644
--- a/usr/iscsiadm.c
+++ b/usr/iscsiadm.c
@@ -260,6 +260,19 @@ session_logout(int rid, node_rec_t *rec)
}
static int
+session_active(int rid)
+{
+ iscsiadm_req_t req;
+ iscsiadm_rsp_t rsp;
+
+ memset(&req, 0, sizeof(req));
+ req.command = MGMT_IPC_SESSION_ACTIVE;
+ req.u.session.rid = rid;
+
+ return do_iscsid(&ipc_fd, &req, &rsp);
+}
+
+static int
config_init(void)
{
int rc;
@@ -726,6 +739,12 @@ main(int argc, char **argv)
goto out;
}
} else if (op == OP_DELETE) {
+ rc = session_active(rid);
+ if (rc > 0 && rc != MGMT_IPC_ERR_NOT_FOUND) {
+ iscsid_handle_error(rc);
+ rc = -1;
+ goto out;
+ }
if (idbm_delete_node(db, &rec)) {
log_error("can not delete record");
rc = -1;
diff --git a/usr/mgmt_ipc.c b/usr/mgmt_ipc.c
index d1f1eaf..4682b34 100644
--- a/usr/mgmt_ipc.c
+++ b/usr/mgmt_ipc.c
@@ -123,6 +123,27 @@ mgmt_ipc_session_login(struct mgmt_ipc_d
}
static mgmt_ipc_err_e
+mgmt_ipc_session_active(queue_task_t *qtask, int rid)
+{
+ iscsi_session_t *session;
+ struct qelem *item;
+ int i;
+
+ for (i = 0; i < num_providers; i++) {
+ item = provider[i].sessions.q_forw;
+ while (item != &provider[i].sessions) {
+ session = (iscsi_session_t *)item;
+ if (session->nrec.id == rid)
+ return MGMT_IPC_ERR_ACTIVE;
+
+ item = item->q_forw;
+ }
+ }
+
+ return MGMT_IPC_ERR_NOT_FOUND;
+}
+
+static mgmt_ipc_err_e
mgmt_ipc_session_activelist(queue_task_t *qtask, iscsiadm_rsp_t *rsp)
{
iscsi_session_t *session;
@@ -383,6 +404,9 @@ mgmt_ipc_handle(struct mgmt_ipc_db *dbt,
rsp.err = mgmt_ipc_session_sync(dbt, qtask, req.u.session.rid,
req.u.session.sid);
break;
+ case MGMT_IPC_SESSION_ACTIVE:
+ rsp.err = mgmt_ipc_session_active(qtask, req.u.session.rid);
+ break;
case MGMT_IPC_SESSION_ACTIVELIST:
rsp.err = mgmt_ipc_session_activelist(qtask, &rsp);
immrsp = 1;
diff --git a/usr/mgmt_ipc.h b/usr/mgmt_ipc.h
index be12070..9564ed5 100644
--- a/usr/mgmt_ipc.h
+++ b/usr/mgmt_ipc.h
@@ -42,6 +42,7 @@ typedef enum mgmt_ipc_err {
MGMT_IPC_ERR_ACCESS = 13,
MGMT_IPC_ERR_TRANS_CAPS = 14,
MGMT_IPC_ERR_EXISTS = 15,
+ MGMT_IPC_ERR_ACTIVE = 16,
} mgmt_ipc_err_e;
typedef enum iscsiadm_cmd {
@@ -58,6 +59,7 @@ typedef enum iscsiadm_cmd {
MGMT_IPC_CONFIG_FILE = 10,
MGMT_IPC_IMMEDIATE_STOP = 11,
MGMT_IPC_SESSION_SYNC = 12,
+ MGMT_IPC_SESSION_ACTIVE = 13,
} iscsiadm_cmd_e;
/* IPC Request */
diff --git a/usr/util.c b/usr/util.c
index a4c0b5a..ba63850 100644
--- a/usr/util.c
+++ b/usr/util.c
@@ -194,6 +194,7 @@ void iscsid_handle_error(int err)
/* 13 */ "daemon access denied",
/* 14 */ "iSCSI transport capability failure",
/* 15 */ "already exists",
+ /* 16 */ "session still active",
};
log_error("initiator reported error (%d - %s)", err, err_msgs[err]);
}