diff --git a/daemon/app_supervisor.c b/daemon/app_supervisor.c new file mode 100644 index 00000000..d1067177 --- /dev/null +++ b/daemon/app_supervisor.c @@ -0,0 +1,230 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2009 Nedko Arnaudov + * + ************************************************************************** + * This file contains implementation of app supervisor object + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "app_supervisor.h" +#include "../dbus/error.h" +#include "../dbus_constants.h" +#include "loader.h" + +struct ladish_app +{ + struct list_head siblings; + uint64_t id; + char * name; + char * commandline; +}; + +struct ladish_app_supervisor +{ + char * opath; + uint64_t version; + uint64_t next_id; + struct list_head applist; +}; + +bool ladish_app_supervisor_create(ladish_app_supervisor_handle * supervisor_handle_ptr, const char * opath) +{ + struct ladish_app_supervisor * supervisor_ptr; + + supervisor_ptr = malloc(sizeof(struct ladish_app_supervisor)); + if (supervisor_ptr == NULL) + { + log_error("malloc() failed to allocate struct ladish_app_supervisor"); + return false; + } + + supervisor_ptr->opath = strdup(opath); + if (supervisor_ptr->opath == NULL) + { + log_error("strdup() failed for app supervisor opath"); + free(supervisor_ptr); + return false; + } + + supervisor_ptr->version = 0; + supervisor_ptr->next_id = 1; + + INIT_LIST_HEAD(&supervisor_ptr->applist); + + *supervisor_handle_ptr = (ladish_app_supervisor_handle)supervisor_ptr; + + return true; +} + +#define supervisor_ptr ((struct ladish_app_supervisor *)supervisor_handle) + +void ladish_app_supervisor_destroy(ladish_app_supervisor_handle supervisor_handle) +{ + struct ladish_app * app_ptr; + + while (!list_empty(&supervisor_ptr->applist)) + { + app_ptr = list_entry(supervisor_ptr->applist.next, struct ladish_app, siblings); + list_del(&app_ptr->siblings); + free(app_ptr->name); + free(app_ptr->commandline); + free(app_ptr); + } + + free(supervisor_ptr); +} + +#undef supervisor_ptr +#define supervisor_ptr ((struct ladish_app_supervisor *)call_ptr->iface_context) + +static void get_all(struct dbus_method_call * call_ptr) +{ + DBusMessageIter iter, array_iter, struct_iter; + struct list_head * node_ptr; + struct ladish_app * app_ptr; + + log_info("get_all called"); + + call_ptr->reply = dbus_message_new_method_return(call_ptr->message); + if (call_ptr->reply == NULL) + { + goto fail; + } + + dbus_message_iter_init_append(call_ptr->reply, &iter); + + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &supervisor_ptr->version)) + { + goto fail_unref; + } + + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ts)", &array_iter)) + { + goto fail_unref; + } + + list_for_each(node_ptr, &supervisor_ptr->applist) + { + app_ptr = list_entry(node_ptr, struct ladish_app, siblings); + + if (!dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter)) + { + goto fail_unref; + } + + if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT64, &app_ptr->id)) + { + goto fail_unref; + } + + log_info("app '%s' (%llu)", app_ptr->name, (unsigned long long)app_ptr->id); + if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &app_ptr->name)) + { + goto fail_unref; + } + + if (!dbus_message_iter_close_container(&array_iter, &struct_iter)) + { + goto fail_unref; + } + } + + if (!dbus_message_iter_close_container(&iter, &array_iter)) + { + goto fail_unref; + } + + return; + +fail_unref: + dbus_message_unref(call_ptr->reply); + call_ptr->reply = NULL; + +fail: + log_error("Ran out of memory trying to construct method return"); +} + +static void run_custom(struct dbus_method_call * call_ptr) +{ + dbus_bool_t terminal; + const char * commandline; + const char * argv[4]; + pid_t pid; + + if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_BOOLEAN, &terminal, DBUS_TYPE_STRING, &commandline, DBUS_TYPE_INVALID)) + { + lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message); + dbus_error_free(&g_dbus_error); + return; + } + + log_info("run_custom(%s,'%s') called", terminal ? "terminal" : "shell", commandline); + + if (terminal) + { + argv[0] = "xterm"; + argv[1] = "-e"; + } + else + { + argv[0] = "sh"; + argv[1] = "-c"; + } + + argv[2] = commandline; + argv[3] = NULL; + + if (!loader_execute(argv, "/", "xxx", false, &pid)) + { + lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "Execution of '%s' failed", commandline); + return; + } + + log_info("pid is %lu", (unsigned long)pid); + + method_return_new_void(call_ptr); +} + +#undef supervisor_ptr + +METHOD_ARGS_BEGIN(GetAll, "Get list of running apps") + METHOD_ARG_DESCRIBE_OUT("list_version", "t", "Version of the list") + METHOD_ARG_DESCRIBE_OUT("apps_list", "a(ts)", "List of running apps") +METHOD_ARGS_END + +METHOD_ARGS_BEGIN(RunCustom, "Start application by supplying commandline") + METHOD_ARG_DESCRIBE_IN("terminal", "b", "Whether to run in terminal") + METHOD_ARG_DESCRIBE_IN("commandline", "s", "Commandline") +METHOD_ARGS_END + +METHODS_BEGIN + METHOD_DESCRIBE(GetAll, get_all) + METHOD_DESCRIBE(RunCustom, run_custom) +METHODS_END + +SIGNALS_BEGIN +SIGNALS_END + +INTERFACE_BEGIN(g_iface_app_supervisor, IFACE_APP_SUPERVISOR) + INTERFACE_DEFAULT_HANDLER + INTERFACE_EXPOSE_METHODS + INTERFACE_EXPOSE_SIGNALS +INTERFACE_END diff --git a/daemon/app_supervisor.h b/daemon/app_supervisor.h new file mode 100644 index 00000000..90e26e6b --- /dev/null +++ b/daemon/app_supervisor.h @@ -0,0 +1,39 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2009 Nedko Arnaudov + * + ************************************************************************** + * This file contains interface to app supervisor object + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef APP_SUPERVISOR_H__712E6589_DCB1_4CE9_9812_4F250D55E8A2__INCLUDED +#define APP_SUPERVISOR_H__712E6589_DCB1_4CE9_9812_4F250D55E8A2__INCLUDED + +#include "common.h" + +typedef struct ladish_app_supervisor_tag { int unused; } * ladish_app_supervisor_handle; + +bool ladish_app_supervisor_create(ladish_app_supervisor_handle * supervisor_handle_ptr, const char * opath); +void ladish_app_supervisor_destroy(ladish_app_supervisor_handle supervisor_handle); + +extern const struct dbus_interface_descriptor g_iface_app_supervisor; + +#endif /* #ifndef APP_SUPERVISOR_H__712E6589_DCB1_4CE9_9812_4F250D55E8A2__INCLUDED */ diff --git a/daemon/loader.c b/daemon/loader.c index 9226d03d..d49c2507 100644 --- a/daemon/loader.c +++ b/daemon/loader.c @@ -26,6 +26,8 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define LADISH_DEBUG + #include "common.h" #include @@ -135,8 +137,8 @@ loader_childs_bury(void) list_del(&child_ptr->siblings); - free(&child_ptr->project_name); - free(&child_ptr->argv0); + free(child_ptr->project_name); + free(child_ptr->argv0); if (!child_ptr->terminal) { @@ -186,6 +188,7 @@ static void loader_sigchld_handler(int signum) void loader_init(void (* on_child_exit)(pid_t pid)) { + g_on_child_exit = on_child_exit; signal(SIGCHLD, loader_sigchld_handler); INIT_LIST_HEAD(&g_childs_list); } @@ -466,7 +469,7 @@ loader_execute( } child_ptr->argv0 = strdup(argv[0]); - if (child_ptr->project_name == NULL) + if (child_ptr->argv0 == NULL) { log_error("strdup() failed to duplicate argv[0] '%s'", argv[0]); goto free_project_name; diff --git a/daemon/studio.c b/daemon/studio.c index 0bf45b7e..e4862de7 100644 --- a/daemon/studio.c +++ b/daemon/studio.c @@ -79,6 +79,7 @@ studio_publish(void) &g_interface_studio, &g_studio, &g_interface_patchbay, ladish_graph_get_dbus_context(g_studio.studio_graph), &g_iface_graph_dict, g_studio.studio_graph, + &g_iface_app_supervisor, g_studio.app_supervisor, NULL); if (object == NULL) { @@ -280,6 +281,12 @@ bool studio_init(void) goto jack_graph_destroy; } + if (!ladish_app_supervisor_create(&g_studio.app_supervisor, STUDIO_OBJECT_PATH)) + { + log_error("ladish_app_supervisor_create() failed."); + goto studio_graph_destroy; + } + ladish_cqueue_init(&g_studio.cmd_queue); ladish_environment_init(&g_studio.env_store); @@ -290,11 +297,13 @@ bool studio_init(void) on_jack_server_disappeared)) { log_error("jack_proxy_init() failed."); - goto studio_graph_destroy; + goto app_supervisor_destroy; } return true; +app_supervisor_destroy: + ladish_app_supervisor_destroy(g_studio.app_supervisor); studio_graph_destroy: ladish_graph_destroy(g_studio.studio_graph, false); jack_graph_destroy: diff --git a/daemon/studio_internal.h b/daemon/studio_internal.h index 22399959..269569d6 100644 --- a/daemon/studio_internal.h +++ b/daemon/studio_internal.h @@ -31,6 +31,7 @@ #include "../jack_proxy.h" #include "../dbus/error.h" #include "virtualizer.h" +#include "app_supervisor.h" #include "cmd.h" #define JACK_CONF_MAX_ADDRESS_SIZE 1024 @@ -137,6 +138,7 @@ struct studio ladish_graph_handle jack_graph; ladish_graph_handle studio_graph; ladish_virtualizer_handle virtualizer; + ladish_app_supervisor_handle app_supervisor; }; struct jack_conf_parameter diff --git a/dbus_constants.h b/dbus_constants.h index fd7f96aa..7863f6d3 100644 --- a/dbus_constants.h +++ b/dbus_constants.h @@ -39,8 +39,7 @@ #define STUDIO_OBJECT_PATH DBUS_BASE_PATH "/Studio" #define IFACE_STUDIO DBUS_NAME_BASE ".Studio" #define IFACE_ROOM DBUS_NAME_BASE ".Room" -#define LAUNCHER_OBJECT_PATH DBUS_BASE_PATH "/Launcher" -#define IFACE_LAUNCHER DBUS_NAME_BASE ".Launcher" +#define IFACE_APP_SUPERVISOR DBUS_NAME_BASE ".AppSupervisor" #define APPLICATION_OBJECT_PATH DBUS_BASE_PATH "/Application" #define IFACE_APPLICATION DBUS_NAME_BASE ".App" #define IFACE_GRAPH_DICT DBUS_NAME_BASE ".GraphDict" diff --git a/wscript b/wscript index 932172d3..67f32082 100644 --- a/wscript +++ b/wscript @@ -218,6 +218,7 @@ def build(bld): 'cmd_unload_studio.c', 'cmd_exit.c', 'cqueue.c', + 'app_supervisor.c', ]: daemon.source.append(os.path.join("daemon", source))