Merge branch 'stable' into 'main'
[ladish.git] / daemon / cmd_change_app_state.c
blobf895b2a81b74588bf22456052d1971416145b0d1
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010,2011,2012 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the "change app state" command
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <ctype.h>
28 #include "cmd.h"
29 #include "studio.h"
30 #include "../proxies/notify_proxy.h"
31 #include "virtualizer.h"
33 struct ladish_command_change_app_state
35 struct ladish_command command; /* must be the first member */
36 char * opath;
37 uint64_t id;
38 unsigned int target_state;
39 const char * target_state_description;
40 bool (* run_target)(struct ladish_command_change_app_state *, ladish_app_supervisor_handle, ladish_app_handle);
41 void (* initiate_stop)(ladish_app_handle app);
44 static bool run_target_start(struct ladish_command_change_app_state * cmd_ptr, ladish_app_supervisor_handle supervisor, ladish_app_handle app)
46 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
47 ASSERT(cmd_ptr->initiate_stop == NULL);
49 if (!ladish_studio_is_started())
51 log_error("cannot start app because studio is not started");
52 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app because studio is not started", NULL);
53 return false;
56 if (ladish_app_is_running(app))
58 log_error("App %s is already running", ladish_app_get_name(app));
59 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app because it is already running", NULL);
60 return false;
63 if (!ladish_app_supervisor_start_app(supervisor, app))
65 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app (execution failed)", NULL);
66 return false;
69 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
70 return true;
73 static
74 bool
75 run_target_stop(
76 struct ladish_command_change_app_state * cmd_ptr,
77 ladish_app_supervisor_handle UNUSED(supervisor),
78 ladish_app_handle app)
80 const char * app_name;
81 uuid_t app_uuid;
83 ASSERT(cmd_ptr->initiate_stop != NULL);
85 app_name = ladish_app_get_name(app);
86 ladish_app_get_uuid(app, app_uuid);
88 if (ladish_app_is_running(app))
90 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
92 cmd_ptr->initiate_stop(app);
93 cmd_ptr->command.state = LADISH_COMMAND_STATE_WAITING;
94 return true;
97 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_WAITING);
98 log_info("Waiting '%s' process termination (%s)...", app_name, cmd_ptr->target_state_description);
99 return true;
102 if (!ladish_virtualizer_is_hidden_app(ladish_studio_get_jack_graph(), app_uuid, app_name))
104 log_info("Waiting '%s' client disappear (%s)...", app_name, cmd_ptr->target_state_description);
105 return true;
108 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
110 log_info("App %s is already stopped (%s)", app_name, cmd_ptr->target_state_description);
113 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
114 return true;
117 #define cmd_ptr ((struct ladish_command_change_app_state *)context)
119 static bool run(void * context)
121 ladish_app_supervisor_handle supervisor;
122 ladish_app_handle app;
124 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
126 log_info("%s app command. opath='%s'", cmd_ptr->target_state_description, cmd_ptr->opath);
129 supervisor = ladish_studio_find_app_supervisor(cmd_ptr->opath);
130 if (supervisor == NULL)
132 log_error("cannot find supervisor '%s' to %s app", cmd_ptr->opath, cmd_ptr->target_state_description);
133 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot change app state because of internal error (unknown supervisor)", NULL);
134 return false;
137 app = ladish_app_supervisor_find_app_by_id(supervisor, cmd_ptr->id);
138 if (app == NULL)
140 log_error("App with ID %"PRIu64" not found (%s)", cmd_ptr->id, cmd_ptr->target_state_description);
141 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot change app state because it is not found", NULL);
142 return false;
145 return cmd_ptr->run_target(cmd_ptr, supervisor, app);
148 static void destructor(void * context)
150 log_info("change_app_state command destructor");
151 free(cmd_ptr->opath);
154 #undef cmd_ptr
156 bool ladish_command_change_app_state(void * call_ptr, struct ladish_cqueue * queue_ptr, const char * opath, uint64_t id, unsigned int target_state)
158 struct ladish_command_change_app_state * cmd_ptr;
159 char * opath_dup;
161 opath_dup = strdup(opath);
162 if (opath_dup == NULL)
164 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "strdup('%s') failed.", opath);
165 goto fail;
168 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_change_app_state));
169 if (cmd_ptr == NULL)
171 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "ladish_command_new() failed.");
172 goto fail_free_opath;
175 cmd_ptr->command.run = run;
176 cmd_ptr->command.destructor = destructor;
177 cmd_ptr->opath = opath_dup;
178 cmd_ptr->id = id;
179 cmd_ptr->target_state = target_state;
181 switch (target_state)
183 case LADISH_APP_STATE_STARTED:
184 cmd_ptr->target_state_description = "start";
185 cmd_ptr->run_target = run_target_start;
186 cmd_ptr->initiate_stop = NULL;
187 break;
188 case LADISH_APP_STATE_STOPPED:
189 cmd_ptr->target_state_description = "stop";
190 cmd_ptr->run_target = run_target_stop;
191 cmd_ptr->initiate_stop = ladish_app_stop;
192 break;
193 case LADISH_APP_STATE_KILL:
194 cmd_ptr->target_state_description = "kill";
195 cmd_ptr->run_target = run_target_stop;
196 cmd_ptr->initiate_stop = ladish_app_kill;
197 break;
198 default:
199 ASSERT_NO_PASS;
200 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "Invalid target state (internal error).", opath);
201 goto fail_destroy_command;
204 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
206 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "ladish_cqueue_add_command() failed.");
207 goto fail_destroy_command;
210 return true;
212 fail_destroy_command:
213 free(cmd_ptr);
214 fail_free_opath:
215 free(opath_dup);
216 fail:
217 return false;