fix compiler warning
[ladish.git] / daemon / cmd_new_app.c
blobc8a2be4f1aabcf02d53d4946af6181fc94fbdc39
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"
30 #include "../dbus/error.h"
32 struct ladish_command_new_app
34 struct ladish_command command; /* must be the first member */
35 char * opath;
36 bool terminal;
37 char * commandline;
38 char * name;
39 char * level;
42 #define cmd_ptr ((struct ladish_command_new_app *)context)
44 static bool run(void * context)
46 char * name;
47 char * name_buffer;
48 size_t len;
49 char * end;
50 unsigned int index;
51 ladish_app_supervisor_handle supervisor;
52 ladish_app_handle app;
54 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
56 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);
58 supervisor = ladish_studio_find_app_supervisor(cmd_ptr->opath);
59 if (supervisor == NULL)
61 log_error("cannot find supervisor '%s' to run app '%s'", cmd_ptr->opath, cmd_ptr->name);
62 return false;
65 if (*cmd_ptr->name)
67 /* allocate and copy app name */
68 len = strlen(cmd_ptr->name);
69 name_buffer = malloc(len + 100);
70 if (name_buffer == NULL)
72 log_error("malloc of app name failed");
73 return false;
76 name = name_buffer;
78 strcpy(name, cmd_ptr->name);
80 end = name + len;
82 else
84 /* allocate app name */
85 len = strlen(cmd_ptr->commandline) + 100;
86 name_buffer = malloc(len);
87 if (name_buffer == NULL)
89 log_error("malloc of app name failed");
90 return false;
93 strcpy(name_buffer, cmd_ptr->commandline);
95 /* use first word as name */
96 end = name_buffer;
97 while (*end)
99 if (isspace(*end))
101 *end = 0;
102 break;
105 end++;
108 name = strrchr(name_buffer, '/');
109 if (name == NULL)
111 name = name_buffer;
113 else
115 name++;
119 /* make the app name unique */
120 index = 2;
121 while (ladish_app_supervisor_find_app_by_name(supervisor, name) != NULL)
123 sprintf(end, "-%u", index);
124 index++;
127 app = ladish_app_supervisor_add(supervisor, name, NULL, true, cmd_ptr->commandline, cmd_ptr->terminal, cmd_ptr->level);
129 free(name_buffer);
131 if (app == NULL)
133 log_error("ladish_app_supervisor_add() failed");
134 return false;
137 if (ladish_studio_is_started())
139 if (!ladish_app_supervisor_start_app(supervisor, app))
141 log_error("Execution of '%s' failed", cmd_ptr->commandline);
142 ladish_app_supervisor_remove_app(supervisor, app);
143 return false;
147 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
148 return true;
151 static void destructor(void * context)
153 log_info("new_app command destructor");
154 free(cmd_ptr->opath);
155 free(cmd_ptr->commandline);
156 free(cmd_ptr->name);
157 free(cmd_ptr->level);
160 #undef cmd_ptr
162 bool
163 ladish_command_new_app(
164 void * call_ptr,
165 struct ladish_cqueue * queue_ptr,
166 const char * opath,
167 bool terminal,
168 const char * commandline,
169 const char * name,
170 const char * level)
172 struct ladish_command_new_app * cmd_ptr;
173 char * opath_dup;
174 char * commandline_dup;
175 char * name_dup;
176 char * level_dup;
178 opath_dup = strdup(opath);
179 if (opath_dup == NULL)
181 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", opath);
182 goto fail;
185 commandline_dup = strdup(commandline);
186 if (commandline_dup == NULL)
188 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", commandline);
189 goto fail_free_opath;
192 name_dup = strdup(name);
193 if (name_dup == NULL)
195 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", name);
196 goto fail_free_commandline;
199 level_dup = strdup(level);
200 if (level_dup == NULL)
202 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", level);
203 goto fail_free_name;
206 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_new_app));
207 if (cmd_ptr == NULL)
209 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
210 goto fail_free_level;
213 cmd_ptr->command.run = run;
214 cmd_ptr->command.destructor = destructor;
215 cmd_ptr->opath = opath_dup;
216 cmd_ptr->commandline = commandline_dup;
217 cmd_ptr->name = name_dup;
218 cmd_ptr->terminal = terminal;
219 cmd_ptr->level = level_dup;
221 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
223 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
224 goto fail_destroy_command;
227 return true;
229 fail_destroy_command:
230 free(cmd_ptr);
231 fail_free_level:
232 free(level_dup);
233 fail_free_name:
234 free(name_dup);
235 fail_free_commandline:
236 free(commandline_dup);
237 fail_free_opath:
238 free(opath_dup);
239 fail:
240 return false;