4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @desc Implement resource API. Initialization routines.
26 #include <dbus/dbus.h>
32 #include "datausage-foreach.h"
33 #include "datausage-quota.h"
34 #include "datausage-reset.h"
35 #include "datausage-restriction.h"
39 static void __attribute__ ((constructor)) librsml_initialize(void);
40 static void __attribute__ ((destructor)) librsml_deinitialize(void);
42 static sqlite3 *database;
45 static void librsml_initialize(void)
47 _D("librsml_initialize");
49 if (dbus_threads_init_default() != TRUE)
50 _E("Failed to initialize dbus threads support");
53 #define SQLITE_BUSY_TIMEOUT 500000
55 static int resourced_db_busy(void UNUSED *user, int attempts)
57 _E("DB locked by another process, attempts number %d", attempts);
59 usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
63 API void libresourced_db_initialize_once(void)
69 _D("libresourced_db_initialize_once");
71 res = sqlite3_open(DATABASE_FULL_PATH, &database);
72 if (res != SQLITE_OK) {
73 _D("Can't open database %s: %s\n", DATABASE_FULL_PATH,
74 sqlite3_errmsg(database));
75 sqlite3_close(database);
79 /* Set how many times we'll repeat our attempts for sqlite_step */
80 if (sqlite3_busy_handler(database, resourced_db_busy, NULL) != SQLITE_OK) {
81 _E("Couldn't set busy handler!");
85 static void librsml_deinitialize(void)
87 finalize_datausage_reset();
88 finalize_datausage_foreach();
89 finalize_datausage_restriction();
90 sqlite3_close(database);
93 API sqlite3 *resourced_get_database(void)
95 libresourced_db_initialize_once();