1 /* -*- Mode: C ; c-basic-offset: 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.
30 #include "../dbus/error.h"
32 struct ladish_command_new_app
34 struct ladish_command command
; /* must be the first member */
42 #define cmd_ptr ((struct ladish_command_new_app *)context)
44 static bool run(void * context
)
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
);
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");
78 strcpy(name
, cmd_ptr
->name
);
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");
93 strcpy(name_buffer
, cmd_ptr
->commandline
);
95 /* use first word as name */
108 name
= strrchr(name_buffer
, '/');
119 /* make the app name unique */
121 while (ladish_app_supervisor_find_app_by_name(supervisor
, name
) != NULL
)
123 sprintf(end
, "-%u", index
);
127 app
= ladish_app_supervisor_add(supervisor
, name
, true, cmd_ptr
->commandline
, cmd_ptr
->terminal
, cmd_ptr
->level
);
133 log_error("ladish_app_supervisor_add() failed");
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
);
147 cmd_ptr
->command
.state
= LADISH_COMMAND_STATE_DONE
;
151 static void destructor(void * context
)
153 log_info("new_app command destructor");
154 free(cmd_ptr
->opath
);
155 free(cmd_ptr
->commandline
);
162 ladish_command_new_app(
164 struct ladish_cqueue
* queue_ptr
,
167 const char * commandline
,
171 struct ladish_command_new_app
* cmd_ptr
;
173 char * commandline_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
);
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
));
200 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "ladish_command_new() failed.");
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
;
220 fail_destroy_command
:
224 fail_free_commandline
:
225 free(commandline_dup
);