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.
30 #include "runscript.h"
32 RUNSCRIPT *new_runscript()
34 Dmsg0(500, "runscript: creating new RUNSCRIPT object\n");
35 RUNSCRIPT *cmd = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
36 memset(cmd, 0, sizeof(RUNSCRIPT));
42 void RUNSCRIPT::reset_default(bool free_strings)
44 if (free_strings && command) {
45 free_pool_memory(command);
47 if (free_strings && target) {
48 free_pool_memory(target);
55 abort_on_error = true;
59 RUNSCRIPT *copy_runscript(RUNSCRIPT *src)
61 Dmsg0(500, "runscript: creating new RUNSCRIPT object from other\n");
63 RUNSCRIPT *dst = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
64 memcpy(dst, src, sizeof(RUNSCRIPT));
69 dst->set_command(src->command);
70 dst->set_target(src->target);
75 void free_runscript(RUNSCRIPT *script)
77 Dmsg0(500, "runscript: freeing RUNSCRIPT object\n");
79 if (script->command) {
80 free_pool_memory(script->command);
83 free_pool_memory(script->target);
88 int run_scripts(JCR *jcr, alist *runscripts, const char *label)
90 Dmsg2(200, "runscript: running all RUNSCRIPT object (%s) JobStatus=%c\n", label, jcr->JobStatus);
96 if (runscripts == NULL) {
97 Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
101 foreach_alist(script, runscripts) {
102 Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
105 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Created)) {
106 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
110 if ((script->when & SCRIPT_Before) && (jcr->JobStatus == JS_Running)) {
111 Dmsg0(200, "runscript: Run it because SCRIPT_Before\n");
115 if (script->when & SCRIPT_After) {
116 if ( (script->on_success && (jcr->JobStatus == JS_Terminated))
118 (script->on_failure && job_canceled(jcr))
121 Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n", script->command,
125 script->when ^= SCRIPT_After; /* reset SCRIPT_After bit */
130 if (!script->is_local()) {
136 status = script->run(jcr, label);
138 /* cancel running job properly */
139 if ( script->abort_on_error
141 && (jcr->JobStatus == JS_Created)
144 set_jcr_job_status(jcr, JS_ErrorTerminated);
151 bool RUNSCRIPT::is_local()
153 if (!target || (strcmp(target, "") == 0)) {
160 /* set this->command to cmd */
161 void RUNSCRIPT::set_command(const POOLMEM *cmd)
163 Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
170 command = get_pool_memory(PM_FNAME);
173 pm_strcpy(command, cmd);
176 /* set this->target to client_name */
177 void RUNSCRIPT::set_target(const POOLMEM *client_name)
179 Dmsg1(500, "runscript: setting target\n", NPRT(client_name));
186 target = get_pool_memory(PM_FNAME);
189 pm_strcpy(target, client_name);
192 int RUNSCRIPT::run(JCR *jcr, const char *name)
194 Dmsg0(200, "runscript: running a RUNSCRIPT object\n");
195 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
198 char line[MAXSTRING];
200 ecmd = edit_job_codes(jcr, ecmd, this->command, "");
201 Dmsg1(100, "runscript: running '%s'...\n", ecmd);
202 Jmsg(jcr, M_INFO, 0, _("%s: run command \"%s\"\n"), name, ecmd);
204 bpipe = open_bpipe(ecmd, 0, "r");
205 free_pool_memory(ecmd);
208 Jmsg(jcr, M_FATAL, 0, _("%s could not execute. ERR=%s\n"), name,
212 while (fgets(line, sizeof(line), bpipe->rfd)) {
213 int len = strlen(line);
214 if (len > 0 && line[len-1] == '\n') {
217 Jmsg(jcr, M_INFO, 0, _("%s: %s\n"), name, line);
219 status = close_bpipe(bpipe);
222 Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d. ERR=%s\n"), name,
223 status, be.strerror(status));
229 void free_runscripts(alist *runscripts)
231 Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n");
234 foreach_alist(elt, runscripts) {
239 void RUNSCRIPT::debug()
241 Dmsg0(200, "runscript: debug\n");
242 Dmsg0(200, _(" --> RunScript\n"));
243 Dmsg1(200, _(" --> Command=%s\n"), NPRT(command));
244 Dmsg1(200, _(" --> Target=%s\n"), NPRT(target));
245 Dmsg1(200, _(" --> RunOnSuccess=%u\n"), on_success);
246 Dmsg1(200, _(" --> RunOnFailure=%u\n"), on_failure);
247 Dmsg1(200, _(" --> AbortJobOnError=%u\n"), abort_on_error);
248 Dmsg1(200, _(" --> RunWhen=%u\n"), when);