Exemplo n.º 1
0
sqlite3* _open_db(range_request* rr)
{
    char * sqlite_db_path;
    sqlite3* db;
    sqlite3_stmt* stmt;
    libcrange* lr = range_request_lr(rr);
    int err;

    /* open the db */
    if (!(db = libcrange_get_cache(lr, "sqlite:nodes"))) {
        sqlite_db_path = libcrange_getcfg(lr, "sqlitedb");
        if (!sqlite_db_path) sqlite_db_path = DEFAULT_SQLITE_DB;

        err = sqlite3_open(sqlite_db_path, &db);
        if (err != SQLITE_OK) {
            return NULL;
        }
        assert(err == SQLITE_OK);
        /* set mmap pragma */
        err = sqlite3_prepare(db, MMAP_PRAGMA_SQL, strlen(MMAP_PRAGMA_SQL), &stmt, NULL);
        if (err != SQLITE_OK) {
            range_request_warn(rr, "allclusters(): cannot query sqlite db");
            return NULL;
        }
        assert(err == SQLITE_OK);
        while(sqlite3_step(stmt) == SQLITE_ROW) {
            // do nothing. Is this even necessary for the mmap_size pragma? docs are unclear
        }
        /* end mmap pragma setup */

        libcrange_set_cache(lr, "sqlite:nodes", db);
    }

    return db;
}
Exemplo n.º 2
0
MDBM * _open_mdbm(range_request* rr)
{
  const char * mdbm_db_path;
  if (!mdbm_cache) {
    libcrange* lr = range_request_lr(rr);
    mdbm_db_path = libcrange_getcfg(lr, "mdbmdb");
    if (!mdbm_db_path) mdbm_db_path = DEFAULT_MDBM_DB;

    mdbm_cache = mdbm_open(mdbm_db_path, MDBM_O_RDONLY, 0, 0, 0);
    if (!mdbm_cache) { range_request_warn(rr, "cannot open mdbm"); }
    assert(mdbm_cache);
  }
  return mdbm_cache;
}
Exemplo n.º 3
0
range* rangefunc_group(range_request* rr, range** r)
{
    range* ret;
    const char** members;
    int i, err;
    sqlite3* db;
    sqlite3_stmt* tag_stmt;
    sqlite3_stmt* all_nodes_stmt;
    apr_pool_t* pool = range_request_pool(rr);
    libcrange* lr = range_request_lr(rr);
    
    ret = range_new(rr);
    members = range_get_hostnames(pool, r[0]);

    if (!(db = libcrange_get_cache(lr, "sqlite:nodes"))) {
	const char* sqlite_db_path = libcrange_getcfg(lr, "sqlitedb");
	if (!sqlite_db_path) sqlite_db_path = DEFAULT_SQLITE_DB;

	err = sqlite3_open(sqlite_db_path, &db);
	if (err != SQLITE_OK) {
	    fprintf(stderr, "%s: %s\n", sqlite_db_path, sqlite3_errmsg(db));
	    return ret;
	}

	libcrange_set_cache(lr, "sqlite:nodes", db);
    }


    /* prepare our selects */
    err = sqlite3_prepare(db, ALL_NODES_SQL, strlen(ALL_NODES_SQL),
                          &all_nodes_stmt, NULL);
    if (err != SQLITE_OK) {
        fprintf(stderr, "%s: %s\n", ALL_NODES_SQL, sqlite3_errmsg(db));
        abort();
    }

    err = sqlite3_prepare(db, RANGE_FROM_TAGS, strlen(RANGE_FROM_TAGS),
                          &tag_stmt, NULL);
    assert(err == SQLITE_OK);

    /* for each group */
    for (i = 0; members[i]; ++i) {
        sqlite3_stmt* stmt;
        if (strcmp(members[i], "ALL") == 0) {
            stmt = all_nodes_stmt;
        } else {
            stmt = tag_stmt;
            /* bind the current group name */
            sqlite3_bind_text(tag_stmt, 1, members[i], strlen(members[i]), SQLITE_STATIC);
        }

        while (sqlite3_step(stmt) == SQLITE_ROW) {
            range* this_group;
            const char* result = (const char*)sqlite3_column_text(stmt, 0);
            if (stmt == all_nodes_stmt) {
                range_add(ret, result);
            } else {
                this_group = do_range_expand(rr, result);
                set_union_inplace(ret->nodes, this_group->nodes);
            }
        }
        sqlite3_reset(stmt);
    }
    sqlite3_finalize(all_nodes_stmt);
    sqlite3_finalize(tag_stmt);

    return ret;
}
Exemplo n.º 4
0
int add_functions_from_pythonmodule(libcrange* lr, apr_pool_t* pool,
                                  set* pythonfunctions,
                                  const char* module, const char* prefix)
{
    const char** exported_functions;
    const char** p;
    const char* module_copy = apr_pstrdup(pool, module);
    const char *python_inc_path = 0;
    static int python_interp = 0;
    PyObject *pModuleName;
    PyObject *pModule;

    // bootstrap the python interpreter if it hasn't been done yet
    if (!python_interp) {
      Py_Initialize();
      // Set up some basic stuff to run the plugins
      PyRun_SimpleString(PYTHON_BOOT);

      python_inc_path = libcrange_getcfg(lr, "python_inc_path");
      if (python_inc_path) {
        // FIXME this doesn't work properly
        // PyImport_ImportModule(module) returns NULL unless a .pyc has been generated
        // after the .pyc is generated things are OK, but only if PYTHONPATH is also set
        PyObject * pMain = PyImport_AddModule("__main__");
        PyObject * pPyIncPath;
        PyObject * pLibcrangeLoadFile;
        pLibcrangeLoadFile = PyObject_GetAttrString(pMain, "libcrange_load_file");
        pPyIncPath = PyString_FromString(python_inc_path);
        PyObject_CallFunctionObjArgs(pLibcrangeLoadFile, pMain, pPyIncPath, NULL); // return None

        Py_DECREF(pLibcrangeLoadFile);
        Py_DECREF(pPyIncPath);
      }
    }

    // import this particular module
    pModule = PyImport_ImportModule(module);
    if (pModule == NULL) {
      printf("ERR: pModule == NULL, module: %s, prefix: %s\n", module, prefix);
      return 0;
    }

    {
      // insert pModule into globals()[module]
      PyObject * pMain = PyImport_AddModule("__main__");
      PyObject_SetAttrString(pMain, module, pModule);
      // pMain borrowed reference, no decref
    }

    /* get the list of functions exported by this module */
    p = exported_functions = get_and_load_exported_functions(lr, pool,
                                                    module, prefix);

    while (*p) {
      /* add functions to the set seen by libcrange */
      set_add(pythonfunctions, *p, (void*)module_copy);
      ++p;
    }

    return 0;
}