static bRC newPlugin(bpContext *ctx) { bRC retval = bRC_Error; struct plugin_ctx *p_ctx; p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx)); if (!p_ctx) { return bRC_Error; } memset(p_ctx, 0, sizeof(struct plugin_ctx)); ctx->pContext = (void *)p_ctx; /* set our context pointer */ /* * For each plugin instance we instantiate a new Python interpreter. */ PyEval_AcquireLock(); p_ctx->interpreter = Py_NewInterpreter(); retval = PyLoadModule(ctx); PyEval_ReleaseThread(p_ctx->interpreter); return retval; }
/* * Handle an event that was generated in Bareos */ static bRC handlePluginEvent(bpContext *ctx, bsdEvent *event, void *value) { bRC retval = bRC_Error; bool event_dispatched = false; POOL_MEM plugin_options(PM_FNAME); plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext; if (!p_ctx) { goto bail_out; } /* * First handle some events internally before calling python if it * want to do some special handling on the event triggered. */ switch (event->eventType) { case bsdEventNewPluginOptions: event_dispatched = true; retval = parse_plugin_definition(ctx, value, plugin_options); break; default: break; } /* * See if we have been triggered in the previous switch if not we have to * always dispatch the event. If we already processed the event internally * we only do a dispatch to the python entry point when that internal processing * was successfull (e.g. retval == bRC_OK). */ if (!event_dispatched || retval == bRC_OK) { PyEval_AcquireThread(p_ctx->interpreter); /* * Now dispatch the event to Python. * First the calls that need special handling. */ switch (event->eventType) { case bsdEventNewPluginOptions: /* * See if we already loaded the Python modules. */ if (!p_ctx->python_loaded) { retval = PyLoadModule(ctx, plugin_options.c_str()); } /* * Only try to call when the loading succeeded. */ if (retval == bRC_OK) { retval = PyParsePluginDefinition(ctx, plugin_options.c_str()); } break; default: /* * Handle the generic events e.g. the ones which are just passed on. * We only try to call Python when we loaded the right module until * that time we pretend the call succeeded. */ if (p_ctx->python_loaded) { retval = PyHandlePluginEvent(ctx, event, value); } else { retval = bRC_OK; } break; } PyEval_ReleaseThread(p_ctx->interpreter); } bail_out: return retval; }