Handles the crash of master provider. (Fix del bug)
authorSung-jae Park <nicesj.park@samsung.com>
Sat, 21 Jul 2012 06:42:04 +0000 (15:42 +0900)
committerSung-jae Park <nicesj.park@samsung.com>
Sat, 21 Jul 2012 06:42:04 +0000 (15:42 +0900)
In case of the master is crashed,
the viewer should care about it.

So the livebox-viewer will delete all existence livebox instances
if the master provider is crashed.

Change-Id: I789c42adcbfde5a3cb28ff55f3b2cfd086e2e170

CMakeLists.txt
include/livebox_internal.h
sample/sample
src/client.c
src/desc_parser.c
src/fb.c
src/livebox.c

index c7b10f9..91e4071 100644 (file)
@@ -44,6 +44,7 @@ ADD_LIBRARY(${PROJECT_NAME} SHARED
        src/desc_parser.c
        src/master_rpc.c
        src/client.c
+       src/critical_log.c
 )
 SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${VERSION_MAJOR})
 SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${VERSION})
index a7178e6..c896866 100644 (file)
@@ -25,6 +25,7 @@ extern void lb_set_period(struct livebox *handler, double period);
 extern struct livebox *lb_ref(struct livebox *handler);
 extern struct livebox *lb_unref(struct livebox *handler);
 extern int lb_send_delete(struct livebox *handler);
+extern int lb_delete_all(void);
 
 enum lb_type { /*!< Must have to be sync with data-provider-master */
        _LB_TYPE_NONE = 0x0,
index c9f84d9..2cacdc0 100755 (executable)
Binary files a/sample/sample and b/sample/sample differ
index 87823fb..26b5e75 100644 (file)
@@ -112,13 +112,12 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
        double timestamp;
        struct livebox *handler;
        struct packet *result;
+       int ret;
 
        if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
                ErrPrint("Invalid arguemnt\n");
-               result = packet_create_reply(packet, "i", -EINVAL);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EINVAL;
+               goto out;
        }
 
        handler = lb_find_livebox_by_timestamp(timestamp);
@@ -128,10 +127,8 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                 * This can be happens only if the user delete a livebox
                 * right after create it before receive created event.
                 */
-               result = packet_create_reply(packet, "i", -ENOENT);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -ENOENT;
+               goto out;
        }
 
        DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
@@ -140,12 +137,16 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                        handler->created_cb(handler, -EFAULT, handler->created_cbdata);
        } else if (handler->state == CREATE) {
                lb_invoke_event_handler(handler, "lb,deleted");
+       } else if (handler->deleted_cb) {
+               handler->deleted_cb(handler, 0, handler->deleted_cbdata);
        }
 
        /* Just try to delete it, if a user didn't remove it from the live box list */
        lb_unref(handler);
+       ret = 0;
 
-       result = packet_create_reply(packet, "i", 0);
+out:
+       result = packet_create_reply(packet, "i", ret);
        if (!result)
                ErrPrint("Failed to create a reply packet\n");
        return result;
@@ -166,10 +167,8 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        ret = packet_get(packet, "sssiid", &pkgname, &id, &fbfile, &lb_w, &lb_h, &priority);
        if (ret != 6) {
                ErrPrint("Invalid argument\n");
-               result = packet_create_reply(packet, "i", -EINVAL);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EINVAL;
+               goto out;
        }
 
        DbgPrint("pkgname: %s, id: %s, fbfile: %s, lb_w: %d, lb_h: %d, priority: %lf\n",
@@ -177,10 +176,8 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
 
        handler = lb_find_livebox(pkgname, id);
        if (!handler) {
-               result = packet_create_reply(packet, "i", -ENOENT);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -ENOENT;
+               goto out;
        }
 
        if (handler->state != CREATE) {
@@ -190,10 +187,8 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
                 * Don't try to notice anything with this, Just ignore all events
                 * Beacuse the user doesn't wants know about this anymore
                 */
-               result = packet_create_reply(packet, "i", 0);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EPERM;
+               goto out;
        }
 
        lb_set_priority(handler, priority);
@@ -201,10 +196,7 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        if (lb_text_lb(handler)) {
                lb_set_size(handler, lb_w, lb_h);
                ret = parse_desc(handler, URI_TO_PATH(id), 0);
-               result = packet_create_reply(packet, "i", ret);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               goto out;
        }
 
        if (lb_get_lb_fb(handler)) {
@@ -221,6 +213,7 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        if (ret == 0)
                lb_invoke_event_handler(handler, "lb,updated");
 
+out:
        result = packet_create_reply(packet, "i", ret);
        if (!result)
                ErrPrint("Failed to create a reply packet\n");
@@ -242,18 +235,14 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack
        ret = packet_get(packet, "ssssii", &pkgname, &id, &descfile, &fbfile, &pd_w, &pd_h);
        if (ret != 6) {
                ErrPrint("Invalid argument\n");
-               result = packet_create_reply(packet, "i", -EINVAL);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EINVAL;
+               goto out;
        }
 
        handler = lb_find_livebox(pkgname, id);
        if (!handler) {
-               result = packet_create_reply(packet, "i", -ENOENT);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -ENOENT;
+               goto out;
        }
 
        if (handler->state != CREATE) {
@@ -263,10 +252,8 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack
                 * So don't try to notice anything about this anymore.
                 * Just ignore all events.
                 */
-               result = packet_create_reply(packet, "i", 0);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EPERM;
+               goto out;
        }
 
        lb_set_pdsize(handler, pd_w, pd_h);
@@ -278,26 +265,21 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack
                        ret = fb_create_buffer(lb_get_pd_fb(handler));
                        if (ret < 0) {
                                ErrPrint("Error: %s\n", strerror(ret));
-                               result = packet_create_reply(packet, "i", ret);
-                               if (!result)
-                                       ErrPrint("Failed to create a reply packet\n");
-                               return result;
+                               goto out;
                        }
                }
 
                ret = fb_sync(lb_get_pd_fb(handler));
                if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(URI_TO_PATH(id)));
-                       result = packet_create_reply(packet, "i", ret);
-                       if (!result)
-                               ErrPrint("Failed to create a reply packet\n");
-                       return result;
+                       goto out;
                }
 
                lb_invoke_event_handler(handler, "pd,updated");
                ret = 0;
        }
 
+out:
        result = packet_create_reply(packet, "i", ret);
        if (!result)
                ErrPrint("Failed to create a reply packet\n");
@@ -343,10 +325,8 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                        &lb_type, &pd_type, &period);
        if (ret != 20) {
                ErrPrint("Invalid argument\n");
-               result = packet_create_reply(packet, "i", -EINVAL);
-               if (!result)
-                       ErrPrint("Failed to create a reply packet\n");
-               return result;
+               ret = -EINVAL;
+               goto out;
        }
 
        ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
@@ -365,10 +345,8 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                handler = lb_new_livebox(pkgname, id, timestamp);
                if (!handler) {
                        ErrPrint("Failed to create a new livebox\n");
-                       result = packet_create_reply(packet, "i", -EFAULT);
-                       if (!result)
-                               ErrPrint("Failed to create a reply packet\n");
-                       return result;
+                       ret = -EFAULT;
+                       goto out;
                }
        } else {
                if (handler->is_created == 1) {
@@ -379,21 +357,16 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                                        content, cluster, category,
                                        lb_fname, pd_fname);
 
-                       result = packet_create_reply(packet, "i", 0);
-                       if (!result)
-                               ErrPrint("Failed to send a reply packet\n");
-
-                       return result;
+                       ret = 0;
+                       goto out;
                }
 
                lb_set_id(handler, id);
 
                if (handler->state != CREATE) {
                        lb_send_delete(handler);
-                       result = packet_create_reply(packet, "i", 0);
-                       if (!result)
-                               ErrPrint("Failed to create a reply packet\n");
-                       return result;
+                       ret = 0;
+                       goto out;
                }
        }
 
@@ -459,7 +432,10 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                lb_invoke_event_handler(handler, "lb,created");
 
        handler->is_created = 1;
-       result = packet_create_reply(packet, "i", 0);
+       ret = 0;
+
+out:
+       result = packet_create_reply(packet, "i", ret);
        if (!result)
                ErrPrint("Failed to create a reply packet\n");
        return result;
@@ -573,13 +549,16 @@ static int disconnected_cb(int handle, void *data)
                return 0;
        }
 
-       s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL); /*!< After 10 secs later, try to connect again */
+       /*!< After 10 secs later, try to connect again */
+       s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
        if (s_info.reconnector == 0) {
                ErrPrint("Failed to fire the reconnector\n");
                make_connection();
        }
 
        lb_invoke_fault_handler("provider,disconnected", MASTER_PKGNAME, "default", "disconnected");
+
+       lb_delete_all();
        return 0;
 }
 
index 6befc14..9bdcca9 100644 (file)
@@ -12,6 +12,7 @@
 #include "desc_parser.h"
 #include "dlist.h"
 #include "util.h"
+#include "critical_log.h"
 
 #define TYPE_TEXT "text"
 #define TYPE_IMAGE "image"
@@ -286,7 +287,7 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
 
                        block = calloc(1, sizeof(*block));
                        if (!block) {
-                               ErrPrint("Heap: %s\n", strerror(errno));
+                               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                update_end(handle, is_pd);
                                fclose(fp);
                                return -ENOMEM;
@@ -394,10 +395,11 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                case VALUE_TYPE:
                        if (idx == block->type_len) {
                                block->type_len += 256;
-                               block->type =
-                                       realloc(block->type, block->type_len);
-                               if (!block->type)
+                               block->type = realloc(block->type, block->type_len);
+                               if (!block->type) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
@@ -415,10 +417,11 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                case VALUE_PART:
                        if (idx == block->part_len) {
                                block->part_len += 256;
-                               block->part =
-                                       realloc(block->part, block->part_len);
-                               if (!block->part)
+                               block->part = realloc(block->part, block->part_len);
+                               if (!block->part) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
@@ -436,10 +439,11 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                case VALUE_DATA:
                        if (idx == block->data_len) {
                                block->data_len += 256;
-                               block->data =
-                                       realloc(block->data, block->data_len);
-                               if (!block->data)
+                               block->data = realloc(block->data, block->data_len);
+                               if (!block->data) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
@@ -457,10 +461,11 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                case VALUE_FILE:
                        if (idx == block->file_len) {
                                block->file_len += 256;
-                               block->file =
-                                       realloc(block->file, block->file_len);
-                               if (!block->file)
+                               block->file = realloc(block->file, block->file_len);
+                               if (!block->file) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
@@ -478,10 +483,11 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                case VALUE_GROUP:
                        if (idx == block->group_len) {
                                block->group_len += 256;
-                               block->group = realloc(block->group,
-                                                       block->group_len);
-                               if (!block->group)
+                               block->group = realloc(block->group, block->group_len);
+                               if (!block->group) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
@@ -499,8 +505,10 @@ int parse_desc(struct livebox *handle, const char *descfile, int is_pd)
                        if (idx == block->id_len) {
                                block->id_len += 256;
                                block->id = realloc(block->id, block->id_len);
-                               if (!block->id)
+                               if (!block->id) {
+                                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                                        goto errout;
+                               }
                        }
 
                        if (ch == '\n') {
index f309c77..6b80ba7 100644 (file)
--- a/src/fb.c
+++ b/src/fb.c
@@ -16,6 +16,7 @@
 #include "debug.h"
 #include "util.h"
 #include "fb.h"
+#include "critical_log.h"
 
 int errno;
 
@@ -128,13 +129,13 @@ struct fb_info *fb_create(const char *id, int w, int h)
 
        info = calloc(1, sizeof(*info));
        if (!info) {
-               ErrPrint("Heap: %s\n", strerror(errno));
+               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                return NULL;
        }
 
        info->id = strdup(id);
        if (!info->id) {
-               ErrPrint("Heap: %s\n", strerror(errno));
+               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                free(info);
                return NULL;
        }
@@ -175,7 +176,7 @@ int fb_create_buffer(struct fb_info *info)
        if (info->type == FB_TYPE_FILE || info->type == FB_TYPE_UNKNOWN) {
                buffer = calloc(1, sizeof(*buffer) + info->bufsz);
                if (!buffer) {
-                       ErrPrint("Heap: %s\n", strerror(errno));
+                       CRITICAL_LOG("Heap: %s\n", strerror(errno));
                        info->bufsz = 0;
                        return -ENOMEM;
                }
index dd390c5..5db7d1c 100644 (file)
@@ -17,6 +17,7 @@
 #include "util.h"
 #include "master_rpc.h"
 #include "client.h"
+#include "critical_log.h"
 
 #define EAPI __attribute__((visibility("default")))
 #define EVENT_INTERVAL 0.05f
@@ -55,8 +56,10 @@ static inline struct cb_info *create_cb_info(ret_cb_t cb, void *data)
        struct cb_info *info;
 
        info = malloc(sizeof(*info));
-       if (!info)
+       if (!info) {
+               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                return NULL;
+       }
 
        info->cb = cb;
        info->data = data;
@@ -95,8 +98,10 @@ static void mouse_event_cb(struct livebox *handler, const struct packet *packet,
        if (!packet)
                return;
 
-       if (packet_get(packet, "i", &ret) != 1)
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
                return;
+       }
 
        if (ret < 0)
                lb_invoke_event_handler(handler, "event,ingored");
@@ -114,15 +119,10 @@ static void resize_cb(struct livebox *handler, const struct packet *result, void
        destroy_cb_info(info);
 
        if (!result) {
-               if (cb)
-                       cb(handler, -EFAULT, cbdata);
-               return;
-       }
-
-       if (packet_get(result, "i", &ret) != 1) {
-               if (cb)
-                       cb(handler, -EINVAL, cbdata);
-               return;
+               ret = -EFAULT;
+       } else if (packet_get(result, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = -EINVAL;
        }
 
        if (cb)
@@ -154,20 +154,14 @@ static void text_signal_cb(struct livebox *handler, const struct packet *result,
        destroy_cb_info(info);
 
        if (!result) {
-               if (cb)
-                       cb(handler, -EFAULT, cbdata);
-               return;
-       }
-
-       if (packet_get(result, "i", &ret) != 1) {
-               if (cb)
-                       cb(handler, -EINVAL, cbdata);
-               return;
+               ret = -EFAULT;
+       } else if (packet_get(result, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = -EINVAL;
        }
 
        if (cb)
                cb(handler, ret, cbdata);
-
        return;
 }
 
@@ -182,15 +176,10 @@ static void set_group_cb(struct livebox *handler, const struct packet *result, v
        cb = info->cb;
 
        if (!result) {
-               if (cb)
-                       cb(handler, -EFAULT, cbdata);
-               return;
-       }
-
-       if (packet_get(result, "i", &ret) != 1) {
-               if (cb)
-                       cb(handler, -EINVAL, cbdata);
-               return;
+               ret = -EFAULT;
+       } else if (packet_get(result, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = -EINVAL;
        }
 
        if (cb)
@@ -211,15 +200,10 @@ static void period_ret_cb(struct livebox *handler, const struct packet *result,
        destroy_cb_info(info);
 
        if (!result) {
-               if (cb)
-                       cb(handler, -EFAULT, cbdata);
-               return;
-       }
-
-       if (packet_get(result, "id", &ret, &period) != 2) {
-               if (cb)
-                       cb(handler, -EINVAL, cbdata);
-               return;
+               ret = -EFAULT;
+       } else if (packet_get(result, "id", &ret, &period) != 2) {
+               ErrPrint("Invalid argument\n");
+               ret = -EINVAL;
        }
 
        if (ret == 0)
@@ -234,19 +218,23 @@ static void del_ret_cb(struct livebox *handler, const struct packet *result, voi
        int ret;
 
        if (!result) {
-               if (handler->deleted_cb)
-                       handler->deleted_cb(handler, -EFAULT, handler->deleted_cbdata);
-
-               return;
+               ret = -EFAULT;
+       } else if (packet_get(result, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = -EINVAL;
        }
 
-       if (packet_get(result, "i", &ret) != 1) {
-               if (handler->deleted_cb)
-                       handler->deleted_cb(handler, -EINVAL, handler->deleted_cbdata);
-               return;
-       }
+       DbgPrint("Returns %d (waiting deleted event)\n", ret);
 
-       DbgPrint("Returns %d\n", ret);
+       /*!
+        * \note
+        * Do not call the deleted callback from here.
+        * master will send the "deleted" event.
+        * Then invoke this callback.
+        *
+        * if (handler->deleted_cb)
+        *      handler->deleted_cb(handler, ret, handler->deleted_cbdata);
+        */
 }
 
 static void new_ret_cb(struct livebox *handler, const struct packet *result, void *data)
@@ -494,6 +482,7 @@ EAPI int livebox_init(void)
        if (!__file_log_fp)
                __file_log_fp = fdopen(1, "w+t");
 #endif
+       critical_log_init();
 
        client_init();
        return 0;
@@ -668,7 +657,7 @@ EAPI int livebox_fault_handler_set(int (*cb)(const char *, const char *, const c
 
        info = malloc(sizeof(*info));
        if (!info) {
-               ErrPrint("Heap: %s\n", strerror(errno));
+               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                return -ENOMEM;
        }
 
@@ -709,7 +698,7 @@ EAPI int livebox_event_handler_set(int (*cb)(struct livebox *, const char *, voi
 
        info = malloc(sizeof(*info));
        if (!info) {
-               ErrPrint("Heap: %s\n", strerror(errno));
+               CRITICAL_LOG("Heap: %s\n", strerror(errno));
                return -ENOMEM;
        }
 
@@ -1512,7 +1501,7 @@ int lb_set_group(struct livebox *handler, const char *cluster, const char *categ
        if (cluster) {
                pc = strdup(cluster);
                if (!pc) {
-                       ErrPrint("Heap: %s\n", strerror(errno));
+                       CRITICAL_LOG("Heap: %s (cluster: %s)\n", strerror(errno), cluster);
                        return -ENOMEM;
                }
        }
@@ -1520,7 +1509,7 @@ int lb_set_group(struct livebox *handler, const char *cluster, const char *categ
        if (category) {
                ps = strdup(category);
                if (!ps) {
-                       ErrPrint("Heap: %s\n", strerror(errno));
+                       CRITICAL_LOG("Heap: %s (category: %s)\n", strerror(errno), category);
                        free(pc);
                        return -ENOMEM;
                }
@@ -1675,6 +1664,20 @@ struct livebox *lb_new_livebox(const char *pkgname, const char *id, double times
        return handler;
 }
 
+int lb_delete_all(void)
+{
+       struct dlist *l;
+       struct dlist *n;
+       struct livebox *handler;
+
+       dlist_foreach_safe(s_info.livebox_list, l, n, handler) {
+               lb_invoke_event_handler(handler, "lb,deleted");
+               lb_unref(handler);
+       }
+
+       return 0;
+}
+
 int lb_set_content(struct livebox *handler, const char *content)
 {
        if (handler->content) {
@@ -1685,7 +1688,7 @@ int lb_set_content(struct livebox *handler, const char *content)
        if (content) {
                handler->content = strdup(content);
                if (!handler->content) {
-                       ErrPrint("Heap: %s\n", strerror(errno));
+                       CRITICAL_LOG("Heap: %s (content: %s)\n", strerror(errno), content);
                        return -ENOMEM;
                }
        }