Added support to get country-code.
[platform/upstream/connman.git] / src / task.c
old mode 100644 (file)
new mode 100755 (executable)
index 97de7e3..280b5e4
@@ -45,8 +45,10 @@ struct connman_task {
        GPtrArray *argv;
        GPtrArray *envp;
        connman_task_exit_t exit_func;
+       connman_task_setup_t setup_func;
        void *exit_data;
        GHashTable *notify;
+       void *setup_data;
 };
 
 static GHashTable *task_hash = NULL;
@@ -93,7 +95,9 @@ static void free_task(gpointer data)
  *
  * Returns: a newly-allocated #connman_task structure
  */
-struct connman_task *connman_task_create(const char *program)
+struct connman_task *connman_task_create(const char *program,
+                                       connman_task_setup_t custom_task_setup,
+                                       void *setup_data)
 {
        struct connman_task *task;
        gint counter;
@@ -102,7 +106,7 @@ struct connman_task *connman_task_create(const char *program)
        DBG("");
 
        task = g_try_new0(struct connman_task, 1);
-       if (task == NULL)
+       if (!task)
                return NULL;
 
        counter = __sync_fetch_and_add(&task_counter, 1);
@@ -116,9 +120,13 @@ struct connman_task *connman_task_create(const char *program)
        str = g_strdup(program);
        g_ptr_array_add(task->argv, str);
 
+       task->setup_func = custom_task_setup;
+
        task->notify = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                        g_free, g_free);
 
+       task->setup_data = setup_data;
+
        DBG("task %p", task);
 
        g_hash_table_insert(task_hash, task->path, task);
@@ -130,7 +138,7 @@ struct connman_task *connman_task_create(const char *program)
  * connman_task_destory:
  * @task: task structure
  *
- * Remove and destory #task
+ * Remove and destroy #task
  */
 void connman_task_destroy(struct connman_task *task)
 {
@@ -167,7 +175,7 @@ int connman_task_add_argument(struct connman_task *task,
 
        DBG("task %p arg %s", task, name);
 
-       if (name == NULL)
+       if (!name)
                return -EINVAL;
 
        str = g_strdup(name);
@@ -175,7 +183,7 @@ int connman_task_add_argument(struct connman_task *task,
 
        va_start(ap, format);
 
-       if (format != NULL) {
+       if (format) {
                str = g_strdup_vprintf(format, ap);
                g_ptr_array_add(task->argv, str);
        }
@@ -202,7 +210,7 @@ int connman_task_add_variable(struct connman_task *task,
 
        DBG("task %p key %s", task, key);
 
-       if (key == NULL)
+       if (!key)
                return -EINVAL;
 
        va_start(ap, format);
@@ -220,7 +228,7 @@ int connman_task_add_variable(struct connman_task *task,
 /**
  * connman_task_set_notify:
  * @task: task structure
- * @member: notifcation method name
+ * @member: notification method name
  * @function: notification callback
  * @user_data: optional notification user data
  *
@@ -234,7 +242,7 @@ int connman_task_set_notify(struct connman_task *task, const char *member,
        DBG("task %p", task);
 
        notify = g_try_new0(struct notify_data, 1);
-       if (notify == NULL)
+       if (!notify)
                return -ENOMEM;
 
        notify->func = function;
@@ -277,6 +285,9 @@ static void task_setup(gpointer user_data)
        sigemptyset(&mask);
        if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
                connman_error("Failed to clean signal mask");
+
+       if (task->setup_func)
+               task->setup_func(task->setup_data);
 }
 
 /**
@@ -293,7 +304,7 @@ int connman_task_run(struct connman_task *task,
                        int *stdin_fd, int *stdout_fd, int *stderr_fd)
 {
        GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD;
-       gboolean result;
+       bool result;
        char **argv, **envp;
 
        DBG("task %p", task);
@@ -301,20 +312,20 @@ int connman_task_run(struct connman_task *task,
        if (task->pid > 0)
                return -EALREADY;
 
-       if (stdout_fd == NULL)
+       if (!stdout_fd)
                flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
 
-       if (stderr_fd == NULL)
+       if (!stderr_fd)
                flags |= G_SPAWN_STDERR_TO_DEV_NULL;
 
        task->exit_func = function;
        task->exit_data = user_data;
 
-       if (g_ptr_array_index(task->argv, task->argv->len - 1) != NULL)
+       if (g_ptr_array_index(task->argv, task->argv->len - 1))
                g_ptr_array_add(task->argv, NULL);
 
-       if (task->envp->len == 0 || g_ptr_array_index(task->envp,
-                                       task->envp->len - 1) != NULL) {
+       if (task->envp->len == 0 ||
+                       g_ptr_array_index(task->envp, task->envp->len - 1)) {
                if (g_hash_table_size(task->notify) > 0) {
                        const char *busname;
                        char *str;
@@ -340,7 +351,7 @@ int connman_task_run(struct connman_task *task,
        result = g_spawn_async_with_pipes(NULL, argv, envp, flags,
                                        task_setup, task, &task->pid,
                                        stdin_fd, stdout_fd, stderr_fd, NULL);
-       if (result == FALSE) {
+       if (!result) {
                connman_error("Failed to spawn %s", argv[0]);
                return -EIO;
        }
@@ -401,14 +412,13 @@ int connman_task_stop(struct connman_task *task)
        if (task->pid > 0) {
                kill(task->pid, SIGTERM);
 
-               g_timeout_add_seconds(0, check_kill,
-                               GINT_TO_POINTER(task->pid));
+               g_idle_add(check_kill, GINT_TO_POINTER(task->pid));
        }
 
        return 0;
 }
 
-static DBusHandlerResult task_filter(DBusConnection *connection,
+static DBusHandlerResult task_filter(DBusConnection *conn,
                                        DBusMessage *message, void *user_data)
 {
        struct connman_task *task;
@@ -419,40 +429,39 @@ static DBusHandlerResult task_filter(DBusConnection *connection,
        if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-       if (dbus_message_has_interface(message,
-                                       CONNMAN_TASK_INTERFACE) == FALSE)
+       if (!dbus_message_has_interface(message, CONNMAN_TASK_INTERFACE))
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
        path = dbus_message_get_path(message);
-       if (path == NULL)
+       if (!path)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
        task = g_hash_table_lookup(task_hash, path);
-       if (task == NULL)
+       if (!task)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
        member = dbus_message_get_member(message);
-       if (member == NULL)
+       if (!member)
                goto send_reply;
 
        notify = g_hash_table_lookup(task->notify, member);
-       if (notify == NULL)
+       if (!notify)
                goto send_reply;
 
        if (notify->func)
                reply = notify->func(task, message, notify->data);
 
 send_reply:
-       if (dbus_message_get_no_reply(message) == FALSE &&
-                                               reply == NULL) {
+       if (!dbus_message_get_no_reply(message) &&
+                                               !reply) {
 
                reply = dbus_message_new_method_return(message);
-               if (reply == NULL)
+               if (!reply)
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
        }
 
-       if (reply != NULL) {
-               dbus_connection_send(connection, reply, NULL);
+       if (reply) {
+               dbus_connection_send(conn, reply, NULL);
 
                dbus_message_unref(reply);
        }