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;
57 RUNSCRIPT *copy_runscript(RUNSCRIPT *src)
59 Dmsg0(500, "runscript: creating new RUNSCRIPT object from other\n");
61 RUNSCRIPT *dst = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
62 memcpy(dst, src, sizeof(RUNSCRIPT));
67 dst->set_command(src->command);
68 dst->set_target(src->target);
73 void free_runscript(RUNSCRIPT *script)
75 Dmsg0(500, "runscript: freeing RUNSCRIPT object\n");
77 if (script->command) {
78 free_pool_memory(script->command);
81 free_pool_memory(script->target);
86 int run_scripts(JCR *jcr, alist *runscripts, const char *label)
88 Dmsg2(200, "runscript: running all RUNSCRIPT object (%s) JobStatus=%c\n", label, jcr->JobStatus);
94 if (runscripts == NULL) {
95 Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
99 foreach_alist(script, runscripts) {
100 Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
103 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Created)) {
104 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
108 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Running)) {
109 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
113 if (script->when & SCRIPT_After) {
114 if ( (script->on_success && (jcr->JobStatus == JS_Terminated))
116 (script->on_failure && job_canceled(jcr))
119 Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n", script->command,
127 if (!script->is_local()) {
133 status = script->run(jcr, label);
135 /* cancel running job properly */
136 if ( script->abort_on_error
138 && (jcr->JobStatus == JS_Created)
141 set_jcr_job_status(jcr, JS_ErrorTerminated);
148 bool RUNSCRIPT::is_local()
150 if (!target || (strcmp(target, "") == 0)) {
157 /* set this->command to cmd */
158 void RUNSCRIPT::set_command(const POOLMEM *cmd)
160 Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
167 command = get_pool_memory(PM_FNAME);
170 pm_strcpy(command, cmd);
173 /* set this->target to client_name */
174 void RUNSCRIPT::set_target(const POOLMEM *client_name)
176 Dmsg1(500, "runscript: setting target\n", NPRT(client_name));
183 target = get_pool_memory(PM_FNAME);
186 pm_strcpy(target, client_name);
189 int RUNSCRIPT::run(JCR *jcr, const char *name)
191 Dmsg0(200, "runscript: running a RUNSCRIPT object\n");
192 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
195 char line[MAXSTRING];
197 ecmd = edit_job_codes(jcr, ecmd, this->command, "");
198 Dmsg1(100, "runscript: running '%s'...\n", ecmd);
199 Jmsg(jcr, M_INFO, 0, _("%s: run command \"%s\"\n"), name, ecmd);
201 bpipe = open_bpipe(ecmd, 0, "r");
202 free_pool_memory(ecmd);
205 Jmsg(jcr, M_FATAL, 0, _("%s could not execute. ERR=%s\n"), name,
209 while (fgets(line, sizeof(line), bpipe->rfd)) {
210 int len = strlen(line);
211 if (len > 0 && line[len-1] == '\n') {
214 Jmsg(jcr, M_INFO, 0, _("%s: %s\n"), name, line);
216 status = close_bpipe(bpipe);
219 Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d. ERR=%s\n"), name,
220 status, be.strerror(status));
226 void free_runscripts(alist *runscripts)
228 Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n");
231 foreach_alist(elt, runscripts) {
236 void RUNSCRIPT::debug()
238 Dmsg0(200, "runscript: debug\n");
239 Dmsg0(200, _(" --> RunScript\n"));
240 Dmsg1(200, _(" --> Command=%s\n"), NPRT(command));
241 Dmsg1(200, _(" --> Target=%s\n"), NPRT(target));
242 Dmsg1(200, _(" --> RunOnSuccess=%u\n"), on_success);
243 Dmsg1(200, _(" --> RunOnFailure=%u\n"), on_failure);
244 Dmsg1(200, _(" --> AbortJobOnError=%u\n"), abort_on_error);
245 Dmsg1(200, _(" --> RunWhen=%u\n"), when);