task: Pass exit code to the exit callback
[platform/upstream/connman.git] / src / task.c
index 74c9b8b..a74c728 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2010  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
@@ -39,6 +39,7 @@ struct notify_data {
 struct connman_task {
        char *path;
        pid_t pid;
+       guint child_watch;
        GPtrArray *argv;
        GPtrArray *envp;
        connman_task_exit_t exit_func;
@@ -69,6 +70,9 @@ static void free_task(gpointer data)
        if (task->pid > 0)
                kill(task->pid, SIGTERM);
 
+       if (task->child_watch > 0)
+               g_source_remove(task->child_watch);
+
        g_ptr_array_foreach(task->envp, free_pointer, NULL);
        g_ptr_array_free(task->envp, TRUE);
 
@@ -242,17 +246,23 @@ int connman_task_set_notify(struct connman_task *task, const char *member,
 static void task_died(GPid pid, gint status, gpointer user_data)
 {
        struct connman_task *task = user_data;
+       int exit_code;
 
-       if (WIFEXITED(status))
-               DBG("task %p exit status %d", task, WEXITSTATUS(status));
-       else
+       if (WIFEXITED(status)) {
+               exit_code = WEXITSTATUS(status);
+               DBG("task %p exit status %d", task, exit_code);
+       } else {
+               exit_code = 0;
                DBG("task %p signal %d", task, WTERMSIG(status));
+       }
 
        g_spawn_close_pid(pid);
        task->pid = -1;
 
+       task->child_watch = 0;
+
        if (task->exit_func)
-               task->exit_func(task, task->exit_data);
+               task->exit_func(task, exit_code, task->exit_data);
 }
 
 static void task_setup(gpointer user_data)
@@ -328,7 +338,7 @@ int connman_task_run(struct connman_task *task,
                return -EIO;
        }
 
-       g_child_watch_add(task->pid, task_died, task);
+       task->child_watch = g_child_watch_add(task->pid, task_died, task);
 
        return 0;
 }