Ejemplo n.º 1
0
static char
query_handler(char *query_name, char *myq)
{

    if(myq == NULL) {
        myq = "SELECT NULL";
    }
    if(query_name == NULL) {
        query_name = "noname";
    }
    if(query_name == "") {
        query_name = "noname";
    }
    dbi_initialize(NULL);
    conn = dbi_conn_new("mysql");
    dbi_conn_set_option(conn, "host", getenv("DB_HOSTNAME"));
    dbi_conn_set_option(conn, "username", getenv("DB_USERNAME"));
    dbi_conn_set_option(conn, "password", getenv("DB_PASSWORD"));
    dbi_conn_set_option(conn, "dbname", getenv("DB_DATABASE"));
    dbi_conn_set_option(conn, "encoding", "UTF-8");
    if (dbi_conn_connect(conn) < 0) {
        printf("Could not connect. Please check the option settings and if the" \
            "specific driver is available\n");
    } else {
        result = dbi_conn_queryf(conn, myq, threshold);
        if (result) {
        xmlAddChild(sql_node,query_doc(result,query_name));
        dbi_result_free(result);
        }
        dbi_conn_close(conn);
    }
    return 0;

}
Ejemplo n.º 2
0
/* The following function is responsible for initializing a connection
 */
static rsRetVal initConn(instanceData *pData, int bSilent)
{
	DEFiRet;
	int iDrvrsLoaded;

	ASSERT(pData != NULL);
	ASSERT(pData->conn == NULL);

	if(bDbiInitialized == 0) {
		/* we need to init libdbi first */
#		ifdef HAVE_DBI_R
		iDrvrsLoaded = dbi_initialize_r((char*) pData->dbiDrvrDir, &dbiInst);
#		else
		iDrvrsLoaded = dbi_initialize((char*) pData->dbiDrvrDir);
#		endif
		if(iDrvrsLoaded == 0) {
			errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi or libdbi drivers not present on this system - suspending.");
			ABORT_FINALIZE(RS_RET_SUSPENDED);
		} else if(iDrvrsLoaded < 0) {
			errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi could not be "
				"initialized (do you have any dbi drivers installed?) - suspending.");
			ABORT_FINALIZE(RS_RET_SUSPENDED);
		}
		bDbiInitialized = 1; /* we are done for the rest of our existence... */
	}

#	ifdef HAVE_DBI_R
	pData->conn = dbi_conn_new_r((char*)pData->drvrName, dbiInst);
#	else
	pData->conn = dbi_conn_new((char*)pData->drvrName);
#	endif
	if(pData->conn == NULL) {
		errmsg.LogError(0, RS_RET_SUSPENDED, "can not initialize libdbi connection");
		ABORT_FINALIZE(RS_RET_SUSPENDED);
	} else { /* we could get the handle, now on with work... */
		/* Connect to database */
		dbi_conn_set_option(pData->conn, "host",     (char*) pData->host);
		dbi_conn_set_option(pData->conn, "username", (char*) pData->usrName);
		dbi_conn_set_option(pData->conn, "dbname",   (char*) pData->dbName);
		if(pData->pwd != NULL)
			dbi_conn_set_option(pData->conn, "password", (char*) pData->pwd);
		if(dbi_conn_connect(pData->conn) < 0) {
			reportDBError(pData, bSilent);
			closeConn(pData); /* ignore any error we may get */
			ABORT_FINALIZE(RS_RET_SUSPENDED);		
		}
		pData->txSupport = dbi_conn_cap_get(pData->conn, "transaction_support");
	}

finalize_it:
	RETiRet;
}
Ejemplo n.º 3
0
/* Connect to the MYSQL database */
static logsql_opendb_ret log_sql_dbi_connect(server_rec *s, logsql_dbconnection *db)
{
    const char *driver = apr_table_get(db->parms,"driver");
	const char *host = apr_table_get(db->parms,"hostname");
	const char *user = apr_table_get(db->parms,"username");
	const char *passwd = apr_table_get(db->parms,"password");
	const char *database = apr_table_get(db->parms,"database");
	const char *s_tcpport = apr_table_get(db->parms,"port");
	unsigned int tcpport = (s_tcpport)?atoi(s_tcpport):0;
	const char *socketfile = apr_table_get(db->parms,"socketfile");
    //dbi_result result;
    dbi_conn_rec *dblink = db->handle;
    if (!dblink) {
        dblink = apr_pcalloc(db->p, sizeof(*dblink));
        db->handle = (void *)dblink;
    }

	dblink->conn = dbi_conn_new(driver);

    dbi_conn_set_option(dblink->conn, "host", host);
    dbi_conn_set_option(dblink->conn, "username", user);
    dbi_conn_set_option(dblink->conn, "password", passwd);
    dbi_conn_set_option(dblink->conn, "dbname", database);
    if (tcpport) {
        dbi_conn_set_option_numeric(dblink->conn, "port", tcpport);
    }

	if (socketfile && !strcmp(driver,"mysql")) {
		dbi_conn_set_option(dblink->conn, "mysql_unix_socket", socketfile);
	}
    
	if (!dbi_conn_connect(dblink->conn)) {
		log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
            "HOST: '%s' PORT: '%d' DB: '%s' USER: '******' SOCKET: '%s'",
			host, tcpport, database, user, socketfile);
		return LOGSQL_OPENDB_SUCCESS;
	} else {
        const char *error;
        dbi_conn_error(dblink->conn, &error);
		log_error(APLOG_MARK, APLOG_ERR, 0, s,
            "DBI Error: %s", error);
		log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
            "HOST: '%s' PORT: '%d' DB: '%s' USER: '******' SOCKET: '%s'",
			host, tcpport, database, user, socketfile);
		return LOGSQL_OPENDB_FAIL;
	}
}
Ejemplo n.º 4
0
static int _sql_setparam(struct sql_table_helper* th,char* key, char* value) {
    char* dbi_errstr=NULL;
    dbi_driver driver;
    /* if not connected */
    if (! th->conn) {
        /* initialize some stuff */
        th->table_next=th->table_start;
        th->result=NULL;
        th->connected=0;
        /* initialize db */
        if (getenv("RRDDEBUGSQL")) {
            fprintf(stderr,"RRDDEBUGSQL: %li: initialize libDBI\n",time(NULL) );
        }
        dbi_initialize(NULL);
        /* load the driver */
        driver=dbi_driver_open(th->dbdriver);
        if (! driver) {
            rrd_set_error( "libdbi - no such driver: %s (possibly a dynamic link problem of the driver being linked without -ldbi)",th->dbdriver);
            return -1;
        }
        /* and connect to driver */
        th->conn=dbi_conn_open(driver);
        /* and handle errors */
        if (! th->conn) {
            rrd_set_error( "libdbi - could not open connection to driver %s",th->dbdriver);
            dbi_shutdown();
            return -1;
        }
    }
    if (th->connected) {
        rrd_set_error( "we are already connected - can not set parameter %s=%s",key,value);
        _sql_close(th);
        return -1;
    }
    if (getenv("RRDDEBUGSQL")) {
        fprintf(stderr,"RRDDEBUGSQL: %li: setting option %s to %s\n",time(NULL),key,value );
    }
    if (strcmp(key, "port") == 0) {
        if (dbi_conn_set_option_numeric(th->conn,key,atoi(value))) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %d - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    } else {
        if (dbi_conn_set_option(th->conn,key,value)) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %s - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    }
    return 0;
}
Ejemplo n.º 5
0
int main() {
	dbi_conn conn;
	dbi_result result;
	int v;

	dbi_initialize(NULL);
	conn = dbi_conn_new("pgsql");

	dbi_conn_set_option(conn, "host", "localhost");
	dbi_conn_set_option(conn, "username", "mud");
	dbi_conn_set_option(conn, "dbname", "mud");

	if ( (v = dbi_conn_connect(conn)) < 0 ) {
		const char* p;

		dbi_conn_error(conn, &p);
		printf(" :: %s\n", p);
		return 0;
	}
	else printf("%d\n", v);
	result = dbi_conn_query(conn, "select * from account");
}
Ejemplo n.º 6
0
static void
set_options(dbi_conn conn, const PairVec& options)
{
    for (auto option : options)
    {
        auto opt = option.first.c_str();
        auto val = option.second.c_str();
        auto result = dbi_conn_set_option(conn, opt, val);
        if (result < 0)
        {
            const char *msg = nullptr;
            int err = dbi_conn_error(conn, &msg);
            PERR("Error setting %s option to %s: %s", opt, val, msg);
            throw std::runtime_error(msg);
        }
    }
}
Ejemplo n.º 7
0
void swd::database::connect(const std::string& driver, const std::string& host,
 const std::string& port, const std::string& username, const std::string& password,
 const std::string& name, const std::string& encoding) {
#if defined(HAVE_DBI_NEW)
    dbi_initialize_r(NULL, &instance_);
    conn_ = dbi_conn_new_r(driver.c_str(), instance_);
#else
    dbi_initialize(NULL);
    conn_ = dbi_conn_new(driver.c_str());
#endif

    dbi_conn_set_option(conn_, "host", host.c_str());
    dbi_conn_set_option(conn_, "port", port.c_str());
    dbi_conn_set_option(conn_, "username", username.c_str());
    dbi_conn_set_option(conn_, "password", password.c_str());
    dbi_conn_set_option(conn_, "dbname", name.c_str());
    dbi_conn_set_option(conn_, "encoding", encoding.c_str());

    /* If the initial connection can not be established the process is shut down. */
    if (dbi_conn_connect(conn_) < 0) {
        throw swd::exceptions::core_exception("Can't connect to database server");
    }
}
Ejemplo n.º 8
0
dbi_conn open_database(const char *dbtype, const char *dbhost, const char *dbport, const char *dbname, const char *dbuser, const char *dbpasswd)
{
  dbi_conn conn;

  dbi_initialize(NULL);
  conn = dbi_conn_new(dbtype);
  if ( ! conn ) { LOG(LOG_ERR, "Cannot start a connection with driver %s", dbtype); }

  dbi_conn_set_option(conn, "host", dbhost);
  dbi_conn_set_option(conn, "port", dbport);
  dbi_conn_set_option(conn, "username", dbuser);
  dbi_conn_set_option(conn, "password", dbpasswd);
  dbi_conn_set_option(conn, "dbname", dbname);
  dbi_conn_set_option(conn, "encoding", "UTF-8");

  if (dbi_conn_connect(conn) < 0) {
    const char *errmsg;
    dbi_conn_error(conn, &errmsg);
    LOG(LOG_ERR, "%s dbhost=%s,dbport=%d,dbname=%s,dbuser=%s,dbpasswd=%s",
           errmsg, dbhost, dbport, dbname, dbuser, dbpasswd);
    return(NULL);
  }
  return(conn);
}
Ejemplo n.º 9
0
static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
{
  dbi_driver driver;
  dbi_conn connection;
  size_t i;
  int status;

  if (db->connection != NULL)
  {
    status = dbi_conn_ping (db->connection);
    if (status != 0) /* connection is alive */
      return (0);

    dbi_conn_close (db->connection);
    db->connection = NULL;
  }

  driver = dbi_driver_open_r (db->driver, dbi_instance);
  if (driver == NULL)
  {
    ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
        db->driver);
    INFO ("dbi plugin: Maybe the driver isn't installed? "
        "Known drivers are:");
    for (driver = dbi_driver_list_r (NULL, dbi_instance);
        driver != NULL;
        driver = dbi_driver_list_r (driver, dbi_instance))
    {
      INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
    }
    return (-1);
  }

  connection = dbi_conn_open (driver);
  if (connection == NULL)
  {
    ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
        db->driver);
    return (-1);
  }

  /* Set all the driver options. Because this is a very very very generic
   * interface, the error handling is kind of long. If an invalid option is
   * encountered, it will get a list of options understood by the driver and
   * report that as `INFO'. This way, users hopefully don't have too much
   * trouble finding out how to configure the plugin correctly.. */
  for (i = 0; i < db->driver_options_num; i++)
  {
    if (db->driver_options[i].is_numeric)
    {
      status = dbi_conn_set_option_numeric (connection,
          db->driver_options[i].key, db->driver_options[i].value.numeric);
      if (status != 0)
      {
        char errbuf[1024];
        ERROR ("dbi plugin: cdbi_connect_database (%s): "
            "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
            db->name,
            db->driver_options[i].key, db->driver_options[i].value.numeric,
            cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      }
    }
    else
    {
      status = dbi_conn_set_option (connection,
          db->driver_options[i].key, db->driver_options[i].value.string);
      if (status != 0)
      {
        char errbuf[1024];
        ERROR ("dbi plugin: cdbi_connect_database (%s): "
            "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
            db->name,
            db->driver_options[i].key, db->driver_options[i].value.string,
            cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      }
    }

    if (status != 0)
    {
      char const *opt;

      INFO ("dbi plugin: This is a list of all options understood "
          "by the `%s' driver:", db->driver);
      for (opt = dbi_conn_get_option_list (connection, NULL);
          opt != NULL;
          opt = dbi_conn_get_option_list (connection, opt))
      {
        INFO ("dbi plugin: * %s", opt);
      }

      dbi_conn_close (connection);
      return (-1);
    }
  } /* for (i = 0; i < db->driver_options_num; i++) */

  status = dbi_conn_connect (connection);
  if (status != 0)
  {
    char errbuf[1024];
    ERROR ("dbi plugin: cdbi_connect_database (%s): "
        "dbi_conn_connect failed: %s",
        db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
    dbi_conn_close (connection);
    return (-1);
  }

  if (db->select_db != NULL)
  {
    status = dbi_conn_select_db (connection, db->select_db);
    if (status != 0)
    {
      char errbuf[1024];
      WARNING ("dbi plugin: cdbi_connect_database (%s): "
          "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
          db->name, db->select_db,
          cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      dbi_conn_close (connection);
      return (-1);
    }
  }

  db->connection = connection;
  return (0);
} /* }}} int cdbi_connect_database */
Ejemplo n.º 10
0
static gboolean
afsql_dd_ensure_initialized_connection(AFSqlDestDriver *self)
{
  if (self->dbi_ctx)
    return TRUE;

  self->dbi_ctx = dbi_conn_new(self->type);

  if (!self->dbi_ctx)
    {
      msg_error("No such DBI driver",
                evt_tag_str("type", self->type),
                NULL);
      return FALSE;
    }

  dbi_conn_set_option(self->dbi_ctx, "host", self->host);

  if (strcmp(self->type, "mysql"))
    dbi_conn_set_option(self->dbi_ctx, "port", self->port);
  else
    dbi_conn_set_option_numeric(self->dbi_ctx, "port", atoi(self->port));

  dbi_conn_set_option(self->dbi_ctx, "username", self->user);
  dbi_conn_set_option(self->dbi_ctx, "password", self->password);
  dbi_conn_set_option(self->dbi_ctx, "dbname", self->database);
  dbi_conn_set_option(self->dbi_ctx, "encoding", self->encoding);
  dbi_conn_set_option(self->dbi_ctx, "auto-commit", self->flags & AFSQL_DDF_EXPLICIT_COMMITS ? "false" : "true");

  /* database specific hacks */
  dbi_conn_set_option(self->dbi_ctx, "sqlite_dbdir", "");
  dbi_conn_set_option(self->dbi_ctx, "sqlite3_dbdir", "");

  /* Set user-specified options */
  g_hash_table_foreach(self->dbd_options, afsql_dd_set_dbd_opt, self->dbi_ctx);
  g_hash_table_foreach(self->dbd_options_numeric, afsql_dd_set_dbd_opt_numeric, self->dbi_ctx);

  if (dbi_conn_connect(self->dbi_ctx) < 0)
    {
      const gchar *dbi_error;

      dbi_conn_error(self->dbi_ctx, &dbi_error);

      msg_error("Error establishing SQL connection",
                evt_tag_str("type", self->type),
                evt_tag_str("host", self->host),
                evt_tag_str("port", self->port),
                evt_tag_str("username", self->user),
                evt_tag_str("database", self->database),
                evt_tag_str("error", dbi_error),
                NULL);

      return FALSE;
    }

  if (self->session_statements != NULL)
    {
      GList *l;

      for (l = self->session_statements; l; l = l->next)
        {
          if (!afsql_dd_run_query(self, (gchar *) l->data, FALSE, NULL))
            {
              msg_error("Error executing SQL connection statement",
                        evt_tag_str("statement", (gchar *) l->data),
                        NULL);

              return FALSE;
            }
        }
    }

  return TRUE;
}
Ejemplo n.º 11
0
static void
afsql_dd_set_dbd_opt(gpointer key, gpointer value, gpointer user_data)
{
  dbi_conn_set_option((dbi_conn)user_data, (gchar *)key, (gchar *)value);
}
Ejemplo n.º 12
0
dbi_result _db_rawquery(const char *file, int line, database *db, int log_dupes, char *qry)
{
    dbi_result result;
    const char *errmsg;
    int tries = 0;

    if (debug > 4) {
        LOGRAW(LOG_DEBUG, qry);
    }

    if (!dbi_conn_ping(db->conn)) {
        if (db->conn) dbi_conn_close(db->conn);
        db->conn = dbi_conn_new(db->driver);
        if (db->conn == NULL) {
            perror(db->driver);
            exit(1);
        }

        dbi_conn_set_option(db->conn, "host", db->host);
        dbi_conn_set_option(db->conn, "port", db->port);
        dbi_conn_set_option(db->conn, "username", db->username);
        dbi_conn_set_option(db->conn, "password", db->password);
        dbi_conn_set_option(db->conn, "dbname", db->name);

retry:
        if (++tries == 3) exit(1);
        if (dbi_conn_connect(db->conn) < 0) {
            dbi_conn_error(db->conn, &errmsg);
            _logsrce = file;
            _logline = line;
            _LOG(LOG_ERR, "%s connection failed to %s:%s:%s %s", db->driver, db->host, db->port, db->name, errmsg);
            sleep(3);
            goto retry;
        }

        {
            char versionstring[VERSIONSTRING_LENGTH];
            dbi_driver driver;

            driver = dbi_conn_get_driver(db->conn);
            dbi_conn_get_engine_version_string(db->conn, versionstring);
            LOG(LOG_INFO, "using driver: %s, version %s, compiled %s", dbi_driver_get_filename(driver), dbi_driver_get_version(driver),
                dbi_driver_get_date_compiled(driver));
            LOG(LOG_INFO, "connected to %s:%s:%s, server version %s", db->host, db->port, db->name, versionstring);
        }
    }

    if (debug > 2) {
        char *src, *dst, buf[4096];

        for (dst = buf, src = qry; *src; src++, dst++) {
            if (*src == '%') *dst++ = '%';
            *dst = *src;
        }
        *dst = 0;
        LOG(LOG_INFO, buf);
    }
    result = dbi_conn_query(db->conn, qry);
    if (result == NULL) {
        int ret = dbi_conn_error(db->conn, &errmsg);
        _logsrce = file;
        _logline = line;
        _LOG(LOG_ERR, "query failed: %s (%s)", errmsg, qry);
        if (ret == DBI_ERROR_NOCONN) {
            dbi_conn_close(db->conn);
            db->conn = NULL;
            sleep(3);
            goto retry;
        }
    }
    return(result);
}
Ejemplo n.º 13
0
/**
 * Parse command line options, create connection object,
 * start the connection and finally create database schema
 *
 * @return DBI connection handle
 */
dbi_conn *tagsistant_db_connection(int start_transaction)
{
	/* DBI connection handler used by subsequent calls to dbi_* functions */
	dbi_conn dbi = NULL;

	if (start_transaction) {
		g_rw_lock_writer_lock(&(tagsistant_query_rwlock));
	} else {
		g_rw_lock_reader_lock(&(tagsistant_query_rwlock));
	}

	/* lock the pool */
	g_mutex_lock(&tagsistant_connection_pool_lock);

	GList *pool = tagsistant_connection_pool;
	while (pool) {
		dbi = (dbi_conn) pool->data;

		/* check if the connection is still alive */
		if (!dbi_conn_ping(dbi) && dbi_conn_connect(dbi) < 0) {
			dbi_conn_close(dbi);
			tagsistant_connection_pool = g_list_delete_link(tagsistant_connection_pool, pool);
			connections--;
		} else {
			tagsistant_connection_pool = g_list_remove_link(tagsistant_connection_pool, pool);
			g_list_free_1(pool);
			break;
		}

		pool = pool->next;
	}

	/*
	 * unlock the pool mutex only if the backend is not SQLite
	 */
	g_mutex_unlock(&tagsistant_connection_pool_lock);

	if (!dbi) {
		// initialize DBI drivers
		if (TAGSISTANT_DBI_MYSQL_BACKEND == dboptions.backend) {
			if (!tagsistant_driver_is_available("mysql")) {
				fprintf(stderr, "MySQL driver not installed\n");
				dbg('s', LOG_ERR, "MySQL driver not installed");
				exit (1);
			}

			// unlucky, MySQL does not provide INTERSECT operator
			tagsistant.sql_backend_have_intersect = 0;

			// create connection
#if TAGSISTANT_REENTRANT_DBI
			dbi = dbi_conn_new_r("mysql", tagsistant.dbi_instance);
#else
			dbi = dbi_conn_new("mysql");
#endif
			if (NULL == dbi) {
				dbg('s', LOG_ERR, "Error creating MySQL connection");
				exit (1);
			}

			// set connection options
			dbi_conn_set_option(dbi, "host",     dboptions.host);
			dbi_conn_set_option(dbi, "dbname",   dboptions.db);
			dbi_conn_set_option(dbi, "username", dboptions.username);
			dbi_conn_set_option(dbi, "password", dboptions.password);
			dbi_conn_set_option(dbi, "encoding", "UTF-8");

		} else if (TAGSISTANT_DBI_SQLITE_BACKEND == dboptions.backend) {
			if (!tagsistant_driver_is_available("sqlite3")) {
				fprintf(stderr, "SQLite3 driver not installed\n");
				dbg('s', LOG_ERR, "SQLite3 driver not installed");
				exit(1);
			}

			// create connection
#if TAGSISTANT_REENTRANT_DBI
			dbi = dbi_conn_new_r("sqlite3", tagsistant.dbi_instance);
#else
			dbi = dbi_conn_new("sqlite3");
#endif
			if (NULL == dbi) {
				dbg('s', LOG_ERR, "Error connecting to SQLite3");
				exit (1);
			}

			// set connection options
			dbi_conn_set_option(dbi, "dbname", "tags.sql");
			dbi_conn_set_option(dbi, "sqlite3_dbdir", tagsistant.repository);

		} else {

			dbg('s', LOG_ERR, "No or wrong database family specified!");
			exit (1);
		}

		// try to connect
		if (dbi_conn_connect(dbi) < 0) {
			int error = dbi_conn_error(dbi, NULL);
			dbg('s', LOG_ERR, "Could not connect to DB (error %d). Please check the --db settings", error);
			exit(1);
		}

		connections++;

		dbg('s', LOG_INFO, "SQL connection established");
	}

	/* start a transaction */
	if (start_transaction) {
#if TAGSISTANT_USE_INTERNAL_TRANSACTIONS
		switch (tagsistant.sql_database_driver) {
			case TAGSISTANT_DBI_SQLITE_BACKEND:
				tagsistant_query("begin transaction", dbi, NULL, NULL);
				break;

			case TAGSISTANT_DBI_MYSQL_BACKEND:
				tagsistant_query("start transaction", dbi, NULL, NULL);
				break;
		}
#else
		dbi_conn_transaction_begin(dbi);
#endif
	}

	return(dbi);
}
Ejemplo n.º 14
0
STATUS spacestate_init(void)
{
	configuration *conf = get_modifiable_conf();
	GError *error = NULL;
	struct sigaction sa;
	dbi_inst dbi_instance = 0;

	/* Establish SIGCHLD handler. */
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = child_handler;

	sigaction(SIGCHLD, &sa, NULL);


	conf->spacestate_host = g_key_file_get_string(conf->keyfile, "spacestate",
	                                              "host", &error);
	if (error) {
		fprintf(stderr, "No spacestate host supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_name = g_key_file_get_string(conf->keyfile, "spacestate",
	                                              "name", &error);
	if (error) {
		fprintf(stderr, "No spacestate name supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_driver = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                "driver", &error);
	if (error) {
		fprintf(stderr, "No spacestate driver supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_username = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "username", &error);
	if (error) {
		fprintf(stderr, "No spacestate username supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_password = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "password", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_hook_open = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "open script", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_hook_close = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "close script", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}



	conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1);
	conf->event_handlers[conf->event_handler_cnt] = spacestate_update;
	conf->event_handler_cnt++;

	DEBUG(1, "Setting properties to %s space state database %s at %s as user %s", conf->spacestate_driver,
		conf->spacestate_name, conf->spacestate_host, conf->spacestate_username);

	dbi_initialize_r(NULL, &dbi_instance);
	conn = dbi_conn_new_r(conf->spacestate_driver, &dbi_instance);
	dbi_conn_set_option(conn, "host", conf->spacestate_host);
	dbi_conn_set_option(conn, "username", conf->spacestate_username);
	dbi_conn_set_option(conn, "password", conf->spacestate_password);
	dbi_conn_set_option(conn, "dbname", conf->spacestate_name);
	dbi_conn_set_option(conn, "encoding", "UTF-8");

	return ST_OK;
}
Ejemplo n.º 15
0
Archivo: dbi.c Proyecto: Bradan/gammu
/* Connects to database */
static GSM_Error SMSDDBI_Connect(GSM_SMSDConfig * Config)
{
	int rc;
	struct GSM_SMSDdbobj *db = Config->db;

	rc = dbi_initialize(Config->driverspath);

	if (rc == 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI did not find any drivers, try using DriversPath option");
		dbi_shutdown();
		return ERR_DB_DRIVER;
	} else if (rc < 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to initialize!");
		return ERR_DB_DRIVER;
	}

	Config->conn.dbi = dbi_conn_new(Config->driver);
	if (Config->conn.dbi == NULL) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to init %s driver!", Config->driver);
		dbi_shutdown();
		return ERR_DB_DRIVER;
	} else {
		SMSD_Log(DEBUG_SQL, Config, "Using DBI driver '%s'", dbi_driver_get_name(dbi_conn_get_driver(Config->conn.dbi)));
	}

	dbi_conn_error_handler(Config->conn.dbi, SMSDDBI_Callback, Config);

	if (dbi_conn_set_option(Config->conn.dbi, "sqlite_dbdir", Config->dbdir) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite_dbdir!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "sqlite3_dbdir", Config->dbdir) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite3_dbdir!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "host", Config->host) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set host!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "username", Config->user) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set username!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "password", Config->password) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set password!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "dbname", Config->database) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set dbname!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "encoding", "UTF-8") != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set encoding!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}

	if (dbi_conn_connect(Config->conn.dbi) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to connect!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONNECT;
	}
	Config->db = db;
	return ERR_NONE;
}
Ejemplo n.º 16
0
static apr_status_t safe_dbi_new_conn(void **resource, void *params,
                                      apr_pool_t * r)
{
    apr_status_t rv = APR_SUCCESS;
    ftpd_dbi_config_rec *conf = params;
    int err_num = 0;
    const char *err_str;
    const char *host = conf->dbi_host;
    const char *driver = conf->dbi_driver;
    const char *name = conf->dbi_name;
    const char *user = conf->dbi_user;
    const char *pwd = conf->dbi_pass;
    ftpd_dbi_rest *myres;

    dbi_conn_count++;

    if (DBI_HARD_MAX_CONNS > dbi_conn_count) {

        ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r,
                      "[mod_ftpd_dbi.c] Creating New DBI Server Connection");

        myres = apr_palloc(r, sizeof(*myres));

        myres->conn = dbi_conn_new(driver);
        if (myres->conn == NULL) {
            ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, r,
                          "[mod_ftpd_dbi.c] DBI Connection Failed. dbi_conn_new returned NULL.");
            rv = !APR_SUCCESS;
            /*
             * modules/ssl/ssl_engine_log.c:103
             *  said this was okay. so i do it.
             */
            exit(1);
        }
        else {
            dbi_conn_set_option(myres->conn, "host", (char *)host);
            dbi_conn_set_option(myres->conn, "username", (char *)user);
            dbi_conn_set_option(myres->conn, "password", (char *)pwd);
            dbi_conn_set_option(myres->conn, "dbname", (char *)name);
            if (dbi_conn_connect(myres->conn) != 0) {
                err_num = dbi_conn_error(myres->conn, (const char **)&err_str);
                /* Connetion Failed */
                ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r,
                              "[mod_ftpd_dbi.c] DBI Connection to %s://%s@%s/%s Failed. Error: (%d) %s",
                              driver, user, host, name, err_num, err_str);
                rv = !APR_SUCCESS;
            }
            else {
                ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, r,
                              "[mod_ftpd_dbi.c] Connection was created sucessfully");
            }
        }
        *resource = myres;
    }
    else {
        /* Error -- we have too many TOTAL DBI Connections. Maybe a Evil User trying to hurt our system? */
        ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r,
                      "[mod_ftpd_dbi.c] DBI Connection Failed. Hard Max Limit of %d Connections has been reached",
                      DBI_HARD_MAX_CONNS);
        /* we didn't create a new connection! */
        dbi_conn_count--;
        rv = !APR_SUCCESS;
    }
    return rv;
}