5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <connman/log.h>
42 static GSList *task_list = NULL;
44 struct task_data *task_find_by_pid(pid_t pid)
48 for (list = task_list; list; list = list->next) {
49 struct task_data *task = list->data;
58 struct task_data *task_find_by_index(int index)
62 for (list = task_list; list; list = list->next) {
63 struct task_data *task = list->data;
65 if (task->index == index)
72 static void task_died(GPid pid, gint status, gpointer user_data)
74 struct task_data *task = user_data;
76 if (WIFEXITED(status))
77 DBG("task %p exit status %d", task, WEXITSTATUS(status));
79 DBG("task %p signal %d", task, WTERMSIG(status));
81 g_spawn_close_pid(pid);
84 task_list = g_slist_remove(task_list, task);
87 task->callback(task->index, task->user_data);
92 static void task_setup(gpointer user_data)
94 struct task_data *task = user_data;
99 struct task_data *task_spawn(int index, char **argv, char **envp,
100 task_cb_t callback, void *user_data)
102 GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD |
103 G_SPAWN_STDOUT_TO_DEV_NULL;
104 struct task_data *task;
106 DBG("index %d", index);
108 task = g_try_new0(struct task_data, 1);
114 task->callback = callback;
115 task->user_data = user_data;
117 if (g_spawn_async(NULL, argv, envp, flags,
118 task_setup, task, &task->pid, NULL) == FALSE) {
119 connman_error("Failed to spawn task");
123 task_list = g_slist_append(task_list, task);
125 g_child_watch_add(task->pid, task_died, task);
127 DBG("task %p pid %d", task, task->pid);
132 int task_kill(struct task_data *task)
134 DBG("task %p", task);
137 kill(task->pid, SIGTERM);
142 void *task_get_data(struct task_data *task)
144 return task->user_data;