Make amd_session_agent run as sokcet activation only
[platform/core/appfw/aul-1.git] / app_launcher.c
1 /*
2  *  app_launcher
3  *
4  * Copyright (c) 2014, Intel Corporation.
5  *
6  * Contact: Baptiste DURAND <baptiste.durand@open.eurogiciel.org>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <ail.h>
30 #include "aul.h"
31
32
33 static char **gargv;
34 static int gargc;
35 bundle *kb = NULL;
36
37 static GMainLoop *mainloop = NULL;
38
39
40 struct launch_arg {
41     char applicationId[256];
42     int flag_debug;
43 } launch_arg;
44
45 static bundle *create_internal_bundle()
46 {
47     bundle *kb;
48     int i;
49     char arg[1024] = {0, };
50     char* val_array[128];
51
52     kb = bundle_create();
53     bundle_add(kb, AUL_K_DEBUG, "1");
54     return kb;
55 }
56
57 int launch(char* appid,int debug_option)
58 {
59     int pid = -1;
60
61
62     if(!debug_option)
63         pid = aul_open_app(appid);
64     else {
65         kb = create_internal_bundle();
66         if (NULL == kb) {
67             printf("bundle creation fail\n");
68             return -1;
69         }
70         pid = aul_launch_app(appid, kb);
71     }
72     return pid;
73 }
74
75
76
77 static int __launch_app_dead_handler(int pid, void *data)
78 {
79     int listen_pid = (int) data;
80
81     if(listen_pid == pid)
82         g_main_loop_quit(mainloop);
83
84     return 0;
85 }
86
87 static gboolean run_func(void *data)
88 {
89     int pid = -1;
90     char *str = NULL;
91     struct launch_arg* launch_arg_data = NULL;
92     launch_arg_data = (struct launch_arg*)data;
93     if ((pid = launch((char*)launch_arg_data->applicationId,launch_arg_data->flag_debug)) > 0) {
94         printf("... successfully launched whit debug %d\n",launch_arg_data->flag_debug);
95     } else {
96         printf("... launch failed\n");
97     }
98     g_main_loop_quit(mainloop);
99     if (kb) {
100         bundle_free(kb);
101         kb = NULL;
102     }
103
104     return TRUE;
105 }
106
107
108 void print_usage(char *program)
109 {
110     printf("Usage : %s [ ... ]\n", program);
111     printf(
112         "   -h                        --help              Display this usage information.\n"
113         "   -l                        --list              Display installed apps list\n"
114         "   -S                        --status            Display running apps list\n"
115         "   -s [tizen application ID] --start             Launch widget with tizen application ID\n"
116         "   -k [tizen application ID] --kill              Kill widget with tizen application ID\n"
117         "   -r [tizen application ID] --is-running        Check whether application is running by tizen application ID,\n"
118         "                                                 If widget is running, 0(zero) will be returned.\n"
119         "   -d                        --debug             Activate debug mode\n"
120         );
121 }
122
123
124 ail_cb_ret_e appinfo_list_appid_namefunc(const ail_appinfo_h appinfo,  void *user_data)
125 {
126     char* package_str_name = NULL;
127     char* package_str_appid = NULL;
128     char* package_str_x_package_type = NULL;
129     ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_APPID_STR, &package_str_appid);
130     ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &package_str_name);
131     ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_PACKAGETYPE_STR, &package_str_x_package_type);
132
133     printf("\t'%s'\t '%s'\t %s\n",package_str_name, package_str_appid, package_str_x_package_type);
134     return AIL_CB_RET_CONTINUE;
135 }
136
137
138 int listApp()
139 {
140     int ret = 0;
141     printf("\tApplication List for user %lu\n", (long)getuid());
142     printf("\tUser's Application \n");
143     printf("\t Name \t AppID  \t Type \n");
144     printf("\t=================================================\n");
145     if ( ail_filter_list_usr_appinfo_foreach(NULL, appinfo_list_appid_namefunc, NULL, getuid() ) != AIL_ERROR_OK )
146         ret = -1;
147     printf("\t=================================================\n");
148     return ret;
149 }
150
151
152
153 int iterfunc_status(const aul_app_info *info, void *data)
154 {
155     printf("\t  %s (%d)\n",info->appid, info->pid);
156     return 0;
157 }
158
159
160
161 int iterfunc_kill(const aul_app_info *info, void *data)
162 {
163     if(!data)
164         return 0;
165     if(strcmp(info->appid,data) == 0) {
166         aul_kill_pid(info->pid);
167     printf("\t Kill pkg_name: %s (%d)\n", info->appid,info->pid);
168     }
169     return 0;
170 }
171
172 int IsAppInstalled(char *appid)
173 {
174     ail_filter_h f;
175     int res = 0;
176     if(!appid)
177         return 0;
178     if (ail_filter_new(&f) != AIL_ERROR_OK)
179         return -1;
180     if (ail_filter_add_str(f, AIL_PROP_X_SLP_APPID_STR, appid) != AIL_ERROR_OK) {
181         ail_filter_destroy(f);
182         return -1;
183     }
184     if (ail_filter_count_usr_appinfo(f, &res, getuid()) != AIL_ERROR_OK) {
185         ail_filter_destroy(f);
186         return -1;
187     }
188     return res;
189 }
190
191 int main(int argc, char **argv)
192 {
193     bool isDebugMode = false;
194     bool dispHelp = false;
195     bool dispList = false;
196     bool dispRunList = false;
197     int next_opt, opt_idx = 0;
198     char op = '\0';
199     int ret = 0;
200     struct launch_arg  args;
201     static struct option long_options[] = {
202             { "help", no_argument, 0, 'h' },
203             { "list", no_argument, 0, 'l' },
204             { "status", no_argument, 0, 'S' },
205             { "start", required_argument, 0, 's' },
206             { "kill", required_argument, 0, 'k' },
207             { "is-running", required_argument, 0, 'r' },
208             { "debug", no_argument, 0, 'd' },
209             { 0, 0, 0, 0 }
210     };
211     memset(&args,0,sizeof(struct launch_arg));
212
213     do {
214             next_opt = getopt_long(argc,
215                                    argv,
216                                    "hlSs:k:r:d",
217                                    long_options,
218                                    &opt_idx);
219
220             switch (next_opt) {
221             case 'h':
222                 if (!dispHelp) {
223                     print_usage(argv[0]);
224                     dispHelp = true;
225                 }
226                 break;
227
228             case 'l':
229                 if (dispList) {
230                     break;
231                 }
232                 if (listApp()) {
233                     printf("Fail to display the list of installed applications");
234                     return -1;
235                 }
236                 dispList = true;
237                 break;
238
239             case 'S':
240                 if (dispRunList) {
241                     break;
242                 }
243                 printf("\t pkg_name (PID)\n");
244                 if (aul_app_get_running_app_info(iterfunc_status, NULL)) {
245                     printf("Fail to display the list of Running applications");
246                     return -1;
247                 }
248                 dispRunList = true;
249                 break;
250
251             case 's':
252             case 'k':
253             case 'r':
254                 if(strlen(optarg) > 255) {
255                     print_usage(argv[0]);
256                     return -1;
257                 } else
258                     strcpy(args.applicationId, optarg);
259                 op = next_opt;
260                 break;
261
262             case 'd':
263                 args.flag_debug = 1;
264                 break;
265
266             case -1:
267                 break;
268
269             default:
270                print_usage(argv[0]);
271                break;
272             }
273         } while (next_opt != -1);
274
275         if (argc == 1)
276             print_usage(argv[0]);
277
278         if (optind < argc) {
279             printf("Wrong option: ");
280             while (optind < argc)
281                printf("%s ", argv[optind++]);
282             printf("\n");
283             print_usage(argv[0]);
284         }
285         if ((op == 's') || (op == 'k') || (op == 'r')) {
286           if (IsAppInstalled(args.applicationId) <= 0) {
287                 printf("The app with ID: %s is not avaible for the user %d \n", args.applicationId, getuid());
288                 return -1;
289           }
290         }
291
292         if (op == 's') {
293             if (strlen(args.applicationId) <= 0) {
294                 printf("result: %s\n", "failed");
295                 return -1;
296             }
297             aul_launch_init(NULL, NULL);
298             g_idle_add(run_func, args.applicationId);
299             mainloop = g_main_loop_new(NULL, FALSE);
300             if (!mainloop) {
301                 printf("failed to create glib main loop\n");
302                 exit(EXIT_FAILURE);
303             }
304             g_main_loop_run(mainloop);
305             return 0;
306         } else if (op == 'k') {
307             bool isRunning = false;
308             isRunning = aul_app_is_running(args.applicationId);
309             if (true == isRunning) {
310                 aul_app_get_running_app_info(iterfunc_kill, args.applicationId);
311             } else {
312                 printf("result: %s\n", "App isn't running");
313                 return 0;
314             }
315         } else if (op == 'r') {
316             bool isRunning = aul_app_is_running(args.applicationId);
317             if (true == isRunning) {
318                 printf("result: %s\n", "running");
319                 return 0;
320             } else {
321                 printf("result: %s\n", "not running");
322                 return -1;
323             }
324         }
325     return 0;
326 }
327