From: Kern Sibbald Date: Mon, 2 Jun 2014 16:00:57 +0000 (+0200) Subject: Better error handling for cancel command X-Git-Tag: Release-7.0.4~2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=5b4a6e32a6defa5fb3d82b8c0dcbb168f77d0568;p=bacula%2Fbacula Better error handling for cancel command --- diff --git a/bacula/src/dird/ua_select.c b/bacula/src/dird/ua_select.c index 68904abe59..72a5d619fd 100644 --- a/bacula/src/dird/ua_select.c +++ b/bacula/src/dird/ua_select.c @@ -1241,34 +1241,35 @@ int select_running_jobs(UAContext *ua, alist *jcrs, const char *reason) sellist sl; int32_t JobId; + if (!ua->argv[i]) { + ua->error_msg(_("No value given for \"jobid\".\n")); + goto bail_out; + } if (!sl.set_string(ua->argv[i], true)) { ua->send_msg("%s", sl.get_errmsg()); - break; + goto bail_out; } - foreach_sellist(JobId, &sl) { jcr = get_jcr_by_id(JobId); if (jcr && jcr->job && acl_access_ok(ua, Job_ACL, jcr->job->name())) { jcrs->append(jcr); - } else if (jcr) { ua->error_msg(_("Unauthorized command from this console " "for JobId=%d.\n"), JobId); free_jcr(jcr); - } else { - if (debug_level > 5) { - ua->warning_msg(_("Warning Job JobId=%d is not running. " - "Continuing anyway...\n"), JobId); - } + ua->warning_msg(_("Warning Job JobId=%d is not running.\n"), JobId); } } + if (jcrs->size() == 0) { + goto bail_out; /* If we did not find specified jobid, get out */ + } break; /* TODO: might want to implement filters (client, status, etc...) */ } else if (strcasecmp(ua->argk[i], NT_("all")) == 0) { foreach_jcr(jcr) { - if (jcr->JobId == 0) { /* this is us */ + if (jcr->JobId == 0) { /* Do not cancel consoles */ continue; } if (!acl_access_ok(ua, Job_ACL, jcr->job->name())) { @@ -1279,42 +1280,54 @@ int select_running_jobs(UAContext *ua, alist *jcrs, const char *reason) } endeach_jcr(jcr); + /* If we have something and no "yes" on command line, get confirmation */ if (jcrs->size() > 0 && find_arg(ua, NT_("yes")) < 0) { char nbuf[1000]; bsnprintf(nbuf, sizeof(nbuf), _("Confirm %s of %d Job%s (yes/no): "), reason, jcrs->size(), jcrs->size()>1?"s":""); if (!get_yesno(ua, nbuf) || ua->pint32_val == 0) { - return -1; + goto bail_out; } } + if (jcrs->size() == 0) { + goto bail_out; /* If we did not find specified jobid, get out */ + } break; } else if (strcasecmp(ua->argk[i], NT_("job")) == 0) { if (!ua->argv[i]) { - break; + ua->error_msg(_("No value given for \"job\".\n")); + goto bail_out; } if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) { - ua->warning_msg(_("Warning Job %s is not running. Continuing anyway ...\n"), ua->argv[i]); + ua->warning_msg(_("Warning Job %s is not running.\n"), ua->argv[i]); jcr = new_jcr(sizeof(JCR), dird_free_jcr); bstrncpy(jcr->Job, ua->argv[i], sizeof(jcr->Job)); } if (jcr && jcr->job && acl_access_ok(ua, Job_ACL, jcr->job->name())) { jcrs->append(jcr); } + if (jcrs->size() == 0) { + goto bail_out; /* If we did not find specified jobid, get out */ + } break; } else if (strcasecmp(ua->argk[i], NT_("ujobid")) == 0) { if (!ua->argv[i]) { - break; + ua->error_msg(_("No value given for \"ujobid\".\n")); + goto bail_out; } if (!(jcr=get_jcr_by_full_name(ua->argv[i]))) { - ua->warning_msg(_("Warning Job %s is not running. Continuing anyway ...\n"), ua->argv[i]); + ua->warning_msg(_("Warning Job %s is not running.\n"), ua->argv[i]); jcr = new_jcr(sizeof(JCR), dird_free_jcr); bstrncpy(jcr->Job, ua->argv[i], sizeof(jcr->Job)); } if (jcr && jcr->job && acl_access_ok(ua, Job_ACL, jcr->job->name())) { jcrs->append(jcr); } + if (jcrs->size() == 0) { + goto bail_out; /* If we did not find specified jobid, get out */ + } break; } } diff --git a/bacula/src/lib/sellist.c b/bacula/src/lib/sellist.c index e3e2589c87..0997068996 100644 --- a/bacula/src/lib/sellist.c +++ b/bacula/src/lib/sellist.c @@ -211,7 +211,8 @@ char *sellist::get_expanded_list() #ifdef TEST_PROGRAM -const char *sin[] = { +/* Test input string */ +const char *sinp[] = { "1,70", "1", "256", @@ -227,6 +228,7 @@ const char *sin[] = { NULL }; +/* Scanned output string -- or ERROR if input is illegal */ const char *sout[] = { "1,70", "1", @@ -249,16 +251,16 @@ int main(int argc, char **argv, char **env) sellist sl; int x; - for (x=0; sin[x] != NULL; x++) { - if (!sl.set_string(sin[x], true)) { - printf("ERR: input: %s ERR=%s", sin[x], sl.get_errmsg()); + for (x=0; sinp[x] != NULL; x++) { + if (!sl.set_string(sinp[x], true)) { + printf("ERR: input: %s ERR=%s", sinp[x], sl.get_errmsg()); continue; } msg = sl.get_expanded_list(); if (sl.get_errmsg() == NULL && strcmp(msg, sout[x]) == 0) { - printf("OK: input: %s output: %s\n", sin[x], msg); + printf("OK: input: %s output: %s\n", sinp[x], msg); } else { - printf("ERR: input: %s gave output: %s ERR=%s\n", sin[x], msg, + printf("ERR: input: %s gave output: %s ERR=%s\n", sinp[x], msg, sl.get_errmsg()); } }