#include <stdlib.h>
#include <stdbool.h>
+#include <glib.h>
#include <bundle.h>
#include <bundle_internal.h>
bool is_sub_comp;
};
-struct cb_data_s {
- aul_comp_context_cb callback;
- void *user_data;
-};
+static void __destroy_comp_context(gpointer data)
+{
+ struct aul_comp_context_s *context;
+
+ context = (struct aul_comp_context_s *)data;
+ if (!context)
+ return;
+
+ if (context->comp_id)
+ free(context->comp_id);
+ if (context->instance_id)
+ free(context->instance_id);
+ if (context->app_id)
+ free(context->app_id);
+ if (context->type)
+ free(context->type);
+ free(context);
+}
+
+static struct aul_comp_context_s *__create_comp_context(bundle *b)
+{
+ struct aul_comp_context_s *context;
+ const char *val;
+
+ context = calloc(1, sizeof(struct aul_comp_context_s));
+ if (!context) {
+ _E("Out of memory");
+ return NULL;
+ }
+
+ val = bundle_get_val(b, AUL_K_PID);
+ if (!val) {
+ _E("Failed to get process ID");
+ goto err;
+ }
+ context->pid = atoi(val);
+
+ val = bundle_get_val(b, AUL_K_STATUS);
+ if (!val) {
+ _E("Failed to get component status");
+ goto err;
+ }
+ context->status = atoi(val);
+
+ val = bundle_get_val(b, AUL_K_IS_SUB_COMP);
+ if (!val) {
+ _E("Failed to get the flag for checking sub comp");
+ goto err;
+ }
+ context->status = atoi(val);
+
+ val = bundle_get_val(b, AUL_K_COMPONENT_ID);
+ if (!val) {
+ _E("Failed to get component ID");
+ goto err;
+ }
+
+ context->comp_id = strdup(val);
+ if (!context->comp_id) {
+ _E("Failed to duplicate component ID");
+ goto err;
+ }
+
+ val = bundle_get_val(b, AUL_K_INSTANCE_ID);
+ if (!val) {
+ _E("Failed to get instance ID");
+ goto err;
+ }
+
+ context->instance_id = strdup(val);
+ if (!context->instance_id) {
+ _E("Failed to duplicate instance ID");
+ goto err;
+ }
+
+ val = bundle_get_val(b, AUL_K_APPID);
+ if (!val) {
+ _E("Failed to get application ID");
+ goto err;
+ }
+
+ context->app_id = strdup(val);
+ if (!context->app_id) {
+ _E("Failed to duplicate application ID");
+ goto err;
+ }
+
+ val = bundle_get_val(b, AUL_K_COMPONENT_TYPE);
+ if (!val) {
+ _E("Failed to get component type");
+ goto err;
+ }
+
+ context->type = strdup(val);
+ if (!context->type) {
+ _E("Failed to duplicate component type");
+ goto err;
+ }
+
+ return context;
+
+err:
+ if (context)
+ __destroy_comp_context(context);
+
+ return NULL;
+}
static void __running_context_cb(app_pkt_t *pkt, void *user_data)
{
- struct cb_data_s *cb_data = (struct cb_data_s *)user_data;
- struct aul_comp_context_s context = { 0, };
+ GList **list = (GList **)user_data;
+ struct aul_comp_context_s *context;
bundle *b = NULL;
- const char *val;
- if (!pkt || !cb_data) {
+ if (!pkt) {
_E("Invalid parameter");
return;
}
if (!b)
return;
- val = bundle_get_val(b, AUL_K_PID);
- if (!val) {
- bundle_free(b);
- return;
- }
- context.pid = atoi(val);
-
- val = bundle_get_val(b, AUL_K_STATUS);
- if (!val) {
- bundle_free(b);
- return;
- }
- context.status = atoi(val);
-
- val = bundle_get_val(b, AUL_K_IS_SUB_COMP);
- if (!val) {
+ context = __create_comp_context(b);
+ if (!context) {
bundle_free(b);
return;
}
- context.is_sub_comp = atoi(val);
-
- bundle_get_str(b, AUL_K_COMPONENT_ID, &context.comp_id);
- bundle_get_str(b, AUL_K_INSTANCE_ID, &context.instance_id);
- bundle_get_str(b, AUL_K_APPID, &context.app_id);
- bundle_get_str(b, AUL_K_COMPONENT_TYPE, &context.type);
-
- cb_data->callback(&context, cb_data->user_data);
bundle_free(b);
+
+ *list = g_list_append(*list, context);
}
API int aul_comp_context_foreach(aul_comp_context_cb callback,
void *user_data)
{
- struct cb_data_s cb_data = {
- callback,
- user_data
- };
+ struct aul_comp_context_s *context;
+ GList *list = NULL;
+ GList *iter;
char buf[32];
bundle *b;
int ret;
if (fd < 0)
return aul_error_convert(fd);
- ret = aul_sock_recv_pkt_with_cb(fd, __running_context_cb, &cb_data);
- if (ret < 0)
+ ret = aul_sock_recv_pkt_with_cb(fd, __running_context_cb, &list);
+ if (ret < 0) {
+ g_list_free_full(list, __destroy_comp_context);
return aul_error_convert(ret);
+ }
+
+ iter = list;
+ while (iter) {
+ context = (struct aul_comp_context_s *)iter->data;
+ iter = g_list_next(iter);
+
+ if (!callback(context, user_data))
+ break;
+ }
+
+ g_list_free_full(list, __destroy_comp_context);
return AUL_R_OK;
}
return AUL_R_OK;
}
-static void __destroy_comp_context(aul_comp_context_h handle)
-{
- struct aul_comp_context_s *context;
-
- context = (struct aul_comp_context_s *)handle;
- if (!context)
- return;
-
- if (context->comp_id)
- free(context->comp_id);
- if (context->instance_id)
- free(context->instance_id);
- if (context->app_id)
- free(context->app_id);
- if (context->type)
- free(context->type);
- free(context);
-}
-
-static struct aul_comp_context_s *__create_comp_context(bundle *b)
-{
- struct aul_comp_context_s *context;
- const char *val;
-
- context = calloc(1, sizeof(struct aul_comp_context_s));
- if (!context) {
- _E("Out of memory");
- return NULL;
- }
-
- val = bundle_get_val(b, AUL_K_PID);
- if (!val) {
- _E("Failed to get process ID");
- goto err;
- }
- context->pid = atoi(val);
-
- val = bundle_get_val(b, AUL_K_STATUS);
- if (!val) {
- _E("Failed to get component status");
- goto err;
- }
- context->status = atoi(val);
-
- val = bundle_get_val(b, AUL_K_IS_SUB_COMP);
- if (!val) {
- _E("Failed to get the flag for checking sub comp");
- goto err;
- }
- context->status = atoi(val);
-
- val = bundle_get_val(b, AUL_K_COMPONENT_ID);
- if (!val) {
- _E("Failed to get component ID");
- goto err;
- }
-
- context->comp_id = strdup(val);
- if (!context->comp_id) {
- _E("Failed to duplicate component ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_INSTANCE_ID);
- if (!val) {
- _E("Failed to get instance ID");
- goto err;
- }
-
- context->instance_id = strdup(val);
- if (!context->instance_id) {
- _E("Failed to duplicate instance ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_APPID);
- if (!val) {
- _E("Failed to get application ID");
- goto err;
- }
-
- context->app_id = strdup(val);
- if (!context->app_id) {
- _E("Failed to duplicate application ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_COMPONENT_TYPE);
- if (!val) {
- _E("Failed to get component type");
- goto err;
- }
-
- context->type = strdup(val);
- if (!context->type) {
- _E("Failed to duplicate component type");
- goto err;
- }
-
- return context;
-
-err:
- if (context)
- __destroy_comp_context(context);
-
- return NULL;
-}
-
API int aul_comp_context_create(const char *comp_id, aul_comp_context_h *handle)
{
return aul_comp_context_create_usr(comp_id, getuid(), handle);