Update gitignore (sometimes compiled *.po files may appear not in build dir)
[ladish.git] / daemon / cmd_new_app.c
blobf8506ccb91e1ec5aa39c43fa1e1ff12d1b723510
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 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 uint8_t 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=%"PRIu8") 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, 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);
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 uint8_t level)
171 struct ladish_command_new_app * cmd_ptr;
172 char * opath_dup;
173 char * commandline_dup;
174 char * name_dup;
176 opath_dup = strdup(opath);
177 if (opath_dup == NULL)
179 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", opath);
180 goto fail;
183 commandline_dup = strdup(commandline);
184 if (commandline_dup == NULL)
186 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", commandline);
187 goto fail_free_opath;
190 name_dup = strdup(name);
191 if (name_dup == NULL)
193 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", name);
194 goto fail_free_commandline;
197 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_new_app));
198 if (cmd_ptr == NULL)
200 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
201 goto fail_free_name;
204 cmd_ptr->command.run = run;
205 cmd_ptr->command.destructor = destructor;
206 cmd_ptr->opath = opath_dup;
207 cmd_ptr->commandline = commandline_dup;
208 cmd_ptr->name = name_dup;
209 cmd_ptr->terminal = terminal;
210 cmd_ptr->level = level;
212 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
214 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
215 goto fail_destroy_command;
218 return true;
220 fail_destroy_command:
221 free(cmd_ptr);
222 fail_free_name:
223 free(name_dup);
224 fail_free_commandline:
225 free(commandline_dup);
226 fail_free_opath:
227 free(opath_dup);
228 fail:
229 return false;