diff --git a/iscsi-scst/kernel/iscsi.c b/iscsi-scst/kernel/iscsi.c index 7242da226..1e0d11f21 100644 --- a/iscsi-scst/kernel/iscsi.c +++ b/iscsi-scst/kernel/iscsi.c @@ -245,7 +245,7 @@ static struct iscsi_cmnd *iscsi_create_tm_clone(struct iscsi_cmnd *cmnd) PRINT_ERROR("Failed to create TM clone for cmnd %p", cmnd); } - TRACE_EXIT_HRES((unsigned long)tm_clone); + TRACE_EXIT_HRES(tm_clone); return tm_clone; } @@ -744,7 +744,7 @@ static struct iscsi_cmnd *iscsi_alloc_rsp(struct iscsi_cmnd *parent) cmnd_get(parent); - TRACE_EXIT_HRES((unsigned long)rsp); + TRACE_EXIT_HRES(rsp); return rsp; } @@ -759,7 +759,7 @@ static inline struct iscsi_cmnd *iscsi_alloc_main_rsp(struct iscsi_cmnd *parent) rsp = iscsi_alloc_rsp(parent); parent->main_rsp = rsp; - TRACE_EXIT_HRES((unsigned long)rsp); + TRACE_EXIT_HRES(rsp); return rsp; } @@ -1047,7 +1047,7 @@ struct iscsi_cmnd *create_status_rsp(struct iscsi_cmnd *req, int status, const u iscsi_init_status_rsp(rsp, status, sense_buf, sense_len); iscsi_set_resid(rsp); - TRACE_EXIT_HRES((unsigned long)rsp); + TRACE_EXIT_HRES(rsp); return rsp; } EXPORT_SYMBOL(create_status_rsp); diff --git a/iscsi-scst/usr/af_alg.c b/iscsi-scst/usr/af_alg.c index eeb44fbbb..a9d147672 100644 --- a/iscsi-scst/usr/af_alg.c +++ b/iscsi-scst/usr/af_alg.c @@ -22,6 +22,7 @@ #include #include #include +#include int af_alg_init(const char *algorithm) { @@ -56,21 +57,33 @@ int af_alg_init(const char *algorithm) return datafd; } -void af_alg_update(int datafd, const void *data_in, size_t len) +int af_alg_update(int datafd, const void *data_in, size_t len) { - send(datafd, data_in, len, MSG_MORE); + ssize_t res; + + res = send(datafd, data_in, len, MSG_MORE); + if (res == -1) + return -errno; + + return 0; } ssize_t af_alg_final(int datafd, void *out, size_t len) { char buffer[1024]; - ssize_t bytes; + ssize_t res; - send(datafd, NULL, 0, 0); + res = send(datafd, NULL, 0, 0); + if (res == -1) + return -errno; - bytes = recv(datafd, buffer, sizeof(buffer), 0); - memcpy(out, buffer, MIN(len, bytes)); - return bytes; + res = recv(datafd, buffer, sizeof(buffer), 0); + if (res == -1) + return -errno; + + memcpy(out, buffer, MIN(len, res)); + + return res; } bool af_alg_supported(char *alg) diff --git a/iscsi-scst/usr/af_alg.h b/iscsi-scst/usr/af_alg.h index 7be0e734c..b565e8f5a 100644 --- a/iscsi-scst/usr/af_alg.h +++ b/iscsi-scst/usr/af_alg.h @@ -23,7 +23,7 @@ #define SCST_AF_ALG_SHA3_256_NAME "sha3-256" int af_alg_init(const char *algorithm); -void af_alg_update(int datafd, const void *data_in, size_t len); +int af_alg_update(int datafd, const void *data_in, size_t len); ssize_t af_alg_final(int datafd, void *out, size_t len); bool af_alg_supported(char *alg); diff --git a/iscsi-scst/usr/chap.c b/iscsi-scst/usr/chap.c index 3d4aec79e..b5224ed63 100644 --- a/iscsi-scst/usr/chap.c +++ b/iscsi-scst/usr/chap.c @@ -33,6 +33,7 @@ #include "md5.h" #include "af_alg.h" #include +#include #include "iscsid.h" @@ -344,47 +345,66 @@ static inline void chap_calc_digest_sha1(char chap_id, const char *secret, int s sha1_final(&ctx, digest); } -static inline void +static inline int chap_calc_digest_af_alg(char *alg, char chap_id, const char *secret, int secret_len, const u8 *challenge, int challenge_len, u8 *digest, int digest_len) { - int datafd = af_alg_init(alg); + int datafd; + ssize_t bytes; char buffer[1024]; - int bytes; + int res = 0; + datafd = af_alg_init(alg); if (datafd < 0) { log_error("CHAP unable to use %s algorithm", alg); - return; + return -EINVAL; } - af_alg_update(datafd, &chap_id, 1); - af_alg_update(datafd, secret, secret_len); - af_alg_update(datafd, challenge, challenge_len); + res = af_alg_update(datafd, &chap_id, 1); + if (res) + goto out; + + res = af_alg_update(datafd, secret, secret_len); + if (res) + goto out; + + res = af_alg_update(datafd, challenge, challenge_len); + if (res) + goto out; + bytes = af_alg_final(datafd, buffer, sizeof(buffer)); + if (bytes < 0) { + res = bytes; + goto out; + } + + memcpy(digest, buffer, MIN(res, digest_len)); + +out: close(datafd); - memcpy(digest, buffer, MIN(bytes, digest_len)); + return res; } -static inline void +static inline int chap_calc_digest_sha256(char chap_id, const char *secret, int secret_len, const u8 *challenge, int challenge_len, u8 *digest) { - chap_calc_digest_af_alg(SCST_AF_ALG_SHA256_NAME, - chap_id, secret, secret_len, - challenge, challenge_len, - digest, CHAP_SHA256_DIGEST_LEN); + return chap_calc_digest_af_alg(SCST_AF_ALG_SHA256_NAME, + chap_id, secret, secret_len, + challenge, challenge_len, + digest, CHAP_SHA256_DIGEST_LEN); } -static inline void +static inline int chap_calc_digest_sha3_256(char chap_id, const char *secret, int secret_len, const u8 *challenge, int challenge_len, u8 *digest) { - chap_calc_digest_af_alg(SCST_AF_ALG_SHA3_256_NAME, - chap_id, secret, secret_len, - challenge, challenge_len, - digest, CHAP_SHA3_256_DIGEST_LEN); + return chap_calc_digest_af_alg(SCST_AF_ALG_SHA3_256_NAME, + chap_id, secret, secret_len, + challenge, challenge_len, + digest, CHAP_SHA3_256_DIGEST_LEN); } /* @@ -470,12 +490,12 @@ static int chap_initiator_auth_check_response(struct connection *conn) { char *value; u8 *his_digest = NULL, *our_digest = NULL; - int digest_len = 0, retval = 0, encoding_format; char pass[ISCSI_NAME_LEN]; + int digest_len = 0, retval = 0, encoding_format; + int res; if (accounts_empty(conn->tid, ISCSI_USER_DIR_INCOMING)) { - log_warning("CHAP initiator auth.: " - "No CHAP credentials configured"); + log_warning("CHAP initiator auth.: No CHAP credentials configured"); retval = CHAP_TARGET_ERROR; goto out; } @@ -486,15 +506,13 @@ static int chap_initiator_auth_check_response(struct connection *conn) } conn->user = strdup(value); - if (conn->user == NULL) { + if (!conn->user) log_error("Unable to duplicate initiator's USER %s", value); - } memset(pass, 0, sizeof(pass)); if (config_account_query(conn->tid, ISCSI_USER_DIR_INCOMING, value, pass) < 0) { - log_warning("CHAP initiator auth.: " - "No valid user/pass combination for initiator %s " - "found", conn->initiator); + log_warning("CHAP initiator auth.: No valid user/pass combination for initiator %s found", + conn->initiator); retval = CHAP_AUTH_ERROR; goto out; } @@ -527,11 +545,14 @@ static int chap_initiator_auth_check_response(struct connection *conn) goto out; } - if (!(his_digest = malloc(digest_len))) { + his_digest = malloc(digest_len); + if (!his_digest) { retval = CHAP_TARGET_ERROR; goto out; } - if (!(our_digest = malloc(digest_len))) { + + our_digest = malloc(digest_len); + if (!our_digest) { retval = CHAP_TARGET_ERROR; goto out; } @@ -555,16 +576,24 @@ static int chap_initiator_auth_check_response(struct connection *conn) our_digest); break; case CHAP_DIGEST_ALG_SHA256: - chap_calc_digest_sha256(conn->auth.chap.id, pass, strlen(pass), - conn->auth.chap.challenge, - conn->auth.chap.challenge_size, - our_digest); + res = chap_calc_digest_sha256(conn->auth.chap.id, pass, strlen(pass), + conn->auth.chap.challenge, + conn->auth.chap.challenge_size, + our_digest); + if (res) { + retval = CHAP_TARGET_ERROR; + goto out; + } break; case CHAP_DIGEST_ALG_SHA3_256: - chap_calc_digest_sha3_256(conn->auth.chap.id, pass, strlen(pass), - conn->auth.chap.challenge, - conn->auth.chap.challenge_size, - our_digest); + res = chap_calc_digest_sha3_256(conn->auth.chap.id, pass, strlen(pass), + conn->auth.chap.challenge, + conn->auth.chap.challenge_size, + our_digest); + if (res) { + retval = CHAP_TARGET_ERROR; + goto out; + } break; default: retval = CHAP_TARGET_ERROR; @@ -572,8 +601,7 @@ static int chap_initiator_auth_check_response(struct connection *conn) } if (memcmp(our_digest, his_digest, digest_len)) { - log_warning("CHAP initiator auth.: " - "authentication of %s failed (wrong secret!?)", + log_warning("CHAP initiator auth.: authentication of %s failed (wrong secret!?)", conn->initiator); retval = CHAP_AUTH_ERROR; goto out; @@ -581,10 +609,8 @@ static int chap_initiator_auth_check_response(struct connection *conn) conn->state = CHAP_AUTH_STATE_RESPONSE; out: - if (his_digest) - free(his_digest); - if (our_digest) - free(our_digest); + free(his_digest); + free(our_digest); return retval; } @@ -593,8 +619,9 @@ static int chap_target_auth_create_response(struct connection *conn) char chap_id, *value, *response = NULL; u8 *challenge = NULL, *digest = NULL; int encoding_format, response_len; - int challenge_len = 0, digest_len = 0, retval = 0; struct iscsi_attr *user; + int challenge_len = 0, digest_len = 0, retval = 0; + int res; if (!(value = text_key_find(conn, "CHAP_I"))) { /* Initiator doesn't want target auth!? */ @@ -606,16 +633,14 @@ static int chap_target_auth_create_response(struct connection *conn) user = account_get_first(conn->tid, ISCSI_USER_DIR_OUTGOING); if (user == NULL) { - log_warning("CHAP target auth.: " - "no outgoing credentials configured%s", + log_warning("CHAP target auth.: no outgoing credentials configured%s", conn->tid ? "." : " for discovery."); retval = CHAP_AUTH_ERROR; goto out; } if (!(value = text_key_find(conn, "CHAP_C"))) { - log_warning("CHAP target auth.: " - "got no challenge from initiator %s", + log_warning("CHAP target auth.: got no challenge from initiator %s", conn->initiator); retval = CHAP_INITIATOR_ERROR; goto out; @@ -629,9 +654,9 @@ static int chap_target_auth_create_response(struct connection *conn) retval = chap_alloc_decode_buffer(value, &challenge, encoding_format); if (retval <= 0) goto out; - else if (retval > 1024) { - log_warning("CHAP target auth.: " - "initiator %s sent challenge of invalid length %d", + + if (retval > 1024) { + log_warning("CHAP target auth.: initiator %s sent challenge of invalid length %d", conn->initiator, challenge_len); retval = CHAP_INITIATOR_ERROR; goto out; @@ -665,11 +690,14 @@ static int chap_target_auth_create_response(struct connection *conn) /* "0x" / "0b" and "\0": */ response_len += 3; - if (!(digest = malloc(digest_len))) { + digest = malloc(digest_len); + if (!digest) { retval = CHAP_TARGET_ERROR; goto out; } - if (!(response = malloc(response_len))) { + + response = malloc(response_len); + if (!response) { retval = CHAP_TARGET_ERROR; goto out; } @@ -684,8 +712,7 @@ static int chap_target_auth_create_response(struct connection *conn) if (!memcmp(challenge, conn->auth.chap.challenge, challenge_len)) { /* ToDo: RFC 3720 demands to close TCP conn */ - log_warning("CHAP target auth.: " - "initiator %s reflected our challenge", + log_warning("CHAP target auth.: initiator %s reflected our challenge", conn->initiator); retval = CHAP_INITIATOR_ERROR; goto out; @@ -702,14 +729,22 @@ static int chap_target_auth_create_response(struct connection *conn) strlen(ISCSI_USER_PASS(user)), challenge, challenge_len, digest); break; case CHAP_DIGEST_ALG_SHA256: - chap_calc_digest_sha256(chap_id, ISCSI_USER_PASS(user), - strlen(ISCSI_USER_PASS(user)), - challenge, challenge_len, digest); + res = chap_calc_digest_sha256(chap_id, ISCSI_USER_PASS(user), + strlen(ISCSI_USER_PASS(user)), + challenge, challenge_len, digest); + if (res) { + retval = CHAP_TARGET_ERROR; + goto out; + } break; case CHAP_DIGEST_ALG_SHA3_256: - chap_calc_digest_sha3_256(chap_id, ISCSI_USER_PASS(user), - strlen(ISCSI_USER_PASS(user)), - challenge, challenge_len, digest); + res = chap_calc_digest_sha3_256(chap_id, ISCSI_USER_PASS(user), + strlen(ISCSI_USER_PASS(user)), + challenge, challenge_len, digest); + if (res) { + retval = CHAP_TARGET_ERROR; + goto out; + } break; default: retval = CHAP_TARGET_ERROR; @@ -723,12 +758,9 @@ static int chap_target_auth_create_response(struct connection *conn) conn->state = STATE_SECURITY_DONE; out: - if (challenge) - free(challenge); - if (digest) - free(digest); - if (response) - free(response); + free(challenge); + free(digest); + free(response); return retval; } diff --git a/qla2x00t/qla2x00-target/qla2x00t.c b/qla2x00t/qla2x00-target/qla2x00t.c index e6357b8b8..3cce89300 100644 --- a/qla2x00t/qla2x00-target/qla2x00t.c +++ b/qla2x00t/qla2x00-target/qla2x00t.c @@ -5841,7 +5841,7 @@ out_free_fcport: kfree(fcport); out: - TRACE_EXIT_HRES((unsigned long)sess); + TRACE_EXIT_HRES(sess); return sess; } diff --git a/scst/include/scst_debug.h b/scst/include/scst_debug.h index 8f5f4ecb8..0b1f348de 100644 --- a/scst/include/scst_debug.h +++ b/scst/include/scst_debug.h @@ -305,56 +305,52 @@ do { \ #define TRACE_ENTRY() \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ - PRINT(KERN_INFO, "[%d]: ENTRY %s", current->pid, \ - __func__); \ - } \ - else { \ - PRINT(KERN_INFO, "ENTRY %s", __func__); \ - } \ + if (trace_flag & TRACE_PID) \ + PRINT(KERN_INFO, "[%d]: ENTRY %s", \ + current->pid, __func__); \ + else \ + PRINT(KERN_INFO, "ENTRY %s", \ + __func__); \ } \ } while (0) #define TRACE_EXIT() \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ - PRINT(KERN_INFO, "[%d]: EXIT %s", current->pid, \ - __func__); \ - } \ - else { \ - PRINT(KERN_INFO, "EXIT %s", __func__); \ - } \ + if (trace_flag & TRACE_PID) \ + PRINT(KERN_INFO, "[%d]: EXIT %s", \ + current->pid, __func__); \ + else \ + PRINT(KERN_INFO, "EXIT %s", \ + __func__); \ } \ } while (0) #define TRACE_EXIT_RES(res) \ do { \ - unsigned long lres = res; \ - \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ - PRINT(KERN_INFO, "[%d]: EXIT %s: %ld", current->pid, \ - __func__, lres); \ - } else { \ + long lres = res; \ + \ + if (trace_flag & TRACE_PID) \ + PRINT(KERN_INFO, "[%d]: EXIT %s: %ld", \ + current->pid, __func__, lres); \ + else \ PRINT(KERN_INFO, "EXIT %s: %ld", \ - __func__, lres); \ - } \ + __func__, lres); \ } \ } while (0) #define TRACE_EXIT_HRES(res) \ do { \ - unsigned long lres = (unsigned long)(res); \ - \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ - PRINT(KERN_INFO, "[%d]: EXIT %s: 0x%lx", current->pid, \ - __func__, lres); \ - } else { \ + unsigned long lres = (unsigned long)(res); \ + \ + if (trace_flag & TRACE_PID) \ + PRINT(KERN_INFO, "[%d]: EXIT %s: 0x%lx", \ + current->pid, __func__, lres); \ + else \ PRINT(KERN_INFO, "EXIT %s: %lx", \ - __func__, lres); \ - } \ + __func__, lres); \ } \ } while (0) #endif diff --git a/scst/src/dev_handlers/scst_user.c b/scst/src/dev_handlers/scst_user.c index b0fbb8ab5..7d3de9c44 100644 --- a/scst/src/dev_handlers/scst_user.c +++ b/scst/src/dev_handlers/scst_user.c @@ -695,7 +695,7 @@ static struct scst_user_cmd *dev_user_alloc_ucmd(struct scst_user_dev *dev, gfp_ TRACE_MEM("ucmd %p allocated", ucmd); out: - TRACE_EXIT_HRES((unsigned long)ucmd); + TRACE_EXIT_HRES(ucmd); return ucmd; } @@ -2544,7 +2544,7 @@ out_unlock: spin_unlock_irq(&dev->udev_cmd_threads.cmd_list_lock); out: - TRACE_EXIT_HRES((__force unsigned int)res); + TRACE_EXIT_HRES(res); return res; } diff --git a/scst/src/dev_handlers/scst_vdisk.c b/scst/src/dev_handlers/scst_vdisk.c index 0ba5ea17c..7b163c891 100644 --- a/scst/src/dev_handlers/scst_vdisk.c +++ b/scst/src/dev_handlers/scst_vdisk.c @@ -695,7 +695,7 @@ static struct scst_vdisk_dev *vdev_find(const char *name) } } - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; } diff --git a/scst/src/scst_event.c b/scst/src/scst_event.c index ba759c2c2..a36c66781 100644 --- a/scst/src/scst_event.c +++ b/scst/src/scst_event.c @@ -1038,7 +1038,7 @@ static __poll_t scst_event_poll(struct file *file, poll_table *wait) out_unlock: mutex_unlock(&scst_event_mutex); - TRACE_EXIT_HRES((__force unsigned int)res); + TRACE_EXIT_HRES(res); return res; } diff --git a/scst/src/scst_lib.c b/scst/src/scst_lib.c index 835c5731c..6fc464961 100644 --- a/scst/src/scst_lib.c +++ b/scst/src/scst_lib.c @@ -2609,7 +2609,7 @@ struct scst_aen *scst_alloc_aen(struct scst_session *sess, aen->lun = scst_pack_lun(unpacked_lun, sess->acg->addr_method); out: - TRACE_EXIT_HRES((unsigned long)aen); + TRACE_EXIT_HRES(aen); return aen; } @@ -5154,7 +5154,7 @@ static struct scst_tgt_dev *scst_find_shared_io_tgt_dev( } out: - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; found: @@ -5934,7 +5934,7 @@ struct scst_cmd *__scst_create_prepare_internal_cmd(const uint8_t *cdb, scst_set_cmd_state(res, SCST_CMD_STATE_PARSE); out: - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; } @@ -5955,7 +5955,7 @@ static struct scst_cmd *scst_create_prepare_internal_cmd( res->atomic = scst_cmd_atomic(orig_cmd); out: - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; } @@ -13616,7 +13616,7 @@ restart: } out: - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; } @@ -13632,7 +13632,7 @@ struct scst_cmd *__scst_check_deferred_commands( res = __scst_check_deferred_commands_locked(order_data, return_first); spin_unlock_irq(&order_data->sn_lock); - TRACE_EXIT_HRES((unsigned long)res); + TRACE_EXIT_HRES(res); return res; } diff --git a/scst/src/scst_pres.c b/scst/src/scst_pres.c index e2f5dc542..cfff018d9 100644 --- a/scst/src/scst_pres.c +++ b/scst/src/scst_pres.c @@ -491,7 +491,7 @@ struct scst_dev_registrant *scst_pr_add_registrant( dev->virt_name, reg->tgt_dev); out: - TRACE_EXIT_HRES((unsigned long)reg); + TRACE_EXIT_HRES(reg); return reg; out_free: diff --git a/scst/src/scst_targ.c b/scst/src/scst_targ.c index fab93cb23..d2c50ab1e 100644 --- a/scst/src/scst_targ.c +++ b/scst/src/scst_targ.c @@ -7158,7 +7158,7 @@ static struct scst_acg *__scst_find_acg(struct scst_tgt *tgt, if (acg == NULL) acg = tgt->default_acg; - TRACE_EXIT_HRES((unsigned long)acg); + TRACE_EXIT_HRES(acg); return acg; } diff --git a/usr/include/debug.h b/usr/include/debug.h index 9fbf92887..384a52db5 100644 --- a/usr/include/debug.h +++ b/usr/include/debug.h @@ -202,53 +202,52 @@ do { \ #define TRACE_ENTRY() \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ + if (trace_flag & TRACE_PID) \ PRINT(LOG_DEBUG, "[%d]: ENTRY %s", \ - gettid(), __func__); \ - } else { \ + gettid(), __func__); \ + else \ PRINT(LOG_DEBUG, "ENTRY %s", \ - __func__); \ - } \ + __func__); \ } \ } while (0) #define TRACE_EXIT() \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ + if (trace_flag & TRACE_PID) \ PRINT(LOG_DEBUG, "[%d]: EXIT %s", \ - gettid(), __func__); \ - } else { \ - PRINT(LOG_DEBUG, "EXIT %s", __func__); \ - } \ + gettid(), __func__); \ + else \ + PRINT(LOG_DEBUG, "EXIT %s", \ + __func__); \ } \ } while (0) #define TRACE_EXIT_RES(res) \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ + long lres = res; \ + \ + if (trace_flag & TRACE_PID) \ PRINT(LOG_DEBUG, "[%d]: EXIT %s: %ld", \ - gettid(), __func__, \ - (long)(res)); \ - } else { \ + gettid(), __func__, lres); \ + else \ PRINT(LOG_DEBUG, "EXIT %s: %ld", \ - __func__, (long)(res)); \ - } \ + __func__, lres); \ } \ } while (0) #define TRACE_EXIT_HRES(res) \ do { \ if (trace_flag & TRACE_ENTRYEXIT) { \ - if (trace_flag & TRACE_PID) { \ + unsigned long lres = (unsigned long)(res); \ + \ + if (trace_flag & TRACE_PID) \ PRINT(LOG_DEBUG, "[%d]: EXIT %s: 0x%lx",\ - gettid(), __func__, \ - (long)(res)); \ - } else { \ + gettid(), __func__, lres); \ + else \ PRINT(LOG_DEBUG, "EXIT %s: %lx", \ - __func__, (long)(res)); \ - } \ + __func__, lres); \ } \ } while (0) diff --git a/usr/stpgd/stpgd_main.c b/usr/stpgd/stpgd_main.c index 04027f0bb..21fe03336 100644 --- a/usr/stpgd/stpgd_main.c +++ b/usr/stpgd/stpgd_main.c @@ -277,11 +277,10 @@ static int stpg_event_loop(void) { int res = 0, status; int event_fd; - uint8_t event_user_buf[1024*1024]; + uint8_t event_user_buf[1024 * 1024]; pid_t c_pid = 0; struct pollfd pl; - struct scst_event_user *event_user = - (struct scst_event_user *)event_user_buf; + struct scst_event_user *event_user = (struct scst_event_user *)event_user_buf; struct scst_event e1; bool first_error = true; @@ -289,8 +288,8 @@ static int stpg_event_loop(void) if (event_fd < 0) { res = -errno; PRINT_ERROR("Unable to open SCST event device %s (%s)", - SCST_EVENT_DEV, strerror(-res)); - goto out; + SCST_EVENT_DEV, strerror(-res)); + return res; } close(stpg_init_report_pipe[0]); @@ -305,10 +304,12 @@ static int stpg_event_loop(void) pl.events = POLLIN; memset(&e1, 0, sizeof(e1)); + e1.event_code = SCST_EVENT_STPG_USER_INVOKE; strncpy(e1.issuer_name, SCST_EVENT_SCST_CORE_ISSUER, sizeof(e1.issuer_name)); e1.issuer_name[sizeof(e1.issuer_name)-1] = '\0'; + PRINT_INFO("Setting allowed event code %d, issuer_name %s", e1.event_code, e1.issuer_name); @@ -323,9 +324,11 @@ static int stpg_event_loop(void) e1.event_code = SCST_EVENT_TM_FN_RECEIVED; strncpy(e1.issuer_name, SCST_EVENT_SCST_CORE_ISSUER, sizeof(e1.issuer_name)); - e1.issuer_name[sizeof(e1.issuer_name)-1] = '\0'; + e1.issuer_name[sizeof(e1.issuer_name) - 1] = '\0'; + PRINT_INFO("Setting allowed event code %d, issuer_name %s", e1.event_code, e1.issuer_name); + res = ioctl(event_fd, SCST_EVENT_ALLOW_EVENT, &e1); if (res != 0) { res = -errno; @@ -337,24 +340,25 @@ static int stpg_event_loop(void) while (1) { memset(event_user_buf, 0, sizeof(event_user_buf)); event_user->max_event_size = sizeof(event_user_buf); + res = ioctl(event_fd, SCST_EVENT_GET_NEXT_EVENT, event_user); if (res != 0) { res = -errno; switch (-res) { case ESRCH: case EBUSY: - TRACE_MGMT_DBG("SCST_EVENT_GET_NEXT_EVENT " - "returned %d (%s)", res, strerror(res)); + TRACE_MGMT_DBG("SCST_EVENT_GET_NEXT_EVENT returned %d (%s)", + res, strerror(res)); /* fall through */ case EINTR: continue; case EAGAIN: - TRACE_DBG("SCST_EVENT_GET_NEXT_EVENT, " - "returned EAGAIN (%d)", -res); + TRACE_DBG("SCST_EVENT_GET_NEXT_EVENT, returned EAGAIN (%d)", + -res); continue; default: - PRINT_ERROR("SCST_EVENT_GET_NEXT_EVENT " - "failed: %d (%s)", res, strerror(-res)); + PRINT_ERROR("SCST_EVENT_GET_NEXT_EVENT failed: %d (%s)", + res, strerror(-res)); if (!first_error) goto out; first_error = false; @@ -365,40 +369,40 @@ again_poll: res = poll(&pl, 1, c_pid > 0 ? 1 : 0); if (res > 0) continue; - else if (res == 0) + if (res == 0) + goto again_poll; + + res = -errno; + switch (res) { + case ESRCH: + case EBUSY: + case EAGAIN: + TRACE_MGMT_DBG("poll() returned %d (%s)", + res, strerror(-res)); + /* fall through */ + case EINTR: + goto again_poll; + default: + PRINT_ERROR("poll() failed: %d (%s)", + res, strerror(-res)); goto again_poll; - else { - res = -errno; - switch (res) { - case ESRCH: - case EBUSY: - case EAGAIN: - TRACE_MGMT_DBG("poll() returned %d " - "(%s)", res, strerror(-res)); - case EINTR: - goto again_poll; - default: - PRINT_ERROR("poll() failed: %d (%s)", - res, strerror(-res)); - goto again_poll; - } } } first_error = true; #ifdef DEBUG PRINT_INFO("event_code %d, issuer_name %s", - event_user->out_event.event_code, - event_user->out_event.issuer_name); + event_user->out_event.event_code, + event_user->out_event.issuer_name); #endif if (event_user->out_event.payload_len != 0) TRACE_BUFFER("payload", event_user->out_event.payload, - event_user->out_event.payload_len); + event_user->out_event.payload_len); if (event_user->out_event.event_code == SCST_EVENT_STPG_USER_INVOKE) { c_pid = fork(); - if (c_pid == -1) + if (c_pid == -1) { PRINT_ERROR("Failed to fork: %d", c_pid); - else if (c_pid == 0) { + } else if (c_pid == 0) { struct scst_event_notify_done d; signal(SIGCHLD, SIG_DFL); @@ -411,19 +415,21 @@ again_poll: res = ioctl(event_fd, SCST_EVENT_NOTIFY_DONE, &d); if (res != 0) { res = -errno; - PRINT_ERROR("SCST_EVENT_NOTIFY_DONE " - "failed: %s (res %d)", - strerror(-res), res); - } else + PRINT_ERROR("SCST_EVENT_NOTIFY_DONE failed: %s (res %d)", + strerror(-res), res); + } else { PRINT_INFO("STPG event completed with status %d", status); + } exit(res); } - } else if (event_user->out_event.event_code == SCST_EVENT_TM_FN_RECEIVED) + } else if (event_user->out_event.event_code == SCST_EVENT_TM_FN_RECEIVED) { stpg_handle_tm_received(event_user); - else + } else { PRINT_ERROR("Unknown event %d received", event_user->out_event.event_code); + } } out: + close(event_fd); return res; }