From 873871edb981d3d6f3a18b0070db4e07b419ed0c Mon Sep 17 00:00:00 2001 From: Danny Auble <da@schedmd.com> Date: Wed, 21 Feb 2018 13:53:47 -0700 Subject: [PATCH] Refactor printing out of multi-line user msgs Bug 4758 --- src/api/allocate.c | 37 ++++++-------------------------- src/common/slurm_protocol_defs.c | 21 +++++++++++++++++- src/common/slurm_protocol_defs.h | 8 +++++++ src/common/slurm_xlator.h | 1 + src/salloc/salloc.c | 14 +++--------- src/sbatch/sbatch.c | 11 +--------- src/srun/libsrun/allocate.c | 13 +++-------- 7 files changed, 43 insertions(+), 62 deletions(-) diff --git a/src/api/allocate.c b/src/api/allocate.c index d3a45fc4f15..b62ff499b2f 100644 --- a/src/api/allocate.c +++ b/src/api/allocate.c @@ -396,7 +396,6 @@ static void _pack_alloc_test(List resp, uint32_t *node_cnt, uint32_t *job_id) { resource_allocation_response_msg_t *alloc; uint32_t inx = 0, pack_node_cnt = 0, pack_job_id = 0; - char *buf, *ptrptr = NULL, *line; ListIterator iter; xassert(resp); @@ -405,15 +404,7 @@ static void _pack_alloc_test(List resp, uint32_t *node_cnt, uint32_t *job_id) pack_node_cnt += alloc->node_cnt; if (pack_job_id == 0) pack_job_id = alloc->job_id; - if (alloc->job_submit_user_msg) { - buf = xstrdup(alloc->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%d: %s", inx, line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } + print_multi_line_string(alloc->job_submit_user_msg, inx); inx++; } list_iterator_destroy(iter); @@ -610,16 +601,9 @@ int slurm_job_will_run(job_desc_msg_t *req) else rc = slurm_job_will_run2(req, &will_run_resp); - if (will_run_resp && will_run_resp->job_submit_user_msg) { - char *line = NULL, *buf = NULL, *ptrptr = NULL; - buf = xstrdup(will_run_resp->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%s", line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } + if (will_run_resp) + print_multi_line_string( + will_run_resp->job_submit_user_msg, -1); if ((rc == 0) && will_run_resp) { if (cluster_flags & CLUSTER_FLAG_BG) @@ -691,16 +675,9 @@ extern int slurm_pack_job_will_run(List job_req_list) will_run_resp = NULL; rc = slurm_job_will_run2(req, &will_run_resp); - if (will_run_resp && will_run_resp->job_submit_user_msg) { - char *line = NULL, *buf = NULL, *ptrptr = NULL; - buf = xstrdup(will_run_resp->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%d: %s", inx, line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } + if (will_run_resp) + print_multi_line_string( + will_run_resp->job_submit_user_msg, inx); if ((rc == SLURM_SUCCESS) && will_run_resp) { if (first_job_id == 0) diff --git a/src/common/slurm_protocol_defs.c b/src/common/slurm_protocol_defs.c index 9c434d63406..34b8542a8e4 100644 --- a/src/common/slurm_protocol_defs.c +++ b/src/common/slurm_protocol_defs.c @@ -82,7 +82,7 @@ strong_alias(node_use_string, slurm_node_use_string); strong_alias(bg_block_state_string, slurm_bg_block_state_string); strong_alias(cray_nodelist2nids, slurm_cray_nodelist2nids); strong_alias(reservation_flags_string, slurm_reservation_flags_string); - +strong_alias(print_multi_line_string, slurm_print_multi_line_string); static void _free_all_front_end_info(front_end_info_msg_t *msg); @@ -5153,3 +5153,22 @@ extern int get_cluster_node_offset(char *cluster_name, return 0; } +extern void print_multi_line_string(char *user_msg, int inx) +{ + char *line, *buf, *ptrptr = NULL; + + if (!user_msg) + return; + + buf = xstrdup(user_msg); + line = strtok_r(buf, "\n", &ptrptr); + while (line) { + if (inx == -1) + info("%s", line); + else + info("%d: %s", inx, line); + line = strtok_r(NULL, "\n", &ptrptr); + } + xfree(buf); +} + diff --git a/src/common/slurm_protocol_defs.h b/src/common/slurm_protocol_defs.h index 45a01578110..d4409ff300f 100644 --- a/src/common/slurm_protocol_defs.h +++ b/src/common/slurm_protocol_defs.h @@ -1538,6 +1538,14 @@ extern bool cluster_in_federation(void *ptr, char *cluster_name); extern int get_cluster_node_offset(char *cluster_name, node_info_msg_t *node_info_ptr); +/* + * Print the char* given. + * + * Each \n will result in a new line. + * If inx is != -1 it is prepended to the string. + */ +extern void print_multi_line_string(char *user_msg, int inx); + /* Given a protocol opcode return its string * description mapping the slurm_msg_type_t * to its name. diff --git a/src/common/slurm_xlator.h b/src/common/slurm_xlator.h index eaacccb773a..30b15ca8c01 100644 --- a/src/common/slurm_xlator.h +++ b/src/common/slurm_xlator.h @@ -362,6 +362,7 @@ #define node_use_string slurm_node_use_string #define bg_block_state_string slurm_bg_block_state_string #define reservation_flags_string slurm_reservation_flags_string +#define print_multi_line_string slurm_print_multi_line_string /* slurmdbd_defs.[ch] functions */ #define slurmdbd_defs_init slurm_slurmdbd_defs_init diff --git a/src/salloc/salloc.c b/src/salloc/salloc.c index 4186afab258..876c0a8ee54 100644 --- a/src/salloc/salloc.c +++ b/src/salloc/salloc.c @@ -191,7 +191,6 @@ int main(int argc, char **argv) bool pack_fini = false; int pack_argc, pack_inx, pack_argc_off; char **pack_argv; - char *line = NULL, *buf = NULL, *ptrptr = NULL; static char *msg = "Slurm job queue full, sleeping and retrying."; slurm_allocation_callbacks_t callbacks; ListIterator iter_req, iter_resp; @@ -471,16 +470,9 @@ int main(int argc, char **argv) /* Allocation granted to regular job */ my_job_id = alloc->job_id; - if (alloc && alloc->job_submit_user_msg) { - buf = xstrdup(alloc->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%s", line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } - + if (alloc) + print_multi_line_string( + alloc->job_submit_user_msg, -1); info("Granted job allocation %u", my_job_id); if (_proc_alloc(alloc) != SLURM_SUCCESS) diff --git a/src/sbatch/sbatch.c b/src/sbatch/sbatch.c index 57846baa85f..733ac5abbe2 100644 --- a/src/sbatch/sbatch.c +++ b/src/sbatch/sbatch.c @@ -84,7 +84,6 @@ int main(int argc, char **argv) submit_response_msg_t *resp = NULL; char *script_name; char *script_body; - char *line = NULL, *buf = NULL, *ptrptr = NULL; char **pack_argv; int script_size = 0, pack_argc, pack_argc_off = 0, pack_inx; int i, rc = SLURM_SUCCESS, retries = 0; @@ -286,15 +285,7 @@ int main(int argc, char **argv) exit(error_exit); } - if (resp->job_submit_user_msg) { - buf = xstrdup(resp->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%s", line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } + print_multi_line_string(resp->job_submit_user_msg, -1); if (!sbopt.parsable) { printf("Submitted batch job %u", resp->job_id); diff --git a/src/srun/libsrun/allocate.c b/src/srun/libsrun/allocate.c index 29f802d9ebd..fbf1d3b741f 100644 --- a/src/srun/libsrun/allocate.c +++ b/src/srun/libsrun/allocate.c @@ -485,7 +485,7 @@ extern resource_allocation_response_msg_t * job_desc_msg_t *j; slurm_allocation_callbacks_t callbacks; int i; - char *line = NULL, *buf = NULL, *ptrptr = NULL; + xassert(srun_opt); if (srun_opt->relative_set && srun_opt->relative) @@ -543,15 +543,8 @@ extern resource_allocation_response_msg_t * } } - if (resp && resp->job_submit_user_msg) { - buf = xstrdup(resp->job_submit_user_msg); - line = strtok_r(buf, "\n", &ptrptr); - while (line) { - info("%s", line); - line = strtok_r(NULL, "\n", &ptrptr); - } - xfree(buf); - } + if (resp) + print_multi_line_string(resp->job_submit_user_msg, -1); if (resp && !destroy_job) { /* -- GitLab