From 810b1c3559b39834ee62fb2abf0e4b7c4c0dc6a5 Mon Sep 17 00:00:00 2001 From: jusung son Date: Thu, 7 Mar 2019 13:38:23 +0900 Subject: [PATCH] Add buxton_error_e to return correct error - The global variable 'errno' can be contaminated by other libraries. Change-Id: Iacd03dc7c1d97cec8f80b03d001a52b01620f3f0 Signed-off-by: jusung son --- backend/gdbm.c | 168 +++--- backend/sqlite.c | 244 ++++---- client/CMakeLists.txt | 1 + client/c_common.c | 12 +- client/c_common.h | 1 + client/c_direct.c | 253 ++++----- client/c_main.c | 26 +- client/c_proc.c | 211 +++---- common/backends.c | 42 +- common/backends.h | 2 +- common/buxton_error.c | 63 +++ common/buxton_error.h | 48 ++ common/cache.c | 9 +- common/common.c | 66 ++- common/common.h | 7 +- common/config.c | 118 ++-- common/config.h | 2 +- common/direct.c | 405 +++++++------- common/proto.c | 144 ++--- common/serialize.c | 484 +++++++++------- daemon/CMakeLists.txt | 1 + daemon/cynara.c | 38 +- daemon/daemon.c | 273 ++++----- daemon/dbus.c | 5 +- daemon/socks.c | 58 +- lib/CMakeLists.txt | 3 +- lib/buxton2.c | 1204 +++++++++++++++++++++++++--------------- lib/include/buxton2_internal.h | 36 -- vconf-compat/vconf.c | 547 +++++++++++------- 29 files changed, 2450 insertions(+), 2021 deletions(-) create mode 100644 common/buxton_error.c create mode 100644 common/buxton_error.h delete mode 100644 lib/include/buxton2_internal.h diff --git a/backend/gdbm.c b/backend/gdbm.c index 7fc363d..920444d 100644 --- a/backend/gdbm.c +++ b/backend/gdbm.c @@ -28,6 +28,7 @@ #include "backend.h" #include "log.h" +#include "buxton_error.h" static GHashTable *dbs; @@ -39,50 +40,55 @@ static void free_db(GDBM_FILE db) 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) @@ -90,15 +96,11 @@ 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) @@ -106,25 +108,25 @@ 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) @@ -133,13 +135,13 @@ 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); @@ -149,12 +151,12 @@ static int remove_db(const char *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, @@ -166,13 +168,13 @@ 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; @@ -182,20 +184,15 @@ static int set_value(const char *dbpath, const char *key, const void *data, 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, @@ -204,31 +201,30 @@ 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) @@ -238,39 +234,31 @@ 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, @@ -280,18 +268,18 @@ 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; @@ -306,8 +294,8 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen, _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++) @@ -325,7 +313,7 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen, bxt_dbg("List '%s'", dbpath); - return 0; + return BUXTON_ERROR_NONE; } static void module_exit(void) @@ -339,11 +327,11 @@ static int module_init(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 = { diff --git a/backend/sqlite.c b/backend/sqlite.c index b1dba16..5ec0b7d 100644 --- a/backend/sqlite.c +++ b/backend/sqlite.c @@ -30,6 +30,7 @@ #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) @@ -44,121 +45,119 @@ static GHashTable *dbs; 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) @@ -166,15 +165,11 @@ 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) @@ -182,25 +177,24 @@ 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) @@ -209,13 +203,13 @@ 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); @@ -225,12 +219,12 @@ static int remove_db(const char *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, @@ -238,40 +232,40 @@ 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; } @@ -279,7 +273,7 @@ static int set_value(const char *dbpath, const char *key, const void *data, 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: @@ -287,7 +281,7 @@ 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); @@ -305,34 +299,34 @@ static int get_value(const char *dbpath, const char *key, void **data, 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; } @@ -340,8 +334,8 @@ static int get_value(const char *dbpath, const char *key, void **data, 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); @@ -354,7 +348,7 @@ 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("Get '%s' Key '%s'", dbpath, key); @@ -371,27 +365,27 @@ static int unset_value(const char *dbpath, const char *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; } @@ -399,7 +393,7 @@ static int unset_value(const char *dbpath, const char *key) 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: @@ -407,7 +401,7 @@ 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); @@ -431,20 +425,20 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen, 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; @@ -459,8 +453,8 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen, _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; } @@ -481,7 +475,7 @@ 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); @@ -505,20 +499,20 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu 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); @@ -527,7 +521,7 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu } 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; } @@ -539,7 +533,7 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu 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; } @@ -549,7 +543,7 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu if (!key_list || !value_list || !value_len_list) { bxt_err("calloc error OOM"); - ret = -1; + ret = BUXTON_ERROR_OUT_OF_MEMORY; goto end; } @@ -558,7 +552,7 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu 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; } @@ -568,7 +562,7 @@ static int get_dump(const char *dbpath, char ***keys, void ***values, int **valu 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]); @@ -588,11 +582,11 @@ end: 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]) @@ -629,11 +623,11 @@ static int module_init(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 = { diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 6e43afe..d700180 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -12,6 +12,7 @@ SET(SRC c_main.c ../common/config.c ../common/serialize.c ../common/cache.c + ../common/buxton_error.c ) ADD_EXECUTABLE(${TARGET} ${SRC}) SET_TARGET_PROPERTIES(${TARGET} PROPERTIES diff --git a/client/c_common.c b/client/c_common.c index 04b61ce..ac5ce54 100644 --- a/client/c_common.c +++ b/client/c_common.c @@ -114,8 +114,8 @@ int c_set_value(enum buxton_key_type type, const char *value, 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)); @@ -147,16 +147,16 @@ int c_set_value(enum buxton_key_type type, const char *value, 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; } diff --git a/client/c_common.h b/client/c_common.h index a90dbb1..be0abde 100644 --- a/client/c_common.h +++ b/client/c_common.h @@ -22,6 +22,7 @@ #include #include "buxton2.h" +#include "buxton_error.h" #include "c_log.h" diff --git a/client/c_direct.c b/client/c_direct.c index a8e817d..f4c22b0 100644 --- a/client/c_direct.c +++ b/client/c_direct.c @@ -33,7 +33,6 @@ #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)" @@ -71,16 +70,12 @@ static void c_exit(void) 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, @@ -89,38 +84,29 @@ 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() @@ -207,35 +193,28 @@ static int c_direct_set(const struct buxton_layer *layer, { 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); @@ -299,37 +278,32 @@ static int c_direct_create(const struct buxton_layer *layer, { 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; } @@ -395,32 +369,24 @@ static int c_direct_get_priv(const struct buxton_layer *layer, { 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); @@ -447,31 +413,23 @@ static int c_direct_set_priv(const struct buxton_layer *layer, 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; @@ -496,30 +454,23 @@ int c_direct_unset(const struct buxton_layer *layer, 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; @@ -532,30 +483,23 @@ int c_direct_list(const struct buxton_layer *layer, 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; @@ -566,7 +510,7 @@ int c_direct_list(const struct buxton_layer *layer, buxton_free_keys(keys); - return 0; + return r; } int c_direct_remove_garbage(const struct buxton_layer *layer, @@ -580,20 +524,16 @@ 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); @@ -601,22 +541,24 @@ int c_direct_remove_garbage(const struct buxton_layer *layer, 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); @@ -627,7 +569,7 @@ int c_direct_remove_garbage(const struct buxton_layer *layer, 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, @@ -637,30 +579,23 @@ 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++) { @@ -698,6 +633,6 @@ int c_direct_dump(const struct buxton_layer *layer, buxton_free_keys(key_list); free(value_list); - return 0; + return BUXTON_ERROR_NONE; } diff --git a/client/c_main.c b/client/c_main.c index 344a816..8274219 100644 --- a/client/c_main.c +++ b/client/c_main.c @@ -31,6 +31,7 @@ #include "c_log.h" #include "c_proc.h" #include "c_direct.h" +#include "buxton_error.h" struct options { char *confpath; @@ -288,7 +289,7 @@ static int get_layer(const char *lnm, uid_t uid, enum buxton_layer_type type, _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; @@ -296,7 +297,7 @@ static int get_layer(const char *lnm, uid_t uid, enum buxton_layer_type type, if (!_layer) { *layer = NULL; - return 0; + return BUXTON_ERROR_NONE; } if (uid == 0) @@ -307,7 +308,7 @@ static int get_layer(const char *lnm, uid_t uid, enum buxton_layer_type type, *layer = _layer; - return 0; + return BUXTON_ERROR_NONE; } static void print_usage(const char *name, const struct command *comm) @@ -367,15 +368,10 @@ static int parse_args(gint *argc, gchar ***argv, struct options *opt) { 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); @@ -390,10 +386,10 @@ static int parse_args(gint *argc, gchar ***argv, struct options *opt) 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[]) @@ -410,6 +406,9 @@ 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; @@ -442,10 +441,9 @@ int main(int argc, char *argv[]) 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, @@ -453,7 +451,7 @@ int main(int argc, char *argv[]) buxton_free_layer(layer); - if (r == -1) + if (r != BUXTON_ERROR_NONE) return EXIT_FAILURE; return EXIT_SUCCESS; diff --git a/client/c_proc.c b/client/c_proc.c index 1fa24cc..da9ef59 100644 --- a/client/c_proc.c +++ b/client/c_proc.c @@ -27,8 +27,6 @@ #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) @@ -39,16 +37,13 @@ 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; @@ -58,16 +53,13 @@ static int _close(void) 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; } @@ -85,16 +77,16 @@ int c_check(UNUSED const struct buxton_layer *layer, 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, @@ -103,31 +95,27 @@ 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); @@ -142,36 +130,30 @@ static int c_set(const struct buxton_layer *layer, { 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; } @@ -231,36 +213,32 @@ static int c_create(const struct buxton_layer *layer, const char *key, { 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; @@ -313,31 +291,27 @@ static int c_get_priv(const struct buxton_layer *layer, { 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); @@ -364,31 +338,24 @@ int c_set_priv(const struct buxton_layer *layer, 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; @@ -413,30 +380,25 @@ int c_unset(const struct buxton_layer *layer, 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; @@ -449,30 +411,25 @@ int c_list(const struct buxton_layer *layer, 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; @@ -483,7 +440,7 @@ int c_list(const struct buxton_layer *layer, buxton_free_keys(keys); - return 0; + return BUXTON_ERROR_NONE; } int c_enable_security(UNUSED const struct buxton_layer *layer, @@ -493,17 +450,17 @@ 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, @@ -513,15 +470,15 @@ 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; } diff --git a/common/backends.c b/common/backends.c index f479a80..9f53758 100644 --- a/common/backends.c +++ b/common/backends.c @@ -28,6 +28,7 @@ #include "backend.h" #include "log.h" +#include "buxton_error.h" static GHashTable *backends; @@ -50,27 +51,28 @@ static void free_backend(struct module *mod) 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) @@ -81,8 +83,10 @@ 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); @@ -117,7 +121,7 @@ static struct module *load_module(const char *moddir, const char *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; } @@ -145,7 +149,7 @@ static int load_modules(const char *moddir) dir = opendir(moddir); if (!dir) { bxt_err("opendir error: %d", errno); - return -1; + return BUXTON_ERROR_IO_ERROR; } while ((result = readdir(dir)) != NULL) { @@ -165,7 +169,7 @@ static int load_modules(const char *moddir) closedir(dir); - return 0; + return BUXTON_ERROR_NONE; } GList *backend_list(void) @@ -189,18 +193,18 @@ void backend_exit(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); diff --git a/common/backends.h b/common/backends.h index 46cb1c6..de74872 100644 --- a/common/backends.h +++ b/common/backends.h @@ -24,7 +24,7 @@ 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); diff --git a/common/buxton_error.c b/common/buxton_error.c new file mode 100644 index 0000000..a77544b --- /dev/null +++ b/common/buxton_error.c @@ -0,0 +1,63 @@ +/* + * 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; +} + diff --git a/common/buxton_error.h b/common/buxton_error.h new file mode 100644 index 0000000..3e02ac6 --- /dev/null +++ b/common/buxton_error.h @@ -0,0 +1,48 @@ +/* + * 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 + +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 diff --git a/common/cache.c b/common/cache.c index 0dbc5ac..feaf410 100644 --- a/common/cache.c +++ b/common/cache.c @@ -26,6 +26,7 @@ #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) */ @@ -264,20 +265,20 @@ int cache_get(enum buxton_layer_type layer_type, const char *key, 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; @@ -286,7 +287,7 @@ int cache_get(enum buxton_layer_type layer_type, const char *key, (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) diff --git a/common/common.c b/common/common.c index e5fa53e..47e928b 100644 --- a/common/common.c +++ b/common/common.c @@ -24,31 +24,38 @@ #include #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 */ @@ -63,8 +70,10 @@ void layer_free(struct buxton_layer *layer) struct buxton_layer *layer_ref(struct buxton_layer *layer) { - if (!layer) + if (!layer) { + bxt_err("Invalid parameter"); return NULL; + } layer->refcnt++; @@ -73,8 +82,10 @@ struct buxton_layer *layer_ref(struct buxton_layer *layer) 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) { @@ -102,15 +113,15 @@ 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) { 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; @@ -119,12 +130,15 @@ char *get_search_key(const struct buxton_layer *layer, const char *key, 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; } diff --git a/common/common.h b/common/common.h index 0f437aa..01de423 100644 --- a/common/common.h +++ b/common/common.h @@ -24,6 +24,7 @@ #include #include "buxton2.h" +#include "buxton_error.h" #ifndef CONFPATH # warning "CONFPATH is not set. default value is used" @@ -105,7 +106,7 @@ struct buxton_layer { 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); @@ -126,6 +127,6 @@ struct buxton_value { 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); diff --git a/common/config.c b/common/config.c index 706d03e..bd27bc9 100644 --- a/common/config.c +++ b/common/config.c @@ -55,18 +55,21 @@ static void free_layer(struct layer *layer) 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; @@ -76,23 +79,32 @@ static GKeyFile *load_conf(const char *confpath) 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; @@ -101,6 +113,7 @@ static struct layer *create_layer(GKeyFile *kf, gchar *name) bxt_err("Layer '%s' : %s", name, err ? err->message : ""); g_clear_error(&err); + ret = BUXTON_ERROR_INVALID_PARAMETER; goto err; } @@ -117,6 +130,7 @@ static struct layer *create_layer(GKeyFile *kf, gchar *name) bxt_err("Layer '%s' : %s", name, err ? err->message : ""); g_clear_error(&err); + ret = BUXTON_ERROR_INVALID_PARAMETER; goto err; } @@ -128,6 +142,7 @@ static struct layer *create_layer(GKeyFile *kf, gchar *name) bxt_err("Layer '%s' : %s", name, err ? err->message : ""); g_clear_error(&err); + ret = BUXTON_ERROR_INVALID_PARAMETER; goto err; } @@ -144,6 +159,7 @@ static struct layer *create_layer(GKeyFile *kf, gchar *name) bxt_err("Layer '%s' : %s", name, err ? err->message : ""); g_clear_error(&err); + ret = BUXTON_ERROR_INVALID_PARAMETER; goto err; } @@ -151,14 +167,15 @@ static struct layer *create_layer(GKeyFile *kf, gchar *name) /* 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) @@ -181,13 +198,20 @@ 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) { @@ -197,8 +221,8 @@ static void add_layers(GKeyFile *kf, GList *backends) 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); @@ -238,11 +262,14 @@ static void load_log_conf(GKeyFile *kf, bool *log_on, int *max_line) { 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) { @@ -293,41 +320,39 @@ out: 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) @@ -339,25 +364,26 @@ 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); @@ -366,5 +392,5 @@ int conf_init(const char *confpath, GList *backends, bool *log_on, int *max_line g_key_file_free(kf); - return 0; + return BUXTON_ERROR_NONE; } diff --git a/common/config.h b/common/config.h index fc7ee68..a96bf82 100644 --- a/common/config.h +++ b/common/config.h @@ -25,6 +25,6 @@ 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); diff --git a/common/direct.c b/common/direct.c index 87d3764..aacbaf0 100644 --- a/common/direct.c +++ b/common/direct.c @@ -43,8 +43,8 @@ static int get_path(uid_t uid, enum buxton_layer_type type, 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) @@ -59,7 +59,7 @@ static int get_path(uid_t uid, enum buxton_layer_type type, 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, @@ -70,37 +70,35 @@ 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, @@ -111,21 +109,19 @@ 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, @@ -137,40 +133,40 @@ 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) @@ -180,45 +176,47 @@ 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; } @@ -230,35 +228,36 @@ static int set_raw(const struct layer *ly, uid_t uid, 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, @@ -270,21 +269,21 @@ 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, @@ -296,27 +295,24 @@ 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) @@ -325,31 +321,33 @@ 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, @@ -360,36 +358,32 @@ 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) @@ -400,36 +394,37 @@ 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) @@ -450,33 +445,34 @@ int direct_list(const struct buxton_layer *layer, 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) @@ -485,7 +481,7 @@ int direct_list(const struct buxton_layer *layer, if (len) *len = _len; - return 0; + return BUXTON_ERROR_NONE; } int direct_dump(const struct buxton_layer *layer, @@ -505,45 +501,46 @@ 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; } @@ -551,7 +548,7 @@ int direct_dump(const struct buxton_layer *layer, _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; } @@ -570,7 +567,7 @@ int direct_dump(const struct buxton_layer *layer, 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; } @@ -587,7 +584,7 @@ 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) @@ -605,8 +602,8 @@ int direct_get_priv(const struct buxton_layer *layer, char **wp; if (!layer || !key || !*key || !priv) { - errno = EINVAL; - return -1; + bxt_err("Invalid parameter"); + return BUXTON_ERROR_INVALID_PARAMETER; } switch (type) { @@ -619,19 +616,16 @@ int direct_get_priv(const struct buxton_layer *layer, 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, @@ -646,8 +640,8 @@ 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) { @@ -655,17 +649,17 @@ int direct_set_priv(const struct buxton_layer *layer, 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: @@ -688,10 +682,7 @@ int direct_set_priv(const struct buxton_layer *layer, free(rp); free(wp); - if (r == -1) - return -1; - - return 0; + return r; } int direct_remove_db(const struct buxton_layer *layer) @@ -702,34 +693,33 @@ 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) @@ -744,25 +734,22 @@ int direct_init(const char *moddir, const char *confpath, bool *log_on, int *max 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; } diff --git a/common/proto.c b/common/proto.c index 73a4b20..ac4d1b0 100644 --- a/common/proto.c +++ b/common/proto.c @@ -86,13 +86,17 @@ static struct recv_info *add_rif(int fd, struct header *hdr, 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)); @@ -147,23 +151,25 @@ static int proto_recv_single(int fd, recv_callback callback, void *user_data) 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) @@ -172,7 +178,10 @@ 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) { @@ -180,25 +189,25 @@ static int recv_data(struct recv_info *rif, uint32_t len) 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 */ @@ -208,9 +217,10 @@ static int proto_recv_frag(int fd, struct header *hdr, 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: @@ -218,7 +228,7 @@ static int proto_recv_frag(int fd, struct header *hdr, 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; @@ -229,40 +239,44 @@ static int proto_recv_frag(int fd, struct header *hdr, 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) @@ -271,8 +285,8 @@ 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); @@ -283,12 +297,11 @@ int proto_recv_async(int fd, recv_callback callback, void *user_data) 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) @@ -299,8 +312,7 @@ int proto_recv_async(int fd, recv_callback callback, void *user_data) 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); @@ -315,8 +327,8 @@ int proto_send_block(int fd, enum message_type type, uint8_t *data, int32_t len) 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))) @@ -342,7 +354,7 @@ int proto_send_block(int fd, enum message_type type, uint8_t *data, int32_t len) continue; bxt_err("send: fd %d poll errno %d", fd, errno); - return -1; + return BUXTON_ERROR_IO_ERROR; } hdr->len = len - sent; @@ -358,14 +370,14 @@ int proto_send_block(int fd, enum message_type type, uint8_t *data, int32_t len) 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) @@ -375,16 +387,17 @@ 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; @@ -410,15 +423,14 @@ int proto_send(int fd, enum message_type type, uint8_t *data, int32_t len) 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); } @@ -429,7 +441,7 @@ int proto_send(int fd, enum message_type type, uint8_t *data, int32_t len) if (r == -1) { bxt_err("send: fd %d errno %d", fd, errno); - return -1; + return BUXTON_ERROR_IO_ERROR; } if (r != sizeof(*hdr) + len) @@ -438,7 +450,7 @@ int proto_send(int fd, enum message_type type, uint8_t *data, int32_t 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) @@ -447,41 +459,45 @@ 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); @@ -495,19 +511,19 @@ int proto_recv(int fd, enum message_type *type, uint8_t **data, int32_t *len) 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; } diff --git a/common/serialize.c b/common/serialize.c index 6882e6b..7710860 100644 --- a/common/serialize.c +++ b/common/serialize.c @@ -41,33 +41,29 @@ int check_key_name(const char *key) 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, @@ -76,26 +72,26 @@ 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) @@ -141,26 +137,34 @@ 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; } /* @@ -182,7 +186,6 @@ int serialz_data(const char *rpriv, const char *wpriv, 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", @@ -190,36 +193,42 @@ int serialz_data(const char *rpriv, const char *wpriv, 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) @@ -227,15 +236,21 @@ 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) { @@ -243,12 +258,15 @@ static int gv_to_val(GVariant *v, struct buxton_value *val) 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; @@ -276,12 +294,10 @@ static int gv_to_val(GVariant *v, struct buxton_value *val) 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, @@ -293,28 +309,33 @@ 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; } } @@ -324,15 +345,16 @@ static int gv_to_values(GVariant *gv, char **rpriv, char **wpriv, 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); @@ -340,13 +362,13 @@ static int gv_to_values(GVariant *gv, char **rpriv, char **wpriv, 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, @@ -363,15 +385,23 @@ 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, @@ -381,8 +411,8 @@ int deserialz_data(uint8_t *data, int len, g_variant_unref(v); g_variant_unref(gv); - if (r == -1) - return -1; + if (r != BUXTON_ERROR_NONE) + return r; if (rpriv) *rpriv = _rpriv; @@ -393,7 +423,7 @@ int deserialz_data(uint8_t *data, int len, if (val) *val = _val; - return 0; + return BUXTON_ERROR_NONE; } void free_request(struct request *req) @@ -413,14 +443,14 @@ static int check_value(const struct buxton_value *val) { 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: @@ -432,10 +462,10 @@ static int check_value(const struct buxton_value *val) 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, @@ -450,8 +480,8 @@ 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: @@ -461,25 +491,20 @@ static int check_request(enum message_type type, 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) @@ -492,23 +517,22 @@ 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)", @@ -520,42 +544,55 @@ int serialz_request(const struct request *req, uint8_t **data, int *len) 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, @@ -564,14 +601,17 @@ 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; @@ -580,18 +620,16 @@ static int set_req(struct buxton_value *val, const char *lnm, uid_t uid, } 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) @@ -599,43 +637,49 @@ 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) { @@ -644,7 +688,7 @@ static int gv_to_req(GVariant *gv, struct request *req) } r = set_req(val, lnm, uid, type, rp, wp, key, req); - if (r == -1) + if (r != BUXTON_ERROR_NONE) free_request(req); return r; @@ -653,39 +697,46 @@ static int gv_to_req(GVariant *gv, struct request *req) 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) @@ -710,15 +761,15 @@ static int check_response(enum message_type type, int32_t 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: @@ -730,19 +781,13 @@ static int check_response(enum message_type type, int32_t res, 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, @@ -752,23 +797,29 @@ 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: @@ -777,8 +828,8 @@ static int res_to_gv(enum message_type type, int32_t res, 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); @@ -791,7 +842,7 @@ static int res_to_gv(enum message_type type, int32_t res, *gv = v; - return 0; + return BUXTON_ERROR_NONE; } int serialz_response(enum message_type type, uint32_t msgid, int32_t res, @@ -806,39 +857,45 @@ 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) @@ -846,34 +903,48 @@ 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) @@ -883,21 +954,22 @@ 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) { @@ -908,15 +980,16 @@ static int gv_to_res(GVariant *gv, struct response *res) 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); @@ -928,7 +1001,7 @@ static int gv_to_res(GVariant *gv, struct response *res) res->val = val; - return 0; + return BUXTON_ERROR_NONE; } int deserialz_response(uint8_t *data, int len, struct response *res) @@ -944,30 +1017,37 @@ 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; } diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index ec7f392..dd8f5df 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -21,6 +21,7 @@ SET(SRC main.c ../common/direct.c ../common/proto.c ../common/cache.c + ../common/buxton_error.c ) ADD_EXECUTABLE(${TARGET} ${SRC}) SET_TARGET_PROPERTIES(${TARGET} PROPERTIES diff --git a/daemon/cynara.c b/daemon/cynara.c index 4d9aa30..14a61b2 100644 --- a/daemon/cynara.c +++ b/daemon/cynara.c @@ -29,6 +29,7 @@ #include "log.h" #include "cynara.h" +#include "buxton_error.h" #define BUXTON_CYNARA_PERMISSIVE_MODE "BUXTON_CYNARA_PERMISSIVE_MODE" @@ -138,15 +139,8 @@ static void status_cb(int old_fd, int new_fd, cynara_async_status status, 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; @@ -229,19 +223,11 @@ static enum buxton_cynara_res check_server(struct bxt_client *client, 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); @@ -279,7 +265,6 @@ enum buxton_cynara_res buxton_cynara_check(struct bxt_client *client, 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", @@ -297,7 +282,6 @@ enum buxton_cynara_res buxton_cynara_check(struct bxt_client *client, if (!cynara) { bxt_err("Cynara is not initialized"); - errno = ENOTCONN; return BUXTON_CYNARA_ERROR; } @@ -352,7 +336,7 @@ int buxton_cynara_init(void) char *skip; if (cynara) - return 0; + return BUXTON_ERROR_NONE; cynara_skip = FALSE; @@ -363,16 +347,18 @@ int buxton_cynara_init(void) } 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) diff --git a/daemon/daemon.c b/daemon/daemon.c index 698ca83..87c9bd7 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -84,12 +84,10 @@ static gboolean signal_cb(gint fd, GIOCondition cond, gpointer data) 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)) { @@ -100,7 +98,6 @@ static gboolean signal_cb(gint fd, GIOCondition cond, gpointer data) switch (si.ssi_signo) { case SIGINT: case SIGTERM: - assert(bxtd->loop); g_main_loop_quit(bxtd->loop); break; case SIGPIPE: @@ -188,9 +185,8 @@ static gboolean del_client(gpointer data) { 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); @@ -206,11 +202,14 @@ static void send_res(struct bxt_client *cli, struct response *resp) 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; } @@ -218,19 +217,20 @@ static void send_res(struct bxt_client *cli, struct response *resp) 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); @@ -239,7 +239,7 @@ static char *get_search_key_u(const struct buxton_layer *layer, const char *key) 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) @@ -253,7 +253,7 @@ 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) { @@ -294,11 +294,8 @@ static void send_notis(struct bxt_daemon *bxtd, struct request *rqst) 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); @@ -315,14 +312,14 @@ static void send_notis(struct bxt_daemon *bxtd, struct request *rqst) 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) { @@ -357,94 +354,62 @@ static void send_notis(struct bxt_daemon *bxtd, struct request *rqst) 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) @@ -465,34 +430,23 @@ 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; @@ -506,7 +460,7 @@ static void proc_notify(struct bxt_client *cli, _write_file_log(LOG_REG_NOTI, cli, rqst); - resp->res = 0; + resp->res = BUXTON_ERROR_NONE; } static void proc_unnotify(struct bxt_client *cli, @@ -515,23 +469,15 @@ 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; } @@ -545,7 +491,7 @@ static void proc_unnotify(struct bxt_client *cli, _write_file_log(LOG_UNREG_NOTI, cli, rqst); - resp->res = 0; + resp->res = BUXTON_ERROR_NONE; free(lykey); } @@ -553,15 +499,10 @@ static void proc_unnotify(struct bxt_client *cli, 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; } @@ -570,23 +511,19 @@ static void proc_set_priv(struct bxt_client *cli, 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; } @@ -596,14 +533,12 @@ static void proc_get_priv(struct bxt_client *cli, 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; } @@ -644,32 +579,29 @@ static int update_client_label(struct bxt_client *cli) { 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, @@ -693,26 +625,26 @@ static proc_func proc_funcs[MSG_MAX] = { 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; } @@ -725,7 +657,10 @@ static void cyn_cb(struct bxt_client *cli, enum buxton_cynara_res res, 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; @@ -737,7 +672,7 @@ static void cyn_cb(struct bxt_client *cli, enum buxton_cynara_res res, break; case BUXTON_CYNARA_DENIED: default: - resp.res = EPERM; + resp.res = BUXTON_ERROR_PERMISSION_DENIED; break; } @@ -757,9 +692,6 @@ static int check_priv(struct bxt_client *cli, struct request *rqst) enum buxton_priv_type type; char *priv; - assert(cli); - assert(rqst); - switch (rqst->type) { case MSG_SET: case MSG_GET: @@ -770,10 +702,8 @@ static int check_priv(struct bxt_client *cli, struct request *rqst) 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); @@ -796,20 +726,22 @@ static int proc_serialized_msg(struct bxt_client *cli, uint8_t *data, int len) 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)); @@ -817,7 +749,8 @@ static int proc_serialized_msg(struct bxt_client *cli, uint8_t *data, int len) 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); @@ -830,7 +763,7 @@ static int proc_serialized_msg(struct bxt_client *cli, uint8_t *data, int len) free_request(rqst); free(rqst); - return 0; + return BUXTON_ERROR_NONE; } static int proc_client_msg(struct bxt_client *cli) @@ -841,8 +774,8 @@ 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: @@ -862,7 +795,7 @@ static int proc_client_msg(struct bxt_client *cli) case MSG_NOTI: default: bxt_err("proc msg: Invalid message type %d", type); - r = -1; + r = BUXTON_ERROR_INVALID_PARAMETER; break; } @@ -913,7 +846,7 @@ static gboolean client_cb(gint fd, GIOCondition cond, gpointer data) } 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; @@ -928,14 +861,14 @@ static void add_client(struct bxt_daemon *bxtd, int fd) 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; } @@ -960,8 +893,6 @@ static gboolean accept_cb(gint fd, GIOCondition cond, gpointer data) struct sockaddr sa; socklen_t addrlen; - assert(bxtd); - bxt_dbg("Accept: fd %d cond %x", fd, cond); addrlen = sizeof(sa); @@ -983,8 +914,6 @@ static gboolean accept_cb(gint fd, GIOCondition cond, gpointer data) 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); @@ -1181,51 +1110,53 @@ static int bxt_init(struct bxt_daemon *bxtd, const char *confpath) { 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; } diff --git a/daemon/dbus.c b/daemon/dbus.c index 6c070e7..bd47c00 100644 --- a/daemon/dbus.c +++ b/daemon/dbus.c @@ -46,13 +46,14 @@ static void __signal_handler(GDBusConnection *connection, { 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); diff --git a/daemon/socks.c b/daemon/socks.c index 12c6afe..b17dc31 100644 --- a/daemon/socks.c +++ b/daemon/socks.c @@ -33,6 +33,7 @@ #include "log.h" #include "socks.h" +#include "buxton_error.h" #define SOCKET_TIMEOUT 5 /* seconds */ @@ -44,12 +45,15 @@ static int sock_create(const char *path) 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)); @@ -61,28 +65,28 @@ static int sock_create(const char *path) 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; @@ -96,14 +100,14 @@ int sock_get_server(const char *path) 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) @@ -135,7 +139,7 @@ int sock_set_client(int fd) 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 ? */ @@ -146,7 +150,7 @@ int sock_set_client(int fd) 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; @@ -154,7 +158,7 @@ int sock_set_client(int fd) 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) @@ -163,21 +167,21 @@ 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) @@ -188,13 +192,13 @@ 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); @@ -206,32 +210,34 @@ int sock_get_client_label(int fd, char **label) 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; } diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 59b2e7b..e5e2474 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,7 +5,8 @@ SET(SRC buxton2.c ../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 diff --git a/lib/buxton2.c b/lib/buxton2.c index 55d6cb9..c9fc9d7 100644 --- a/lib/buxton2.c +++ b/lib/buxton2.c @@ -32,7 +32,7 @@ #include #include "buxton2.h" -#include "buxton2_internal.h" +#include "buxton_error.h" #include "common.h" #include "log.h" @@ -93,20 +93,22 @@ struct buxton_client { 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: @@ -132,31 +134,37 @@ static struct buxton_value *value_create(enum buxton_key_type type, void *value) 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; } @@ -165,39 +173,88 @@ EXPORT struct buxton_value *buxton_value_create_string(const char *s) 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; } @@ -210,13 +267,13 @@ static int value_get(const struct buxton_value *val, void *dest, 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) { @@ -242,47 +299,97 @@ static int value_get(const struct buxton_value *val, void *dest, *(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( @@ -291,13 +398,17 @@ 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; @@ -305,6 +416,8 @@ EXPORT struct buxton_value *buxton_value_duplicate( _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; } } @@ -320,21 +433,36 @@ EXPORT void buxton_value_free(struct buxton_value *val) 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; } @@ -342,14 +470,18 @@ EXPORT void buxton_layer_set_uid(struct buxton_layer *layer, uid_t 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; } @@ -361,34 +493,41 @@ EXPORT void buxton_free_layer(struct buxton_layer *layer) 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, @@ -397,15 +536,16 @@ 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); @@ -415,7 +555,7 @@ static int find_noti(struct buxton_client *client, *noti = _noti; pthread_mutex_unlock(&client->lock); - return 0; + return BUXTON_ERROR_NONE; } static GList *copy_noti_callbacks(GList *callbacks) @@ -423,8 +563,10 @@ 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; @@ -464,30 +606,31 @@ static int proc_msg_noti(struct buxton_client *client, uint8_t *data, int len) 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); @@ -495,15 +638,15 @@ static int proc_msg_noti(struct buxton_client *client, uint8_t *data, int len) 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, @@ -512,15 +655,16 @@ 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); @@ -528,14 +672,15 @@ static int add_noti(struct buxton_client *client, 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); @@ -544,7 +689,7 @@ static int add_noti(struct buxton_client *client, *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, @@ -553,24 +698,26 @@ 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; @@ -580,20 +727,22 @@ static int add_noticb(struct bxt_noti *noti, buxton_notify_callback notify, 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); @@ -609,29 +758,32 @@ static void proc_msg_notify(struct buxton_client *client, struct bxt_req *req, 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) @@ -640,14 +792,15 @@ 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); @@ -657,14 +810,16 @@ static int proc_msg_res(struct buxton_client *client, uint8_t *data, int len) 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); @@ -672,14 +827,19 @@ static int proc_msg_res(struct buxton_client *client, uint8_t *data, int len) 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; } @@ -689,7 +849,7 @@ static int proc_msg_res(struct buxton_client *client, uint8_t *data, int len) 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, @@ -697,8 +857,6 @@ 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); @@ -728,36 +886,36 @@ static int proc_msg(struct buxton_client *client) 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; @@ -771,32 +929,35 @@ static int wait_msg(struct buxton_client *client, guint32 msgid) 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); @@ -805,7 +966,7 @@ static int wait_msg(struct buxton_client *client, guint32 msgid) /* req is processed */ if (!req) { pthread_mutex_unlock(&client->lock); - return 0; + return BUXTON_ERROR_NONE; } pthread_mutex_unlock(&client->lock); @@ -814,13 +975,12 @@ static int wait_msg(struct buxton_client *client, guint32 msgid) } 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) @@ -839,46 +999,44 @@ static int send_req(struct buxton_client *client, const struct request *rqst) 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; @@ -891,15 +1049,16 @@ static struct bxt_req *set_value(struct buxton_client *client, 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, @@ -908,11 +1067,13 @@ 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; } @@ -926,8 +1087,10 @@ static void set_value_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); - + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -942,21 +1105,23 @@ EXPORT int buxton_set_value_sync(struct buxton_client *client, 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; } @@ -964,22 +1129,23 @@ EXPORT int buxton_set_value_sync(struct buxton_client *client, 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; @@ -991,15 +1157,16 @@ static struct bxt_req *get_value(struct buxton_client *client, 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, @@ -1007,11 +1174,12 @@ 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; } @@ -1025,7 +1193,10 @@ static void get_value_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; @@ -1042,28 +1213,31 @@ EXPORT int buxton_get_value_sync(struct buxton_client *client, 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; } @@ -1073,22 +1247,23 @@ EXPORT int buxton_get_value_sync(struct buxton_client *client, 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; @@ -1099,15 +1274,17 @@ static struct bxt_req *list_keys(struct buxton_client *client, 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, @@ -1115,11 +1292,12 @@ 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; } @@ -1135,16 +1313,20 @@ static void list_keys_sync_cb(int status, const struct buxton_layer *layer, 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; } @@ -1169,28 +1351,31 @@ EXPORT int buxton_list_keys_sync(struct buxton_client *client, 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; } @@ -1207,10 +1392,13 @@ static gboolean call_resp_cb(gpointer data) { 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); @@ -1225,18 +1413,21 @@ static int call_resp(int status, const struct buxton_layer *layer, { 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); @@ -1246,27 +1437,22 @@ static int call_resp(int status, const 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; @@ -1281,15 +1467,17 @@ static struct bxt_req *register_noti(struct buxton_client *client, 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, @@ -1302,28 +1490,35 @@ 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; } @@ -1337,7 +1532,10 @@ static void reg_noti_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -1352,40 +1550,49 @@ EXPORT int buxton_register_notification_sync(struct buxton_client *client, 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; } @@ -1401,8 +1608,10 @@ static int del_noticb(struct bxt_noti *noti, buxton_notify_callback notify, gboolean f; int cnt; - assert(noti); - assert(notify); + if (!noti || !notify) { + bxt_err("Invalid parameter"); + return BUXTON_ERROR_INVALID_PARAMETER; + } cnt = 0; f = FALSE; @@ -1426,32 +1635,28 @@ static int del_noticb(struct bxt_noti *noti, buxton_notify_callback notify, 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; @@ -1463,15 +1668,16 @@ static struct bxt_req *unregister_noti(struct buxton_client *client, 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, @@ -1485,37 +1691,50 @@ 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; } @@ -1529,7 +1748,10 @@ static void unreg_noti_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -1545,26 +1767,29 @@ EXPORT int buxton_unregister_notification_sync(struct buxton_client *client, 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; } @@ -1575,21 +1800,23 @@ EXPORT int buxton_unregister_notification_sync(struct buxton_client *client, 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; } @@ -1597,11 +1824,12 @@ EXPORT int buxton_unregister_notification_sync(struct buxton_client *client, 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; @@ -1609,13 +1837,14 @@ static struct bxt_req *create_value(struct buxton_client *client, 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; @@ -1630,15 +1859,16 @@ static struct bxt_req *create_value(struct buxton_client *client, 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, @@ -1648,12 +1878,14 @@ 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; } @@ -1667,7 +1899,10 @@ static void create_value_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -1684,22 +1919,24 @@ EXPORT int buxton_create_value_sync(struct buxton_client *client, 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; } @@ -1707,22 +1944,23 @@ EXPORT int buxton_create_value_sync(struct buxton_client *client, 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; @@ -1734,15 +1972,16 @@ static struct bxt_req *unset_value(struct buxton_client *client, 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, @@ -1750,11 +1989,13 @@ 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; } @@ -1768,7 +2009,10 @@ static void unset_value_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -1783,21 +2027,23 @@ EXPORT int buxton_unset_value_sync(struct buxton_client *client, 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; } @@ -1805,11 +2051,12 @@ EXPORT int buxton_unset_value_sync(struct buxton_client *client, 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; @@ -1817,18 +2064,18 @@ static struct bxt_req *set_priv(struct buxton_client *client, 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; @@ -1844,15 +2091,16 @@ static struct bxt_req *set_priv(struct buxton_client *client, 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, @@ -1862,12 +2110,14 @@ 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; } @@ -1881,7 +2131,10 @@ static void set_priv_sync_cb(int status, const struct buxton_layer *layer, { struct response *resp = user_data; - assert(resp); + if (!resp) { + bxt_err("Invalid parameter"); + return; + } resp->res = status; } @@ -1898,22 +2151,24 @@ EXPORT int buxton_set_privilege_sync(struct buxton_client *client, 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; } @@ -1921,28 +2176,29 @@ EXPORT int buxton_set_privilege_sync(struct buxton_client *client, 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; @@ -1954,15 +2210,16 @@ static struct bxt_req *get_priv(struct buxton_client *client, 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, @@ -1971,11 +2228,13 @@ 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; } @@ -1989,11 +2248,14 @@ static void get_priv_sync_cb(int status, const struct buxton_layer *layer, { 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); } @@ -2007,28 +2269,31 @@ EXPORT int buxton_get_privilege_sync(struct buxton_client *client, 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; } @@ -2046,13 +2311,17 @@ static void security_sync_cb(int status, const struct buxton_layer *layer, { 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; @@ -2060,13 +2329,19 @@ static struct bxt_req *security_control(struct buxton_client *client, 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; @@ -2084,26 +2359,29 @@ static struct bxt_req *security_control(struct buxton_client *client, 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; } @@ -2120,21 +2398,23 @@ EXPORT int buxton_enable_security_sync(struct buxton_client *client) 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; } @@ -2146,11 +2426,13 @@ EXPORT int buxton_disable_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, 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; } @@ -2167,21 +2449,23 @@ EXPORT int buxton_disable_security_sync(struct buxton_client *client) 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; } @@ -2195,27 +2479,36 @@ static void update_label_sync_cb(int status, const struct buxton_layer *layer, { 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; @@ -2229,26 +2522,29 @@ static struct bxt_req *update_client_label(struct buxton_client *client, 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; } @@ -2265,21 +2561,23 @@ EXPORT int buxton_update_client_label_sync(struct buxton_client *client) 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; } @@ -2312,7 +2610,8 @@ static gboolean close_conn(gpointer data) 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); @@ -2383,21 +2682,21 @@ static void free_client(struct buxton_client *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; @@ -2411,10 +2710,12 @@ int connect_server(const char *addr) 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) @@ -2423,7 +2724,10 @@ 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); @@ -2461,15 +2765,16 @@ EXPORT int buxton_close(struct buxton_client *client) 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; } @@ -2483,16 +2788,20 @@ EXPORT int buxton_open_full(struct buxton_client **client, bool attach_fd, 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; } @@ -2507,8 +2816,9 @@ EXPORT int buxton_open_full(struct buxton_client **client, bool attach_fd, 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, @@ -2516,16 +2826,18 @@ EXPORT int buxton_open_full(struct buxton_client **client, bool attach_fd, 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; } @@ -2552,13 +2864,3 @@ EXPORT int buxton_open(struct buxton_client **client, 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; -} diff --git a/lib/include/buxton2_internal.h b/lib/include/buxton2_internal.h deleted file mode 100644 index 3866bd4..0000000 --- a/lib/include/buxton2_internal.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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__ */ diff --git a/vconf-compat/vconf.c b/vconf-compat/vconf.c index adec99e..fd83b8a 100644 --- a/vconf-compat/vconf.c +++ b/vconf-compat/vconf.c @@ -30,7 +30,7 @@ #include #include "vconf.h" -#include "buxton2_internal.h" +#include "buxton_error.h" #ifndef EXPORT # define EXPORT __attribute__((visibility("default"))) @@ -42,6 +42,16 @@ #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); @@ -70,8 +80,6 @@ struct callback_info { int source_id; /**< GSource id for call_noti_cb*/ }; -static bool last_result; - static void _vconf_free_keynode(struct _keynode_t *keynode) { if (!keynode) @@ -109,8 +117,11 @@ static void _vconf_finish(void) 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; } @@ -119,8 +130,11 @@ EXPORT char *vconf_keynode_get_name(keynode_t *keynode) 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; } @@ -129,13 +143,17 @@ EXPORT int vconf_keynode_get_type(keynode_t *keynode) 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; } @@ -144,13 +162,17 @@ EXPORT int vconf_keynode_get_int(keynode_t *keynode) 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; } @@ -159,13 +181,17 @@ EXPORT double vconf_keynode_get_dbl(keynode_t *keynode) 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; } @@ -174,13 +200,17 @@ EXPORT int vconf_keynode_get_bool(keynode_t *keynode) 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; } @@ -200,7 +230,8 @@ static void _vconf_free_noti(struct noti *noti) 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)) { @@ -220,9 +251,8 @@ static void _vconf_free_noti(struct noti *noti) static void _vconf_con_close(void) { _refcnt--; - if (_refcnt) { + if (_refcnt) return; - } buxton_free_layer(system_layer); system_layer = NULL; @@ -254,9 +284,9 @@ static void _vconf_restore_connection(enum buxton_status status, 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; } @@ -271,15 +301,15 @@ static int _vconf_con_open(void) 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) { @@ -310,7 +340,7 @@ static void _vconf_to_vconf_t(const struct buxton_value *val, keynode_t *node) assert(node); r = buxton_value_get_type(val, &type); - if (r == -1) + if (r != 0) type = BUXTON_TYPE_UNKNOWN; switch (type) { @@ -465,8 +495,10 @@ static struct noti_cb *_vconf_find_noti_cb(struct noti *noti, vconf_callback_fn { 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; @@ -483,14 +515,16 @@ static int _vconf_add_noti(struct noti *noti, vconf_callback_fn cb, void *user_d { 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; @@ -498,8 +532,10 @@ static int _vconf_add_noti(struct noti *noti, vconf_callback_fn cb, void *user_d } 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; @@ -511,35 +547,36 @@ static int _vconf_add_noti(struct noti *noti, vconf_callback_fn cb, void *user_d 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 */ @@ -551,9 +588,9 @@ static void _vconf_restore_noti_cb(gpointer key, gpointer value, gpointer user_d 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++; @@ -566,38 +603,42 @@ EXPORT int vconf_notify_key_changed(const char *key, vconf_callback_fn cb, { 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); } @@ -605,10 +646,12 @@ EXPORT int vconf_notify_key_changed(const char *key, vconf_callback_fn cb, _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) @@ -616,12 +659,9 @@ 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; @@ -636,15 +676,19 @@ EXPORT int vconf_ignore_key_changed(const char *key, vconf_callback_fn cb) 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; } @@ -652,7 +696,7 @@ EXPORT int vconf_ignore_key_changed(const char *key, vconf_callback_fn cb) if (!noti) { _vconf_con_close(); pthread_mutex_unlock(&vconf_lock); - buxton_errno_set(ENOENT); + VCONF_SET_ERRNO(ENOENT); return -1; } @@ -660,7 +704,7 @@ EXPORT int vconf_ignore_key_changed(const char *key, vconf_callback_fn cb) if (!noticb || !noticb->active) { _vconf_con_close(); pthread_mutex_unlock(&vconf_lock); - buxton_errno_set(ENOENT); + VCONF_SET_ERRNO(ENOENT); return -1; } @@ -680,8 +724,8 @@ EXPORT int vconf_ignore_key_changed(const char *key, vconf_callback_fn cb) 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(); @@ -700,19 +744,18 @@ static int _vconf_set(const char *key, const struct buxton_value *val) { 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); @@ -724,96 +767,115 @@ EXPORT int vconf_set_int(const char *key, int intval) { 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, @@ -822,32 +884,31 @@ 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; } } @@ -862,27 +923,28 @@ EXPORT int vconf_get_int(const char *key, int *intval) 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; } @@ -891,28 +953,29 @@ EXPORT int vconf_get_bool(const char *key, int *boolval) 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; } @@ -922,25 +985,28 @@ EXPORT char *vconf_get_str(const char *key) 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; @@ -951,27 +1017,28 @@ EXPORT int vconf_get_dbl(const char *key, double *dblval) 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; } @@ -983,7 +1050,7 @@ EXPORT int vconf_get_ext_errno(void) if (last_result) return VCONF_OK; - switch (buxton_errno_get()) { + switch (last_errno) { case ENOENT: ret = VCONF_ERROR_FILE_NO_ENT; break; @@ -1022,7 +1089,8 @@ EXPORT keylist_t *vconf_keylist_new(void) EXPORT int vconf_keylist_free(keylist_t *keylist) { if (!keylist) { - buxton_errno_set(EINVAL); + LOGE("Invalid parameter"); + VCONF_SET_ERRNO(EINVAL); return -1; } @@ -1038,8 +1106,10 @@ static struct _keynode_t *_vconf_find_keynode(struct _keylist_t *keylist, 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; @@ -1056,20 +1126,20 @@ static struct _keynode_t *_vconf_get_keynode(struct _keylist_t *keylist, { 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; } @@ -1085,13 +1155,16 @@ EXPORT int vconf_keylist_add_int(keylist_t *keylist, 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); @@ -1108,13 +1181,16 @@ EXPORT int vconf_keylist_add_bool(keylist_t *keylist, 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); @@ -1131,13 +1207,16 @@ EXPORT int vconf_keylist_add_dbl(keylist_t *keylist, 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); @@ -1155,17 +1234,22 @@ EXPORT int vconf_keylist_add_str(keylist_t *keylist, 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; } @@ -1183,12 +1267,14 @@ EXPORT int vconf_keylist_add_null(keylist_t *keylist, const char *keyname) 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; } @@ -1200,13 +1286,15 @@ EXPORT int vconf_keylist_del(keylist_t *keylist, const char *keyname) 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; } @@ -1222,14 +1310,14 @@ EXPORT keynode_t *vconf_keylist_nextnode(keylist_t *keylist) 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; @@ -1244,14 +1332,15 @@ EXPORT int vconf_keylist_rewind(keylist_t *keylist) 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; } @@ -1266,34 +1355,43 @@ static int _vconf_set_keynode_value(struct buxton_value *v, struct _keynode_t *k 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; @@ -1312,18 +1410,17 @@ static struct _keynode_t *_vconf_alloc_keynode(const char *keyname) 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; } @@ -1331,18 +1428,18 @@ static struct _keynode_t *_vconf_alloc_keynode(const char *keyname) 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; } @@ -1356,28 +1453,33 @@ EXPORT int vconf_get(keylist_t *keylist, 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; } @@ -1408,7 +1510,8 @@ EXPORT int vconf_set(keylist_t *keylist) GList *l; if (!keylist) { - buxton_errno_set(EINVAL); + LOGE("Invalid parameter"); + VCONF_SET_ERRNO(EINVAL); return -1; } @@ -1434,8 +1537,8 @@ EXPORT int vconf_set(keylist_t *keylist) 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; @@ -1450,26 +1553,33 @@ EXPORT int vconf_unset(const char *in_key) /* 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) @@ -1485,28 +1595,33 @@ 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; } @@ -1517,7 +1632,7 @@ EXPORT int vconf_unset_recursive(const char *in_dir) 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); @@ -1538,26 +1653,33 @@ EXPORT int vconf_sync_key(const char *in_key) 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, @@ -1566,7 +1688,8 @@ 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; } -- 2.7.4