2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula common code library interface to Python
32 * Kern Sibbald, November MMIV
43 #undef _POSIX_C_SOURCE
46 #include "pythonlib.h"
48 /* Forward referenced subroutines */
49 static void init_python_lock();
50 static void term_python_lock();
52 static PyObject *bacula_module = NULL; /* We create this */
53 static PyObject *StartUp_module = NULL; /* We import this */
55 /* These are the daemon events or methods that are defined */
56 static PyObject *JobStart_method = NULL;
57 static PyObject *JobEnd_method = NULL;
58 static PyObject *Exit_method = NULL;
60 static PyObject *set_bacula_events(PyObject *self, PyObject *args);
61 static PyObject *bacula_write(PyObject *self, PyObject *args);
63 PyObject *find_method(PyObject *eventsObject, PyObject *method, const char *name);
65 /* Define Bacula daemon method entry points */
66 static PyMethodDef BaculaMethods[] = {
67 {"set_events", set_bacula_events, METH_VARARGS, "Define Bacula events."},
68 {"write", bacula_write, METH_VARARGS, "Write output."},
69 {NULL, NULL, 0, NULL} /* last item */
72 static char my_version[] = VERSION " " BDATE;
75 * This is a Bacula Job type as defined in Python. We store a pointer
76 * to the jcr. That is all we need, but the user's script may keep
77 * local data attached to this.
79 typedef struct s_JobObject {
84 static PyTypeObject JobType = {
85 PyObject_HEAD_INIT(NULL)
86 /* Other items filled in in code below */
89 /* Return the JCR pointer from the JobObject */
90 JCR *get_jcr_from_PyObject(PyObject *self)
95 return ((JobObject *)self)->jcr;
98 /* Start the interpreter */
99 void init_python_interpreter(init_python_interpreter_args *args)
103 if (!args->scriptdir || args->scriptdir[0] == 0) {
104 Dmsg1(100, "No script dir. prog=%s\n", args->modulename);
107 Dmsg2(100, "Script dir=%s prog=%s\n", args->scriptdir, args->modulename);
109 Py_SetProgramName((char *)args->progname);
111 PyEval_InitThreads();
112 bacula_module = Py_InitModule("bacula", BaculaMethods);
113 PyModule_AddStringConstant(bacula_module, "Name", my_name);
114 PyModule_AddStringConstant(bacula_module, "Version", my_version);
115 PyModule_AddStringConstant(bacula_module, "ConfigFile", (char *)args->configfile);
116 PyModule_AddStringConstant(bacula_module, "WorkingDir", (char *)args->workingdir);
117 if (!bacula_module) {
118 Jmsg0(NULL, M_ERROR_TERM, 0, _("Could not initialize Python\n"));
120 bsnprintf(buf, sizeof(buf), "import sys\n"
121 "sys.path.append('%s')\n", args->scriptdir);
122 if (PyRun_SimpleString(buf) != 0) {
123 Jmsg1(NULL, M_ERROR_TERM, 0, _("Could not Run Python string %s\n"), buf);
126 /* Explicitly set values we want */
127 JobType.tp_name = "Bacula.Job";
128 JobType.tp_basicsize = sizeof(JobObject);
129 JobType.tp_flags = Py_TPFLAGS_DEFAULT;
130 JobType.tp_doc = "Bacula Job object";
131 JobType.tp_getattr = args->job_getattr;
132 JobType.tp_setattr = args->job_setattr;
134 if (PyType_Ready(&JobType) != 0) {
135 Jmsg0(NULL, M_ERROR_TERM, 0, _("Could not initialize Python Job type.\n"));
138 StartUp_module = PyImport_ImportModule((char *)args->modulename);
139 if (!StartUp_module) {
140 Emsg2(M_ERROR, 0, _("Could not import Python script %s/%s. Python disabled.\n"),
141 args->scriptdir, args->modulename);
142 if (PyErr_Occurred()) {
144 Dmsg0(000, "Python Import error.\n");
147 PyEval_ReleaseLock();
151 void term_python_interpreter()
153 if (StartUp_module) {
154 Py_XDECREF(StartUp_module);
160 static PyObject *set_bacula_events(PyObject *self, PyObject *args)
164 Dmsg0(100, "In set_bacula_events.\n");
165 if (!PyArg_ParseTuple(args, "O:set_bacula_events", &eObject)) {
168 JobStart_method = find_method(eObject, JobStart_method, "JobStart");
169 JobEnd_method = find_method(eObject, JobEnd_method, "JobEnd");
170 Exit_method = find_method(eObject, Exit_method, "Exit");
177 /* Write text to daemon output */
178 static PyObject *bacula_write(PyObject *self, PyObject *args)
181 if (!PyArg_ParseTuple(args, "s:write", &text)) {
185 Jmsg(NULL, M_INFO, 0, "%s", text);
192 * Check that a method exists and is callable.
194 PyObject *find_method(PyObject *eventsObject, PyObject *method, const char *name)
196 Py_XDECREF(method); /* release old method if any */
197 method = PyObject_GetAttrString(eventsObject, (char *)name);
198 if (method == NULL) {
199 Dmsg1(000, "Python method %s not found\n", name);
200 } else if (PyCallable_Check(method) == 0) {
201 Dmsg1(000, "Python object %s found but not a method.\n", name);
205 Dmsg1(100, "Got method %s\n", name);
212 * Generate and process a Bacula event by importing a Python
213 * module and running it.
215 * Returns: 0 if Python not configured or module not found
219 int generate_daemon_event(JCR *jcr, const char *event)
223 PyObject *result = NULL;
224 char *obj_fmt = (char *)"O";
226 if (!StartUp_module) {
227 Dmsg0(100, "No startup module.\n");
231 Dmsg1(100, "event=%s\n", event);
233 // PyEval_AcquireLock();
234 if (strcmp(event, "JobStart") == 0) {
235 if (!JobStart_method) {
239 /* Create JCR argument to send to function */
240 pJob = (PyObject *)PyObject_New(JobObject, &JobType);
242 Jmsg(jcr, M_ERROR, 0, _("Could not create Python Job Object.\n"));
245 ((JobObject *)pJob)->jcr = jcr;
246 bstrncpy(jcr->event, event, sizeof(jcr->event));
247 result = PyObject_CallFunction(JobStart_method, obj_fmt, pJob);
248 jcr->event[0] = 0; /* no event in progress */
249 if (result == NULL) {
250 JobStart_method = NULL;
251 if (PyErr_Occurred()) {
253 Dmsg0(000, "Python JobStart error.\n");
255 Jmsg(jcr, M_ERROR, 0, _("Python function \"%s\" not found.\n"), event);
259 jcr->Python_job = (void *)pJob;
263 } else if (strcmp(event, "JobEnd") == 0) {
264 if (!JobEnd_method || !jcr->Python_job) {
265 stat = 0; /* probably already here */
268 bstrncpy(jcr->event, event, sizeof(jcr->event));
269 Dmsg1(100, "Call daemon event=%s\n", event);
270 result = PyObject_CallFunction(JobEnd_method, obj_fmt, jcr->Python_job);
271 jcr->event[0] = 0; /* no event in progress */
272 if (result == NULL) {
273 if (PyErr_Occurred()) {
275 Dmsg2(000, "Python JobEnd error. job=%p JobId=%d\n", jcr->Python_job,
277 JobEnd_method = NULL;
279 Jmsg(jcr, M_ERROR, 0, _("Python function \"%s\" not found.\n"), event);
283 } else if (strcmp(event, "Exit") == 0) {
288 result = PyObject_CallFunction(Exit_method, NULL);
289 if (result == NULL) {
294 Jmsg1(jcr, M_ABORT, 0, _("Unknown Python daemon event %s\n"), event);
299 Py_XDECREF((PyObject *)jcr->Python_job);
300 jcr->Python_job = NULL;
301 Py_XDECREF((PyObject *)jcr->Python_events);
302 jcr->Python_events = NULL;
308 // PyEval_ReleaseLock();
312 static brwlock_t python_rwlock;
314 static void init_python_lock()
317 if ((errstat=rwl_init(&python_rwlock)) != 0) {
319 Emsg1(M_ABORT, 0, _("Unable to initialize the Python lock. ERR=%s\n"),
320 be.bstrerror(errstat));
325 static void term_python_lock()
327 rwl_destroy(&python_rwlock);
330 /* This applies to a drive and to Volumes */
334 if ((errstat=rwl_writelock(&python_rwlock)) != 0) {
336 Emsg2(M_ABORT, 0, "Python rwl_writelock failure. stat=%d: ERR=%s\n",
337 errstat, be.bstrerror(errstat));
344 if ((errstat=rwl_writeunlock(&python_rwlock)) != 0) {
346 Emsg2(M_ABORT, 0, "Python rwl_writeunlock failure. stat=%d: ERR=%s\n",
347 errstat, be.bstrerror(errstat));
354 * No Python configured -- create external entry points and
355 * dummy routines so that library code can call this without
356 * problems even if it is not configured.
358 int generate_daemon_event(JCR *jcr, const char *event) { return 0; }
360 #endif /* HAVE_PYTHON */