2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <sys/types.h>
30 #include <livebox-errno.h>
37 #include "client_life.h"
38 #include "slave_life.h"
51 static int load_abi_table(void)
67 static const char *field[] = {
71 const char *ptr = NULL;
73 char group[MAX_ABI + 1];
74 char pkgname[MAX_PKGNAME + 1];
76 fp = fopen("/usr/share/"PACKAGE"/abi.ini", "rt");
78 return LB_STATUS_ERROR_IO;
82 while ((ch = getc(fp)) != EOF && state != ERROR) {
105 } else if (idx < MAX_ABI) {
108 ErrPrint("Overflow\n");
119 /* New group started */
131 ErrPrint("unknown tag\n");
136 if (*ptr == '\0' && ch == '=') {
142 } else if (*ptr == ch) {
147 while (ptr >= field[idx]) {
158 if (idx == 0) { /* LTRIM */
165 } else if (isspace(ch)) {
169 ret = abi_add_entry(group, pkgname);
171 ErrPrint("Failed to add %s for %s\n", pkgname, group);
176 } else if (idx < MAX_PKGNAME) {
180 ErrPrint("Overflow\n");
194 if (state == VALUE) {
200 ret = abi_add_entry(group, pkgname);
202 ErrPrint("Failed to add %s for %s\n", pkgname, group);
211 if (fclose(fp) != 0) {
212 ErrPrint("fclose: %s\n", strerror(errno));
214 return LB_STATUS_SUCCESS;
217 static inline int build_client_info(struct pkg_info *info)
219 static const char *dml = "SELECT auto_launch, pd_size FROM client WHERE pkgid = ?";
226 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
227 if (ret != SQLITE_OK) {
228 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
229 return LB_STATUS_ERROR_IO;
232 ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT);
233 if (ret != SQLITE_OK) {
234 ErrPrint("Failed to bind a pkgname %s\n", package_name(info));
235 sqlite3_finalize(stmt);
236 return LB_STATUS_ERROR_IO;
239 if (sqlite3_step(stmt) != SQLITE_ROW) {
240 ErrPrint("%s has no records (%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
242 sqlite3_clear_bindings(stmt);
243 sqlite3_finalize(stmt);
244 return LB_STATUS_ERROR_IO;
247 package_set_auto_launch(info, (const char *)sqlite3_column_text(stmt, 0));
249 tmp = (const char *)sqlite3_column_text(stmt, 1);
250 if (tmp && strlen(tmp)) {
251 if (sscanf(tmp, "%dx%d", &width, &height) != 2) {
252 ErrPrint("Failed to get PD width and Height (%s)\n", tmp);
254 package_set_pd_width(info, width);
255 package_set_pd_height(info, height);
260 sqlite3_clear_bindings(stmt);
261 sqlite3_finalize(stmt);
262 return LB_STATUS_SUCCESS;
265 static inline int build_provider_info(struct pkg_info *info)
267 static const char *dml = "SELECT provider.network, provider.abi, provider.secured, provider.box_type, provider.box_src, provider.box_group, provider.pd_type, provider.pd_src, provider.pd_group, provider.libexec, provider.timeout, provider.period, provider.script, provider.pinup, pkgmap.appid FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?";
273 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
274 if (ret != SQLITE_OK) {
275 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
276 return LB_STATUS_ERROR_IO;
279 if (sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
280 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
281 sqlite3_finalize(stmt);
282 return LB_STATUS_ERROR_IO;
285 if (sqlite3_bind_text(stmt, 2, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
286 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
287 sqlite3_finalize(stmt);
288 return LB_STATUS_ERROR_IO;
291 if (sqlite3_step(stmt) != SQLITE_ROW) {
292 ErrPrint("%s has no record(%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
294 sqlite3_clear_bindings(stmt);
295 sqlite3_finalize(stmt);
296 return LB_STATUS_ERROR_IO;
299 appid = (const char *)sqlite3_column_text(stmt, 14);
300 if (!appid || !strlen(appid)) {
301 ErrPrint("Failed to execute the DML for %s\n", package_name(info));
303 sqlite3_clear_bindings(stmt);
304 sqlite3_finalize(stmt);
305 return LB_STATUS_ERROR_IO;
308 package_set_network(info, sqlite3_column_int(stmt, 0));
309 package_set_secured(info, sqlite3_column_int(stmt, 2));
311 tmp = (const char *)sqlite3_column_text(stmt, 1);
312 if (tmp && strlen(tmp)) {
313 package_set_abi(info, tmp);
316 package_set_lb_type(info, sqlite3_column_int(stmt, 3));
317 tmp = (const char *)sqlite3_column_text(stmt, 4);
318 if (tmp && strlen(tmp)) {
319 package_set_lb_path(info, tmp);
321 tmp = (const char *)sqlite3_column_text(stmt, 5);
322 if (tmp && strlen(tmp)) {
323 package_set_lb_group(info, tmp);
327 package_set_pd_type(info, sqlite3_column_int(stmt, 6));
328 tmp = (const char *)sqlite3_column_text(stmt, 7);
329 if (tmp && strlen(tmp)) {
330 package_set_pd_path(info, tmp);
332 tmp = (const char *)sqlite3_column_text(stmt, 8);
333 if (tmp && strlen(tmp)) {
334 package_set_pd_group(info, tmp);
338 tmp = (const char *)sqlite3_column_text(stmt, 9);
339 if (tmp && strlen(tmp)) {
340 package_set_libexec(info, tmp);
343 package_set_timeout(info, sqlite3_column_int(stmt, 10));
345 tmp = (const char *)sqlite3_column_text(stmt, 11);
346 if (tmp && strlen(tmp)) {
347 package_set_period(info, atof(tmp));
350 tmp = (const char *)sqlite3_column_text(stmt, 12);
351 if (tmp && strlen(tmp)) {
352 package_set_script(info, tmp);
355 package_set_pinup(info, sqlite3_column_int(stmt, 13));
358 sqlite3_clear_bindings(stmt);
359 sqlite3_finalize(stmt);
360 return LB_STATUS_SUCCESS;
363 static inline int build_box_size_info(struct pkg_info *info)
365 static const char *dml = "SELECT size_type FROM box_size WHERE pkgid = ?";
368 unsigned int size_type;
369 unsigned int size_list;
371 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
372 if (ret != SQLITE_OK) {
373 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
374 return LB_STATUS_ERROR_IO;
377 if (sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
378 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
379 sqlite3_finalize(stmt);
380 return LB_STATUS_ERROR_IO;
384 while (sqlite3_step(stmt) == SQLITE_ROW) {
385 size_type = sqlite3_column_int(stmt, 0);
386 size_list |= size_type;
389 package_set_size_list(info, size_list);
392 sqlite3_clear_bindings(stmt);
393 sqlite3_finalize(stmt);
394 return LB_STATUS_SUCCESS;
397 static inline int load_context_option(struct context_item *item, int id)
399 static const char *dml = "SELECT key, value FROM option WHERE option_id = ?";
405 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
406 if (ret != SQLITE_OK) {
407 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
408 return LB_STATUS_ERROR_IO;
411 ret = sqlite3_bind_int(stmt, 1, id);
412 if (ret != SQLITE_OK) {
413 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
414 ret = LB_STATUS_ERROR_IO;
418 ret = LB_STATUS_ERROR_NOT_EXIST;
419 while (sqlite3_step(stmt) == SQLITE_ROW) {
420 key = (const char *)sqlite3_column_text(stmt, 0);
421 if (!key || !strlen(key)) {
422 ErrPrint("KEY is nil\n");
426 value = (const char *)sqlite3_column_text(stmt, 1);
427 if (!value || !strlen(value)) {
428 ErrPrint("VALUE is nil\n");
432 ret = group_add_option(item, key, value);
440 sqlite3_clear_bindings(stmt);
441 sqlite3_finalize(stmt);
445 static inline int load_context_item(struct context_info *info, int id)
447 static const char *dml = "SELECT ctx_item, option_id FROM groupmap WHERE id = ?";
448 struct context_item *item;
450 const char *ctx_item;
454 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
455 if (ret != SQLITE_OK) {
456 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
457 return LB_STATUS_ERROR_IO;
460 ret = sqlite3_bind_int(stmt, 1, id);
461 if (ret != SQLITE_OK) {
462 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
463 ret = LB_STATUS_ERROR_IO;
467 ret = LB_STATUS_ERROR_NOT_EXIST;
468 while (sqlite3_step(stmt) == SQLITE_ROW) {
469 ctx_item = (const char *)sqlite3_column_text(stmt, 0);
470 option_id = sqlite3_column_int(stmt, 1);
472 item = group_add_context_item(info, ctx_item);
474 ErrPrint("Failed to add a new context item\n");
475 ret = LB_STATUS_ERROR_FAULT;
479 ret = load_context_option(item, option_id);
487 sqlite3_clear_bindings(stmt);
488 sqlite3_finalize(stmt);
492 static inline int build_group_info(struct pkg_info *info)
494 static const char *dml = "SELECT id, cluster, category FROM groupinfo WHERE pkgid = ?";
498 const char *cluster_name;
499 const char *category_name;
500 struct cluster *cluster;
501 struct category *category;
502 struct context_info *ctx_info;
504 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
505 if (ret != SQLITE_OK) {
506 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
507 return LB_STATUS_ERROR_IO;
510 ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT);
511 if (ret != SQLITE_OK) {
512 ErrPrint("Failed to bind a package name(%s)\n", package_name(info));
513 sqlite3_finalize(stmt);
514 return LB_STATUS_ERROR_IO;
517 while (sqlite3_step(stmt) == SQLITE_ROW) {
518 id = sqlite3_column_int(stmt, 0);
519 cluster_name = (const char *)sqlite3_column_text(stmt, 1);
520 if (!cluster_name || !strlen(cluster_name)) {
521 DbgPrint("Cluster name is not valid\n");
525 category_name = (const char *)sqlite3_column_text(stmt, 2);
526 if (!category_name || !strlen(category_name)) {
527 DbgPrint("Category name is not valid\n");
531 cluster = group_find_cluster(cluster_name);
533 cluster = group_create_cluster(cluster_name);
535 ErrPrint("Failed to create a cluster(%s)\n", cluster_name);
540 category = group_find_category(cluster, category_name);
542 category = group_create_category(cluster, category_name);
544 ErrPrint("Failed to create a category(%s)\n", category_name);
551 * Step 1. Get the list of the context item from the DB using 'id'
552 * {context_item, option_id}
553 * Step 2. Get the list of the options from the DB using option_id
556 ctx_info = group_create_context_info(category, package_name(info));
558 ret = load_context_item(ctx_info, id);
560 if (ret == LB_STATUS_ERROR_NOT_EXIST) {
561 DbgPrint("Has no specific context info\n");
563 DbgPrint("Context info is not valid\n");
564 group_destroy_context_info(ctx_info);
570 package_add_ctx_info(info, ctx_info);
576 sqlite3_clear_bindings(stmt);
577 sqlite3_finalize(stmt);
578 return LB_STATUS_SUCCESS;
581 HAPI int io_is_exists(const char *lbid)
586 if (!s_info.handle) {
587 ErrPrint("DB is not ready\n");
588 return LB_STATUS_ERROR_IO;
591 ret = sqlite3_prepare_v2(s_info.handle, "SELECT COUNT(pkgid) FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
592 if (ret != SQLITE_OK) {
593 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
594 return LB_STATUS_ERROR_IO;
597 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
598 if (ret != SQLITE_OK) {
599 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
600 ret = LB_STATUS_ERROR_IO;
604 if (sqlite3_step(stmt) != SQLITE_ROW) {
605 ErrPrint("%s has no record (%s)\n", lbid, sqlite3_errmsg(s_info.handle));
606 ret = LB_STATUS_ERROR_IO;
610 ret = sqlite3_column_int(stmt, 0);
613 sqlite3_finalize(stmt);
617 HAPI char *io_livebox_pkgname(const char *pkgname)
626 if (!s_info.handle) {
627 ErrPrint("DB is not ready\n");
631 ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
632 if (ret != SQLITE_OK) {
633 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
637 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
638 if (ret != SQLITE_OK) {
639 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
643 ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
644 if (ret != SQLITE_OK) {
645 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
649 if (sqlite3_step(stmt) != SQLITE_ROW) {
650 ErrPrint("%s has no record (%s)\n", pkgname, sqlite3_errmsg(s_info.handle));
654 tmp = (char *)sqlite3_column_text(stmt, 0);
655 if (tmp && strlen(tmp)) {
658 ErrPrint("Heap: %s\n", strerror(errno));
664 sqlite3_finalize(stmt);
668 HAPI int io_crawling_liveboxes(int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data)
672 if (!s_info.handle) {
673 ErrPrint("DB is not ready\n");
678 ret = sqlite3_prepare_v2(s_info.handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
679 if (ret != SQLITE_OK) {
680 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
686 while (sqlite3_step(stmt) == SQLITE_ROW) {
687 pkgid = (const char *)sqlite3_column_text(stmt, 0);
688 if (!pkgid || !strlen(pkgid)) {
692 lbid = (const char *)sqlite3_column_text(stmt, 1);
693 if (!lbid || !strlen(lbid)) {
697 prime = (int)sqlite3_column_int(stmt, 1);
699 if (cb(pkgid, lbid, prime, data) < 0) {
701 sqlite3_finalize(stmt);
702 return LB_STATUS_ERROR_CANCEL;
707 sqlite3_finalize(stmt);
711 dir = opendir(ROOT_PATH);
713 ErrPrint("Error: %s\n", strerror(errno));
717 while ((ent = readdir(dir))) {
718 if (ent->d_name[0] == '.') {
722 if (cb(ent->d_name, ent->d_name, -2, data) < 0) {
723 if (closedir(dir) < 0) {
724 ErrPrint("closedir: %s\n", strerror(errno));
726 return LB_STATUS_ERROR_CANCEL;
730 if (closedir(dir) < 0) {
731 ErrPrint("closedir: %s\n", strerror(errno));
735 return LB_STATUS_SUCCESS;
738 HAPI int io_update_livebox_package(const char *pkgid, int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data)
746 return LB_STATUS_ERROR_INVALID;
749 if (!s_info.handle) {
750 ErrPrint("DB is not ready\n");
751 return LB_STATUS_ERROR_INVALID;
754 ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
755 if (ret != SQLITE_OK) {
756 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
757 return LB_STATUS_ERROR_FAULT;
760 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
761 if (ret != SQLITE_OK) {
762 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
763 ret = LB_STATUS_ERROR_FAULT;
768 while (sqlite3_step(stmt) == SQLITE_ROW) {
769 lbid = (char *)sqlite3_column_text(stmt, 0);
770 if (!lbid || !strlen(lbid)) {
774 prime = sqlite3_column_int(stmt, 1);
776 if (cb(pkgid, lbid, prime, data) < 0) {
777 DbgPrint("Callback canceled\n");
785 sqlite3_finalize(stmt);
789 HAPI int io_load_package_db(struct pkg_info *info)
793 if (!s_info.handle) {
794 ErrPrint("DB is not ready\n");
795 return LB_STATUS_ERROR_IO;
798 ret = build_provider_info(info);
803 ret = build_client_info(info);
808 ret = build_box_size_info(info);
813 ret = build_group_info(info);
818 return LB_STATUS_SUCCESS;
821 static inline int db_init(void)
826 ret = db_util_open_with_options(DBFILE, &s_info.handle, SQLITE_OPEN_READONLY, NULL);
827 if (ret != SQLITE_OK) {
828 ErrPrint("Failed to open a DB\n");
829 return LB_STATUS_ERROR_IO;
832 if (lstat(DBFILE, &stat) < 0) {
833 db_util_close(s_info.handle);
834 s_info.handle = NULL;
835 ErrPrint("%s\n", strerror(errno));
836 return LB_STATUS_ERROR_IO;
839 if (!S_ISREG(stat.st_mode)) {
840 ErrPrint("Invalid file\n");
841 db_util_close(s_info.handle);
842 s_info.handle = NULL;
843 return LB_STATUS_ERROR_INVALID;
846 if (stat.st_size <= 0) {
847 DbgPrint("Size is %d (But use this ;)\n", stat.st_size);
850 return LB_STATUS_SUCCESS;
853 static inline int db_fini(void)
855 if (!s_info.handle) {
856 return LB_STATUS_SUCCESS;
859 db_util_close(s_info.handle);
860 s_info.handle = NULL;
862 return LB_STATUS_SUCCESS;
865 HAPI int io_init(void)
870 DbgPrint("DB initialized: %d\n", ret);
872 ret = load_abi_table();
873 DbgPrint("ABI table is loaded: %d\n", ret);
875 return LB_STATUS_SUCCESS;
878 HAPI int io_fini(void)
885 DbgPrint("DB finalized: %d\n", ret);
886 return LB_STATUS_SUCCESS;