Merge branch 'stable' into 'main'
[ladish.git] / daemon / cmd_new_app.c
blob580f2834b59bfda69a62a1f216f482baad45dde5
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010, 2011 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the "new app" 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"
31 struct ladish_command_new_app
33 struct ladish_command command; /* must be the first member */
34 char * opath;
35 bool terminal;
36 char * commandline;
37 char * name;
38 char * level;
41 #define cmd_ptr ((struct ladish_command_new_app *)context)
43 static bool run(void * context)
45 char * name;
46 char * name_buffer;
47 size_t len;
48 char * end;
49 unsigned int index;
50 ladish_app_supervisor_handle supervisor;
51 ladish_app_handle app;
53 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
55 log_info("new_app command. opath='%s', name='%s', %s, commandline='%s', level='%s') called", cmd_ptr->opath, cmd_ptr->name, cmd_ptr->terminal ? "terminal" : "shell", cmd_ptr->commandline, cmd_ptr->level);
57 supervisor = ladish_studio_find_app_supervisor(cmd_ptr->opath);
58 if (supervisor == NULL)
60 log_error("cannot find supervisor '%s' to run app '%s'", cmd_ptr->opath, cmd_ptr->name);
61 return false;
64 if (*cmd_ptr->name)
66 /* allocate and copy app name */
67 len = strlen(cmd_ptr->name);
68 name_buffer = malloc(len + 100);
69 if (name_buffer == NULL)
71 log_error("malloc of app name failed");
72 return false;
75 name = name_buffer;
77 strcpy(name, cmd_ptr->name);
79 end = name + len;
81 else
83 /* allocate app name */
84 len = strlen(cmd_ptr->commandline) + 100;
85 name_buffer = malloc(len);
86 if (name_buffer == NULL)
88 log_error("malloc of app name failed");
89 return false;
92 strcpy(name_buffer, cmd_ptr->commandline);
94 /* use first word as name */
95 end = name_buffer;
96 while (*end)
98 if (isspace(*end))
100 *end = 0;
101 break;
104 end++;
107 name = strrchr(name_buffer, '/');
108 if (name == NULL)
110 name = name_buffer;
112 else
114 name++;
118 /* make the app name unique */
119 index = 2;
120 while (ladish_app_supervisor_find_app_by_name(supervisor, name) != NULL)
122 sprintf(end, "-%u", index);
123 index++;
126 app = ladish_app_supervisor_add(supervisor, name, NULL, true, cmd_ptr->commandline, cmd_ptr->terminal, cmd_ptr->level);
128 free(name_buffer);
130 if (app == NULL)
132 log_error("ladish_app_supervisor_add() failed");
133 return false;
136 if (ladish_studio_is_started())
138 if (!ladish_app_supervisor_start_app(supervisor, app))
140 log_error("Execution of '%s' failed", cmd_ptr->commandline);
141 ladish_app_supervisor_remove_app(supervisor, app);
142 return false;
146 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
147 return true;
150 static void destructor(void * context)
152 log_info("new_app command destructor");
153 free(cmd_ptr->opath);
154 free(cmd_ptr->commandline);
155 free(cmd_ptr->name);
156 free(cmd_ptr->level);
159 #undef cmd_ptr
161 bool
162 ladish_command_new_app(
163 void * call_ptr,
164 struct ladish_cqueue * queue_ptr,
165 const char * opath,
166 bool terminal,
167 const char * commandline,
168 const char * name,
169 const char * level)
171 struct ladish_command_new_app * cmd_ptr;
172 char * opath_dup;
173 char * commandline_dup;
174 char * name_dup;
175 char * level_dup;
177 opath_dup = strdup(opath);
178 if (opath_dup == NULL)
180 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "strdup('%s') failed.", opath);
181 goto fail;
184 commandline_dup = strdup(commandline);
185 if (commandline_dup == NULL)
187 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "strdup('%s') failed.", commandline);
188 goto fail_free_opath;
191 name_dup = strdup(name);
192 if (name_dup == NULL)
194 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "strdup('%s') failed.", name);
195 goto fail_free_commandline;
198 level_dup = strdup(level);
199 if (level_dup == NULL)
201 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "strdup('%s') failed.", level);
202 goto fail_free_name;
205 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_new_app));
206 if (cmd_ptr == NULL)
208 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "ladish_command_new() failed.");
209 goto fail_free_level;
212 cmd_ptr->command.run = run;
213 cmd_ptr->command.destructor = destructor;
214 cmd_ptr->opath = opath_dup;
215 cmd_ptr->commandline = commandline_dup;
216 cmd_ptr->name = name_dup;
217 cmd_ptr->terminal = terminal;
218 cmd_ptr->level = level_dup;
220 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
222 cdbus_error(call_ptr, DBUS_ERROR_FAILED, "ladish_cqueue_add_command() failed.");
223 goto fail_destroy_command;
226 return true;
228 fail_destroy_command:
229 free(cmd_ptr);
230 fail_free_level:
231 free(level_dup);
232 fail_free_name:
233 free(name_dup);
234 fail_free_commandline:
235 free(commandline_dup);
236 fail_free_opath:
237 free(opath_dup);
238 fail:
239 return false;