- The global variable 'errno' can be contaminated by other libraries.
Change-Id: Iacd03dc7c1d97cec8f80b03d001a52b01620f3f0
Signed-off-by: jusung son <jusung07.son@samsung.com>
#include "backend.h"
#include "log.h"
+#include "buxton_error.h"
static GHashTable *dbs;
gdbm_close(db);
}
-static GDBM_FILE open_gdbm(const char *dbpath, bool readonly)
+static int open_gdbm(const char *dbpath, bool readonly, GDBM_FILE *db)
{
- GDBM_FILE db;
+ GDBM_FILE tmp_db;
char *nm;
int cache_size;
int r;
- assert(dbpath);
+ if (!dbpath || !db) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (!dbs) {
- errno = ENODEV;
- return NULL;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
- db = g_hash_table_lookup(dbs, dbpath);
- if (db)
- return db;
+ tmp_db = g_hash_table_lookup(dbs, dbpath);
+ if (tmp_db) {
+ *db = tmp_db;
+ return BUXTON_ERROR_NONE;
+ }
nm = strdup(dbpath);
if (!nm) {
- errno = ENOMEM;
- return NULL;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
- db = gdbm_open(nm, 0, readonly ? GDBM_READER : GDBM_WRCREAT,
+ tmp_db = gdbm_open(nm, 0, readonly ? GDBM_READER : GDBM_WRCREAT,
S_IRUSR | S_IWUSR, NULL);
- if (!db) {
+ if (!tmp_db) {
bxt_err("Open '%s' failed: %s", dbpath,
gdbm_strerror(gdbm_errno));
- errno = EIO;
free(nm);
- return NULL;
+ return BUXTON_ERROR_IO_ERROR;
}
cache_size = 50;
- r = gdbm_setopt(db, GDBM_CACHESIZE, &cache_size, sizeof(cache_size));
+ r = gdbm_setopt(tmp_db, GDBM_CACHESIZE, &cache_size, sizeof(cache_size));
if (r == -1)
bxt_err("Set option failed: %s", gdbm_strerror(gdbm_errno));
- g_hash_table_insert(dbs, nm, db);
+ g_hash_table_insert(dbs, nm, tmp_db);
bxt_dbg("Open '%s'", dbpath);
- return db;
+ *db = tmp_db;
+ return BUXTON_ERROR_NONE;
}
static int open_db(const char *dbpath, bool readonly)
GDBM_FILE db;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_gdbm(dbpath, readonly);
- if (!db)
- return -1;
-
- return 0;
+ return open_gdbm(dbpath, readonly, &db);
}
static int close_db(const char *dbpath)
GDBM_FILE db;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!dbs) {
- errno = ENODEV;
- return -1;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
db = g_hash_table_lookup(dbs, dbpath);
if (!db)
- return 0;
+ return BUXTON_ERROR_NONE;
g_hash_table_remove(dbs, dbpath);
bxt_info("close '%s'", dbpath);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int remove_db(const char *dbpath)
int r;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!dbs) {
- errno = ENODEV;
- return -1;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
db = g_hash_table_lookup(dbs, dbpath);
r = unlink(dbpath);
if (r == -1) {
bxt_err("Remove '%s' failed: %d", dbpath, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Remove '%s'", dbpath);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int set_value(const char *dbpath, const char *key, const void *data,
datum d_data;
if (!dbpath || !*dbpath || !key || !*key || !data || dlen <= 0) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_gdbm(dbpath, false);
- if (!db)
- return -1;
+ r = open_gdbm(dbpath, false, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
d_key.dptr = (char *)key;
d_key.dsize = strlen(key) + 1;
r = gdbm_store(db, d_key, d_data, GDBM_REPLACE);
if (r) {
- if (gdbm_errno == GDBM_READER_CANT_STORE)
- errno = EROFS;
-
- bxt_err("Set '%s' failed: %s", key,
- gdbm_strerror(gdbm_errno));
-
- return -1;
+ bxt_err("Set '%s' failed: %s", key, gdbm_strerror(gdbm_errno));
+ return BUXTON_ERROR_IO_ERROR;
}
gdbm_sync(db);
bxt_dbg("Set '%s' Key '%s'", dbpath, key);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int get_value(const char *dbpath, const char *key, void **data,
GDBM_FILE db;
datum d_key;
datum d_data;
+ int r;
if (!dbpath || !*dbpath || !key || !*key || !data || !dlen) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_gdbm(dbpath, readonly);
- if (!db)
- return -1;
+ r = open_gdbm(dbpath, readonly, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
d_key.dptr = (char *)key;
d_key.dsize = strlen(key) + 1;
d_data = gdbm_fetch(db, d_key);
- if (d_data.dptr == NULL) {
- errno = ENOENT;
- return -1;
- }
+ if (d_data.dptr == NULL)
+ return BUXTON_ERROR_NOT_EXIST;
*data = d_data.dptr;
*dlen = d_data.dsize;
bxt_dbg("Get '%s' Key '%s'", dbpath, key);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int unset_value(const char *dbpath, const char *key)
datum d_key;
if (!dbpath || !*dbpath || !key || !*key) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_gdbm(dbpath, false);
- if (!db)
- return -1;
+ r = open_gdbm(dbpath, false, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
d_key.dptr = (char *)key;
d_key.dsize = strlen(key) + 1;
r = gdbm_delete(db, d_key);
if (r) {
- switch (gdbm_errno) {
- case GDBM_READER_CANT_DELETE:
- errno = EROFS;
- break;
- case GDBM_ITEM_NOT_FOUND:
- errno = ENOENT;
- break;
- default:
- errno = EIO;
- break;
- }
-
- return -1;
+ if (gdbm_errno == GDBM_ITEM_NOT_FOUND)
+ return BUXTON_ERROR_NOT_EXIST;
+
+ bxt_err("unset '%s' failed: %d", key, gdbm_errno);
+ return BUXTON_ERROR_IO_ERROR;
}
gdbm_sync(db);
bxt_dbg("Unset '%s' Key '%s'", dbpath, key);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int list_keys(const char *dbpath, char ***keys, unsigned int *klen,
GList *list;
GList *l;
datum d_key;
- int i;
+ int i, r;
unsigned int _klen;
char **_keys;
if (!dbpath || !*dbpath || !keys) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_gdbm(dbpath, readonly);
- if (!db)
- return -1;
+ r = open_gdbm(dbpath, readonly, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
_klen = 0;
list = NULL;
_keys = malloc(sizeof(void *) * (_klen + 1));
if (!_keys) {
g_list_free_full(list, (GDestroyNotify)free);
- errno = ENOMEM;
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
for (i = 0, l = list; l && i < _klen; l = g_list_next(l), i++)
bxt_dbg("List '%s'", dbpath);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static void module_exit(void)
dbs = g_hash_table_new_full(g_str_hash, g_str_equal,
(GDestroyNotify)free, (GDestroyNotify)free_db);
if (!dbs) {
- errno = ENOMEM;
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
DEFINE_BUXTON_BACKEND = {
#include "backend.h"
#include "log.h"
+#include "buxton_error.h"
#define BUXTON_DEFAULT_WAL_AUTOCHECKPOINT 100
#define BASE_DB_PATH tzplatform_getenv(TZ_SYS_RO_ETC)
static void free_db(sqlite3 *db)
{
- int errno_backup;
if (!db)
return;
- errno_backup = errno;
- if (sqlite3_close(db) == SQLITE_OK)
- errno = errno_backup;
+ sqlite3_close(db);
}
-static sqlite3 *open_sqlite3(const char *dbpath, bool readonly)
+static int open_sqlite3(const char *dbpath, bool readonly, sqlite3 **db)
{
- sqlite3 *db;
+ sqlite3 *tmp_db;
char *nm;
int r;
bool db_exist = false;
- assert(dbpath);
+ if (!dbpath || !db) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (!dbs) {
- errno = ENODEV;
- return NULL;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
- db = g_hash_table_lookup(dbs, dbpath);
- if (db) {
+ tmp_db = g_hash_table_lookup(dbs, dbpath);
+ if (tmp_db) {
int is_ro;
- is_ro = sqlite3_db_readonly(db, "main");
+ is_ro = sqlite3_db_readonly(tmp_db, "main");
if (is_ro != readonly) {
bxt_dbg("'%s' : readonly[%d], required readonly[%d]",
dbpath, is_ro, readonly);
g_hash_table_remove(dbs, dbpath);
} else {
- return db;
+ *db = tmp_db;
+ return BUXTON_ERROR_NONE;
}
}
- nm = strdup(dbpath);
- if (!nm) {
- errno = ENOMEM;
- return NULL;
- }
-
- if (access(nm, F_OK) == 0)
+ if (access(dbpath, F_OK) == 0)
db_exist = true;
- r = sqlite3_open_v2(dbpath, &db,
+ r = sqlite3_open_v2(dbpath, &tmp_db,
readonly ? SQLITE_OPEN_READONLY :
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (db_exist && r == SQLITE_CORRUPT) {
- bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(db));
+ bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(tmp_db));
if (!strncmp(BASE_DB_PATH, dbpath, strlen(BASE_DB_PATH))) {
- free(nm);
- return NULL;
+ bxt_err("Invalid parameter (%s)", dbpath);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- bxt_err("Try to remove %s and restore to default value.",
- dbpath);
+ bxt_err("Try to remove %s and restore to default value.", dbpath);
if (unlink(dbpath)) {
- free(nm);
- return NULL;
+ bxt_err("Failed to unlink (%s)", dbpath);
+ return BUXTON_ERROR_IO_ERROR;
}
db_exist = false;
- r = sqlite3_open_v2(dbpath, &db,
+ r = sqlite3_open_v2(dbpath, &tmp_db,
readonly ? SQLITE_OPEN_READONLY :
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
}
if (r) {
- bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(db));
- errno = EIO;
- free(nm);
- return NULL;
+ bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(tmp_db));
+ return BUXTON_ERROR_IO_ERROR;
}
if (!db_exist) {
if (strncmp(BASE_DB_PATH, dbpath, strlen(BASE_DB_PATH))) {
- r = sqlite3_exec(db, "PRAGMA journal_mode = WAL",
+ r = sqlite3_exec(tmp_db, "PRAGMA journal_mode = WAL",
NULL, NULL, NULL);
if (r) {
bxt_err("change journal mode '%s' failed: %s",
- dbpath, sqlite3_errmsg(db));
- errno = EIO;
- free(nm);
- sqlite3_close(db);
- return NULL;
+ dbpath, sqlite3_errmsg(tmp_db));
+ sqlite3_close(tmp_db);
+ return BUXTON_ERROR_IO_ERROR;
}
}
- r = sqlite3_exec(db, QUERY_CREATE_TABLE_BUXTON,
+ r = sqlite3_exec(tmp_db, QUERY_CREATE_TABLE_BUXTON,
NULL, NULL, NULL);
if (r != SQLITE_OK) {
bxt_err("Create tables '%s' failed: %s",
- dbpath, sqlite3_errmsg(db));
- errno = EIO;
- free(nm);
- sqlite3_close(db);
- return NULL;
+ dbpath, sqlite3_errmsg(tmp_db));
+
+ sqlite3_close(tmp_db);
+ return BUXTON_ERROR_IO_ERROR;
}
}
if (strncmp(BASE_DB_PATH, dbpath, strlen(BASE_DB_PATH))) {
- r = sqlite3_wal_autocheckpoint(db, BUXTON_DEFAULT_WAL_AUTOCHECKPOINT);
+ r = sqlite3_wal_autocheckpoint(tmp_db, BUXTON_DEFAULT_WAL_AUTOCHECKPOINT);
if (r != SQLITE_OK)
bxt_err("SET DEFAULT_WAL_AUTOCHECKPOINT failed : %d", r);
}
- g_hash_table_insert(dbs, nm, db);
+
+ nm = strdup(dbpath);
+ if (!nm) {
+ bxt_err("out of memory");
+ sqlite3_close(tmp_db);
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_hash_table_insert(dbs, nm, tmp_db);
bxt_dbg("Open '%s'", dbpath);
- return db;
+ *db = tmp_db;
+ return BUXTON_ERROR_NONE;
}
static int open_db(const char *dbpath, bool readonly)
sqlite3 *db;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, readonly);
- if (!db)
- return -1;
-
- return 0;
+ return open_sqlite3(dbpath, readonly, &db);
}
static int close_db(const char *dbpath)
sqlite3 *db;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!dbs) {
- errno = ENODEV;
- return -1;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
db = g_hash_table_lookup(dbs, dbpath);
if (!db)
- return 0;
+ return BUXTON_ERROR_NONE;
g_hash_table_remove(dbs, dbpath);
-
bxt_info("close '%s'", dbpath);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int remove_db(const char *dbpath)
int r;
if (!dbpath || !*dbpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!dbs) {
- errno = ENODEV;
- return -1;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
db = g_hash_table_lookup(dbs, dbpath);
r = unlink(dbpath);
if (r == -1) {
bxt_err("Remove '%s' failed: %d", dbpath, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Remove '%s'", dbpath);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int set_value(const char *dbpath, const char *key, const void *data,
{
sqlite3 *db;
int r;
- int ret = 0;
+ int ret = BUXTON_ERROR_NONE;
const char insert_query[] =
"insert or replace into buxton(key, data) values(?,?)";
sqlite3_stmt *stmt;
if (!dbpath || !*dbpath || !key || !*key || !data || dlen <= 0) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, false);
- if (!db)
- return -1;
+ r = open_sqlite3(dbpath, false, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = sqlite3_prepare_v2(db, insert_query, strlen(insert_query),
&stmt, NULL);
if (r != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
r, sqlite3_extended_errcode(db));
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (r) {
bxt_err("Sqlite3 error [%d] : <%s> preparing <%s> querry\n", r,
sqlite3_errmsg(db), insert_query);
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
r = sqlite3_bind_blob(stmt, 2, data, dlen, SQLITE_STATIC);
if (r) {
bxt_err("Sqlite3 error [%d] : <%s> preparing <%s> querry\n", r,
sqlite3_errmsg(db), insert_query);
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
if (r != SQLITE_DONE) {
bxt_err("Sqlite3 error [%d] : <%s> executing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
end:
if (r != SQLITE_OK) {
bxt_err("Sqlite3 error [%d] : <%s> finalizing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Set '%s' Key '%s'", dbpath, key);
const char select_query[] = "select data from buxton where key = ?";
if (!dbpath || !*dbpath || !key || !*key || !data || !dlen) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, readonly);
- if (!db)
- return -1;
+ r = open_sqlite3(dbpath, readonly, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = sqlite3_prepare_v2(db, select_query, strlen(select_query),
&stmt, NULL);
if (r != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
r, sqlite3_extended_errcode(db));
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (r) {
bxt_err("Sqlite3 error [%d] : <%s> preparing <%s> querry\n", r,
sqlite3_errmsg(db), select_query);
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
r = sqlite3_step(stmt);
if (r != SQLITE_ROW) {
- errno = ENOENT;
- ret = -1;
+ bxt_dbg("Failed to sqlite3_step [%d]", r);
+ ret = BUXTON_ERROR_NOT_EXIST;
goto end;
}
if (*dlen > 0) {
*data = malloc(sizeof(void *) * (*dlen + 1));
if (*data == NULL) {
- errno = ENOMEM;
- ret = -1;
+ bxt_err("out of memory");
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto end;
}
memcpy(*data, (void *)sqlite3_column_blob(stmt, 0), *dlen);
if (r != SQLITE_OK) {
bxt_err("Sqlite3 error [%d] : <%s> finalizing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Get '%s' Key '%s'", dbpath, key);
const char delete_query[] = "delete from buxton where key = ?;";
if (!dbpath || !*dbpath || !key || !*key) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, false);
- if (!db)
- return -1;
+ r = open_sqlite3(dbpath, false, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = sqlite3_prepare_v2(db, delete_query, strlen(delete_query),
&stmt, NULL);
if (r != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
r, sqlite3_extended_errcode(db));
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (r) {
bxt_err("Sqlite3 error [%d] : <%s> preparing <%s> querry\n", r,
sqlite3_errmsg(db), delete_query);
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
if (r != SQLITE_DONE) {
bxt_err("Sqlite3 error [%d] : <%s> executing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
end:
if (r != SQLITE_OK) {
bxt_err("Sqlite3 error [%d] : <%s> finalizing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Unset '%s' Key '%s'", dbpath, key);
sqlite3_stmt *stmt;
if (!dbpath || !*dbpath || !keys) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, readonly);
- if (!db)
- return -1;
+ r = open_sqlite3(dbpath, readonly, &db);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = sqlite3_prepare_v2(db, list_query, strlen(list_query),
&stmt, NULL);
if (r != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
r, sqlite3_extended_errcode(db));
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
_klen = 0;
_keys = malloc(sizeof(void *) * (_klen + 1));
if (!_keys) {
g_list_free_full(list, (GDestroyNotify)free);
- errno = ENOMEM;
- ret = -1;
+ bxt_err("out of memory");
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto end;
}
if (r != SQLITE_OK) {
bxt_err("Sqlite3 error [%d] : <%s> finalizing statement\n", r,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("List '%s'", dbpath);
int *value_len_list = NULL;
if (!dbpath || !*dbpath || !keys || !values || !value_len) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- db = open_sqlite3(dbpath, true);
- if (!db)
- return -1;
+ ret = open_sqlite3(dbpath, true, &db);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
sql_ret = sqlite3_prepare_v2(db, count_query, strlen(count_query),
&stmt, NULL);
if (sql_ret != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
sql_ret, sqlite3_extended_errcode(db));
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
sql_ret = sqlite3_step(stmt);
} else {
bxt_err("sqlite3_step error, ret = %d, extended = %d\n",
sql_ret, sqlite3_extended_errcode(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
if (sql_ret != SQLITE_OK) {
bxt_err("prepare error, ret = %d, extended = %d\n",
sql_ret, sqlite3_extended_errcode(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
if (!key_list || !value_list || !value_len_list) {
bxt_err("calloc error OOM");
- ret = -1;
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto end;
}
if (sql_ret != SQLITE_ROW) {
bxt_err("sqlite3_step error, ret = %d, extended = %d index = %d , count = %d",
sql_ret, sqlite3_extended_errcode(db), index, key_count);
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
goto end;
}
value_list[index] = malloc(sizeof(void *) * (value_len_list[index] + 1));
if (value_list[index] == NULL) {
bxt_err("malloc error");
- ret = -1;
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto end;
}
memcpy(value_list[index], (void *)sqlite3_column_blob(stmt, 1), value_len_list[index]);
if (sql_ret != SQLITE_OK) {
bxt_err("Sqlite3 error [%d] : <%s> finalizing statement\n", sql_ret,
sqlite3_errmsg(db));
- ret = -1;
+ ret = BUXTON_ERROR_IO_ERROR;
}
}
- if (ret != 0) {
+ if (ret != BUXTON_ERROR_NONE) {
if (key_list) {
for (index = 0; index < key_count; index++) {
if (key_list[index])
dbs = g_hash_table_new_full(g_str_hash, g_str_equal,
(GDestroyNotify)free, (GDestroyNotify)free_db);
if (!dbs) {
- errno = ENOMEM;
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
DEFINE_BUXTON_BACKEND = {
../common/config.c
../common/serialize.c
../common/cache.c
+ ../common/buxton_error.c
)
ADD_EXECUTABLE(${TARGET} ${SRC})
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
struct buxton_value _val;
if (!value || !val) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
memset(&_val, 0, sizeof(_val));
break;
default:
bxt_err("Set: Unknown type: %d", type);
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (errno || ((end && *end != '\0'))) {
- bxt_err("Set: '%s': Invalid number", value);
- return -1;
+ bxt_err("Set: '%s': Invalid number(%d,%d)", value, type, errno);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
*val = _val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
#include <string.h>
#include "buxton2.h"
+#include "buxton_error.h"
#include "c_log.h"
#include "c_common.h"
#include "c_direct.h"
-#define BUFFER_SIZE 1024
#define BXT_LOG_FOLDER "/run/buxton2/log"
#define BXT_NULL_STRING "(VCONF_NULL)"
static int c_init(void)
{
int r;
- char err_buf[BUFFER_SIZE];
r = direct_init(MODULE_DIR, confpath ? confpath : CONFPATH, &_log_on, &_log_max_line);
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Init: %s", err_buf);
- return -1;
- }
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("failed to init %s", confpath);
- return 0;
+ return r;
}
int c_direct_get(const struct buxton_layer *layer,
{
int r;
struct buxton_value val = { BUXTON_TYPE_UNKNOWN, };
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get: Layer '%s' Key '%s': %s",
+ bxt_err("Get: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_get(layer, key, &val);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get: Layer '%s' Key '%s': %s",
- buxton_layer_get_name(layer), key,
- err_buf);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Get: Layer '%s' Key '%s': %s %d",
+ buxton_layer_get_name(layer), key, buxton_err_get_str(r), r);
+ return r;
}
-
c_print_value(layer, key, &val);
-
value_free(&val);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static bool _init_log()
{
int r;
struct buxton_value val;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !value) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s",
+ bxt_err("Set: Layer '%s' Key '%s' Value '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", value ? value : "",
- err_buf);
- return -1;
+ key ? key : "", value ? value : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_set_value(type, value, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_set(layer, key, &val);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s",
- buxton_layer_get_name(layer), key, value,
- err_buf);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s %d",
+ buxton_layer_get_name(layer), key, value, buxton_err_get_str(r), r);
+ return r;
}
_write_file_log(key, value);
{
int r;
struct buxton_value val;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !value || !rpriv || !wpriv) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s",
+ bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
key ? key : "", value ? value : "",
- rpriv ? rpriv : "", wpriv ? wpriv : "",
- err_buf);
- return -1;
+ rpriv ? rpriv : "", wpriv ? wpriv : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_set_value(type, value, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
+ bxt_err("Create: ");
r = direct_create(layer, key, rpriv, wpriv, &val);
c_exit();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s",
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s %d",
buxton_layer_get_name(layer), key, value,
- rpriv, wpriv, err_buf);
- }
+ rpriv, wpriv, buxton_err_get_str(r), r);
return r;
}
{
int r;
char *priv;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get-priv: Layer '%s' Key '%s': %s",
+ bxt_err("Get-priv: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "",
- err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
+ if (r != BUXTON_ERROR_NONE)
return r;
r = direct_get_priv(layer, key, type, &priv);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get-priv: Layer '%s' Key '%s': %s",
- buxton_layer_get_name(layer), key,
- err_buf);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Get-priv: Layer '%s' Key '%s': %s %d",
+ buxton_layer_get_name(layer), key, buxton_err_get_str(r), r);
+ return r;
}
c_print_priv(layer, key, type, priv);
const char *key, const char *priv, enum buxton_priv_type type)
{
int r;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !priv) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set-priv: Layer '%s' Key '%s' Priv. '%s': %s",
+ bxt_err("Set-priv: Layer '%s' Key '%s' Priv. '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", priv ? priv : "",
- err_buf);
- return -1;
+ key ? key : "", priv ? priv : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_set_priv(layer, key, type, priv);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set-priv: Layer '%s' Key '%s' Priv. '%s': %s",
- buxton_layer_get_name(layer), key, priv,
- err_buf);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Set-priv: Layer '%s' Key '%s' Priv. '%s': %s %d",
+ buxton_layer_get_name(layer), key, priv, buxton_err_get_str(r), r);
}
return r;
UNUSED const char *rpriv, UNUSED const char *wpriv)
{
int r;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Unset: Layer '%s' Key '%s': %s",
+ bxt_err("Unset: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_unset(layer, key);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Unset: Layer '%s' Key '%s': %s",
- buxton_layer_get_name(layer), key,
- err_buf);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Unset: Layer '%s' Key '%s': %s %d",
+ buxton_layer_get_name(layer), key, buxton_err_get_str(r), r);
}
return r;
int r;
char **keys;
char **k;
- char err_buf[BUFFER_SIZE];
if (!layer) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s",
- layer ? buxton_layer_get_name(layer) : "",
- err_buf);
- return -1;
+ bxt_err("List: Layer '%s': Invalid parameter",
+ layer ? buxton_layer_get_name(layer) : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_list(layer, &keys, NULL, true);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s", buxton_layer_get_name(layer),
- err_buf);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("List: Layer '%s': %s %d", buxton_layer_get_name(layer),
+ buxton_err_get_str(r), r);
+ return r;
}
k = keys;
buxton_free_keys(keys);
- return 0;
+ return r;
}
int c_direct_remove_garbage(const struct buxton_layer *layer,
unsigned int len;
int r;
int i;
- char err_buf[BUFFER_SIZE];
if (!layer) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Remove garbage data : Layer '%s' : %s",
- layer ? buxton_layer_get_name(layer) : "",
- err_buf);
- return -1;
+ bxt_err("Remove garbage data : Layer '%s' : Invalid parameter",
+ layer ? buxton_layer_get_name(layer) : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
normal_layer = buxton_create_layer(layer->name);
buxton_layer_set_type(normal_layer, BUXTON_LAYER_NORMAL);
buxton_layer_set_type(base_layer, BUXTON_LAYER_BASE);
r = direct_list(normal_layer, &keys, &len, false);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
buxton_free_layer(normal_layer);
buxton_free_layer(base_layer);
- return -1;
+ bxt_err("Remove garbage data: Layer '%s' : %s %d",
+ buxton_layer_get_name(normal_layer),
+ buxton_err_get_str(r), r);
+ return r;
}
i = 0;
while (i < len) {
r = direct_get(base_layer, keys[i], &val);
- if (r == -1 && errno == ENOENT) {
+ if (r == BUXTON_ERROR_NOT_EXIST) {
r = direct_unset(normal_layer, keys[i]);
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Remove garbage data: Layer '%s' Key '%s': %s",
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Remove garbage data: Layer '%s' Key '%s': %s %d",
buxton_layer_get_name(normal_layer), keys[i],
- err_buf);
+ buxton_err_get_str(r), r);
}
}
value_free(&val);
buxton_free_layer(normal_layer);
buxton_free_layer(base_layer);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int c_direct_dump(const struct buxton_layer *layer,
int r, key_len, i;
struct buxton_value *value_list;
char **key_list;
- char err_buf[BUFFER_SIZE];
if (!layer) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s",
- layer ? buxton_layer_get_name(layer) : "",
- err_buf);
- return -1;
+ bxt_err("List: Layer '%s': Invalid parameter",
+ layer ? buxton_layer_get_name(layer) : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_init();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = direct_dump(layer, &key_list, &value_list, &key_len);
-
c_exit();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s", buxton_layer_get_name(layer),
- err_buf);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("List: Layer '%s': %s %d", buxton_layer_get_name(layer),
+ buxton_err_get_str(r), r);
+ return r;
}
for (i = 0 ; i < key_len; i++) {
buxton_free_keys(key_list);
free(value_list);
- return 0;
+ return BUXTON_ERROR_NONE;
}
#include "c_log.h"
#include "c_proc.h"
#include "c_direct.h"
+#include "buxton_error.h"
struct options {
char *confpath;
_layer = buxton_create_layer(lnm);
if (!_layer) {
bxt_err("create layer '%s' error", lnm);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
} else {
_layer = NULL;
if (!_layer) {
*layer = NULL;
- return 0;
+ return BUXTON_ERROR_NONE;
}
if (uid == 0)
*layer = _layer;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static void print_usage(const char *name, const struct command *comm)
{ NULL }
};
- assert(argc);
- assert(argv);
- assert(*argv);
- assert(**argv);
-
optctx = g_option_context_new(NULL);
if (!optctx) {
bxt_err("option new error");
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
g_option_context_add_main_entries(optctx, entries, NULL);
bxt_err("option parse error: %s", err->message);
usage(**argv);
g_clear_error(&err);
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
int main(int argc, char *argv[])
.help = FALSE,
};
+ if (!argc || !argv || !*argv || !**argv)
+ usage("");
+
r = parse_args(&argc, &argv, &opt);
if (r == -1)
return EXIT_FAILURE;
r = get_layer(argc > 2 ? argv[2] : NULL, (uid_t)opt.uid,
opt.install ? BUXTON_LAYER_BASE : BUXTON_LAYER_NORMAL,
&layer);
- if (r == -1)
+ if (r != BUXTON_ERROR_NONE)
return EXIT_FAILURE;
- assert(func);
r = func(layer, argc > 3 ? argv[3] : NULL,
argc > 4 ? argv[4] : NULL,
argc > 5 ? argv[5] : NULL,
buxton_free_layer(layer);
- if (r == -1)
+ if (r != BUXTON_ERROR_NONE)
return EXIT_FAILURE;
return EXIT_SUCCESS;
#include "c_common.h"
#include "c_proc.h"
-#define BUFFER_SIZE 1024
-
static struct buxton_client *client;
static void status_cb(enum buxton_status status, void *data)
static int _close(void)
{
int r;
- char err_buf[BUFFER_SIZE];
if (!client)
- return 0;
+ return BUXTON_ERROR_NONE;
r = buxton_close(client);
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("close: %s", err_buf);
- }
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("close: %s %d", buxton_err_get_str(r), r);
client = NULL;
static int _open(void)
{
int r;
- char err_buf[BUFFER_SIZE];
if (client)
- return 0;
+ return BUXTON_ERROR_NONE;
r = buxton_open(&client, status_cb, NULL);
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("open: %s", err_buf);
- }
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("open: %s %d", buxton_err_get_str(r), r);
return r;
}
struct buxton_client *cli;
r = buxton_open(&cli, NULL, NULL);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
printf("Failed to connect the Buxton service\n");
- return -1;
+ return r;
}
printf("Buxton service is available\n");
buxton_close(cli);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int c_get(const struct buxton_layer *layer,
{
int r;
struct buxton_value *val;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get: Layer '%s' Key '%s': %s",
+ bxt_err("Get: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_get_value_sync(client, layer, key, &val);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get: Layer '%s' Key '%s': %s",
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Get: Layer '%s' Key '%s': %s %d",
buxton_layer_get_name(layer), key,
- err_buf);
- return -1;
+ buxton_err_get_str(r), r);
+ return r;
}
c_print_value(layer, key, val);
{
int r;
struct buxton_value val;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !value) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s",
+ bxt_err("Set: Layer '%s' Key '%s' Value '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", value ? value : "",
- err_buf);
- return -1;
+ key ? key : "", value ? value : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_set_value(type, value, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_set_value_sync(client, layer, key, &val);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s",
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("Set: Layer '%s' Key '%s' Value '%s': %s %d",
buxton_layer_get_name(layer), key, value,
- err_buf);
- }
+ buxton_err_get_str(r), r);
return r;
}
{
int r;
struct buxton_value val;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !value || !rpriv || !wpriv) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s",
+ bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
key ? key : "", value ? value : "",
- rpriv ? rpriv : "", wpriv ? wpriv : "",
- err_buf);
- return -1;
+ rpriv ? rpriv : "", wpriv ? wpriv : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = c_set_value(type, value, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_create_value_sync(client, layer, key, rpriv, wpriv, &val);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s",
+ if (r != BUXTON_ERROR_NONE) {
+ r = buxton_err_get_errno();
+ bxt_err("Create: '%s' '%s' '%s' Priv '%s' '%s': %s %d",
buxton_layer_get_name(layer), key, value,
- rpriv, wpriv, err_buf);
+ rpriv, wpriv, buxton_err_get_str(r), r);
}
return r;
{
int r;
char *priv;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get-priv: Layer '%s' Key '%s': %s",
+ bxt_err("Get-priv: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_get_privilege_sync(client, layer, key, type, &priv);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Get-priv: Layer '%s' Key '%s': %s",
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Get-priv: Layer '%s' Key '%s': %s %d",
buxton_layer_get_name(layer), key,
- err_buf);
- return -1;
+ buxton_err_get_str(r), r);
+ return r;
}
c_print_priv(layer, key, type, priv);
const char *key, const char *priv, enum buxton_priv_type type)
{
int r;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key || !priv) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set-priv: Layer '%s' Key '%s' Priv '%s': %s",
+ bxt_err("Set-priv: Layer '%s' Key '%s' Priv '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", priv ? priv : "",
- err_buf);
- return -1;
+ key ? key : "", priv ? priv : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_set_privilege_sync(client, layer, key, type, priv);
_close();
-
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Set-priv: Layer '%s' Key '%s' Priv '%s': %s",
- buxton_layer_get_name(layer), key, priv,
- err_buf);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Set-priv: Layer '%s' Key '%s' Priv '%s': %s %d",
+ buxton_layer_get_name(layer), key, priv, buxton_err_get_str(r), r);
}
return r;
UNUSED const char *rpriv, UNUSED const char *wpriv)
{
int r;
- char err_buf[BUFFER_SIZE];
if (!layer || !key || !*key) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Unset: Layer '%s' Key '%s': %s",
+ bxt_err("Unset: Layer '%s' Key '%s': Invalid parameter",
layer ? buxton_layer_get_name(layer) : "",
- key ? key : "", err_buf);
- return -1;
+ key ? key : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_unset_value_sync(client, layer, key);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("Unset: Layer '%s' Key '%s': %s",
- buxton_layer_get_name(layer), key,
- err_buf);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Unset: Layer '%s' Key '%s': %s %d",
+ buxton_layer_get_name(layer), key, buxton_err_get_str(r), r);
}
return r;
int r;
char **keys;
char **k;
- char err_buf[BUFFER_SIZE];
if (!layer) {
- errno = EINVAL;
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s",
- layer ? buxton_layer_get_name(layer) : "",
- err_buf);
- return -1;
+ bxt_err("List: Layer '%s': Invalid parameter",
+ layer ? buxton_layer_get_name(layer) : "");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_list_keys_sync(client, layer, &keys, NULL);
_close();
- if (r == -1) {
- strerror_r(errno, err_buf, sizeof(err_buf));
- bxt_err("List: Layer '%s': %s", buxton_layer_get_name(layer),
- err_buf);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("List: Layer '%s': %s %d", buxton_layer_get_name(layer),
+ buxton_err_get_str(r), r);
+ return r;
}
k = keys;
buxton_free_keys(keys);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int c_enable_security(UNUSED const struct buxton_layer *layer,
int r;
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_enable_security_sync(client);
_close();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- return 0;
+ return BUXTON_ERROR_NONE;
}
int c_disable_security(UNUSED const struct buxton_layer *layer,
int r;
r = _open();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = buxton_disable_security_sync(client);
_close();
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- return 0;
+ return BUXTON_ERROR_NONE;
}
#include "backend.h"
#include "log.h"
+#include "buxton_error.h"
static GHashTable *backends;
free(mod);
}
-const struct backend *backend_get(const char *name)
+int backend_get(const char *name, const struct backend **backend_get)
{
struct module *mod;
- if (!name || !*name) {
- errno = EINVAL;
- return NULL;
+ if (!name || !*name || !backend_get) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!backends) {
- errno = ENOENT;
- return NULL;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
mod = g_hash_table_lookup(backends, name);
if (!mod) {
- errno = ENOENT;
- return NULL;
+ bxt_err("%s does not exist", name);
+ return BUXTON_ERROR_NOT_EXIST;
}
- return mod->backend;
+ *backend_get = mod->backend;
+ return BUXTON_ERROR_NONE;
}
static struct module *load_module(const char *moddir, const char *modname)
int r;
char modpath[FILENAME_MAX];
- assert(moddir);
- assert(modname);
+ if (!moddir || !modname) {
+ bxt_err("Invalid parameter");
+ return NULL;
+ }
snprintf(modpath, sizeof(modpath), "%s/%s", moddir, modname);
r = backend->module_init();
if (r) {
- bxt_err("load '%s' error: init: %d", modpath, errno);
+ bxt_err("load '%s' error: init: %d", modpath, r);
goto err;
}
dir = opendir(moddir);
if (!dir) {
bxt_err("opendir error: %d", errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
while ((result = readdir(dir)) != NULL) {
closedir(dir);
- return 0;
+ return BUXTON_ERROR_NONE;
}
GList *backend_list(void)
int backend_init(const char *moddir)
{
if (!moddir || !*moddir) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (backends)
- return 0;
+ return BUXTON_ERROR_NONE;
backends = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, (GDestroyNotify)free_backend);
if (!backends) {
- errno = ENOMEM;
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
return load_modules(moddir);
int backend_init(const char *moddir);
void backend_exit(void);
-const struct backend *backend_get(const char *name);
+int backend_get(const char *name, const struct backend **backend_get);
/* Do not free the content of list. use g_list_free() */
GList *backend_list(void);
--- /dev/null
+/*
+ * Buxton
+ *
+ * Copyright (C) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "buxton_error.h"
+
+#ifndef EXPORT
+# define EXPORT __attribute__((visibility("default")))
+#endif
+
+static int buxton_errno;
+
+EXPORT char* buxton_err_get_str(int err)
+{
+ switch (err) {
+ case BUXTON_ERROR_INVALID_PARAMETER:
+ return "BUXTON_ERROR_INVALID_PARAMETER";
+ case BUXTON_ERROR_OUT_OF_MEMORY:
+ return "BUXTON_ERROR_OUT_OF_MEMORY";
+ case BUXTON_ERROR_IO_ERROR:
+ return "BUXTON_ERROR_IO_ERROR";
+ case BUXTON_ERROR_PERMISSION_DENIED:
+ return "BUXTON_ERROR_PERMISSION_DENIED";
+ case BUXTON_ERROR_INVALID_OPERATION:
+ return "BUXTON_ERROR_INVALID_OPERATION";
+ case BUXTON_ERROR_NOT_EXIST:
+ return "BUXTON_ERROR_NOT_EXIST";
+ case BUXTON_ERROR_EXIST:
+ return "BUXTON_ERROR_EXIST";
+ case BUXTON_ERROR_TIME_OUT:
+ return "BUXTON_ERROR_TIME_OUT";
+ case BUXTON_ERROR_DATA_TOO_LONG:
+ return "BUXTON_ERROR_DATA_TOO_LONG";
+ default:
+ return "Unknow error";
+ }
+}
+
+void buxton_err_set_errno(int err)
+{
+ buxton_errno = -(err);
+ errno = -(err);
+}
+
+EXPORT int buxton_err_get_errno(void)
+{
+ return buxton_errno;
+}
+
--- /dev/null
+/*
+ * Buxton
+ *
+ * Copyright (C) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <tizen_error.h>
+
+typedef enum _buxton_error {
+ BUXTON_ERROR_NONE = TIZEN_ERROR_NONE,
+ BUXTON_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+ BUXTON_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+ BUXTON_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */
+ BUXTON_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */
+ BUXTON_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Function not implemented */
+ BUXTON_ERROR_NOT_EXIST = TIZEN_ERROR_NO_SUCH_FILE,
+ BUXTON_ERROR_EXIST = TIZEN_ERROR_FILE_EXISTS,
+ BUXTON_ERROR_TIME_OUT = TIZEN_ERROR_CONNECTION_TIME_OUT,
+ BUXTON_ERROR_DATA_TOO_LONG = TIZEN_ERROR_FILE_NAME_TOO_LONG,
+ BUXTON_ERROR_INTERRUPTED = TIZEN_ERROR_INTERRUPTED_SYS_CALL
+
+} buxton_error_e;
+
+char* buxton_err_get_str(int err);
+void buxton_err_set_errno(int err);
+int buxton_err_get_errno();
+
+#ifdef __cplusplus
+}
+#endif
#include "buxton2.h"
#include "log.h"
#include "cache.h"
+#include "buxton_error.h"
#define CACHE_COUNT_MAX 2000
#define CACHE_SIZE_MAX (1024 * 1024) /* 1MB per cache (MAX) */
bxt_cache *cache = get_cache(layer_type);
if (!cache)
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
hash_table = cache->hash_table;
raw_data = g_hash_table_lookup(hash_table, key);
if (!raw_data)
- return -1;
+ return BUXTON_ERROR_NOT_EXIST;
cdata = (cache_data *)raw_data;
*data = malloc(cdata->len);
if (!*data)
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
memcpy(*data, cdata->data, cdata->len);
*data_len = cdata->len;
(layer_type == BUXTON_LAYER_NORMAL) ?
"NORMAL" : "BASE", key, cdata->len);
- return 0;
+ return BUXTON_ERROR_NONE;
}
void cache_clear(enum buxton_layer_type layer_type)
#include <errno.h>
#include "common.h"
+#include "log.h"
-struct buxton_layer *layer_create(const char *layer_name)
+
+int layer_create(const char *layer_name, struct buxton_layer **layer)
{
- struct buxton_layer *layer;
+ struct buxton_layer *tmp_layer;
- if (!layer_name || !*layer_name) {
- errno = EINVAL;
- return NULL;
+ if (!layer_name || !*layer_name || !layer) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- layer = calloc(1, sizeof(*layer));
- if (!layer)
- return NULL;
+ tmp_layer = calloc(1, sizeof(*tmp_layer));
+ if (!tmp_layer) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
- layer->name = strdup(layer_name);
- if (!layer->name) {
- free(layer);
- return NULL;
+ tmp_layer->name = strdup(layer_name);
+ if (!tmp_layer->name) {
+ free(tmp_layer);
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
- layer->refcnt = 1;
- layer->uid = getuid();
- layer->type = BUXTON_LAYER_NORMAL;
+ tmp_layer->refcnt = 1;
+ tmp_layer->uid = getuid();
+ tmp_layer->type = BUXTON_LAYER_NORMAL;
- return layer;
+ *layer = tmp_layer;
+
+ return BUXTON_ERROR_NONE;
}
/* ignore ref. count */
struct buxton_layer *layer_ref(struct buxton_layer *layer)
{
- if (!layer)
+ if (!layer) {
+ bxt_err("Invalid parameter");
return NULL;
+ }
layer->refcnt++;
struct buxton_layer *layer_unref(struct buxton_layer *layer)
{
- if (!layer)
+ if (!layer) {
+ bxt_err("Invalid parameter");
return NULL;
+ }
layer->refcnt--;
if (layer->refcnt == 0) {
}
}
-char *get_search_key(const struct buxton_layer *layer, const char *key,
- const char *uid)
+int get_search_key(const struct buxton_layer *layer, const char *key,
+ const char *uid, char **layer_key)
{
char *lykey;
int keylen;
- if (!layer || !key || !*key) {
- errno = EINVAL;
- return NULL;
+ if (!layer || !key || !*key || !layer_key) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
keylen = strlen(layer->name) + strlen(key) + 2;
keylen += strlen(uid) + 1;
lykey = malloc(keylen);
- if (!lykey)
- return NULL;
+ if (!lykey) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
snprintf(lykey, keylen, "%s\t%s%s%s", layer->name, key,
uid ? "\t" : "", uid ? uid : "");
+ *layer_key = lykey;
- return lykey;
+ return BUXTON_ERROR_NONE;
}
#include <glib.h>
#include "buxton2.h"
+#include "buxton_error.h"
#ifndef CONFPATH
# warning "CONFPATH is not set. default value is used"
enum buxton_layer_type type;
};
-struct buxton_layer *layer_create(const char *layer_name);
+int layer_create(const char *layer_name, struct buxton_layer **layer);
void layer_free(struct buxton_layer *layer);
struct buxton_layer *layer_ref(struct buxton_layer *layer);
void value_free(struct buxton_value *val);
-char *get_search_key(const struct buxton_layer *layer, const char *key,
- const char *uid);
+int get_search_key(const struct buxton_layer *layer, const char *key,
+ const char *uid, char **layer_key);
free(layer);
}
-static GKeyFile *load_conf(const char *confpath)
+static int load_conf(const char *confpath, GKeyFile **keyfile)
{
GKeyFile *kf;
gboolean b;
GError *err;
- assert(confpath);
+ if (!confpath || !keyfile) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
kf = g_key_file_new();
if (!kf) {
- errno = ENOMEM;
- return NULL;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
err = NULL;
err ? err->message : "");
g_clear_error(&err);
g_key_file_free(kf);
- return NULL;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return kf;
+ *keyfile = kf;
+
+ return BUXTON_ERROR_NONE;
}
-static struct layer *create_layer(GKeyFile *kf, gchar *name)
+static int create_layer(GKeyFile *kf, gchar *name, struct layer **new_layer)
{
GError *err;
struct layer *layer;
gchar *s;
+ int ret;
- assert(name && *name);
+ if (!name || !*name || !new_layer) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
layer = calloc(1, sizeof(*layer));
- if (!layer)
+ if (!layer) {
+ bxt_err("out of memory");
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto err;
+ }
/* 'Type' */
err = NULL;
bxt_err("Layer '%s' : %s",
name, err ? err->message : "");
g_clear_error(&err);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
goto err;
}
bxt_err("Layer '%s' : %s",
name, err ? err->message : "");
g_clear_error(&err);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
goto err;
}
bxt_err("Layer '%s' : %s",
name, err ? err->message : "");
g_clear_error(&err);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
goto err;
}
bxt_err("Layer '%s' : %s",
name, err ? err->message : "");
g_clear_error(&err);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
goto err;
}
/* Layer name */
layer->name = name;
+ *new_layer = layer;
- return layer;
+ return BUXTON_ERROR_NONE;
err:
g_free(name);
free_layer(layer);
- return NULL;
+ return ret;
}
static gboolean has_backend(GList *backends, const char *name)
static void add_layers(GKeyFile *kf, GList *backends)
{
gchar **lays;
- int i;
+ int i, ret;
struct layer *layer;
struct layer *f;
gboolean b;
- assert(kf);
- assert(layers);
+ if (!kf || !backends) {
+ bxt_err("Invalid parameter");
+ return;
+ }
+
+ if (!layers) {
+ bxt_err("layers is not initilized.");
+ return;
+ }
lays = g_key_file_get_groups(kf, NULL);
if (!lays) {
i = 0;
while (lays[i]) {
- layer = create_layer(kf, lays[i++]);
- if (!layer)
+ ret = create_layer(kf, lays[i++], &layer);
+ if (ret != BUXTON_ERROR_NONE)
continue;
b = has_backend(backends, layer->backend);
{
GError *err = NULL;
gchar *buf = NULL;
- assert(kf);
-
bool onoff;
int max = 0;
+ if (!kf || !log_on || !max_line) {
+ bxt_err("Invalid parameter");
+ return;
+ }
+
/* 'OnOff' */
buf = g_key_file_get_string(kf, G_LOG_CONF, K_LOG_ON, &err);
if (!buf) {
g_key_file_remove_group(kf, G_LOG_CONF, NULL);
}
-const struct layer *conf_get_layer(const char *name)
+int conf_get_layer(const char *name, const struct layer **layer)
{
- const struct layer *layer;
-
- if (!name || !*name) {
- errno = EINVAL;
- return NULL;
+ if (!name || !*name || !layer) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (!layers) {
- errno = ENOENT;
- return NULL;
+ bxt_err("layers is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
- layer = g_hash_table_lookup(layers, name);
- if (!layer) {
+ *layer = g_hash_table_lookup(layers, name);
+ if (!*layer) {
bxt_dbg("Layer '%s' not exist", name);
- errno = ENOENT;
+ return BUXTON_ERROR_NOT_EXIST;
}
- return layer;
+ return BUXTON_ERROR_NONE;
}
int conf_remove(const char *name)
{
const struct layer *layer;
- gboolean b;
+ int ret;
- layer = conf_get_layer(name);
- if (!layer)
- return -1;
+ ret = conf_get_layer(name, &layer);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
- b = g_hash_table_remove(layers, name);
+ g_hash_table_remove(layers, name);
- return b ? 0 : -1;
+ return BUXTON_ERROR_NONE;
}
void conf_exit(void)
int conf_init(const char *confpath, GList *backends, bool *log_on, int *max_line)
{
GKeyFile *kf;
+ int ret;
- if (!confpath || !*confpath) {
- errno = EINVAL;
- return -1;
+ if (!confpath || !*confpath || !log_on || !max_line) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (layers)
- return 0;
+ return BUXTON_ERROR_NONE;
- kf = load_conf(confpath);
- if (!kf)
- return -1;
+ ret = load_conf(confpath, &kf);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
layers = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, (GDestroyNotify)free_layer);
if (!layers) {
- errno = ENOMEM;
+ bxt_err("out of memory");
g_key_file_free(kf);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
load_log_conf(kf, log_on, max_line);
g_key_file_free(kf);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int conf_init(const char *confpath, GList *backends, bool *log_on, int *max_line);
void conf_exit(void);
-const struct layer *conf_get_layer(const char *name);
+int conf_get_layer(const char *name, const struct layer **layer);
int conf_remove(const char *name);
char suffix[16];
if (!ly || !path || sz <= 0) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (type == BUXTON_LAYER_NORMAL)
snprintf(path, sz, "%s/%s%s.db", prefix, ly->name, suffix);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int get_raw(const struct layer *ly, uid_t uid,
const struct backend *backend;
char path[FILENAME_MAX];
- assert(ly);
- assert(key);
- assert(data);
- assert(len);
+ if (!ly || !key || !data || !len) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->get_value) {
bxt_err("Get: backend '%s' has no get func", backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
- if (!cache_get(type, key, (void **)data, len))
- return 0;
+ if (cache_get(type, key, (void **)data, len) == BUXTON_ERROR_NONE)
+ return BUXTON_ERROR_NONE;
r = get_path(uid, type, ly, path, sizeof(path));
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->get_value(path, key, (void **)data, len,
(type == BUXTON_LAYER_BASE) ? true : false);
- if (r == -1) {
- if (errno != ENOENT)
- bxt_err("Get: get_value: %d", errno);
- return -1;
- }
+ if (r != BUXTON_ERROR_NONE)
+ return r;
cache_insert(type, key, *data, *len);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int get_val(const struct layer *ly, uid_t uid,
uint8_t *data;
int len;
- assert(ly);
- assert(key);
+ if (!ly || !key) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = get_raw(ly, uid, type, key, &data, &len);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = deserialz_data(data, len, rpriv, wpriv, val);
-
free(data);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
int direct_get(const struct buxton_layer *layer,
struct buxton_value db_val;
if (!layer || !key || !*key || !val) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
/* First, refer to base db */
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, NULL, NULL,
&base_val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (layer->type == BUXTON_LAYER_BASE) {
*val = base_val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
r = get_val(ly, layer->uid, BUXTON_LAYER_NORMAL, key, NULL, NULL,
&db_val);
- if (r == -1) {
- if (errno == ENOENT) {
+ if (r != BUXTON_ERROR_NONE) {
+ if (r == BUXTON_ERROR_NOT_EXIST) {
*val = base_val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
value_free(&base_val);
- return -1;
+ return r;
}
value_free(&base_val);
*val = db_val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
int direct_check(const struct buxton_layer *layer, const char *key)
struct buxton_value val;
if (!layer || !key || !*key) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, NULL, NULL, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
value_free(&val);
- return 0;
+ return BUXTON_ERROR_NONE;
}
-static int is_updated_data(enum buxton_layer_type type, const char *key,
+static bool is_updated_data(enum buxton_layer_type type, const char *key,
uint8_t *data, int len)
{
uint8_t *cur_data;
int cur_data_len;
+ int ret;
- if (cache_get(type, key, (void **)&cur_data, &cur_data_len))
- return -1;
+ ret = cache_get(type, key, (void **)&cur_data, &cur_data_len);
+ if (ret != BUXTON_ERROR_NONE)
+ return false;
if (cur_data_len != len) {
free(cur_data);
- return -1;
+ return false;
}
if (memcmp(cur_data, data, len)) {
free(cur_data);
- return -1;
+ return false;
}
free(cur_data);
- return 0;
+ return true;
}
const struct backend *backend;
char path[FILENAME_MAX];
- assert(ly);
- assert(key);
- assert(data);
- assert(len > 0);
+ if (!ly || !key || !data || len <= 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->set_value) {
bxt_err("Set: backend '%s' has no set func", backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
- if (!is_updated_data(type, key, data, len)) {
+ if (is_updated_data(type, key, data, len)) {
bxt_dbg("skip setting (same data) - (%s)", key);
- return 0;
+ return BUXTON_ERROR_NONE;
}
r = get_path(uid, type, ly, path, sizeof(path));
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->set_value(path, key, data, len);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
cache_update_data(type, key, data, len);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int set_val(const struct layer *ly, uid_t uid,
uint8_t *data;
int len;
- assert(val);
+ if (!val) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = serialz_data(rpriv ? rpriv : "", wpriv ? wpriv : "", val,
&data, &len);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = set_raw(ly, uid, type, key, data, len);
free(data);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
int direct_set(const struct buxton_layer *layer,
char *wp;
if (!layer || !key || !*key || !val) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, &rp, &wp, NULL);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = set_val(ly, layer->uid, layer->type, key, rp, wp, val);
free(rp);
free(wp);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
static int direct_close(const struct buxton_layer *layer, const struct layer *ly)
char path[FILENAME_MAX];
const struct backend *backend;
- assert(layer);
- assert(ly);
+ if (!layer || !ly) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->close_db) {
bxt_err("DirectCreate: backend '%s' has no close_db func",
backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
r = get_path(layer->uid, BUXTON_LAYER_BASE, ly, path, sizeof(path));
-
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->close_db(path);
- if (r == -1) {
- bxt_err("DirectCreate: close_db: %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("DirectCreate: close_db: %s %d", buxton_err_get_str(r), r);
+ return r;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
int direct_create(const struct buxton_layer *layer, const char *key,
const struct layer *ly;
if (!layer || !key || !*key || !val) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = check_key_name(key);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, NULL, NULL, NULL);
- if (r == 0) {
- errno = EEXIST;
- return -1;
- }
+ if (r == BUXTON_ERROR_NONE)
+ return BUXTON_ERROR_EXIST;
r = direct_close(layer, ly);
- if (r == -1) {
- bxt_err("failed to close db(%s)", ly->name);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("failed to close db(%s) %d", ly->name, r);
+ return r;
}
r = set_val(ly, layer->uid, BUXTON_LAYER_BASE, key, rpriv, wpriv, val);
- if (r == -1)
- return -1;
- return 0;
+ return r;
}
int direct_unset(const struct buxton_layer *layer, const char *key)
char path[FILENAME_MAX];
if (!layer || !key || !*key) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->unset_value) {
bxt_err("Unset: backend '%s' has no unset func",
backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
r = get_path(layer->uid, layer->type, ly, path, sizeof(path));
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->unset_value(path, key);
- if (r == -1) {
- bxt_err("Unset: unset_value: %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("Unset: unset_value: %s %d", buxton_err_get_str(r), r);
+ return r;
}
cache_remove(layer->type, key);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int comp_str(const void *pa, const void *pb)
unsigned int _len;
if (!layer || !names) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->list_keys) {
bxt_err("List: backend '%s' has no list func",
backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
r = get_path(layer->uid, is_base ?
BUXTON_LAYER_BASE : BUXTON_LAYER_NORMAL,
ly, path, sizeof(path));
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->list_keys(path, names, &_len, is_base);
- if (r == -1) {
- bxt_err("List: list_keys: %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("List: list_keys: %s %d", buxton_err_get_str(r), r);
+ return r;
}
if (_len > 1)
if (len)
*len = _len;
- return 0;
+ return BUXTON_ERROR_NONE;
}
int direct_dump(const struct buxton_layer *layer,
int len_tmp;
if (!layer || !key_list || !value_list || !len) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ ret = conf_get_layer(layer->name, &ly);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
- backend = backend_get(ly->backend);
- assert(backend);
+ ret = backend_get(ly->backend, &backend);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
if (!backend->get_dump) {
bxt_err("dump: backend '%s' has no list func",
backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
ret = get_path(layer->uid, BUXTON_LAYER_BASE,
ly, path, sizeof(path));
- if (ret == -1)
- return -1;
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
/*base_keys is sorted data */
ret = backend->get_dump(path, &base_keys, &base_values, &base_value_len, &base_len);
- if (ret == -1) {
+ if (ret != BUXTON_ERROR_NONE) {
bxt_err("dump: get_dump failed");
- return -1;
+ return ret;
}
ret = get_path(layer->uid, BUXTON_LAYER_NORMAL,
ly, path, sizeof(path));
- if (ret == -1) {
+ if (ret != BUXTON_ERROR_NONE) {
bxt_err("dump: get_path failed");
goto out;
}
/* normal_keys is sorted data */
ret = backend->get_dump(path, &normal_keys, &normal_values, &normal_value_len, &nomal_len);
- if (ret == -1) {
+ if (ret != BUXTON_ERROR_NONE) {
bxt_err("dump: get_dump failed");
goto out;
}
_value_list = calloc(base_len, sizeof(struct buxton_value));
if (_value_list == NULL) {
bxt_err("dump: calloc failed");
- ret = -1;
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
goto out;
}
len_tmp = base_value_len[i];
}
ret = deserialz_data((uint8_t *)tmp, len_tmp, NULL, NULL, &_value_list[i]);
- if (ret == -1) {
+ if (ret != BUXTON_ERROR_NONE) {
bxt_err("dump: deserialz_data failed %s ", base_keys[i]);
goto out;
}
free(base_value_len);
free(normal_value_len);
- if (ret != 0) {
+ if (ret != BUXTON_ERROR_NONE) {
buxton_free_keys(base_keys);
if (_value_list)
char **wp;
if (!layer || !key || !*key || !priv) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
switch (type) {
wp = priv;
break;
default:
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, rp, wp, NULL);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
int direct_set_priv(const struct buxton_layer *layer,
struct buxton_value val;
if (!layer || !key || !*key || !priv) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
switch (type) {
case BUXTON_PRIV_WRITE:
break;
default:
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = get_val(ly, layer->uid, BUXTON_LAYER_BASE, key, &rp, &wp, &val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
switch (type) {
case BUXTON_PRIV_READ:
free(rp);
free(wp);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
int direct_remove_db(const struct buxton_layer *layer)
char path[FILENAME_MAX];
if (!layer) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- ly = conf_get_layer(layer->name);
- if (!ly)
- return -1;
+ r = conf_get_layer(layer->name, &ly);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- backend = backend_get(ly->backend);
- assert(backend);
+ r = backend_get(ly->backend, &backend);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (!backend->remove_db) {
bxt_err("Remove db : backend '%s' has no remove_db func",
backend->name);
- return -1;
+ return BUXTON_ERROR_INVALID_OPERATION;
}
r = get_path(layer->uid, layer->type, ly, path, sizeof(path));
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = backend->remove_db(path);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE)
bxt_err("remote user memory db failed");
- return -1;
- }
- return 0;
+ return r;
}
void direct_exit(void)
GList *backends;
if (!moddir || !*moddir || !confpath || !*confpath) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = backend_init(moddir);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
backends = backend_list();
if (!backends)
- return -1;
+ return BUXTON_ERROR_NOT_EXIST;
r = conf_init(confpath, backends, log_on, max_line);
g_list_free(backends);
- if (r == -1)
- return -1;
-
- return 0;
+ return r;
}
struct recv_info *rif;
struct recv_info *f;
- assert(callback);
- assert(hdr);
+ if (!callback || !hdr) {
+ bxt_err("Invalid parameter");
+ return NULL;
+ }
/* 8 is pad for alignment */
rif = malloc(sizeof(*rif) + hdr->total + 8);
- if (!rif)
+ if (!rif) {
+ bxt_err("out of memory");
return NULL;
+ }
memset(rif, 0, sizeof(*rif));
uint8_t *data;
int32_t len;
- assert(fd >= 0);
- assert(callback);
+ if (!callback || fd < 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = proto_recv(fd, &type, &data, &len);
pthread_mutex_unlock(&recv_lock);
- if (r == -1) {
- if (errno == EAGAIN || errno == EINTR)
- return 0;
+ if (r != BUXTON_ERROR_NONE) {
+ if (r == BUXTON_ERROR_INTERRUPTED)
+ return BUXTON_ERROR_NONE;
- return -1;
+ return r;
}
callback(user_data, type, data, len);
free(data);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int recv_data(struct recv_info *rif, uint32_t len)
struct header *hdr;
uint8_t buf[MSG_MTU];
- assert(rif);
+ if (!rif) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = recv(rif->fd, buf, sizeof(*hdr) + len, 0);
if (r <= 0) {
bxt_dbg("recv: fd %d closed", rif->fd);
} else {
if (errno == EAGAIN)
- return 0;
+ return BUXTON_ERROR_NONE;
bxt_err("recv: fd %d errno %d", rif->fd, errno);
}
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (r < sizeof(*hdr) + len) {
bxt_err("recv: fd %d expect %u > received %d",
rif->fd, (uint32_t)sizeof(*hdr) + len, r);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
hdr = (struct header *)buf;
memcpy(&rif->data[rif->recved], hdr->data, len);
rif->recved += len;
- return 0;
+ return BUXTON_ERROR_NONE;
}
/* recv_lock should be unlocked before return */
int r;
struct recv_info *rif;
- assert(fd >= 0);
- assert(hdr);
- assert(callback);
+ if (!callback || !hdr || fd < 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
switch (hdr->type) {
case MSG_FIRST:
if (!rif) {
flush_data(fd, sizeof(*hdr) + hdr->len);
pthread_mutex_unlock(&recv_lock);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
r = recv_data(rif, hdr->len);
break;
bxt_err("recv: fd %d seq %u not exist", fd, hdr->seq);
flush_data(fd, sizeof(*hdr) + hdr->len);
pthread_mutex_unlock(&recv_lock);
- return -1;
+ return BUXTON_ERROR_NOT_EXIST;
}
r = recv_data(rif, hdr->len);
- if (hdr->type == MSG_LAST && r == 0 && rif->recved < rif->len) {
+ if (hdr->type == MSG_LAST && r == BUXTON_ERROR_NONE && rif->recved < rif->len) {
bxt_err("recv: fd %d packet lost %d/%d",
fd, rif->recved, rif->len);
- r = -1;
+ r = BUXTON_ERROR_IO_ERROR;
}
break;
default:
bxt_err("recv: fd %d unknown type %x", fd, hdr->type);
rif = NULL;
- r = -1;
+ r = BUXTON_ERROR_IO_ERROR;
break;
}
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
remove_rif(rif);
pthread_mutex_unlock(&recv_lock);
- return -1;
+ return r;
}
if (hdr->type == MSG_LAST) {
recv_list = g_list_remove(recv_list, rif);
pthread_mutex_unlock(&recv_lock);
- assert(rif->callback);
- rif->callback(rif->user_data, rif->type, rif->data, rif->len);
- remove_rif(rif);
+ if (rif->callback) {
+ rif->callback(rif->user_data, rif->type, rif->data, rif->len);
+ remove_rif(rif);
+ } else {
+ bxt_err("rif->callback does not exist");
+ return BUXTON_ERROR_NOT_EXIST;
+ }
} else {
pthread_mutex_unlock(&recv_lock);
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
int proto_recv_async(int fd, recv_callback callback, void *user_data)
struct header hdr;
if (fd < 0 || !callback) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
pthread_mutex_lock(&recv_lock);
bxt_dbg("recv: fd %d closed", fd);
} else {
if (errno == EAGAIN || errno == EINTR)
- return 0;
-
- bxt_err("recv: fd %d errno %d", fd, errno);
+ return BUXTON_ERROR_NONE;
}
+ bxt_err("recv: fd %d errno %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (hdr.type == MSG_SINGLE)
fd, hdr.total, MSG_TOTAL_MAX);
flush_data(fd, sizeof(hdr) + hdr.len);
pthread_mutex_unlock(&recv_lock);
- errno = EMSGSIZE;
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
return proto_recv_frag(fd, &hdr, callback, user_data);
uint8_t buf[MSG_MTU];
if (fd < 0 || !data || len <= 0) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (len <= (MSG_MTU - sizeof(*hdr)))
continue;
bxt_err("send: fd %d poll errno %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
hdr->len = len - sent;
r = send(fd, hdr, sizeof(*hdr) + hdr->len, MSG_NOSIGNAL);
if (r == -1) {
bxt_err("send: fd %d errno %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
sent += hdr->len;
}
bxt_dbg("send: fd %d sent %d", fd, sent);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int proto_send(int fd, enum message_type type, uint8_t *data, int32_t len)
uint8_t *buf;
struct pollfd fds[1];
- assert(fd >= 0);
- assert(data);
- assert(len > 0);
+ if (!data || len <= 0 || fd < 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
bxt_dbg("send: fd %d type %d len %d start", fd, type, len);
buf = malloc(sizeof(*hdr) + len);
if (!buf) {
bxt_err("send: send buffer alloc error");
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
hdr = (struct header *)buf;
bxt_err("send: fd %d poll errno %d", fd, errno);
free(buf);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (r == 0) {
bxt_err("send: fd %d poll timeout", fd);
free(buf);
- errno = ETIMEDOUT;
- return -1;
+ return BUXTON_ERROR_TIME_OUT;
}
} while (r < 0);
}
if (r == -1) {
bxt_err("send: fd %d errno %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (r != sizeof(*hdr) + len)
bxt_dbg("send: fd %d sent %d", fd, r - sizeof(*hdr));
- return 0;
+ return BUXTON_ERROR_NONE;
}
int proto_recv(int fd, enum message_type *type, uint8_t **data, int32_t *len)
struct header hdr;
uint8_t *_data;
- assert(fd >= 0);
- assert(type);
- assert(data);
- assert(len);
+ if (!type || !data || !len || fd < 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = recv(fd, &hdr, sizeof(hdr), 0);
if (r <= 0) {
if (r == 0) {
bxt_dbg("recv: fd %d closed", fd);
} else {
- if (errno != EAGAIN && errno != EINTR)
+ if (errno == EAGAIN || errno == EINTR) {
+ bxt_info("recv: fd %d %s", fd, errno == EAGAIN ? "EAGAIN" : "EINTR");
+ return BUXTON_ERROR_INTERRUPTED;
+ } else {
bxt_err("recv: fd %d errno %d", fd, errno);
+ }
}
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (r != sizeof(hdr) || hdr.len == 0 || hdr.type != MSG_SINGLE
|| hdr.total != hdr.len) {
bxt_err("recv: fd %d Invalid message", fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (hdr.total >= MSG_SINGLE_MAX) {
bxt_err("recv: fd %d message size %d >= %d",
fd, hdr.total, MSG_SINGLE_MAX);
flush_data(fd, hdr.total);
- errno = EMSGSIZE;
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
_data = malloc(hdr.total);
if (!_data) {
+ bxt_err("out of memory");
flush_data(fd, hdr.total);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
r = recv(fd, _data, hdr.total, 0);
free(_data);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (r != hdr.total) {
bxt_err("recv: fd %d expect size %d > received %d",
fd, hdr.total, r);
free(_data);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
*type = hdr.mtype;
*data = _data;
*len = hdr.total;
- return 0;
+ return BUXTON_ERROR_NONE;
}
while (*p) {
/* from 0x21 '!' to 0x7e '~' */
if (*p < 0x21 || *p > 0x7e) {
- errno = EINVAL;
bxt_err("Key name has invalid character '%x'", *p);
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
p++;
len++;
if (len > KEY_NAME_MAX) {
- errno = ENAMETOOLONG;
bxt_err("Key name is too long");
- return -1;
+ return BUXTON_ERROR_DATA_TOO_LONG;
}
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
static inline int check_val(const char *s)
{
if (!s)
- return 0;
+ return BUXTON_ERROR_NONE;
- if (strlen(s) > VALUE_MAX) {
- errno = EMSGSIZE;
- return -1;
- }
+ if (strlen(s) > VALUE_MAX)
+ return BUXTON_ERROR_DATA_TOO_LONG;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int check_values(const char *rpriv, const char *wpriv,
int r;
r = check_val(rpriv);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_err("Read priv. string length is too long");
- return -1;
+ return r;
}
r = check_val(wpriv);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_err("Write priv. string length is too long");
- return -1;
+ return r;
}
if (val && val->type == BUXTON_TYPE_STRING) {
r = check_val(val->value.s);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_err("Value string length is too long");
- return -1;
+ return r;
}
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
static GVariant *val_to_gv(const struct buxton_value *val)
return gv;
}
-static uint8_t *gv_to_data(GVariant *gv, int *len)
+static int gv_to_data(GVariant *gv, int *len, uint8_t **data)
{
- uint8_t *data;
+ uint8_t *_data;
int _len;
- assert(gv);
- assert(len);
+ if (!(gv) || !len) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
_len = g_variant_get_size(gv);
- assert(_len > 0);
+ if (_len <= 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- data = malloc(_len);
- if (!data)
- return NULL;
+ _data = malloc(_len);
+ if (!_data) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
- g_variant_store(gv, data);
+ g_variant_store(gv, _data);
*len = _len;
+ *data = _data;
- return data;
+ return BUXTON_ERROR_NONE;
}
/*
int r;
if (!rpriv || !wpriv || !val || !data || !len) {
- errno = EINVAL;
bxt_err("serialize data: invalid argument:%s%s%s%s%s",
rpriv ? "" : " read priv",
wpriv ? "" : " write priv",
data ? "" : " data",
len ? "" : " len");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = check_values(rpriv, wpriv, val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
v = val_to_gv(val);
- if (!v) {
- errno = EINVAL;
- return -1;
- }
+ if (!v)
+ return BUXTON_ERROR_INVALID_PARAMETER;
vv = g_variant_new("(ssv)", rpriv, wpriv, v);
- assert(vv);
+ if (!vv) {
+ bxt_err("out of memory");
+ g_variant_unref(v);
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
gv = g_variant_new_variant(vv);
- assert(gv);
+ if (!gv) {
+ bxt_err("out of memory");
+ g_variant_unref(vv);
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
- _data = gv_to_data(gv, &_len);
+ r = gv_to_data(gv, &_len, &_data);
g_variant_unref(gv);
- if (!_data)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
*data = _data;
*len = _len;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int gv_to_val(GVariant *v, struct buxton_value *val)
const char *t;
const char *s;
- assert(v);
- assert(val);
+ if (!v || !val) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
t = g_variant_get_type_string(v);
- assert(t);
+ if (!t) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
+
if (!strncmp(t, "()", sizeof("()"))) {
val->type = BUXTON_TYPE_UNKNOWN;
- return 0;
+ return BUXTON_ERROR_NONE;
}
switch (*t) {
val->type = BUXTON_TYPE_STRING;
s = g_variant_get_string(v, NULL);
- assert(s);
-
+ if (!s) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
val->value.s = strdup(s);
- if (!val->value.s)
- return -1;
-
+ if (!val->value.s) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
break;
case 'i':
val->type = BUXTON_TYPE_INT32;
break;
default:
bxt_err("DeSerialz: Invalid variant type: %s", t);
- errno = EBADMSG;
-
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int gv_to_values(GVariant *gv, char **rpriv, char **wpriv,
const char *wp;
int r;
- assert(gv);
+ if (!gv) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (!rpriv && !wpriv && !val)
- return 0;
+ return BUXTON_ERROR_NONE;
vt = g_variant_get_type_string(gv);
if (strncmp(vt, "(ssv)", sizeof("(ssv)"))) {
bxt_err("Deserialize: Unsupported type: %s", vt);
- errno = EBADMSG;
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
g_variant_get(gv, "(&s&sv)", &rp, &wp, &v);
- assert(rp);
- assert(wp);
- assert(v);
+
+ if (!rp || !wp || !v) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (rpriv) {
*rpriv = strdup(rp);
if (!*rpriv) {
+ bxt_err("out of memory");
g_variant_unref(v);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
}
if (rpriv)
free(*rpriv);
+ bxt_err("out of memory");
g_variant_unref(v);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
}
if (val) {
memset(val, 0, sizeof(*val));
r = gv_to_val(v, val);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
if (rpriv)
free(*rpriv);
free(*wpriv);
g_variant_unref(v);
- return -1;
+ return r;
}
}
g_variant_unref(v);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int deserialz_data(uint8_t *data, int len,
errno = EINVAL;
bxt_err("Deserialize data: invalid argument:%s%s",
data ? "" : " data", len > 0 ? "" : " len");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
gv = g_variant_new_from_data(G_VARIANT_TYPE("v"),
data, len, TRUE, NULL, NULL);
- assert(gv);
+
+ if (!gv) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
g_variant_get(gv, "v", &v);
- assert(v);
+ if (!v) {
+ bxt_err("Invalid parameter");
+ g_variant_unref(gv);
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = gv_to_values(v,
rpriv ? &_rpriv : NULL,
g_variant_unref(v);
g_variant_unref(gv);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
if (rpriv)
*rpriv = _rpriv;
if (val)
*val = _val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
void free_request(struct request *req)
{
if (!val) {
bxt_err("Serialize: value is NULL");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
switch (val->type) {
case BUXTON_TYPE_STRING:
if (!val->value.s) {
bxt_err("Serialize: value has NULL string");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
break;
case BUXTON_TYPE_INT32:
break;
default:
bxt_err("Serialize: buxton_value has unknown type");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int check_request(enum message_type type,
case MSG_SET_WP:
case MSG_SET_RP:
r = check_value(val);
- if (r == -1)
- goto err;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
case MSG_GET:
case MSG_UNSET:
case MSG_NOTIFY:
case MSG_CTRL:
if (!key || !*key) {
bxt_err("Serialize: key is NULL or empty string");
- goto err;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = check_key_name(key);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
case MSG_LIST:
break;
default:
bxt_err("Serialize: message type is invalid: %d", type);
- goto err;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
-
-err:
- errno = EINVAL;
-
- return -1;
+ return BUXTON_ERROR_NONE;
}
int serialz_request(const struct request *req, uint8_t **data, int *len)
uint8_t *_data;
if (!data || !len || !req || !req->layer) {
- errno = EINVAL;
bxt_err("Serialize request: invalid argument:%s%s%s%s",
data ? "" : " data",
len ? "" : " len",
req ? "" : " req",
req && !req->layer ? " layer" : "");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = check_request(req->type, req->key, req->val);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
v = val_to_gv(req->val);
if (!v) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
vv = g_variant_new("(uuissssv)",
req->wpriv ? req->wpriv : "",
req->key ? req->key : "",
v);
- assert(vv);
+ if (!vv) {
+ bxt_err("out of memory");
+ g_variant_unref(v);
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
gv = g_variant_new("(qv)", req->type, vv);
- assert(gv);
+ if (!gv) {
+ bxt_err("out of memory");
+ g_variant_unref(vv);
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
- _data = gv_to_data(gv, &_len);
+ r = gv_to_data(gv, &_len, &_data);
g_variant_unref(gv);
- if (!_data)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
*data = _data;
*len = _len;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static inline int _strdup(const char *src, char **dest)
{
char *s;
- assert(dest);
+ if (!dest) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (!src) {
*dest = NULL;
- return 0;
+ return BUXTON_ERROR_NONE;
}
s = strdup(src);
- if (!s)
- return -1;
+ if (!s) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
*dest = s;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int set_req(struct buxton_value *val, const char *lnm, uid_t uid,
{
int r;
- assert(req);
+ if (!req) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
req->val = val;
if (lnm && *lnm) {
- req->layer = layer_create(lnm);
- if (!req->layer)
- return -1;
+ r = layer_create(lnm, &req->layer);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
req->layer->uid = uid;
req->layer->type = type;
}
r = _strdup(rp, &req->rpriv);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = _strdup(wp, &req->wpriv);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = _strdup(key, &req->key);
- if (r == -1)
- return -1;
- return 0;
+ return r;
}
static int gv_to_req(GVariant *gv, struct request *req)
const char *vt;
uint32_t uid;
int32_t type;
- const char *lnm;
- const char *key;
- const char *rp;
- const char *wp;
- GVariant *v;
+ const char *lnm = NULL;
+ const char *key = NULL;
+ const char *rp = NULL;
+ const char *wp = NULL;
+ GVariant *v = NULL;
int r;
struct buxton_value *val;
- assert(gv);
- assert(req);
+ if (!gv || !req) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
vt = g_variant_get_type_string(gv);
if (strncmp(vt, "(uuissssv)", sizeof("(uuissssv)"))) {
bxt_err("DeSerialz: Unsupported type: %s", vt);
- errno = EBADMSG;
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
val = calloc(1, sizeof(*val));
- if (!val)
- return -1;
+ if (!val) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
g_variant_get(gv, "(uui&s&s&s&sv)", &req->msgid, &uid, &type,
&lnm, &rp, &wp, &key, &v);
- assert(v);
- assert(lnm);
- assert(rp);
- assert(wp);
- assert(key);
+
+ if (!v || !lnm || !rp || !wp || !key) {
+ if (v)
+ g_variant_unref(v);
+ value_free(val);
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = gv_to_val(v, val);
g_variant_unref(v);
- if (r == -1) {
- free(val);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ value_free(val);
+ return r;
}
if (val->type == BUXTON_TYPE_UNKNOWN) {
}
r = set_req(val, lnm, uid, type, rp, wp, key, req);
- if (r == -1)
+ if (r != BUXTON_ERROR_NONE)
free_request(req);
return r;
int deserialz_request(uint8_t *data, int len, struct request *req)
{
GVariant *gv;
- GVariant *v;
+ GVariant *v = NULL;
int r;
struct request _req;
if (!data || len <= 0 || !req) {
- errno = EINVAL;
bxt_err("Deserialize request: invalid argument:%s%s%s",
data ? "" : " data",
len > 0 ? "" : " len",
req ? "" : " req");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
gv = g_variant_new_from_data(G_VARIANT_TYPE("(qv)"),
data, len, TRUE, NULL, NULL);
- assert(gv);
+
+ if (!gv) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
memset(&_req, 0, sizeof(_req));
g_variant_get(gv, "(qv)", &_req.type, &v);
- assert(v);
+ if (!v) {
+ g_variant_unref(gv);
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = gv_to_req(v, &_req);
g_variant_unref(v);
g_variant_unref(gv);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
*req = _req;
- return 0;
+ return BUXTON_ERROR_NONE;
}
void free_response(struct response *res)
case MSG_LIST:
if (!names) {
bxt_err("Serialize: names is NULL");
- goto err;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
break;
case MSG_GET:
case MSG_GET_WP:
case MSG_GET_RP:
r = check_value(val);
- if (r == -1)
- goto err;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
break;
case MSG_SET:
case MSG_CREAT:
case MSG_CTRL:
break;
case MSG_NOTI:
- errno = ENOTSUP;
bxt_err("Serialize: MSG_NOTI type has no response");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
default:
- goto err;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
-
-err:
- errno = EINVAL;
-
- return -1;
+ return BUXTON_ERROR_NONE;
}
static int res_to_gv(enum message_type type, int32_t res,
GVariantBuilder *builder;
GVariant *v;
- assert(gv);
+ if (!gv) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
if (res) {
*gv = g_variant_new_tuple(NULL, 0);
- return 0;
+ return BUXTON_ERROR_NONE;
}
switch (type) {
case MSG_LIST:
+ if (!names) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
- assert(names);
+
while (*names) {
g_variant_builder_add(builder, "s", *names);
names++;
}
v = g_variant_new("as", builder);
- assert(v);
g_variant_builder_unref(builder);
break;
case MSG_GET:
if (val) {
v = val_to_gv(val);
if (!v) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
} else {
v = g_variant_new_tuple(NULL, 0);
*gv = v;
- return 0;
+ return BUXTON_ERROR_NONE;
}
int serialz_response(enum message_type type, uint32_t msgid, int32_t res,
uint8_t *_data;
if (!data || !len) {
- errno = EINVAL;
bxt_err("Serialize response: invalid argument:%s%s",
data ? "" : " data",
len ? "" : " len");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = check_response(type, res, val, names);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
r = res_to_gv(type, res, val, names, &v);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
- assert(v);
vv = g_variant_new("(uiuv)", msgid, res, nmlen, v);
- assert(vv);
+ if (!vv) {
+ g_variant_unref(v);
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
gv = g_variant_new("(qv)", type, vv);
- assert(gv);
+ if (!gv) {
+ g_variant_unref(vv);
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
- _data = gv_to_data(gv, &_len);
+ r = gv_to_data(gv, &_len, &_data);
g_variant_unref(gv);
- if (!_data)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
*data = _data;
*len = _len;
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int gv_to_res_list(GVariant *gv, struct response *res)
GVariantIter iter;
gsize len;
const char *s;
- int i;
+ int i, ret = BUXTON_ERROR_NONE;
g_variant_iter_init(&iter, gv);
len = g_variant_iter_n_children(&iter);
res->names = calloc(len + 1, sizeof(void *));
- if (!res->names)
- return -1;
+ if (!res->names) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
i = 0;
while (g_variant_iter_next(&iter, "&s", &s)) {
- assert(s);
+ if (!s) {
+ bxt_err("Invalid parameter %d %d", i, len);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
+ break;
+ }
+
res->names[i] = strdup(s);
- if (!res->names[i])
+ if (!res->names[i]) {
+ bxt_err("out of memory");
+ ret = BUXTON_ERROR_OUT_OF_MEMORY;
break;
- i++;
+ }
- assert(i <= len);
+ i++;
+ if (i > len) {
+ bxt_err("Invalid parameter %d %d", i, len);
+ ret = BUXTON_ERROR_INVALID_PARAMETER;
+ break;
+ }
}
/* NULL terminated */
res->names[i] = NULL;
- if (i < len) {
+ if (i < len || ret != BUXTON_ERROR_NONE) {
buxton_free_keys(res->names);
- return -1;
+ return ret == BUXTON_ERROR_NONE ? BUXTON_ERROR_INVALID_PARAMETER : ret;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int gv_to_res(GVariant *gv, struct response *res)
struct buxton_value *val;
int r;
- assert(gv);
- assert(res);
+ if (!gv || !res) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
vt = g_variant_get_type_string(gv);
if (strncmp(vt, "(uiuv)", sizeof("(uiuv)"))) {
bxt_err("DeSerialz: Unsupported type: %s", vt);
- errno = EBADMSG;
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
g_variant_get(gv, "(uiuv)", &res->msgid, &res->res, &res->nmlen, &v);
if (res->res) {
g_variant_unref(v);
- return 0;
+ return BUXTON_ERROR_NONE;
}
if (res->type == MSG_LIST) {
val = calloc(1, sizeof(*val));
if (!val) {
+ bxt_err("out of memory");
g_variant_unref(v);
- return -1;
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
r = gv_to_val(v, val);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
free(val);
g_variant_unref(v);
- return -1;
+ return r;
}
g_variant_unref(v);
res->val = val;
- return 0;
+ return BUXTON_ERROR_NONE;
}
int deserialz_response(uint8_t *data, int len, struct response *res)
data ? "" : " data",
len > 0 ? "" : " len",
res ? "" : " response");
- return -1;
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
gv = g_variant_new_from_data(G_VARIANT_TYPE("(qv)"),
data, len, TRUE, NULL, NULL);
- assert(gv);
+ if (!gv) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
memset(&_res, 0, sizeof(_res));
g_variant_get(gv, "(qv)", &_res.type, &v);
- assert(v);
+ if (!v) {
+ g_variant_unref(gv);
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = gv_to_res(v, &_res);
g_variant_unref(v);
g_variant_unref(gv);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
free_response(&_res);
- return -1;
+ return r;
}
*res = _res;
- return 0;
+ return BUXTON_ERROR_NONE;
}
../common/direct.c
../common/proto.c
../common/cache.c
+ ../common/buxton_error.c
)
ADD_EXECUTABLE(${TARGET} ${SRC})
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
#include "log.h"
#include "cynara.h"
+#include "buxton_error.h"
#define BUXTON_CYNARA_PERMISSIVE_MODE "BUXTON_CYNARA_PERMISSIVE_MODE"
static enum buxton_cynara_res check_cache(const char *clabel, const char *sess,
const char *uid, const char *priv)
{
- int r;
-
- assert(cynara);
- assert(clabel);
- assert(sess);
- assert(uid);
- assert(priv);
+ int r = cynara_async_check_cache(cynara, clabel, sess, uid, priv);
- r = cynara_async_check_cache(cynara, clabel, sess, uid, priv);
switch (r) {
case CYNARA_API_ACCESS_ALLOWED:
r = BUXTON_CYNARA_ALLOWED;
int r;
struct bxt_cyn_cb *cyn_cb;
- assert(cynara);
- assert(cynara_tbl);
-
- assert(client);
- assert(clabel);
- assert(sess);
- assert(uid);
- assert(priv);
- assert(callback);
-
cyn_cb = calloc(1, sizeof(*cyn_cb));
- if (!cyn_cb)
+ if (!cyn_cb) {
+ bxt_err("out of memory");
return BUXTON_CYNARA_ERROR;
+ }
r = cynara_async_create_request(cynara, clabel, sess, uid, priv,
&cyn_cb->id, resp_cb, cyn_cb);
char uid_str[16];
if (!client || !client_label || !session || !priv || !callback) {
- errno = EINVAL;
bxt_err("cynara check: invalid argument:%s%s%s%s%s",
client ? "" : " client",
client_label ? "" : " client_label",
if (!cynara) {
bxt_err("Cynara is not initialized");
- errno = ENOTCONN;
return BUXTON_CYNARA_ERROR;
}
char *skip;
if (cynara)
- return 0;
+ return BUXTON_ERROR_NONE;
cynara_skip = FALSE;
}
cynara_tbl = g_hash_table_new_full(NULL, NULL, NULL, free_cb);
- if (!cynara_tbl)
- return -1;
+ if (!cynara_tbl) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
r = cynara_async_initialize(&cynara, NULL, status_cb, NULL);
if (r != CYNARA_API_SUCCESS) {
cyn_err("init", r);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
void buxton_cynara_exit(void)
int r;
struct signalfd_siginfo si;
- assert(bxtd);
-
r = read(fd, &si, sizeof(struct signalfd_siginfo));
if (r == -1) {
bxt_err("Read signalfd: %d", errno);
- return G_SOURCE_REMOVE;
+ return G_SOURCE_CONTINUE;
}
if (r != sizeof(struct signalfd_siginfo)) {
switch (si.ssi_signo) {
case SIGINT:
case SIGTERM:
- assert(bxtd->loop);
g_main_loop_quit(bxtd->loop);
break;
case SIGPIPE:
{
struct bxt_client *cli = data;
- assert(cli);
- assert(cli->bxtd);
- assert(cli->bxtd->clients);
+ if (!cli || !cli->bxtd || !cli->bxtd->clients)
+ return G_SOURCE_REMOVE;
buxton_cynara_cancel(cli);
uint8_t *data;
int len;
+ if (resp->res != BUXTON_ERROR_NONE)
+ bxt_err("send res: error %d", resp->res);
+
r = serialz_response(resp->type, resp->msgid, resp->res, resp->val,
resp->nmlen, resp->names, &data, &len);
- if (r == -1) {
- bxt_err("send res: fd %d msgid %u: serialize error %d",
- cli->fd, resp->msgid, errno);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("send res: fd %d msgid %u: serialize error : %d",
+ cli->fd, resp->msgid, r);
return;
}
free(data);
- if (r == -1)
- bxt_err("send res: error %d", errno);
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("send res: error %d", r);
}
-static char *get_search_key_u(const struct buxton_layer *layer, const char *key)
+static int get_search_key_u(const struct buxton_layer *layer, const char *key, char **lykey)
{
char uid[16];
char *u;
const struct layer *ly;
+ int ret;
- ly = conf_get_layer(layer->name);
- if (!ly)
- return NULL;
+ ret = conf_get_layer(layer->name, &ly);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
if (ly->type == LAYER_USER) {
snprintf(uid, sizeof(uid), "%d", layer->uid);
u = NULL;
}
- return get_search_key(layer, key, u);
+ return get_search_key(layer, key, u, lykey);
}
static gboolean _send_notis(gpointer key, gpointer value, gpointer data)
_data = g_base64_decode(g_data, &_len);
r = proto_send(cli->fd, MSG_NOTI, (uint8_t *)_data, (int)_len);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_err("send noti again key : %s pid : %d errno : %d",
(char *)key, cli->cred.pid, errno);
if (errno == EWOULDBLOCK || errno == EAGAIN) {
int len;
gchar *_data;
- assert(bxtd);
- assert(rqst);
-
- lykey = get_search_key_u(rqst->layer, rqst->key);
- if (!lykey)
+ r = get_search_key_u(rqst->layer, rqst->key, &lykey);
+ if (r != BUXTON_ERROR_NONE)
return;
noti = g_hash_table_lookup(bxtd->notis, lykey);
req.val = rqst->val;
r = serialz_request(&req, &data, &len);
- if (r == -1)
+ if (r != BUXTON_ERROR_NONE)
return;
for (l = noti->clients; l; l = g_list_next(l)) {
struct bxt_client *cli = l->data;
r = proto_send(cli->fd, req.type, data, len);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_err("send notis: cli pid : %d label : %s key : %s error %d",
cli->cred.pid, cli->label, rqst->key, errno);
if (errno == EWOULDBLOCK || errno == EAGAIN) {
static void proc_set(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
-
- assert(rqst);
- assert(resp);
+ resp->res = direct_set(rqst->layer, rqst->key, rqst->val);
- r = direct_set(rqst->layer, rqst->key, rqst->val);
- if (r == -1) {
- resp->res = errno;
- return;
- }
- _write_file_log(LOG_SET, cli, rqst);
-
- resp->res = 0;
+ if (resp->res == BUXTON_ERROR_NONE)
+ _write_file_log(LOG_SET, cli, rqst);
}
static void proc_get(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
struct buxton_value *val;
- assert(rqst);
- assert(resp);
-
val = calloc(1, sizeof(*val));
if (!val) {
- resp->res = ENOMEM;
+ bxt_err("out of memory");
+ resp->res = BUXTON_ERROR_OUT_OF_MEMORY;
return;
}
- r = direct_get(rqst->layer, rqst->key, val);
- if (r == -1) {
+ resp->res = direct_get(rqst->layer, rqst->key, val);
+ if (resp->res != BUXTON_ERROR_NONE) {
free(val);
- resp->res = errno;
return;
}
- resp->res = 0;
resp->val = val;
}
static void proc_list(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
-
- assert(rqst);
- assert(resp);
-
- r = direct_list(rqst->layer, &resp->names, &resp->nmlen, true);
- resp->res = (r == -1) ? errno : 0;
+ resp->res = direct_list(rqst->layer, &resp->names, &resp->nmlen, true);
}
static void proc_create(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
-
- assert(cli);
- assert(rqst);
- assert(resp);
if (cli->cred.uid != 0) {
- resp->res = EPERM;
+ resp->res = BUXTON_ERROR_PERMISSION_DENIED;
return;
}
- r = direct_create(rqst->layer, rqst->key, rqst->rpriv, rqst->wpriv,
+ resp->res = direct_create(rqst->layer, rqst->key, rqst->rpriv, rqst->wpriv,
rqst->val);
- resp->res = (r == -1) ? errno : 0;
}
static void proc_unset(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
-
- assert(cli);
- assert(rqst);
- assert(resp);
if (cli->cred.uid != 0) {
- resp->res = EPERM;
+ resp->res = BUXTON_ERROR_PERMISSION_DENIED;
return;
}
- r = direct_unset(rqst->layer, rqst->key);
- resp->res = (r == -1) ? errno : 0;
+ resp->res = direct_unset(rqst->layer, rqst->key);
}
static void add_cli(struct bxt_noti *noti, struct bxt_client *client)
static void proc_notify(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
char *lykey;
struct bxt_noti *noti;
- assert(cli);
- assert(rqst);
- assert(resp);
-
- assert(rqst->layer);
- assert(rqst->key);
-
- r = direct_check(rqst->layer, rqst->key);
- if (r == -1) {
- resp->res = errno;
+ resp->res = direct_check(rqst->layer, rqst->key);
+ if (resp->res != BUXTON_ERROR_NONE)
return;
- }
- lykey = get_search_key_u(rqst->layer, rqst->key);
- if (!lykey) {
- resp->res = errno;
+ resp->res = get_search_key_u(rqst->layer, rqst->key, &lykey);
+ if (resp->res != BUXTON_ERROR_NONE)
return;
- }
noti = g_hash_table_lookup(cli->bxtd->notis, lykey);
if (!noti) {
noti = calloc(1, sizeof(*noti));
if (!noti) {
- resp->res = errno;
+ bxt_err("out of memory");
+ resp->res = BUXTON_ERROR_OUT_OF_MEMORY;
return;
}
noti->layer_key = lykey;
_write_file_log(LOG_REG_NOTI, cli, rqst);
- resp->res = 0;
+ resp->res = BUXTON_ERROR_NONE;
}
static void proc_unnotify(struct bxt_client *cli,
char *lykey;
struct bxt_noti *noti;
- assert(cli);
- assert(rqst);
- assert(resp);
-
- assert(rqst->layer);
- assert(rqst->key);
-
- lykey = get_search_key_u(rqst->layer, rqst->key);
- if (!lykey) {
- resp->res = errno;
+ resp->res = get_search_key_u(rqst->layer, rqst->key, &lykey);
+ if (resp->res != BUXTON_ERROR_NONE)
return;
- }
noti = g_hash_table_lookup(cli->bxtd->notis, lykey);
if (!noti) {
+ bxt_dbg("%s does not exist", lykey);
free(lykey);
- resp->res = ENOENT;
+ resp->res = BUXTON_ERROR_NOT_EXIST;
return;
}
_write_file_log(LOG_UNREG_NOTI, cli, rqst);
- resp->res = 0;
+ resp->res = BUXTON_ERROR_NONE;
free(lykey);
}
static void proc_set_priv(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
enum buxton_priv_type type;
- assert(cli);
- assert(rqst);
- assert(resp);
-
if (cli->cred.uid != 0) {
- resp->res = EPERM;
+ resp->res = BUXTON_ERROR_PERMISSION_DENIED;
return;
}
else
type = BUXTON_PRIV_READ;
- r = direct_set_priv(rqst->layer, rqst->key, type, rqst->val->value.s);
- resp->res = (r == -1) ? errno : 0;
+ resp->res = direct_set_priv(rqst->layer, rqst->key, type, rqst->val->value.s);
}
static void proc_get_priv(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- int r;
enum buxton_priv_type type;
struct buxton_value *val;
- assert(rqst);
- assert(resp);
-
val = calloc(1, sizeof(*val));
if (!val) {
- resp->res = ENOMEM;
+ bxt_err("out of memory");
+ resp->res = BUXTON_ERROR_OUT_OF_MEMORY;
return;
}
type = BUXTON_PRIV_READ;
val->type = BUXTON_TYPE_PRIVILEGE;
- r = direct_get_priv(rqst->layer, rqst->key, type, &val->value.s);
- if (r == -1) {
+ resp->res = direct_get_priv(rqst->layer, rqst->key, type, &val->value.s);
+ if (resp->res != BUXTON_ERROR_NONE) {
free(val);
- resp->res = errno;
return;
}
- resp->res = 0;
resp->val = val;
}
{
g_hash_table_foreach(cli->bxtd->clients, (GHFunc)update_label,
&(cli->cred.pid));
- return 0;
+ return BUXTON_ERROR_NONE;
}
static void proc_control(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- assert(cli);
- assert(rqst);
- assert(resp);
if (!strcmp(rqst->key, "set_security_mode")) {
if (cli->cred.uid != 0) {
- resp->res = EPERM;
+ resp->res = BUXTON_ERROR_PERMISSION_DENIED;
return;
}
if (rqst->val->value.b == TRUE)
buxton_cynara_enable();
else
buxton_cynara_disable();
- resp->res = 0;
+ resp->res = BUXTON_ERROR_NONE;
return;
} else if (!strcmp(rqst->key, "update_client_label")) {
resp->res = update_client_label(cli);
return;
}
- resp->res = ENOTSUP;
+ resp->res = BUXTON_ERROR_INVALID_OPERATION;
}
typedef void (*proc_func)(struct bxt_client *cli,
static void proc_msg(struct bxt_client *cli,
struct request *rqst, struct response *resp)
{
- assert(cli);
- assert(rqst);
- assert(resp);
+ if (!cli || !rqst || !resp || !rqst->layer) {
+ bxt_err("Invalid parameter");
+ return;
+ }
if (rqst->type <= MSG_UNKNOWN || rqst->type >= MSG_MAX) {
bxt_err("proc msg: invalid type %d", rqst->type);
- resp->res = EINVAL;
+ resp->res = BUXTON_ERROR_INVALID_PARAMETER;
return;
}
- assert(rqst->layer);
if (cli->cred.uid != 0 && cli->cred.uid != rqst->layer->uid) {
/* Only root can access other user's */
- resp->res = EPERM;
+ resp->res = BUXTON_ERROR_PERMISSION_DENIED;
return;
}
if (!proc_funcs[rqst->type]) {
bxt_err("proc msg: %d not supported", rqst->type);
- resp->res = ENOTSUP;
+ resp->res = BUXTON_ERROR_INVALID_OPERATION;
return;
}
struct request *rqst = data;
struct response resp;
- assert(rqst);
+ if (!rqst) {
+ bxt_err("Invalid parameter");
+ return;
+ }
memset(&resp, 0, sizeof(resp));
resp.type = rqst->type;
break;
case BUXTON_CYNARA_DENIED:
default:
- resp.res = EPERM;
+ resp.res = BUXTON_ERROR_PERMISSION_DENIED;
break;
}
enum buxton_priv_type type;
char *priv;
- assert(cli);
- assert(rqst);
-
switch (rqst->type) {
case MSG_SET:
case MSG_GET:
type = BUXTON_PRIV_READ;
r = direct_get_priv(rqst->layer, rqst->key, type, &priv);
- if (r == -1) {
- r = BUXTON_CYNARA_ERROR;
+ if (r != BUXTON_ERROR_NONE)
break;
- }
bxt_dbg("priv '%s'", priv);
struct response resp;
rqst = calloc(1, sizeof(*rqst));
- if (!rqst)
- return -1;
+ if (!rqst) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
r = deserialz_request(data, len, rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
free(rqst);
- return -1;
+ return r;
}
r = check_priv(cli, rqst);
/* wait for cynara response, rqst should be freed in callback */
if (r == BUXTON_CYNARA_UNKNOWN)
- return 0;
+ return BUXTON_ERROR_NONE;
memset(&resp, 0, sizeof(resp));
resp.msgid = rqst->msgid;
if (r != BUXTON_CYNARA_ALLOWED)
- resp.res = r == BUXTON_CYNARA_DENIED ? EPERM : errno;
+ resp.res = r == BUXTON_CYNARA_DENIED ?
+ BUXTON_ERROR_PERMISSION_DENIED : r;
else
proc_msg(cli, rqst, &resp);
free_request(rqst);
free(rqst);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int proc_client_msg(struct bxt_client *cli)
enum message_type type;
r = proto_recv(cli->fd, &type, &data, &len);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
switch (type) {
case MSG_SET:
case MSG_NOTI:
default:
bxt_err("proc msg: Invalid message type %d", type);
- r = -1;
+ r = BUXTON_ERROR_INVALID_PARAMETER;
break;
}
}
r = proc_client_msg(cli);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
cli->fd_id = 0;
g_idle_add(del_client, cli);
return G_SOURCE_REMOVE;
struct bxt_client *cli;
r = sock_set_client(fd);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
close(fd);
return;
}
cli = calloc(1, sizeof(*cli));
if (!cli) {
- bxt_err("Client %d: %d", fd, errno);
+ bxt_err("Client %d: out of memory", fd);
close(fd);
return;
}
struct sockaddr sa;
socklen_t addrlen;
- assert(bxtd);
-
bxt_dbg("Accept: fd %d cond %x", fd, cond);
addrlen = sizeof(sa);
static void resume_accept(struct bxt_daemon *bxtd)
{
- assert(bxtd);
-
if (bxtd->sk_id == 0) {
bxt_err("Resume calling accept()");
bxtd->sk_id = g_unix_fd_add(bxtd->sk, G_IO_IN, accept_cb, bxtd);
{
int r;
- assert(bxtd);
-
bxtd->clients = g_hash_table_new_full(g_direct_hash, g_direct_equal,
(GDestroyNotify)free_client, NULL);
- if (!bxtd->clients)
- return -1;
+ if (!bxtd->clients) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
bxtd->notis = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, (GDestroyNotify)free_noti);
- if (!bxtd->notis)
- return -1;
+ if (!bxtd->notis) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
bxtd->sigfd = create_sigfd();
g_unix_fd_add(bxtd->sigfd, G_IO_IN, signal_cb, bxtd);
r = direct_init(MODULE_DIR, confpath, &_log_on, &_log_max_line);
- if (r == -1)
- return -1;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
bxtd->sk = sock_get_server(SOCKPATH);
- if (bxtd->sk == -1)
- return -1;
+ if (bxtd->sk < 0)
+ return bxtd->sk;
bxtd->sk_id = g_unix_fd_add(bxtd->sk, G_IO_IN, accept_cb, bxtd);
- buxton_cynara_init();
+ r = buxton_cynara_init();
+ if (r != BUXTON_ERROR_NONE)
+ return r;
buxton_dbus_init();
bxtd->loop = g_main_loop_new(NULL, FALSE);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int start_daemon(struct bxt_daemon *bxtd, const char *confpath)
{
int r;
- assert(bxtd);
-
if (!confpath)
confpath = CONFPATH;
r = bxt_init(bxtd, confpath);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
bxt_exit(bxtd);
return EXIT_FAILURE;
}
{
struct buxton_layer *layer;
guint uid;
+ int ret;
if (g_strcmp0(signal_name, LOGIND_SIGNAL_USER_REMOVED))
return;
g_variant_get(parameters, "(uo)", &uid, NULL);
- layer = layer_create("user-memory");
- if (layer) {
+ ret = layer_create("user-memory", &layer);
+ if (ret == BUXTON_ERROR_NONE && layer) {
layer->uid = uid;
direct_remove_db(layer);
layer_free(layer);
#include "log.h"
#include "socks.h"
+#include "buxton_error.h"
#define SOCKET_TIMEOUT 5 /* seconds */
int fd;
struct sockaddr_un sa;
- assert(path && *path);
+ if (!path || !*path) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
bxt_err("Socket '%s': socket %d", path, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
memset(&sa, 0, sizeof(sa));
if (r == -1 && errno != ENOENT) {
bxt_err("Socket '%s': unlink %d", path, errno);
close(fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
if (r == -1) {
bxt_err("Socket '%s': bind %d", path, errno);
close(fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = chmod(sa.sun_path, 0666);
if (r == -1) {
bxt_err("Socket '%s': chmod %d", path, errno);
close(fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
r = listen(fd, SOMAXCONN);
if (r == -1) {
bxt_err("Socket '%s': listen %d", path, errno);
close(fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
return fd;
int fd;
if (!path || !*path) {
- errno = EINVAL;
- return -1;
+ bxt_err("dbs is not initilized.");
+ return BUXTON_ERROR_INVALID_OPERATION;
}
n = sd_listen_fds(0);
if (n < 0) {
bxt_err("sd_listen_fds: %d", n);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
if (n == 0)
r = fcntl(fd, F_SETFL, O_NONBLOCK);
if (r == -1) {
bxt_err("Client %d: set NONBLOCK: %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
/* need SO_PRIORITY ? */
sizeof(struct timeval));
if (r == -1) {
bxt_err("Client %d: set SO_RCVTIMEO: %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
on = 1;
if (r == -1)
bxt_err("Client %d: set SO_PASSCRED: %d", fd, errno);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int sock_get_client_cred(int fd, struct ucred *cred)
socklen_t len;
if (fd < 0 || !cred) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
len = sizeof(*cred);
r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &len);
if (r == -1) {
bxt_err("Client %d: get SO_PEERCRED: %d", fd, errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Client %d: pid %d uid %u gid %u", fd,
cred->pid, cred->uid, cred->gid);
- return 0;
+ return BUXTON_ERROR_NONE;
}
int sock_get_client_label(int fd, char **label)
char *l;
if (fd < 0 || !label) {
- errno = EINVAL;
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (smack_not_supported) {
*label = NULL;
- return 0;
+ return BUXTON_ERROR_NONE;
}
len = sizeof(buf);
if (r == 0 && sizeof(buf) > len) {
*label = strdup(buf);
bxt_dbg("Client %d: Label '%s'", fd, label);
- return 0;
+ return BUXTON_ERROR_NONE;
} else if (errno != ERANGE) {
bxt_err("Client %d: get SO_PEERSEC: %d", fd, errno);
if (errno == ENOPROTOOPT) {
*label = NULL;
smack_not_supported = 1;
- return 0;
+ return BUXTON_ERROR_NONE;
}
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
l = calloc(1, len + 1);
- if (!l)
- return -1;
+ if (!l) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, l, &len);
if (r == -1) {
bxt_err("Cleint %d: get SO_PEERSEC: %d", fd, errno);
free(l);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
bxt_dbg("Client %d: Label '%s'", fd, l);
*label = l;
- return 0;
+ return BUXTON_ERROR_NONE;
}
../common/proto.c
../common/serialize.c
../common/cache.c
- ../common/common.c)
+ ../common/common.c
+ ../common/buxton_error.c)
INCLUDE_DIRECTORIES(include)
ADD_LIBRARY(${TARGET} SHARED ${SRC})
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
#include <glib-unix.h>
#include "buxton2.h"
-#include "buxton2_internal.h"
+#include "buxton_error.h"
#include "common.h"
#include "log.h"
static GList *clients; /* data: buxton_client */
static pthread_mutex_t clients_lock = PTHREAD_MUTEX_INITIALIZER;
static guint32 client_msgid;
-static int buxton_errno;
-static struct buxton_value *value_create(enum buxton_key_type type, void *value)
+static int value_create(enum buxton_key_type type, void *value,
+ struct buxton_value **new_value)
{
struct buxton_value *val;
- if (!value) {
- buxton_errno_set(EINVAL);
- return NULL;
+ if (!value || !new_value) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
val = calloc(1, sizeof(*val));
- if (!val)
- return NULL;
+ if (!val) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
switch (type) {
case BUXTON_TYPE_STRING:
break;
default:
free(val);
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Unknow type %d", type);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
val->type = type;
+ *new_value = val;
- return val;
+ return BUXTON_ERROR_NONE;
}
EXPORT struct buxton_value *buxton_value_create_string(const char *s)
{
struct buxton_value *v;
char *str;
+ int ret;
if (!s) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return NULL;
}
str = strdup(s);
- if (!str)
+ if (!str) {
+ bxt_err("out of memory");
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return NULL;
+ }
- v = value_create(BUXTON_TYPE_STRING, &str);
- if (!v) {
- free(str);
+ ret = value_create(BUXTON_TYPE_STRING, &str, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
return NULL;
}
EXPORT struct buxton_value *buxton_value_create_int32(int32_t i)
{
- return value_create(BUXTON_TYPE_INT32, &i);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_INT32, &i, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT struct buxton_value *buxton_value_create_uint32(uint32_t u)
{
- return value_create(BUXTON_TYPE_UINT32, &u);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_UINT32, &u, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT struct buxton_value *buxton_value_create_int64(int64_t i64)
{
- return value_create(BUXTON_TYPE_INT64, &i64);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_INT64, &i64, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT struct buxton_value *buxton_value_create_uint64(uint64_t u64)
{
- return value_create(BUXTON_TYPE_UINT64, &u64);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_UINT64, &u64, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT struct buxton_value *buxton_value_create_double(double d)
{
- return value_create(BUXTON_TYPE_DOUBLE, &d);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_DOUBLE, &d, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT struct buxton_value *buxton_value_create_boolean(int32_t b)
{
- return value_create(BUXTON_TYPE_BOOLEAN, &b);
+ struct buxton_value *v;
+ int ret;
+
+ ret = value_create(BUXTON_TYPE_BOOLEAN, &b, &v);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+ return v;
}
EXPORT int buxton_value_get_type(const struct buxton_value *val,
enum buxton_key_type *type)
{
if (!val || !type) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
enum buxton_key_type type)
{
if (!val || !dest) {
- buxton_errno_set(EINVAL);
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (val->type != type) {
- buxton_errno_set(ENOTSUP);
- return -1;
+ bxt_err("Invalid parameter : type mismatch %d %d", val->type, type);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
switch (type) {
*(int32_t *)dest = val->value.b;
break;
default:
- break;
+ bxt_err("Unknow type %d", type);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_value_get_string(const struct buxton_value *val,
const char **s)
{
- return value_get(val, s, BUXTON_TYPE_STRING);
+ int ret;
+
+ ret = value_get(val, s, BUXTON_TYPE_STRING);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_int32(const struct buxton_value *val, int32_t *i)
{
- return value_get(val, i, BUXTON_TYPE_INT32);
+ int ret;
+
+ ret = value_get(val, i, BUXTON_TYPE_INT32);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_uint32(const struct buxton_value *val, uint32_t *u)
{
- return value_get(val, u, BUXTON_TYPE_UINT32);
+ int ret;
+
+ ret = value_get(val, u, BUXTON_TYPE_UINT32);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_int64(const struct buxton_value *val, int64_t *i64)
{
- return value_get(val, i64, BUXTON_TYPE_INT64);
+ int ret;
+
+ ret = value_get(val, i64, BUXTON_TYPE_INT64);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_uint64(const struct buxton_value *val,
uint64_t *u64)
{
- return value_get(val, u64, BUXTON_TYPE_UINT64);
+ int ret;
+
+ ret = value_get(val, u64, BUXTON_TYPE_UINT64);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_double(const struct buxton_value *val, double *d)
{
- return value_get(val, d, BUXTON_TYPE_DOUBLE);
+ int ret;
+
+ ret = value_get(val, d, BUXTON_TYPE_DOUBLE);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT int buxton_value_get_boolean(const struct buxton_value *val, int32_t *b)
{
- return value_get(val, b, BUXTON_TYPE_BOOLEAN);
+ int ret;
+
+ ret = value_get(val, b, BUXTON_TYPE_BOOLEAN);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return -1;
+ }
+ return 0;
}
EXPORT struct buxton_value *buxton_value_duplicate(
struct buxton_value *_val;
if (!val) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return NULL;
}
_val = malloc(sizeof(*_val));
- if (!_val)
+ if (!_val) {
+ bxt_err("out of memory");
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return NULL;
+ }
*_val = *val;
_val->value.s = strdup(val->value.s);
if (!_val->value.s) {
free(_val);
+ bxt_err("out of memory");
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return NULL;
}
}
EXPORT struct buxton_layer *buxton_create_layer(const char *layer_name)
{
- return layer_create(layer_name);
+ struct buxton_layer *layer;
+ int ret;
+
+ ret = layer_create(layer_name, &layer);
+ if (ret != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(ret);
+ return NULL;
+ }
+
+ return layer;
}
EXPORT const char *buxton_layer_get_name(const struct buxton_layer *layer)
{
- if (!layer)
+ if (!layer) {
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return NULL;
+ }
return layer->name;
}
EXPORT void buxton_layer_set_uid(struct buxton_layer *layer, uid_t uid)
{
- if (!layer)
+ if (!layer) {
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return;
+ }
layer->uid = uid;
}
EXPORT void buxton_layer_set_type(struct buxton_layer *layer,
enum buxton_layer_type type)
{
- if (!layer)
+ if (!layer) {
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return;
-
+ }
switch (type) {
case BUXTON_LAYER_NORMAL:
case BUXTON_LAYER_BASE:
break;
default:
+ bxt_err("Unknow type %d", type);
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return;
}
layer_unref(layer);
}
-static struct bxt_req *create_req(const struct buxton_layer *layer,
+static int create_req(const struct buxton_layer *layer,
const char *key, buxton_response_callback callback,
- buxton_list_callback list_cb, void *data)
+ buxton_list_callback list_cb, void *data, struct bxt_req **req)
{
- struct bxt_req *req;
+ struct bxt_req *_req;
- assert(layer);
- assert(callback || list_cb);
+ if (!layer || !(callback || list_cb)) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- req = calloc(1, sizeof(*req));
- if (!req)
- return NULL;
+ _req = calloc(1, sizeof(*_req));
+ if (!_req) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
if (key) {
- req->key = strdup(key);
- if (!req->key) {
- free(req);
- return NULL;
+ _req->key = strdup(key);
+ if (!_req->key) {
+ free(_req);
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
}
- req->layer = layer_ref((struct buxton_layer *)layer);
- req->callback = callback;
- req->list_cb = list_cb;
- req->data = data;
- req->msgid = __atomic_fetch_add(&client_msgid, 1, __ATOMIC_RELAXED);
+ _req->layer = layer_ref((struct buxton_layer *)layer);
+ _req->callback = callback;
+ _req->list_cb = list_cb;
+ _req->data = data;
+ _req->msgid = __atomic_fetch_add(&client_msgid, 1, __ATOMIC_RELAXED);
+
+ *req = _req;
- return req;
+ return BUXTON_ERROR_NONE;
}
static int find_noti(struct buxton_client *client,
{
char *lykey;
struct bxt_noti *_noti;
+ int ret;
- assert(client);
- assert(layer);
- assert(key && *key);
- assert(noti);
+ if (!client || !layer || !key || !*key || !noti) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- lykey = get_search_key(layer, key, NULL);
- if (!lykey)
- return -1;
+ ret = get_search_key(layer, key, NULL, &lykey);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
pthread_mutex_lock(&client->lock);
_noti = g_hash_table_lookup(client->noti_cbs, lykey);
*noti = _noti;
pthread_mutex_unlock(&client->lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static GList *copy_noti_callbacks(GList *callbacks)
GList *l;
GList *copy;
- if (!callbacks)
+ if (!callbacks) {
+ bxt_err("Invalid parameter");
return NULL;
+ }
for (l = callbacks; l; l = g_list_next(l)) {
struct bxt_noti_cb *noticb = l->data;
GList *l;
GList *copy_callbacks;
- assert(client);
- assert(data);
- assert(len > 0);
+ if (!client || !data || len <= 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = deserialz_request(data, len, &rqst);
- if (r == -1) {
- bxt_err("proc noti: deserialize errno %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("proc noti: deserialize errno %d", r);
+ return r;
}
noti = NULL;
r = find_noti(client, rqst.layer, rqst.key, ¬i);
- if (r == -1) {
- bxt_err("proc noti: '%s' '%s' not registered",
- rqst.layer->name, rqst.key);
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("proc noti: '%s' '%s' not registered (%d)",
+ rqst.layer->name, rqst.key, r);
free_request(&rqst);
- return -1;
+ return r;
}
if (!noti) {
bxt_err("proc noti: '%s' '%s' callback not exist",
rqst.layer->name, rqst.key);
free_request(&rqst);
- return -1;
+ return BUXTON_ERROR_NOT_EXIST;
}
pthread_mutex_lock(¬i->cbs_lock);
for (l = copy_callbacks; l; l = g_list_next(l)) {
struct bxt_noti_cb *noticb = l->data;
- assert(noticb->callback);
- noticb->callback(rqst.layer, rqst.key, rqst.val, noticb->data);
+ if (noticb->callback)
+ noticb->callback(rqst.layer, rqst.key, rqst.val, noticb->data);
}
noti->callbacks = free_copy_callbacks(noti->callbacks, copy_callbacks);
pthread_mutex_unlock(¬i->cbs_lock);
free_request(&rqst);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int add_noti(struct buxton_client *client,
{
char *lykey;
struct bxt_noti *_noti;
+ int ret;
- assert(client);
- assert(layer);
- assert(key && *key);
- assert(noti);
+ if (!client || !layer || !key || !*key || !noti) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- lykey = get_search_key(layer, key, NULL);
- if (!lykey)
- return -1;
+ ret = get_search_key(layer, key, NULL, &lykey);
+ if (ret != BUXTON_ERROR_NONE)
+ return ret;
pthread_mutex_lock(&client->lock);
_noti = g_hash_table_lookup(client->noti_cbs, lykey);
free(lykey);
*noti = _noti;
pthread_mutex_unlock(&client->lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
_noti = calloc(1, sizeof(*_noti));
if (!_noti) {
free(lykey);
pthread_mutex_unlock(&client->lock);
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
_noti->layer_key = lykey;
pthread_mutex_init(&_noti->cbs_lock, NULL);
*noti = _noti;
pthread_mutex_unlock(&client->lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static int add_noticb(struct bxt_noti *noti, buxton_notify_callback notify,
GList *l;
struct bxt_noti_cb *noticb;
- assert(noti);
- assert(notify);
+ if (!noti || !notify) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
pthread_mutex_lock(¬i->cbs_lock);
for (l = noti->callbacks; l; l = g_list_next(l)) {
noticb = l->data;
if (noticb->callback == notify) {
- buxton_errno_set(EEXIST);
pthread_mutex_unlock(¬i->cbs_lock);
- return -1;
+ return BUXTON_ERROR_EXIST;
}
}
noticb = calloc(1, sizeof(*noticb));
if (!noticb) {
pthread_mutex_unlock(¬i->cbs_lock);
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
noticb->ref_cnt = 1;
noti->callbacks = g_list_append(noti->callbacks, noticb);
pthread_mutex_unlock(¬i->cbs_lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static void del_noti(struct buxton_client *client,
const struct buxton_layer *layer, const char *key)
{
char *lykey;
+ int ret;
- assert(client);
- assert(layer);
- assert(key && *key);
+ if (!client || !layer || !key || !*key) {
+ bxt_err("Invalid parameter");
+ return;
+ }
- lykey = get_search_key(layer, key, NULL);
- if (!lykey)
+ ret = get_search_key(layer, key, NULL, &lykey);
+ if (ret != BUXTON_ERROR_NONE)
return;
pthread_mutex_lock(&client->lock);
struct bxt_noti *noti;
int r;
- assert(client);
- assert(req);
- assert(resp);
+ if (!client || !req || !resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
- if (resp->res == 0) {
+ if (resp->res == BUXTON_ERROR_NONE) {
r = add_noti(client, req->layer, req->key, ¬i);
- if (r == -1) {
- resp->res = errno;
- bxt_err("add noti: errno %d", errno);
+ if (r != BUXTON_ERROR_NONE) {
+ resp->res = r;
+ bxt_err("add noti: errno %d", r);
} else {
if (noti->reg == FALSE)
noti->reg = (resp->res == 0);
r = add_noticb(noti, req->notify, req->notify_data);
- if (r == -1 && errno != EEXIST) {
- resp->res = errno;
- bxt_err("add noticb: errno %d", errno);
+ if (r != BUXTON_ERROR_NONE && r != BUXTON_ERROR_EXIST) {
+ resp->res = r;
+ bxt_err("add noticb: errno %d", r);
}
}
}
- assert(req->callback);
- req->callback(resp->res, req->layer, req->key, resp->val, req->data);
+ if (req->callback)
+ req->callback(resp->res, req->layer, req->key, resp->val, req->data);
+ else
+ bxt_err("proc msg(%d,%d): callback not exist", resp->msgid, resp->type);
}
static int proc_msg_res(struct buxton_client *client, uint8_t *data, int len)
struct response resp;
struct bxt_req *req;
- assert(client);
- assert(data);
- assert(len > 0);
+ if (!client || !data || len <= 0) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
r = deserialz_response(data, len, &resp);
- if (r == -1) {
- bxt_err("proc msg: deserialize errno %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("proc msg: deserialize errno %d", r);
+ return r;
}
pthread_mutex_lock(&client->lock);
if (!req) {
bxt_err("proc msg: msgid %d not exist", resp.msgid);
free_response(&resp);
- return 0;
+ return BUXTON_ERROR_NONE;
}
switch (resp.type) {
case MSG_LIST:
- assert(req->list_cb);
- req->list_cb(resp.res, req->layer, resp.names, resp.nmlen,
- req->data);
+ if (req->list_cb)
+ req->list_cb(resp.res, req->layer, resp.names, resp.nmlen, req->data);
+ else
+ bxt_err("proc msg(%d,%d): list_cb not exist", resp.msgid, resp.type);
+
break;
case MSG_NOTIFY:
proc_msg_notify(client, req, &resp);
case MSG_UNNOTIFY:
del_noti(client, req->layer, req->key);
- assert(req->callback);
- req->callback(resp.res, req->layer, req->key, resp.val,
- req->data);
+ if (req->callback)
+ req->callback(resp.res, req->layer, req->key, resp.val, req->data);
+ else
+ bxt_err("proc msg(%d,%d): callback not exist", resp.msgid, resp.type);
+
break;
default:
- assert(req->callback);
- req->callback(resp.res, req->layer, req->key, resp.val,
+ if (req->callback)
+ req->callback(resp.res, req->layer, req->key, resp.val,
req->data);
+ else
+ bxt_err("proc msg(%d,%d): callback not exist", resp.msgid, resp.type);
+
break;
}
g_hash_table_remove(client->req_cbs, GUINT_TO_POINTER(resp.msgid));
pthread_mutex_unlock(&client->lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
static void proc_msg_cb(void *user_data,
{
struct buxton_client *client = user_data;
- assert(client);
-
switch (type) {
case MSG_NOTI:
proc_msg_noti(client, data, len);
int r;
GList *f;
- assert(client);
-
f = g_list_find(clients, client);
if (!f) {
bxt_dbg("recv msg: cli %p removed\n", client);
- return 0;
+ return BUXTON_ERROR_NONE;
}
r = proto_recv_async(client->fd, proc_msg_cb, client);
- if (r == -1) {
- bxt_err("recv msg: fd %d errno %d", client->fd, errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("recv msg: fd %d errno %d", client->fd, r);
+ return r;
}
- return 0;
+ return BUXTON_ERROR_NONE;
}
#define TS_SUB(a, b) (((a)->tv_sec - (b)->tv_sec) * 1000 \
+ ((a)->tv_nsec - (b)->tv_nsec) / 1000000)
static int wait_msg(struct buxton_client *client, guint32 msgid)
{
- int r;
+ int r, ret;
struct pollfd fds[1];
struct timespec to;
struct timespec t;
int32_t ms;
struct bxt_req *req;
- assert(client);
- assert(client->fd >= 0);
+ if (client->fd < 0) {
+ bxt_err("Invalid parameter (%d)", client->fd);
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
fds[0].fd = client->fd;
fds[0].events = POLLIN;
while (ms > 0) {
r = poll(fds, 1, ms > 10 ? 10 : ms);
+ ret = BUXTON_ERROR_NONE;
switch (r) {
case -1:
- bxt_err("wait response: poll: fd %d errno %d",
- client->fd, errno);
if (errno == EINTR) {
clock_gettime(CLOCK_MONOTONIC, &t);
ms = TS_SUB(&to, &t);
continue;
}
+ bxt_err("wait response: poll: fd %d errno %d",
+ client->fd, errno);
+ ret = BUXTON_ERROR_IO_ERROR;
break;
case 0:
/* poll timeout, do nothing */
break;
default:
- r = proc_msg(client);
+ ret = proc_msg(client);
break;
}
/* poll or proc error */
- if (r == -1) {
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_lock(&client->lock);
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(msgid));
pthread_mutex_unlock(&client->lock);
- return -1;
+
+ return ret;
}
pthread_mutex_lock(&client->lock);
/* req is processed */
if (!req) {
pthread_mutex_unlock(&client->lock);
- return 0;
+ return BUXTON_ERROR_NONE;
}
pthread_mutex_unlock(&client->lock);
}
bxt_err("wait response: timeout");
- buxton_errno_set(ETIMEDOUT);
pthread_mutex_lock(&client->lock);
g_hash_table_remove(client->req_cbs, GUINT_TO_POINTER(msgid));
pthread_mutex_unlock(&client->lock);
- return -1;
+ return BUXTON_ERROR_TIME_OUT;
}
static void free_req(struct bxt_req *req)
uint8_t *data;
int len;
- assert(client);
- assert(rqst);
-
- if (client->fd == -1) {
- buxton_errno_set(ENOTCONN);
- return -1;
+ if (client->fd < 0) {
+ bxt_err("Invalid parameter(%d)", client->fd);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
r = serialz_request(rqst, &data, &len);
- if (r == -1) {
- bxt_err("send req: serialize errno %d", errno);
- return -1;
+ if (r != BUXTON_ERROR_NONE) {
+ bxt_err("send req: serialize errno %d", r);
+ return r;
}
r = proto_send(client->fd, rqst->type, data, len);
- if (r == -1)
- bxt_err("send req: errno %d", errno);
+ if (r != BUXTON_ERROR_NONE)
+ bxt_err("send req: errno %d", r);
free(data);
return r;
}
-static struct bxt_req *set_value(struct buxton_client *client,
+static int set_value(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
const struct buxton_value *val,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
if (!client || !layer || !key || !*key || !val || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_SET;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_set_value(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int ret;
pthread_mutex_lock(&clients_lock);
- req = set_value(client, layer, key, val, callback, user_data);
- if (!req) {
+ ret = set_value(client, layer, key, val, callback, user_data, &req);
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(ret);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
-
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = set_value(client, layer, key, val, set_value_sync_cb, &resp);
- if (!req) {
+ r = set_value(client, layer, key, val, set_value_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *get_value(struct buxton_client *client,
+static int get_value(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
if (!client || !layer || !key || !*key || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_GET;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_get_value(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
-
+ int ret;
pthread_mutex_lock(&clients_lock);
- req = get_value(client, layer, key, callback, user_data);
- if (!req) {
+ ret = get_value(client, layer, key, callback, user_data, &req);
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(ret);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
struct response resp;
if (!val) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = get_value(client, layer, key, get_value_sync_cb, &resp);
- if (!req) {
+ r = get_value(client, layer, key, get_value_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *list_keys(struct buxton_client *client,
+static int list_keys(struct buxton_client *client,
const struct buxton_layer *layer,
- buxton_list_callback callback, void *user_data)
+ buxton_list_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
if (!client || !layer || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, NULL, NULL, callback, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, NULL, NULL, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_LIST;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_list_keys(struct buxton_client *client,
buxton_list_callback callback, void *user_data)
{
struct bxt_req *req;
-
+ int ret;
pthread_mutex_lock(&clients_lock);
- req = list_keys(client, layer, callback, user_data);
- if (!req) {
+ ret = list_keys(client, layer, callback, user_data, &req);
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(ret);
return -1;
}
char **_names;
int i;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
- if (resp->res)
+ if (resp->res != BUXTON_ERROR_NONE)
return;
nms = calloc(len + 1, sizeof(void *));
if (!nms) {
- resp->res = ENOMEM;
+ bxt_err("out of memory");
+ resp->res = BUXTON_ERROR_OUT_OF_MEMORY;
return;
}
struct response resp;
if (!names) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = list_keys(client, layer, list_keys_sync_cb, &resp);
- if (!req) {
+ r = list_keys(client, layer, list_keys_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
{
struct bxt_noti_res *res = data;
- assert(res);
- assert(res->callback);
+ if (!res || !res->callback) {
+ bxt_err("Invalid parameter");
+ return G_SOURCE_REMOVE;
+ }
- res->callback(res->res, res->layer, res->key, NULL, res->data);
+ buxton_err_set_errno(res->res);
+ res->callback(buxton_err_get_errno(), res->layer, res->key, NULL, res->data);
layer_unref(res->layer);
free(res->key);
{
struct bxt_noti_res *res;
- assert(layer);
- assert(key);
- assert(callback);
+ if (!layer || !key || !callback) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
res = calloc(1, sizeof(*res));
- if (!res)
- return -1;
-
+ if (!res) {
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
+ }
res->key = strdup(key);
if (!res->key) {
free(res);
- return -1;
+ bxt_err("out of memory");
+ return BUXTON_ERROR_OUT_OF_MEMORY;
}
res->layer = layer_ref((struct buxton_layer *)layer);
g_idle_add(call_resp_cb, res);
- return 0;
+ return BUXTON_ERROR_NONE;
}
-static struct bxt_req *register_noti(struct buxton_client *client,
+static int register_noti(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
buxton_notify_callback notify, void *notify_data,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
- assert(client);
- assert(layer);
- assert(key && *key);
- assert(notify);
- assert(callback);
-
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
req->notify = notify;
req->notify_data = notify_data;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_register_notification(struct buxton_client *client,
struct bxt_req *req;
if (!client || !layer || !key || !*key || !notify || !callback) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
r = find_noti(client, layer, key, ¬i);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
if (noti && noti->reg == TRUE) {
r = add_noticb(noti, notify, notify_data);
pthread_mutex_unlock(&clients_lock);
- return call_resp(r == -1 ? errno : 0, layer, key,
- callback, user_data);
+ r = call_resp(r, layer, key, callback, user_data);
+ if (r != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(r);
+ return -1;
+ }
+ return 0;
}
- req = register_noti(client, layer, key, notify, notify_data, callback,
- user_data);
- if (!req) {
+ r = register_noti(client, layer, key, notify, notify_data, callback,
+ user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
struct response resp;
if (!client || !layer || !key || !*key || !notify) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
r = find_noti(client, layer, key, ¬i);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
if (noti && noti->reg == TRUE) {
pthread_mutex_unlock(&clients_lock);
- return add_noticb(noti, notify, notify_data);
+ r = add_noticb(noti, notify, notify_data);
+ if (r != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(r);
+ return -1;
+ }
+ return 0;
}
memset(&resp, 0, sizeof(resp));
- req = register_noti(client, layer, key, notify, notify_data,
- reg_noti_sync_cb, &resp);
- if (!req) {
+ r = register_noti(client, layer, key, notify, notify_data,
+ reg_noti_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
gboolean f;
int cnt;
- assert(noti);
- assert(notify);
+ if (!noti || !notify) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
cnt = 0;
f = FALSE;
pthread_mutex_unlock(¬i->cbs_lock);
if (!f) {
- buxton_errno_set(ENOENT);
- return -1;
+ bxt_err("no such a notify");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (count)
*count = cnt;
- return 0;
+ return BUXTON_ERROR_NONE;
}
-static struct bxt_req *unregister_noti(struct buxton_client *client,
+static int unregister_noti(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
- assert(client);
- assert(layer);
- assert(key && *key);
- assert(callback);
-
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_UNNOTIFY;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_unregister_notification(struct buxton_client *client,
int cnt;
if (!client || !layer || !key || !*key || !notify || !callback) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
r = find_noti(client, layer, key, ¬i);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
if (!noti) {
- buxton_errno_set(ENOENT);
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_NOT_EXIST);
return -1;
}
r = del_noticb(noti, notify, &cnt);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
- return call_resp(errno, layer, key, callback, user_data);
+ r = call_resp(r, layer, key, callback, user_data);
+ if (r != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(r);
+ return -1;
+ }
+ return 0;
}
if (cnt || noti->reg == FALSE) {
pthread_mutex_unlock(&clients_lock);
- return call_resp(0, layer, key, callback, user_data);
+ r = call_resp(BUXTON_ERROR_NONE, layer, key, callback, user_data);
+ if (r != BUXTON_ERROR_NONE) {
+ buxton_err_set_errno(r);
+ return -1;
+ }
+ return 0;
}
- req = unregister_noti(client, layer, key, callback, user_data);
- if (!req) {
+ r = unregister_noti(client, layer, key, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
struct response resp;
if (!client || !layer || !key || !*key || !notify) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
r = find_noti(client, layer, key, ¬i);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
if (!noti) {
- buxton_errno_set(ENOENT);
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_NOT_EXIST);
return -1;
}
r = del_noticb(noti, notify, &cnt);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
memset(&resp, 0, sizeof(resp));
- req = unregister_noti(client, layer, key, unreg_noti_sync_cb, &resp);
- if (!req) {
+ r = unregister_noti(client, layer, key, unreg_noti_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *create_value(struct buxton_client *client,
+static int create_value(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
const char *read_privilege, const char *write_privilege,
const struct buxton_value *val,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
if (!client || !layer || !key || !*key || !read_privilege
|| !write_privilege || !val || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_CREAT;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_create_value(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int ret;
pthread_mutex_lock(&clients_lock);
- req = create_value(client, layer, key, read_privilege, write_privilege,
- val, callback, user_data);
- if (!req) {
+ ret = create_value(client, layer, key, read_privilege, write_privilege,
+ val, callback, user_data, &req);
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(ret);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = create_value(client, layer, key, read_privilege, write_privilege,
- val, create_value_sync_cb, &resp);
- if (!req) {
+ r = create_value(client, layer, key, read_privilege, write_privilege,
+ val, create_value_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *unset_value(struct buxton_client *client,
+static int unset_value(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
if (!client || !layer || !key || !*key || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_UNSET;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_unset_value(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int ret;
pthread_mutex_lock(&clients_lock);
- req = unset_value(client, layer, key, callback, user_data);
- if (!req) {
+ ret = unset_value(client, layer, key, callback, user_data, &req);
+ if (ret != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(ret);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = unset_value(client, layer, key, unset_value_sync_cb, &resp);
- if (!req) {
+ r = unset_value(client, layer, key, unset_value_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *set_priv(struct buxton_client *client,
+static int set_priv(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
enum buxton_priv_type type,
const char *privilege,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct buxton_value val;
if (!client || !layer || !key || !*key || !privilege || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (type <= BUXTON_PRIV_UNKNOWN || type >= BUXTON_PRIV_MAX) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter (%d)", type);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = type == BUXTON_PRIV_READ ? MSG_SET_RP : MSG_SET_WP;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_set_privilege(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int r;
pthread_mutex_lock(&clients_lock);
- req = set_priv(client, layer, key, type, privilege,
- callback, user_data);
- if (!req) {
+ r = set_priv(client, layer, key, type, privilege,
+ callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = set_priv(client, layer, key, type, privilege,
- set_priv_sync_cb, &resp);
- if (!req) {
+ r = set_priv(client, layer, key, type, privilege,
+ set_priv_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
return 0;
}
-static struct bxt_req *get_priv(struct buxton_client *client,
+static int get_priv(struct buxton_client *client,
const struct buxton_layer *layer, const char *key,
enum buxton_priv_type type,
- buxton_response_callback callback, void *user_data)
+ buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
if (!client || !layer || !key || !*key || !callback) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
if (type <= BUXTON_PRIV_UNKNOWN || type >= BUXTON_PRIV_MAX) {
- buxton_errno_set(EINVAL);
- return NULL;
+ bxt_err("Invalid parameter (%d)", type);
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
- req = create_req(layer, key, callback, NULL, user_data);
- if (!req)
- return NULL;
+ r = create_req(layer, key, callback, NULL, user_data, &req);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = type == BUXTON_PRIV_READ ? MSG_GET_RP : MSG_GET_WP;
g_hash_table_insert(client->req_cbs, GUINT_TO_POINTER(req->msgid), req);
r = send_req(client, &rqst);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_get_privilege(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int r;
pthread_mutex_lock(&clients_lock);
- req = get_priv(client, layer, key, type, callback, user_data);
- if (!req) {
+ r = get_priv(client, layer, key, type, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
- if (!status)
+ if (status == BUXTON_ERROR_NONE)
resp->val = buxton_value_duplicate(val);
}
struct response resp;
if (!privilege) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = get_priv(client, layer, key, type, get_priv_sync_cb, &resp);
- if (!req) {
+ r = get_priv(client, layer, key, type, get_priv_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
-static struct bxt_req *security_control(struct buxton_client *client,
- int32_t enable, buxton_response_callback callback, void *user_data)
+static int security_control(struct buxton_client *client,
+ int32_t enable, buxton_response_callback callback, void *user_data,
+ struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct buxton_layer *layer;
struct buxton_value val;
- layer = layer_create("dummy");
+ if (!client || !callback) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- req = create_req(layer, NULL, callback, NULL, user_data);
+ r = layer_create("dummy", &layer);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
+ r = create_req(layer, NULL, callback, NULL, user_data, &req);
layer_unref(layer);
- if (!req)
- return NULL;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_CTRL;
r = send_req(client, &rqst);
free(rqst.key);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_enable_security(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int r;
pthread_mutex_lock(&clients_lock);
- req = security_control(client, TRUE, callback, user_data);
- if (!req) {
+ r = security_control(client, TRUE, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = security_control(client, TRUE, security_sync_cb, &resp);
- if (!req) {
+ r = security_control(client, TRUE, security_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int r;
pthread_mutex_lock(&clients_lock);
- req = security_control(client, FALSE, callback, user_data);
- if (!req) {
+ r = security_control(client, FALSE, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = security_control(client, FALSE, security_sync_cb, &resp);
- if (!req) {
+ r = security_control(client, FALSE, security_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
{
struct response *resp = user_data;
- assert(resp);
+ if (!resp) {
+ bxt_err("Invalid parameter");
+ return;
+ }
resp->res = status;
}
-static struct bxt_req *update_client_label(struct buxton_client *client,
+static int update_client_label(struct buxton_client *client,
buxton_response_callback callback,
- void *user_data)
+ void *user_data, struct bxt_req **new_req)
{
int r;
struct bxt_req *req;
struct request rqst;
struct buxton_layer *layer;
- layer = layer_create("dummy");
+ if (!client || !callback) {
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
+ }
- req = create_req(layer, NULL, callback, NULL, user_data);
+ r = layer_create("dummy", &layer);
+ if (r != BUXTON_ERROR_NONE)
+ return r;
+ r = create_req(layer, NULL, callback, NULL, user_data, &req);
layer_unref(layer);
- if (!req)
- return NULL;
+ if (r != BUXTON_ERROR_NONE)
+ return r;
memset(&rqst, 0, sizeof(rqst));
rqst.type = MSG_CTRL;
r = send_req(client, &rqst);
free(rqst.key);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
g_hash_table_remove(client->req_cbs,
GUINT_TO_POINTER(req->msgid));
pthread_mutex_unlock(&client->lock);
- return NULL;
+ return r;
}
+ *new_req = req;
pthread_mutex_unlock(&client->lock);
- return req;
+ return BUXTON_ERROR_NONE;
}
EXPORT int buxton_update_client_label(struct buxton_client *client,
buxton_response_callback callback, void *user_data)
{
struct bxt_req *req;
+ int r;
pthread_mutex_lock(&clients_lock);
- req = update_client_label(client, callback, user_data);
- if (!req) {
+ r = update_client_label(client, callback, user_data, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
memset(&resp, 0, sizeof(resp));
pthread_mutex_lock(&clients_lock);
- req = update_client_label(client, update_label_sync_cb, &resp);
- if (!req) {
+ r = update_client_label(client, update_label_sync_cb, &resp, &req);
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
r = wait_msg(client, req->msgid);
- if (r == -1) {
+ if (r != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
- if (resp.res) {
- buxton_errno_set(resp.res);
+ if (resp.res != BUXTON_ERROR_NONE) {
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(resp.res);
return -1;
}
buxton_status_callback callback = NULL;
void * user_data;
- assert(cli);
+ if (!cli)
+ return G_SOURCE_REMOVE;
pthread_mutex_lock(&clients_lock);
f = g_list_find(clients, cli);
free(cli);
}
-int connect_server(const char *addr)
+int connect_server(const char *addr, int *new_fd)
{
int fd;
struct sockaddr_un sa;
int r;
if (!addr) {
- buxton_errno_set(EINVAL);
- return -1;
+ bxt_err("Invalid parameter");
+ return BUXTON_ERROR_INVALID_PARAMETER;
}
fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (fd == -1) {
bxt_err("connect: socket errno %d", errno);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
sa.sun_family = AF_UNIX;
else
bxt_dbg("connect: connect errno %d", errno);
close(fd);
- return -1;
+ return BUXTON_ERROR_IO_ERROR;
}
- return fd;
+ *new_fd = fd;
+
+ return BUXTON_ERROR_NONE;
}
static gboolean recv_cb(gint fd, GIOCondition cond, gpointer data)
int r;
GList *f;
- assert(cli);
+ if (!cli) {
+ bxt_err("Invalid parameter");
+ return G_SOURCE_REMOVE;
+ }
pthread_mutex_lock(&clients_lock);
f = g_list_find(clients, cli);
GList *l;
if (!client) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
l = g_list_find(clients, client);
if (!l) {
- buxton_errno_set(ENOENT);
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_NOT_EXIST);
return -1;
}
buxton_status_callback callback, void *user_data)
{
struct buxton_client *cli;
+ int r;
if (!client) {
- buxton_errno_set(EINVAL);
+ bxt_err("Invalid parameter");
+ buxton_err_set_errno(BUXTON_ERROR_INVALID_PARAMETER);
return -1;
}
pthread_mutex_lock(&clients_lock);
cli = calloc(1, sizeof(*cli));
if (!cli) {
+ bxt_err("out of memory");
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return -1;
}
if (!cli->req_cbs) {
pthread_mutex_unlock(&cli->lock);
free_client(cli);
- buxton_errno_set(ENOMEM);
+ bxt_err("out of memory");
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return -1;
}
cli->noti_cbs = g_hash_table_new_full(g_str_hash, g_str_equal,
if (!cli->noti_cbs) {
pthread_mutex_unlock(&cli->lock);
free_client(cli);
- buxton_errno_set(ENOMEM);
+ bxt_err("out of memory");
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(BUXTON_ERROR_OUT_OF_MEMORY);
return -1;
}
pthread_mutex_unlock(&cli->lock);
- cli->fd = connect_server(SOCKPATH);
- if (cli->fd == -1) {
+ r = connect_server(SOCKPATH, &cli->fd);
+ if (r != BUXTON_ERROR_NONE) {
free_client(cli);
pthread_mutex_unlock(&clients_lock);
+ buxton_err_set_errno(r);
return -1;
}
return buxton_open_full(client, true, callback, user_data);
}
-EXPORT void buxton_errno_set(int err)
-{
- buxton_errno = err;
- errno = err;
-}
-
-EXPORT int buxton_errno_get()
-{
- return buxton_errno;
-}
+++ /dev/null
-/*
- * Buxton
- *
- * Copyright (C) 2019 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __BUXTON_INTERNAL_H__
-#define __BUXTON_INTERNALH__
-
-/**
- * @file buxton2_internal.h
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void buxton_errno_set(int err);
-int buxton_errno_get();
-
-#ifdef __cplusplus
-}
-#endif
-#endif /* __BUXTON_H__ */
#include <dlog.h>
#include "vconf.h"
-#include "buxton2_internal.h"
+#include "buxton_error.h"
#ifndef EXPORT
# define EXPORT __attribute__((visibility("default")))
#define LOG_TAG "VCONF"
+static bool last_result;
+static int last_errno;
+
+#define VCONF_SET_ERRNO(err) do { \
+ LOGE("err no %d(%s)", err, buxton_err_get_str(-err)); \
+ last_errno = err; \
+ errno = err; \
+ last_result = false; \
+ } while (0)
+
static void _vconf_restore_noti_cb(gpointer key, gpointer value, gpointer user_data);
static void _vconf_con_close(void);
int source_id; /**< GSource id for call_noti_cb*/
};
-static bool last_result;
-
static void _vconf_free_keynode(struct _keynode_t *keynode)
{
if (!keynode)
EXPORT char *vconf_keynode_get_name(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode || !keynode->keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return NULL;
}
EXPORT int vconf_keynode_get_type(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
EXPORT int vconf_keynode_get_int(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
if (keynode->type != VCONF_TYPE_INT) {
- buxton_errno_set(ENOTSUP);
+ LOGE("type mismatch %d", keynode->type);
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
EXPORT double vconf_keynode_get_dbl(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
if (keynode->type != VCONF_TYPE_DOUBLE) {
- buxton_errno_set(ENOTSUP);
+ LOGE("type mismatch %d", keynode->type);
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
EXPORT int vconf_keynode_get_bool(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
if (keynode->type != VCONF_TYPE_BOOL) {
- buxton_errno_set(ENOTSUP);
+ LOGE("type mismatch %d", keynode->type);
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
EXPORT char *vconf_keynode_get_str(keynode_t *keynode)
{
+ last_result = true;
+
if (!keynode) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return NULL;
}
if (keynode->type != VCONF_TYPE_STRING) {
- buxton_errno_set(ENOTSUP);
+ LOGE("type mismatch %d", keynode->type);
+ VCONF_SET_ERRNO(EINVAL);
return NULL;
}
GList *l;
GList *n;
- assert(noti);
+ if (!noti)
+ return;
for (l = noti->noti_list, n = g_list_next(l);
l; l = n, n = g_list_next(n)) {
static void _vconf_con_close(void)
{
_refcnt--;
- if (_refcnt) {
+ if (_refcnt)
return;
- }
buxton_free_layer(system_layer);
system_layer = NULL;
buxton_close(client);
r = buxton_open(&client, _vconf_restore_connection, NULL);
- if (r == -1) {
+ if (r != 0) {
_refcnt = 0;
- LOGE("Can't connect to buxton: %d", buxton_errno_get());
+ LOGE("Can't connect to buxton: %d", buxton_err_get_errno());
pthread_mutex_unlock(&vconf_lock);
return;
}
int r;
_refcnt++;
- if (_refcnt > 1) {
+ if (_refcnt > 1)
return 0;
- }
r = buxton_open(&client, _vconf_restore_connection, NULL);
- if (r == -1) {
- LOGE("Can't connect to buxton: %d", buxton_errno_get());
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("Can't connect to buxton: %d", r);
_refcnt--;
- return -1;
+ return r;
}
if (noti_tbl) {
assert(node);
r = buxton_value_get_type(val, &type);
- if (r == -1)
+ if (r != 0)
type = BUXTON_TYPE_UNKNOWN;
switch (type) {
{
GList *l;
- assert(noti);
- assert(cb);
+ if (!noti || !cb) {
+ LOGE("Invalid parameter");
+ return NULL;
+ }
for (l = noti->noti_list; l; l = g_list_next(l)) {
struct noti_cb *noticb = l->data;
{
struct noti_cb *noticb;
- assert(noti);
- assert(cb);
+ if (!noti || !cb) {
+ LOGE("Invalid parameter");
+ return EINVAL;
+ }
noticb = _vconf_find_noti_cb(noti, cb);
if (noticb) {
if (noticb->active) {
- buxton_errno_set(EEXIST);
- return -1;
+ LOGE("noticb is already exist (%s)", noti->key);
+ return EEXIST;
} else {
noticb->active = true;
return 0;
}
noticb = calloc(1, sizeof(*noticb));
- if (!noticb)
- return -1;
+ if (!noticb) {
+ LOGE("out of memory");
+ return ENOMEM;
+ }
noticb->cb = cb;
noticb->user_data = user_data;
return 0;
}
-static struct noti *_vconf_create_noti(const char *key, vconf_callback_fn cb,
- void *user_data)
+static int _vconf_create_noti(const char *key, vconf_callback_fn cb,
+ void *user_data, struct noti **new_noti)
{
int r;
struct noti *noti;
- assert(key);
- assert(cb);
-
noti = calloc(1, sizeof(*noti));
- if (!noti)
- return NULL;
+ if (!noti) {
+ LOGE("out of memory");
+ return ENOMEM;
+ }
noti->key = strdup(key);
if (!noti->key) {
free(noti);
- return NULL;
+ LOGE("out of memory");
+ return ENOMEM;
}
r = _vconf_add_noti(noti, cb, user_data);
- if (r == -1) {
+ if (r != 0) {
free(noti->key);
free(noti);
- return NULL;
+ return r;
}
g_hash_table_insert(noti_tbl, noti->key, noti);
+ *new_noti = noti;
- return noti;
+ return 0;
}
/* LCOV_EXCL_START */
r = buxton_register_notification_sync(client,
_vconf_get_layer(_key), _key, _vconf_notify_cb, NULL);
- if (r == -1) {
+ if (r != 0) {
LOGE("vconf_notify_key_changed: key '%s' add notify error %d",
- _key, buxton_errno_get());
+ _key, buxton_err_get_errno());
g_hash_table_remove(noti_tbl, key);
} else {
_refcnt++;
{
int r;
struct noti *noti;
- last_result = false;
+ last_result = true;
if (!key || !cb) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
noti = g_hash_table_lookup(noti_tbl, key);
if (!noti) {
- noti = _vconf_create_noti(key, cb, user_data);
- if (!noti) {
+ r = _vconf_create_noti(key, cb, user_data, ¬i);
+ if (r != 0) {
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
r = buxton_register_notification_sync(client, _vconf_get_layer(key), key,
_vconf_notify_cb, NULL);
- if (r == -1) {
+ if (r != 0) {
+ r = buxton_err_get_errno();
LOGE("vconf_notify_key_changed: key '%s' add notify error %d",
- key, buxton_errno_get());
+ key, r);
g_hash_table_remove(noti_tbl, key);
- }
+ } else {
/* increase reference count */
- if (r == 0)
r = _vconf_con_open();
+ }
} else {
r = _vconf_add_noti(noti, cb, user_data);
}
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
- if (r == 0)
- last_result = true;
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
- return r;
+ return 0;
}
static int _vconf_unregister_noti(struct noti *noti)
int cnt;
GList *l;
- assert(noti);
-
cnt = 0;
- for (l = noti->noti_list; l; l = g_list_next(l)) {
+ for (l = noti->noti_list; l; l = g_list_next(l))
cnt++;
- }
if (cnt > 0)
return cnt;
struct noti *noti;
struct noti_cb *noticb;
+ last_result = true;
+
if (!key || !cb) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
if (!noti) {
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
- buxton_errno_set(ENOENT);
+ VCONF_SET_ERRNO(ENOENT);
return -1;
}
if (!noticb || !noticb->active) {
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
- buxton_errno_set(ENOENT);
+ VCONF_SET_ERRNO(ENOENT);
return -1;
}
r = buxton_unregister_notification_sync(client, _vconf_get_layer(key),
key, _vconf_notify_cb);
- if (r == -1)
- LOGE("unregister error '%s' %d", noti->key, buxton_errno_get());
+ if (r != 0)
+ LOGE("unregister error '%s' %d", noti->key, buxton_err_get_errno());
else
_vconf_con_close();
{
int r;
- assert(key);
- assert(val);
-
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
- return -1;
+ return r;
}
r = buxton_set_value_sync(client, _vconf_get_layer(key), key, val);
- if (r == -1)
- LOGE("set value: key '%s' errno %d", key, buxton_errno_get());
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("set value: key '%s' errno %d", key, r);
+ }
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
{
int r;
struct buxton_value *val;
- last_result = false;
+ last_result = true;
if (!key) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
val = buxton_value_create_int32(intval);
- if (!val)
+ if (!val) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
r = _vconf_set(key, val);
- if (r == 0)
- last_result = true;
-
buxton_value_free(val);
- return r;
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
+
+ return 0;
}
EXPORT int vconf_set_bool(const char *key, int boolval)
{
int r;
struct buxton_value *val;
- last_result = false;
+ last_result = true;
if (!key) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
val = buxton_value_create_boolean(boolval);
- if (!val)
+ if (!val) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
r = _vconf_set(key, val);
- if (r == 0)
- last_result = true;
-
buxton_value_free(val);
- return r;
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
+
+ return 0;
}
EXPORT int vconf_set_str(const char *key, const char *strval)
{
int r;
struct buxton_value *val;
- last_result = false;
+ last_result = true;
if (!key || !strval) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
val = buxton_value_create_string(strval);
- if (!val)
+ if (!val) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
r = _vconf_set(key, val);
- if (r == 0)
- last_result = true;
-
buxton_value_free(val);
- return r;
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
+
+ return 0;
}
EXPORT int vconf_set_dbl(const char *key, double dblval)
{
int r;
struct buxton_value *val;
- last_result = false;
+ last_result = true;
if (!key) {
- buxton_errno_set(EINVAL);
- return -1;
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
}
val = buxton_value_create_double(dblval);
- if (!val)
+ if (!val) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
r = _vconf_set(key, val);
- if (r == 0)
- last_result = true;
-
buxton_value_free(val);
- return r;
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
+
+ return 0;
}
static int _vconf_get(const char *key, enum buxton_key_type type,
int r;
struct buxton_value *v;
- assert(key);
- assert(val);
-
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
- return -1;
+ return r;
}
r = buxton_get_value_sync(client, _vconf_get_layer(key), key, &v);
- if (r == -1) {
- LOGE("get value: key '%s' errno %d", key, buxton_errno_get());
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("get value: key '%s' errno %d", key, r);
} else {
enum buxton_key_type t;
r = buxton_value_get_type(v, &t);
- if (r == -1)
+ if (r != 0)
t = BUXTON_TYPE_UNKNOWN;
if (t != type) {
+ LOGE("type mismatch %d", t);
buxton_value_free(v);
- buxton_errno_set(ENOTSUP);
- r = -1;
+ r = BUXTON_ERROR_INVALID_PARAMETER;
} else {
*val = v;
+ r = BUXTON_ERROR_NONE;
}
}
int r;
struct buxton_value *val;
int32_t i;
- last_result = false;
+ last_result = true;
if (!key || !intval) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
r = _vconf_get(key, BUXTON_TYPE_INT32, &val);
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
return -1;
+ }
r = buxton_value_get_int32(val, &i);
-
buxton_value_free(val);
-
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
*intval = i;
- last_result = true;
-
return 0;
}
int r;
struct buxton_value *val;
int32_t b;
- last_result = false;
+ last_result = true;
if (!key || !boolval) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
r = _vconf_get(key, BUXTON_TYPE_BOOLEAN, &val);
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
return -1;
+ }
r = buxton_value_get_boolean(val, &b);
-
buxton_value_free(val);
-
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
*boolval = b;
- last_result = true;
-
return 0;
}
struct buxton_value *val;
const char *s;
char *str;
- last_result = false;
+ last_result = true;
if (!key) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return NULL;
}
r = _vconf_get(key, BUXTON_TYPE_STRING, &val);
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
return NULL;
+ }
r = buxton_value_get_string(val, &s);
- if (r == -1)
- s = NULL;
- else if (r == 0)
- last_result = true;
+ if (r != 0) {
+ buxton_value_free(val);
+ VCONF_SET_ERRNO(buxton_err_get_errno());
+ return NULL;
+ }
str = s ? strdup(s) : NULL;
-
buxton_value_free(val);
return str;
int r;
struct buxton_value *val;
double d;
- last_result = false;
+ last_result = true;
if (!key || !dblval) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
r = _vconf_get(key, BUXTON_TYPE_DOUBLE, &val);
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(r);
return -1;
+ }
r = buxton_value_get_double(val, &d);
-
buxton_value_free(val);
-
- if (r == -1)
+ if (r != 0) {
+ VCONF_SET_ERRNO(buxton_err_get_errno());
return -1;
+ }
*dblval = d;
- last_result = true;
-
return 0;
}
if (last_result)
return VCONF_OK;
- switch (buxton_errno_get()) {
+ switch (last_errno) {
case ENOENT:
ret = VCONF_ERROR_FILE_NO_ENT;
break;
EXPORT int vconf_keylist_free(keylist_t *keylist)
{
if (!keylist) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
struct _keynode_t *keynode;
GList *l;
- assert(keylist);
- assert(keyname);
+ if (!keylist || !keyname) {
+ LOGE("Invalid parameter");
+ return NULL;
+ }
for (l = keylist->list; l; l = g_list_next(l)) {
keynode = l->data;
{
struct _keynode_t *keynode;
- assert(keylist);
- assert(keyname);
-
keynode = _vconf_find_keynode(keylist, keyname);
if (keynode)
return keynode;
keynode = calloc(1, sizeof(*keynode));
- if (!keynode)
+ if (!keynode) {
+ LOGE("out of memory");
return NULL;
+ }
keynode->keyname = strdup(keyname);
if (!keynode->keyname) {
free(keynode);
+ LOGE("out of memory");
return NULL;
}
struct _keynode_t *keynode;
if (!keylist || !keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
keynode = _vconf_get_keynode(keylist, keyname);
- if (!keynode)
+ if (!keynode) {
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
+ }
if (keynode->type == VCONF_TYPE_STRING)
free(keynode->value.s);
struct _keynode_t *keynode;
if (!keylist || !keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
keynode = _vconf_get_keynode(keylist, keyname);
- if (!keynode)
+ if (!keynode) {
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
+ }
if (keynode->type == VCONF_TYPE_STRING)
free(keynode->value.s);
struct _keynode_t *keynode;
if (!keylist || !keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
keynode = _vconf_get_keynode(keylist, keyname);
- if (!keynode)
+ if (!keynode) {
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
+ }
if (keynode->type == VCONF_TYPE_STRING)
free(keynode->value.s);
char *s;
if (!keylist || !keyname || !value) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
s = strdup(value);
- if (!s)
+ if (!s) {
+ LOGE("out of memory");
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
+ }
keynode = _vconf_get_keynode(keylist, keyname);
if (!keynode) {
free(s);
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
}
struct _keynode_t *keynode;
if (!keylist || !keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
keynode = _vconf_get_keynode(keylist, keyname);
if (!keynode) {
+ VCONF_SET_ERRNO(ENOMEM);
return -1;
}
struct _keynode_t *keynode;
if (!keylist || !keyname) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
keynode = _vconf_find_keynode(keylist, keyname);
if (!keynode) {
- buxton_errno_set(ENOENT);
+ LOGE("%s doesn't exist", keyname);
+ VCONF_SET_ERRNO(ENOENT);
return -1;
}
GList *next;
if (!keylist) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return NULL;
}
next = g_list_next(keylist->cursor);
- if (!next) {
+ if (!next)
next = g_list_first(keylist->cursor);
- }
if (next)
node = next->data;
GList *l;
if (!keylist) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
l = g_list_last(keylist->cursor);
if (!l) {
- buxton_errno_set(ENOENT);
+ VCONF_SET_ERRNO(ENOENT);
return -1;
}
enum buxton_key_type t;
const char *s;
- assert(v);
- assert(keynode);
+ if (!v || !keynode) {
+ LOGE("Invalid parameter");
+ return EINVAL;
+ }
r = buxton_value_get_type(v, &t);
if (r == -1)
t = BUXTON_TYPE_UNKNOWN;
+ r = 0;
switch (t) {
case BUXTON_TYPE_INT32:
keynode->type = VCONF_TYPE_INT;
- r = buxton_value_get_int32(v, &keynode->value.i);
+ if (buxton_value_get_int32(v, &keynode->value.i) != 0)
+ r = buxton_err_get_errno();
break;
case BUXTON_TYPE_BOOLEAN:
keynode->type = VCONF_TYPE_BOOL;
- r = buxton_value_get_boolean(v, &keynode->value.b);
+ if (buxton_value_get_boolean(v, &keynode->value.b) != 0)
+ r = buxton_err_get_errno();
break;
case BUXTON_TYPE_DOUBLE:
keynode->type = VCONF_TYPE_DOUBLE;
- r = buxton_value_get_double(v, &keynode->value.d);
+ if (buxton_value_get_double(v, &keynode->value.d) != 0)
+ r = buxton_err_get_errno();
break;
case BUXTON_TYPE_STRING:
keynode->type = VCONF_TYPE_STRING;
- r = buxton_value_get_string(v, &s);
- if (r != -1) {
+ if (buxton_value_get_string(v, &s) != 0) {
+ r = buxton_err_get_errno();
+ } else {
if (s) {
keynode->value.s = strdup(s);
- if (!keynode->value.s)
- r = -1;
+ if (!keynode->value.s) {
+ LOGE("out of memory");
+ r = ENOMEM;
+ }
}
}
break;
struct buxton_value *v;
struct _keynode_t *keynode;
- assert(client);
- assert(keyname);
-
r = buxton_get_value_sync(client, _vconf_get_layer(keyname), keyname, &v);
if (r == -1) {
- LOGE("get value: key '%s' errno %d", keyname, buxton_errno_get());
+ r = buxton_err_get_errno();
+ LOGE("get value: key '%s' errno %d", keyname, r);
return NULL;
}
keynode = calloc(1, sizeof(*keynode));
if (!keynode) {
buxton_value_free(v);
+ LOGE("out of memory");
return NULL;
}
if (!keynode->keyname) {
free(keynode);
buxton_value_free(v);
+ LOGE("out of memory");
return NULL;
}
r = _vconf_set_keynode_value(v, keynode);
- if (r == -1) {
+ if (r != 0) {
free(keynode);
buxton_value_free(v);
return NULL;
}
buxton_value_free(v);
-
return keynode;
}
int dirlen;
if (!keylist || !in_parentDIR) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
dirlen = strlen(in_parentDIR);
if (dirlen < 2) { /* minimum is "db" */
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter %s", in_parentDIR);
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
r = buxton_list_keys_sync(client, _vconf_get_layer(in_parentDIR), &names, &len);
- if (r == -1) {
- LOGE("get key list: errno %d", buxton_errno_get());
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("get key list: errno %d", r);
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
GList *l;
if (!keylist) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
r = 0;
break;
}
- if (r == -1)
- LOGE("set key '%s' errno %d", keynode->keyname, errno);
+ if (r != 0)
+ LOGE("set key '%s' errno %d", keynode->keyname, last_errno);
}
return 0;
/* LCOV_EXCL_START */
if (!in_key) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
r = buxton_unset_value_sync(client, _vconf_get_layer(in_key), in_key);
- if (r == -1)
- LOGE("unset value: key '%s' errno %d", in_key, buxton_errno_get());
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
+
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("unset value: key '%s' errno %d", in_key, r);
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
/* LCOV_EXCL_STOP */
- return r;
+ return 0;
}
EXPORT int vconf_unset_recursive(const char *in_dir)
/* LCOV_EXCL_START */
if (!in_dir) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
dirlen = strlen(in_dir);
if (dirlen < 2) { /* minimum is "db" */
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter %s", in_dir);
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
r = buxton_list_keys_sync(client, _vconf_get_layer(in_dir), &names, &len);
- if (r == -1) {
- LOGE("get key list: errno %d", buxton_errno_get());
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("get key list: errno %d", r);
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
pthread_mutex_unlock(&vconf_lock);
r = vconf_unset(names[i]);
pthread_mutex_lock(&vconf_lock);
- if (r == -1) {
+ if (r != 0) {
buxton_free_keys(names);
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
int r;
struct buxton_value *v;
- assert(in_key);
+ if (!in_key) {
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
+ return -1;
+ }
pthread_mutex_lock(&vconf_lock);
r = _vconf_con_open();
- if (r == -1) {
+ if (r != 0) {
pthread_mutex_unlock(&vconf_lock);
+ VCONF_SET_ERRNO(r);
return -1;
}
r = buxton_get_value_sync(client, _vconf_get_layer(in_key), in_key, &v);
- if (r == -1) {
- LOGE("get value: key '%s'", in_key);
- } else {
- r = 0;
- }
_vconf_con_close();
pthread_mutex_unlock(&vconf_lock);
- return r;
+ if (r != 0) {
+ r = buxton_err_get_errno();
+ LOGE("get value: key '%s' %d", in_key, r);
+ VCONF_SET_ERRNO(r);
+ return -1;
+ }
+
+ return 0;
}
EXPORT int vconf_keylist_lookup(keylist_t *keylist, const char *keyname,
struct _keynode_t *keynode;
if (!keylist || !keyname || !return_node) {
- buxton_errno_set(EINVAL);
+ LOGE("Invalid parameter");
+ VCONF_SET_ERRNO(EINVAL);
return -1;
}