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
37 connman_task_notify_t func;
47 connman_task_exit_t exit_func;
52 static GHashTable *task_hash = NULL;
54 static volatile int task_counter;
56 static DBusConnection *connection;
58 static void free_pointer(gpointer data, gpointer user_data)
63 static void free_task(gpointer data)
65 struct connman_task *task = data;
69 g_hash_table_destroy(task->notify);
73 kill(task->pid, SIGTERM);
75 if (task->child_watch > 0)
76 g_source_remove(task->child_watch);
78 g_ptr_array_foreach(task->envp, free_pointer, NULL);
79 g_ptr_array_free(task->envp, TRUE);
81 g_ptr_array_foreach(task->argv, free_pointer, NULL);
82 g_ptr_array_free(task->argv, TRUE);
89 * connman_task_create:
90 * @program: name of executable
92 * Allocate a new task of given #program
94 * Returns: a newly-allocated #connman_task structure
96 struct connman_task *connman_task_create(const char *program)
98 struct connman_task *task;
104 task = g_try_new0(struct connman_task, 1);
108 counter = __sync_fetch_and_add(&task_counter, 1);
110 task->path = g_strdup_printf("/task/%d", counter);
113 task->argv = g_ptr_array_new();
114 task->envp = g_ptr_array_new();
116 str = g_strdup(program);
117 g_ptr_array_add(task->argv, str);
119 task->notify = g_hash_table_new_full(g_str_hash, g_str_equal,
122 DBG("task %p", task);
124 g_hash_table_insert(task_hash, task->path, task);
130 * connman_task_destory:
131 * @task: task structure
133 * Remove and destory #task
135 void connman_task_destroy(struct connman_task *task)
137 DBG("task %p", task);
139 g_hash_table_remove(task_hash, task->path);
143 * connman_task_get_path:
144 * @task: task structure
148 const char *connman_task_get_path(struct connman_task *task)
154 * connman_task_add_argument:
155 * @task: task structure
156 * @name: argument name
157 * @format: format string
158 * @Varargs: list of arguments
160 * Add a new command line argument
162 int connman_task_add_argument(struct connman_task *task,
163 const char *name, const char *format, ...)
168 DBG("task %p arg %s", task, name);
173 str = g_strdup(name);
174 g_ptr_array_add(task->argv, str);
176 va_start(ap, format);
178 if (format != NULL) {
179 str = g_strdup_vprintf(format, ap);
180 g_ptr_array_add(task->argv, str);
189 * connman_task_add_variable:
190 * @task: task structure
191 * @key: variable name
192 * @format: format string
193 * @Varargs: list of arguments
195 * Add a new environment variable
197 int connman_task_add_variable(struct connman_task *task,
198 const char *key, const char *format, ...)
203 DBG("task %p key %s", task, key);
208 va_start(ap, format);
210 val = g_strdup_vprintf(format, ap);
211 str = g_strdup_printf("%s=%s", key, format ? format : "");
212 g_ptr_array_add(task->envp, str);
221 * connman_task_set_notify:
222 * @task: task structure
223 * @member: notifcation method name
224 * @function: notification callback
225 * @user_data: optional notification user data
227 * Set notification handler for #member
229 int connman_task_set_notify(struct connman_task *task, const char *member,
230 connman_task_notify_t function, void *user_data)
232 struct notify_data *notify;
234 DBG("task %p", task);
236 notify = g_try_new0(struct notify_data, 1);
240 notify->func = function;
241 notify->data = user_data;
243 g_hash_table_replace(task->notify, g_strdup(member), notify);
248 static void task_died(GPid pid, gint status, gpointer user_data)
250 struct connman_task *task = user_data;
253 if (WIFEXITED(status)) {
254 exit_code = WEXITSTATUS(status);
255 DBG("task %p exit status %d", task, exit_code);
258 DBG("task %p signal %d", task, WTERMSIG(status));
261 g_spawn_close_pid(pid);
264 task->child_watch = 0;
267 task->exit_func(task, exit_code, task->exit_data);
270 static void task_setup(gpointer user_data)
273 struct connman_task *task = user_data;
275 DBG("task %p", task);
278 if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
279 connman_error("Failed to clean signal mask");
284 * @task: task structure
285 * @function: exit callback
286 * @user_data: optional exit user data
287 * @fd: optional spawn with pipe
289 * Execute program specified by #task
291 int connman_task_run(struct connman_task *task,
292 connman_task_exit_t function, void *user_data,
293 int *stdin_fd, int *stdout_fd, int *stderr_fd)
295 GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD;
299 DBG("task %p", task);
304 if (stdout_fd == NULL)
305 flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
307 if (stderr_fd == NULL)
308 flags |= G_SPAWN_STDERR_TO_DEV_NULL;
310 task->exit_func = function;
311 task->exit_data = user_data;
313 if (g_ptr_array_index(task->argv, task->argv->len - 1) != NULL)
314 g_ptr_array_add(task->argv, NULL);
316 if (task->envp->len == 0 || g_ptr_array_index(task->envp,
317 task->envp->len - 1) != NULL) {
318 if (g_hash_table_size(task->notify) > 0) {
322 busname = dbus_bus_get_unique_name(connection);
323 str = g_strdup_printf("CONNMAN_BUSNAME=%s", busname);
324 g_ptr_array_add(task->envp, str);
326 str = g_strdup_printf("CONNMAN_INTERFACE=%s",
327 CONNMAN_TASK_INTERFACE);
328 g_ptr_array_add(task->envp, str);
330 str = g_strdup_printf("CONNMAN_PATH=%s", task->path);
331 g_ptr_array_add(task->envp, str);
334 g_ptr_array_add(task->envp, NULL);
337 argv = (char **) task->argv->pdata;
338 envp = (char **) task->envp->pdata;
340 result = g_spawn_async_with_pipes(NULL, argv, envp, flags,
341 task_setup, task, &task->pid,
342 stdin_fd, stdout_fd, stderr_fd, NULL);
343 if (result == FALSE) {
344 connman_error("Failed to spawn %s", argv[0]);
348 task->child_watch = g_child_watch_add(task->pid, task_died, task);
353 static gboolean force_kill_timeout(gpointer user_data)
355 pid_t pid = GPOINTER_TO_INT(user_data);
357 if (kill(pid, SIGKILL) == 0)
358 connman_warn("killing pid %d by force", pid);
364 static gboolean kill_timeout(gpointer user_data)
366 pid_t pid = GPOINTER_TO_INT(user_data);
368 if (kill(pid, SIGINT) == 0)
369 g_timeout_add_seconds(1, force_kill_timeout,
370 GINT_TO_POINTER(pid));
376 static gboolean check_kill(gpointer user_data)
378 pid_t pid = GPOINTER_TO_INT(user_data);
380 if (kill(pid, 0) == 0) {
381 connman_info("pid %d was not killed, "
382 "retrying after 2 sec", pid);
383 g_timeout_add_seconds(2, kill_timeout,
384 GINT_TO_POINTER(pid));
393 * @task: task structure
395 * Stop program specified by #task
397 int connman_task_stop(struct connman_task *task)
399 DBG("task %p", task);
402 kill(task->pid, SIGTERM);
404 g_timeout_add_seconds(0, check_kill,
405 GINT_TO_POINTER(task->pid));
411 static DBusHandlerResult task_filter(DBusConnection *connection,
412 DBusMessage *message, void *user_data)
414 struct connman_task *task;
415 struct notify_data *notify;
416 const char *path, *member;
417 DBusMessage *reply = NULL;
419 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
420 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
422 if (dbus_message_has_interface(message,
423 CONNMAN_TASK_INTERFACE) == FALSE)
424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
426 path = dbus_message_get_path(message);
428 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
430 task = g_hash_table_lookup(task_hash, path);
432 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
434 member = dbus_message_get_member(message);
438 notify = g_hash_table_lookup(task->notify, member);
443 reply = notify->func(task, message, notify->data);
446 if (dbus_message_get_no_reply(message) == FALSE &&
449 reply = dbus_message_new_method_return(message);
451 return DBUS_HANDLER_RESULT_NEED_MEMORY;
455 dbus_connection_send(connection, reply, NULL);
457 dbus_message_unref(reply);
460 return DBUS_HANDLER_RESULT_HANDLED;
463 static const char *task_rule = "type=method_call"
464 ",interface=" CONNMAN_TASK_INTERFACE;
466 int __connman_task_init(void)
470 connection = connman_dbus_get_connection();
472 dbus_connection_add_filter(connection, task_filter, NULL, NULL);
475 __sync_synchronize();
477 task_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
480 dbus_bus_add_match(connection, task_rule, NULL);
481 dbus_connection_flush(connection);
486 void __connman_task_cleanup(void)
490 dbus_bus_remove_match(connection, task_rule, NULL);
491 dbus_connection_flush(connection);
493 g_hash_table_destroy(task_hash);
496 dbus_connection_remove_filter(connection, task_filter, NULL);
498 dbus_connection_unref(connection);