2 * Manipulation routines for RunScript list
4 * Eric Bollengier, May 2006
10 Copyright (C) 2000-2006 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 version 2 as amended with additional clauses defined in the
15 file LICENSE in the main source directory.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 the file LICENSE for additional details.
28 #include "runscript.h"
30 RUNSCRIPT *new_runscript()
32 Dmsg0(500, "runscript: creating new RUNSCRIPT object\n");
33 RUNSCRIPT *cmd = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
34 memset(cmd, 0, sizeof(RUNSCRIPT));
40 void RUNSCRIPT::reset_default(bool free_strings)
42 if (free_strings && command) {
43 free_pool_memory(command);
45 if (free_strings && target) {
46 free_pool_memory(target);
53 abort_on_error = true;
55 old_proto = false; /* TODO: drop this with bacula 1.42 */
58 RUNSCRIPT *copy_runscript(RUNSCRIPT *src)
60 Dmsg0(500, "runscript: creating new RUNSCRIPT object from other\n");
62 RUNSCRIPT *dst = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
63 memcpy(dst, src, sizeof(RUNSCRIPT));
68 dst->set_command(src->command);
69 dst->set_target(src->target);
74 void free_runscript(RUNSCRIPT *script)
76 Dmsg0(500, "runscript: freeing RUNSCRIPT object\n");
78 if (script->command) {
79 free_pool_memory(script->command);
82 free_pool_memory(script->target);
87 int run_scripts(JCR *jcr, alist *runscripts, const char *label)
89 Dmsg2(200, "runscript: running all RUNSCRIPT object (%s) JobStatus=%c\n", label, jcr->JobStatus);
95 if (runscripts == NULL) {
96 Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
100 foreach_alist(script, runscripts) {
101 Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
104 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Created)) {
105 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
109 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Running)) {
110 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
114 if (script->when & SCRIPT_After) {
115 if ( (script->on_success && (jcr->JobStatus == JS_Terminated))
117 (script->on_failure && job_canceled(jcr))
120 Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n", script->command,
128 if (!script->is_local()) {
134 status = script->run(jcr, label);
136 /* cancel running job properly */
137 if ( script->abort_on_error
139 && (jcr->JobStatus == JS_Created)
142 set_jcr_job_status(jcr, JS_ErrorTerminated);
149 bool RUNSCRIPT::is_local()
151 if (!target || (strcmp(target, "") == 0)) {
158 /* set this->command to cmd */
159 void RUNSCRIPT::set_command(const POOLMEM *cmd)
161 Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
168 command = get_pool_memory(PM_FNAME);
171 pm_strcpy(command, cmd);
174 /* set this->target to client_name */
175 void RUNSCRIPT::set_target(const POOLMEM *client_name)
177 Dmsg1(500, "runscript: setting target\n", NPRT(client_name));
184 target = get_pool_memory(PM_FNAME);
187 pm_strcpy(target, client_name);
190 int RUNSCRIPT::run(JCR *jcr, const char *name)
192 Dmsg0(200, "runscript: running a RUNSCRIPT object\n");
193 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
196 char line[MAXSTRING];
198 ecmd = edit_job_codes(jcr, ecmd, this->command, "");
199 Dmsg1(100, "runscript: running '%s'...\n", ecmd);
200 Jmsg(jcr, M_INFO, 0, _("%s: run command \"%s\"\n"), name, ecmd);
202 bpipe = open_bpipe(ecmd, 0, "r");
203 free_pool_memory(ecmd);
206 Jmsg(jcr, M_FATAL, 0, _("Runscript: %s could not execute. ERR=%s\n"), name,
210 while (fgets(line, sizeof(line), bpipe->rfd)) {
211 int len = strlen(line);
212 if (len > 0 && line[len-1] == '\n') {
215 Jmsg(jcr, M_INFO, 0, _("%s: %s\n"), name, line);
217 status = close_bpipe(bpipe);
220 Jmsg(jcr, M_FATAL, 0, _("Runscript: %s returned non-zero status=%d. ERR=%s\n"), name,
221 be.code(status), be.strerror(status));
227 void free_runscripts(alist *runscripts)
229 Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n");
232 foreach_alist(elt, runscripts) {
237 void RUNSCRIPT::debug()
239 Dmsg0(200, "runscript: debug\n");
240 Dmsg0(200, _(" --> RunScript\n"));
241 Dmsg1(200, _(" --> Command=%s\n"), NPRT(command));
242 Dmsg1(200, _(" --> Target=%s\n"), NPRT(target));
243 Dmsg1(200, _(" --> RunOnSuccess=%u\n"), on_success);
244 Dmsg1(200, _(" --> RunOnFailure=%u\n"), on_failure);
245 Dmsg1(200, _(" --> AbortJobOnError=%u\n"), abort_on_error);
246 Dmsg1(200, _(" --> RunWhen=%u\n"), when);