示例#1
0
文件: spindly.c 项目: jordanm/spindly
static PyObject *spindly_js(PyObject *self, PyObject *args) {
    JSRuntime *runtime;
    JSContext *context;
    JSObject *global;

    char *script;
    Py_ssize_t script_length;
    PyObject *params = NULL;
    int timeout = 10;

    jsval rvalue;

    int error = 0;
    struct watchdog *wd = NULL;

    if (!PyArg_ParseTuple(args, "s#|Oi:js", &script, &script_length, &params, &timeout)) {
        return NULL;
    }
    if (params != NULL && !PyDict_Check(params)) {
        return PyErr_Format(PyExc_TypeError, "params must be a dict");
    }

    runtime = JS_NewRuntime(1024L * 1024L);
    if (!runtime) {
        return PyErr_Format(PyExc_SystemError, "unable to initialize JS runtime\n");
    }

    context = JS_NewContext(runtime, 8192);
    if (!context) {
        JS_DestroyRuntime(runtime);
        return PyErr_Format(PyExc_SystemError, "unable to initialize JS context\n");
    }

    JS_SetContextPrivate(context, &error);
    JS_SetOptions(context, JSOPTION_VAROBJFIX);
    JS_SetVersion(context, JSVERSION_LATEST);
    JS_SetErrorReporter(context, raise_python_exception);
    JS_SetOperationCallback(context, js_destroy);

    global = JS_NewCompartmentAndGlobalObject(context, &global_class, NULL);
    JS_InitStandardClasses(context, global);

    if (params != NULL) {
        populate_javascript_object(context, global, params);
    }

    if (timeout > 0) {
        wd = run_watchdog(context, timeout);
        if (wd == NULL) {
            shutdown(runtime, context);
            return PyErr_Format(PyExc_SystemError, "unable to initialize JS watchdog\n");
        }
    }

    JSBool retval = JS_EvaluateScript(context, global, script, script_length, "spindly", 1, &rvalue);
    if (wd) {
        shutdown_watchdog(wd);
    }

    if (retval == JS_FALSE || error == 1) {
        shutdown(runtime, context);
        return NULL;
    }

    PyObject *obj = to_python_object(context, rvalue);
    shutdown(runtime, context);
    return obj;
}
示例#2
0
int main(int argc, char **argv) {
    struct sigaction sa;
    char *cmdline_str;
    char **argv_array;
    GMainLoop *loop;

    argv_array = argv_to_array(argc, argv);
    cmdline_str = get_command_line(argv_array);
    set_argv_for_child_process(argv_array);

    cps_config_init(argc, argv);

    if(global_config.daemonize) {
        /* When daemonizing, a child process will be launched with daemonization disabled */
        daemonize();
        run_watchdog(argv_array);
        /* Watchdog should never stop ! */
        exit(EXIT_FAILURE);
    }

    /* The child (or not daemonized process) will continue here. */
    L (LOGLEVEL_WARNING, PACKAGE_NAME "-" PACKAGE_VERSION " starting");
    L (LOGLEVEL_DEBUG, "Command line : %s", cmdline_str);
    g_free(cmdline_str);

    /* Signals initialization {{{ */
    memset (&sa, 0, sizeof (sa));
    sa.sa_sigaction = signal_handler_for_stop;
    sa.sa_flags = SA_SIGINFO;
    sigemptyset(&(sa.sa_mask));
    if(0 != sigaction(SIGTERM, &sa, NULL)) {
        L (LOGLEVEL_CRITICAL, "Could not set signal handler for TERM");
        exit(EXIT_FAILURE);
    }
    if(0 != sigaction(SIGINT, &sa, NULL)) {
        L (LOGLEVEL_CRITICAL, "Could not set signal handler for INT");
        close_all_and_exit(EXIT_FAILURE);
    }

    if(0 != sigaction(SIGQUIT, &sa, NULL)) {
        L (LOGLEVEL_CRITICAL, "Could not set signal handler for QUIT");
        close_all_and_exit(EXIT_FAILURE);
    }

    memset (&sa, 0, sizeof (sa));
    sa.sa_handler = SIG_IGN;
    sa.sa_flags = SA_RESTART;
    sigemptyset(&(sa.sa_mask));
    if(0 != sigaction(SIGPIPE, &sa, NULL)) {
        L (LOGLEVEL_CRITICAL, "Could not set signal handler for PIPE");
        exit(EXIT_FAILURE);
    }

    /* }}} */

    cpsa_init();

    loop = g_main_new(TRUE);
    g_timeout_add_seconds(global_config.agent__interval, cpsa_get_process, NULL);

    g_main_run( loop );

    L (LOGLEVEL_WARNING, PACKAGE_NAME "-" PACKAGE_VERSION " ending");

    exit(EXIT_SUCCESS);
}