dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / src / task.c
index 41710cd..97de7e3 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -240,7 +240,7 @@ int connman_task_set_notify(struct connman_task *task, const char *member,
        notify->func = function;
        notify->data = user_data;
 
-       g_hash_table_insert(task->notify, g_strdup(member), notify);
+       g_hash_table_replace(task->notify, g_strdup(member), notify);
 
        return 0;
 }
@@ -350,6 +350,44 @@ int connman_task_run(struct connman_task *task,
        return 0;
 }
 
+static gboolean force_kill_timeout(gpointer user_data)
+{
+       pid_t pid = GPOINTER_TO_INT(user_data);
+       if (pid > 0) {
+               if (kill(pid, SIGKILL) == 0)
+                       connman_warn("killing pid %d by force", pid);
+       }
+
+       return FALSE;
+}
+
+static gboolean kill_timeout(gpointer user_data)
+{
+       pid_t pid = GPOINTER_TO_INT(user_data);
+       if (pid > 0) {
+               if (kill(pid, SIGINT) == 0)
+                       g_timeout_add_seconds(1, force_kill_timeout,
+                                       GINT_TO_POINTER(pid));
+       }
+
+       return FALSE;
+}
+
+static gboolean check_kill(gpointer user_data)
+{
+       pid_t pid = GPOINTER_TO_INT(user_data);
+       if (pid > 0) {
+               if (kill(pid, 0) == 0) {
+                       connman_info("pid %d was not killed, "
+                                       "retrying after 2 sec", pid);
+                       g_timeout_add_seconds(2, kill_timeout,
+                                       GINT_TO_POINTER(pid));
+               }
+       }
+
+       return FALSE;
+}
+
 /**
  * connman_task_stop:
  * @task: task structure
@@ -360,9 +398,13 @@ int connman_task_stop(struct connman_task *task)
 {
        DBG("task %p", task);
 
-       if (task->pid > 0)
+       if (task->pid > 0) {
                kill(task->pid, SIGTERM);
 
+               g_timeout_add_seconds(0, check_kill,
+                               GINT_TO_POINTER(task->pid));
+       }
+
        return 0;
 }
 
@@ -372,6 +414,7 @@ static DBusHandlerResult task_filter(DBusConnection *connection,
        struct connman_task *task;
        struct notify_data *notify;
        const char *path, *member;
+       DBusMessage *reply = NULL;
 
        if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
@@ -388,29 +431,32 @@ static DBusHandlerResult task_filter(DBusConnection *connection,
        if (task == NULL)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-       if (dbus_message_get_no_reply(message) == FALSE) {
-               DBusMessage *reply;
+       member = dbus_message_get_member(message);
+       if (member == NULL)
+               goto send_reply;
+
+       notify = g_hash_table_lookup(task->notify, member);
+       if (notify == NULL)
+               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) {
 
                reply = dbus_message_new_method_return(message);
                if (reply == NULL)
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
+       }
 
+       if (reply != NULL) {
                dbus_connection_send(connection, reply, NULL);
 
                dbus_message_unref(reply);
        }
 
-       member = dbus_message_get_member(message);
-       if (member == NULL)
-               return DBUS_HANDLER_RESULT_HANDLED;
-
-       notify = g_hash_table_lookup(task->notify, member);
-       if (notify == NULL)
-               return DBUS_HANDLER_RESULT_HANDLED;
-
-       if (notify->func)
-               notify->func(task, message, notify->data);
-
        return DBUS_HANDLER_RESULT_HANDLED;
 }