2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/types.h>
27 #include "vconf-keys.h"
29 #include "ss_launch.h"
33 #define _S(str) ((str == NULL) ? "" : str)
35 int ss_set_current_lang(void)
39 lang = vconf_get_str(VCONFKEY_LANGSET);
42 ret = setenv("LANG", lang, 1);
50 static void prepare_exec(void)
57 maxfd = getdtablesize();
58 for (i = 3; i < maxfd; i++)
61 for (i = 0; i < _NSIG; i++)
64 /* RESET oomadj value */
65 sprintf(buf,"/proc/%d/oom_adj",getpid());
73 static int parse_cmd(const char *cmdline, char **argv, int max_args)
78 int escape = 0, squote = 0, dquote = 0;
80 if (cmdline == NULL || cmdline[0] == '\0')
83 bufp = buf = malloc(strlen(cmdline) + 1);
84 if (bufp == NULL || buf == NULL)
111 if (!squote && !dquote) {
113 if (nargs < max_args)
114 argv[nargs++] = strdup(buf);
128 if (nargs < max_args)
129 argv[nargs++] = strdup(buf);
132 argv[nargs++] = NULL;
138 int launch_app_with_nice(const char *file, char *const argv[], pid_t *pid, int _nice)
143 if (file == NULL || access(file, X_OK) != 0) {
144 PRT_TRACE_ERR("launch app error: Invalid input");
149 if (pid && (*pid > 0 && kill(*pid, 0) != -1))
155 PRT_TRACE_ERR("fork error: %s", strerror(errno));
160 if (_pid > 0) { /* parent */
171 if (ret == -1 && errno != 0)
172 PRT_TRACE_ERR("nice error: %s", strerror(errno));
174 ret = execvp(file, argv);
177 PRT_TRACE_ERR("exec. error: %s", strerror(errno));
181 int launch_app_cmd_with_nice(const char *cmdline, int _nice)
186 char *argv[MAX_ARGS + 1];
188 nargs = parse_cmd(cmdline, argv, MAX_ARGS + 1);
190 PRT_TRACE_ERR("launch app error: Invalid input");
195 ret = launch_app_with_nice(argv[0], argv, NULL, _nice);
197 for (i = 0; i < nargs; i++)
203 int launch_app_cmd(const char *cmdline)
205 return launch_app_cmd_with_nice(cmdline, 0);
208 int ss_launch_if_noexist(const char *execpath, const char *arg, ...)
217 if (execpath == NULL) {
221 if (pid = sysman_get_pid(execpath) > 0)
224 va_start(argptr, arg);
225 flag = va_arg(argptr, int);
227 if (flag & SS_LAUNCH_NICE)
228 nice_value = va_arg(argptr, int);
232 ss_set_current_lang();
235 buf_size = strlen(execpath) + strlen(arg) + 10;
236 buf = malloc(buf_size);
238 /* Do something for not enought memory error */
239 PRT_TRACE_ERR("Malloc failed");
243 snprintf(buf, buf_size, "%s %s", execpath, arg);
244 pid = launch_app_cmd_with_nice(buf, nice_value);
252 int ss_launch_evenif_exist(const char *execpath, const char *arg, ...)
262 if (execpath == NULL) {
267 va_start(argptr, arg);
268 flag = va_arg(argptr, int);
270 if (flag & SS_LAUNCH_NICE)
271 nice_value = va_arg(argptr, int);
275 ss_set_current_lang();
279 buf_size = strlen(execpath) + strlen(arg) + 10;
280 buf = malloc(buf_size);
282 /* Do something for not enought memory error */
283 PRT_TRACE_ERR("Malloc failed");
287 snprintf(buf, buf_size, "%s %s", execpath, arg);
288 pid = launch_app_cmd_with_nice(buf, nice_value);
296 int ss_launch_after_kill_if_exist(const char *execpath, const char *arg, ...)
306 if (execpath == NULL) {
311 if ((exist_pid = sysman_get_pid(execpath)) > 0)
312 kill(exist_pid, SIGTERM);
314 va_start(argptr, arg);
315 flag = va_arg(argptr, int);
317 if (flag & SS_LAUNCH_NICE)
318 nice_value = va_arg(argptr, int);
322 ss_set_current_lang();
326 buf_size = strlen(execpath) + strlen(arg) + 10;
327 buf = malloc(buf_size);
329 /* Do something for not enought memory error */
330 PRT_TRACE_ERR("Malloc Failed");
334 snprintf(buf, buf_size, "%s %s", execpath, arg);
335 pid = launch_app_cmd_with_nice(buf, nice_value);