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
35 connman_task_notify_t func;
45 connman_task_exit_t exit_func;
50 static GHashTable *task_hash = NULL;
52 static volatile gint task_counter;
54 static DBusConnection *connection;
56 static void free_pointer(gpointer data, gpointer user_data)
61 static void free_task(gpointer data)
63 struct connman_task *task = data;
67 g_hash_table_destroy(task->notify);
71 kill(task->pid, SIGTERM);
73 if (task->child_watch > 0)
74 g_source_remove(task->child_watch);
76 g_ptr_array_foreach(task->envp, free_pointer, NULL);
77 g_ptr_array_free(task->envp, TRUE);
79 g_ptr_array_foreach(task->argv, free_pointer, NULL);
80 g_ptr_array_free(task->argv, TRUE);
87 * connman_task_create:
88 * @program: name of executable
90 * Allocate a new task of given #program
92 * Returns: a newly-allocated #connman_task structure
94 struct connman_task *connman_task_create(const char *program)
96 struct connman_task *task;
102 task = g_try_new0(struct connman_task, 1);
106 counter = g_atomic_int_exchange_and_add(&task_counter, 1);
108 task->path = g_strdup_printf("/task/%d", counter);
111 task->argv = g_ptr_array_new();
112 task->envp = g_ptr_array_new();
114 str = g_strdup(program);
115 g_ptr_array_add(task->argv, str);
117 task->notify = g_hash_table_new_full(g_str_hash, g_str_equal,
120 DBG("task %p", task);
122 g_hash_table_insert(task_hash, task->path, task);
128 * connman_task_destory:
129 * @task: task structure
131 * Remove and destory #task
133 void connman_task_destroy(struct connman_task *task)
135 DBG("task %p", task);
137 g_hash_table_remove(task_hash, task->path);
141 * connman_task_get_path:
142 * @task: task structure
146 const char *connman_task_get_path(struct connman_task *task)
152 * connman_task_add_argument:
153 * @task: task structure
154 * @name: argument name
155 * @format: format string
156 * @Varargs: list of arguments
158 * Add a new command line argument
160 int connman_task_add_argument(struct connman_task *task,
161 const char *name, const char *format, ...)
166 DBG("task %p arg %s", task, name);
171 str = g_strdup(name);
172 g_ptr_array_add(task->argv, str);
174 va_start(ap, format);
176 if (format != NULL) {
177 str = g_strdup_vprintf(format, ap);
178 g_ptr_array_add(task->argv, str);
187 * connman_task_add_variable:
188 * @task: task structure
189 * @key: variable name
190 * @format: format string
191 * @Varargs: list of arguments
193 * Add a new environment variable
195 int connman_task_add_variable(struct connman_task *task,
196 const char *key, const char *format, ...)
201 DBG("task %p key %s", task, key);
206 va_start(ap, format);
208 val = g_strdup_vprintf(format, ap);
209 str = g_strdup_printf("%s=%s", key, format ? format : "");
210 g_ptr_array_add(task->envp, str);
219 * connman_task_set_notify:
220 * @task: task structure
221 * @member: notifcation method name
222 * @function: notification callback
223 * @user_data: optional notification user data
225 * Set notification handler for #member
227 int connman_task_set_notify(struct connman_task *task, const char *member,
228 connman_task_notify_t function, void *user_data)
230 struct notify_data *notify;
232 DBG("task %p", task);
234 notify = g_try_new0(struct notify_data, 1);
238 notify->func = function;
239 notify->data = user_data;
241 g_hash_table_insert(task->notify, g_strdup(member), notify);
246 static void task_died(GPid pid, gint status, gpointer user_data)
248 struct connman_task *task = user_data;
250 if (WIFEXITED(status))
251 DBG("task %p exit status %d", task, WEXITSTATUS(status));
253 DBG("task %p signal %d", task, WTERMSIG(status));
255 g_spawn_close_pid(pid);
258 task->child_watch = 0;
261 task->exit_func(task, task->exit_data);
264 static void task_setup(gpointer user_data)
266 struct connman_task *task = user_data;
268 DBG("task %p", task);
273 * @task: task structure
274 * @function: exit callback
275 * @user_data: optional exit user data
276 * @fd: optional spawn with pipe
278 * Execute program specified by #task
280 int connman_task_run(struct connman_task *task,
281 connman_task_exit_t function, void *user_data,
282 int *stdin_fd, int *stdout_fd, int *stderr_fd)
284 GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD;
288 DBG("task %p", task);
293 if (stdout_fd == NULL)
294 flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
296 if (stderr_fd == NULL)
297 flags |= G_SPAWN_STDERR_TO_DEV_NULL;
299 task->exit_func = function;
300 task->exit_data = user_data;
302 if (g_ptr_array_index(task->argv, task->argv->len - 1) != NULL)
303 g_ptr_array_add(task->argv, NULL);
305 if (task->envp->len == 0 || g_ptr_array_index(task->envp,
306 task->envp->len - 1) != NULL) {
307 if (g_hash_table_size(task->notify) > 0) {
311 busname = dbus_bus_get_unique_name(connection);
312 str = g_strdup_printf("CONNMAN_BUSNAME=%s", busname);
313 g_ptr_array_add(task->envp, str);
315 str = g_strdup_printf("CONNMAN_INTERFACE=%s",
316 CONNMAN_TASK_INTERFACE);
317 g_ptr_array_add(task->envp, str);
319 str = g_strdup_printf("CONNMAN_PATH=%s", task->path);
320 g_ptr_array_add(task->envp, str);
323 g_ptr_array_add(task->envp, NULL);
326 argv = (char **) task->argv->pdata;
327 envp = (char **) task->envp->pdata;
329 result = g_spawn_async_with_pipes(NULL, argv, envp, flags,
330 task_setup, task, &task->pid,
331 stdin_fd, stdout_fd, stderr_fd, NULL);
332 if (result == FALSE) {
333 connman_error("Failed to spawn %s", argv[0]);
337 task->child_watch = g_child_watch_add(task->pid, task_died, task);
344 * @task: task structure
346 * Stop program specified by #task
348 int connman_task_stop(struct connman_task *task)
350 DBG("task %p", task);
353 kill(task->pid, SIGTERM);
358 static DBusHandlerResult task_filter(DBusConnection *connection,
359 DBusMessage *message, void *user_data)
361 struct connman_task *task;
362 struct notify_data *notify;
363 const char *path, *member;
365 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
366 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
368 if (dbus_message_has_interface(message,
369 CONNMAN_TASK_INTERFACE) == FALSE)
370 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
372 path = dbus_message_get_path(message);
374 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
376 task = g_hash_table_lookup(task_hash, path);
378 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
380 if (dbus_message_get_no_reply(message) == FALSE) {
384 reply = dbus_message_new_method_return(message);
386 return DBUS_HANDLER_RESULT_NEED_MEMORY;
388 result = dbus_connection_send(connection, reply, NULL);
390 dbus_message_unref(reply);
393 member = dbus_message_get_member(message);
395 return DBUS_HANDLER_RESULT_HANDLED;
397 notify = g_hash_table_lookup(task->notify, member);
399 return DBUS_HANDLER_RESULT_HANDLED;
402 notify->func(task, message, notify->data);
404 return DBUS_HANDLER_RESULT_HANDLED;
407 static const char *task_rule = "type=method_call"
408 ",interface=" CONNMAN_TASK_INTERFACE;
410 int __connman_task_init(void)
414 connection = connman_dbus_get_connection();
416 dbus_connection_add_filter(connection, task_filter, NULL, NULL);
418 g_atomic_int_set(&task_counter, 0);
420 task_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
423 dbus_bus_add_match(connection, task_rule, NULL);
424 dbus_connection_flush(connection);
429 void __connman_task_cleanup(void)
433 dbus_bus_remove_match(connection, task_rule, NULL);
434 dbus_connection_flush(connection);
436 g_hash_table_destroy(task_hash);
439 dbus_connection_remove_filter(connection, task_filter, NULL);
441 dbus_connection_unref(connection);