Remove warnings, remove unused test code
[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
30 #include <pkgmgr-info.h>
31
32 #include "aul.h"
33
34 static GMainLoop *mainloop = NULL;
35
36 struct launch_arg {
37         char appid[256];
38         char **argv;
39         int argc;
40         int flag_debug;
41         int sync;
42 } launch_arg;
43
44 static bundle *_create_internal_bundle(struct launch_arg *args)
45 {
46         bundle *b;
47         int i;
48
49         if (!args->flag_debug && args->argc < 2)
50                 return NULL;
51
52         b = bundle_create();
53         if (args->flag_debug)
54                 bundle_add(b, AUL_K_DEBUG, "1");
55
56         for (i = 0; i + 1 < args->argc; i += 2) {
57                 bundle_add(b, args->argv[i], args->argv[i + 1]);
58                 if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
59                                 !strcmp(args->argv[i + 1], "SYNC"))
60                         args->sync = 1;
61         }
62
63         return b;
64 }
65
66 static int __launch_app_dead_handler(int pid, void *data)
67 {
68         int listen_pid = (int)data;
69
70         if (listen_pid == pid)
71                 g_main_loop_quit(mainloop);
72
73         return 0;
74 }
75
76 static gboolean run_func(void *data)
77 {
78         int pid;
79         bundle *kb;
80         struct launch_arg *launch_arg_data = (struct launch_arg *)data;
81
82         kb = _create_internal_bundle(launch_arg_data);
83         pid = aul_launch_app((char *)launch_arg_data->appid, kb);
84
85         if (kb) {
86                 bundle_free(kb);
87                 kb = NULL;
88         }
89
90         if (pid > 0) {
91                 printf("... successfully launched pid = %d with debug %d\n",
92                                 pid, launch_arg_data->flag_debug);
93                 if (launch_arg_data->sync) {
94                         aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)pid);
95                         return FALSE;
96                 }
97         } else {
98                 printf("... launch failed\n");
99         }
100
101         g_main_loop_quit(mainloop);
102
103         return FALSE;
104 }
105
106 static void print_usage(char *program)
107 {
108         printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
109         printf(
110                         "   -h                        --help              Display this usage information.\n"
111                         "   -l                        --list              Display installed apps list\n"
112                         "   -S                        --status            Display running apps list\n"
113                         "   -s [tizen application ID] --start             Launch widget with tizen application ID\n"
114                         "   -k [tizen application ID] --kill              Kill widget with tizen application ID\n"
115                         "   -r [tizen application ID] --is-running        Check whether application is running by tizen application ID,\n"
116                         "                                                 If widget is running, 0(zero) will be returned.\n"
117                         "   -d                        --debug             Activate debug mode\n"
118               );
119 }
120
121 static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
122 {
123         char *label;
124         char *appid;
125
126         if (pkgmgrinfo_appinfo_get_label(handle, &label)) {
127                 printf("Failed to get app label\n");
128                 return -1;
129         }
130
131         if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
132                 printf("Failed to get appid\n");
133                 return -1;
134         }
135
136         printf("\t'%s'\t '%s'\n", label, appid);
137
138         return 0;
139 }
140
141 static int list_app(void)
142 {
143         int ret = 0;
144
145         printf("\tApplication List for user %lu\n", (long)getuid());
146         printf("\tUser's Application \n");
147         printf("\t Name \t AppID \n");
148         printf("\t=================================================\n");
149         if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
150                                 getuid(), NULL) != PMINFO_R_OK)
151                 ret = -1;
152         printf("\t=================================================\n");
153         return ret;
154 }
155
156 static int __iterfunc_status(const aul_app_info *info, void *data)
157 {
158         printf("\t  %s (%d)\n", info->appid, info->pid);
159         return 0;
160 }
161
162 static int __iterfunc_kill(const aul_app_info *info, void *data)
163 {
164         if (!data)
165                 return 0;
166         if (strcmp(info->appid, data) == 0) {
167                 aul_kill_pid(info->pid);
168                 printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
169         }
170         return 0;
171 }
172
173 static int is_app_installed(char *appid)
174 {
175         int is_installed = 0;
176         pkgmgrinfo_appinfo_filter_h filter;
177
178         if (pkgmgrinfo_appinfo_filter_create(&filter)) {
179                 printf("Failed to create filter\n");
180                 return -1;
181         }
182
183         if (pkgmgrinfo_appinfo_filter_add_string(filter,
184                                 PMINFO_APPINFO_PROP_APP_ID, appid)) {
185                 printf("Failed to add filter string\n");
186                 pkgmgrinfo_appinfo_filter_destroy(filter);
187                 return -1;
188         }
189
190         if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
191                                 getuid())) {
192                 printf("Failed to get filter count\n");
193                 pkgmgrinfo_appinfo_filter_destroy(filter);
194                 return -1;
195         }
196
197         pkgmgrinfo_appinfo_filter_destroy(filter);
198
199         return is_installed;
200 }
201
202 int main(int argc, char **argv)
203 {
204         bool disp_help = false;
205         bool disp_list = false;
206         bool disp_run_list = false;
207         bool is_running;
208         int next_opt;
209         int opt_idx = 0;
210         char op = '\0';
211         struct launch_arg args;
212         static struct option long_options[] = {
213                 { "help", no_argument, 0, 'h' },
214                 { "list", no_argument, 0, 'l' },
215                 { "status", no_argument, 0, 'S' },
216                 { "start", required_argument, 0, 's' },
217                 { "args", required_argument, 0, 'a' },
218                 { "kill", required_argument, 0, 'k' },
219                 { "is-running", required_argument, 0, 'r' },
220                 { "debug", no_argument, 0, 'd' },
221                 { 0, 0, 0, 0 }
222         };
223         memset(&args, 0, sizeof(struct launch_arg));
224
225         do {
226                 next_opt = getopt_long(argc,
227                                 argv,
228                                 "hlSs:k:r:d",
229                                 long_options,
230                                 &opt_idx);
231
232                 switch (next_opt) {
233                 case 'h':
234                         if (!disp_help) {
235                                 print_usage(argv[0]);
236                                 disp_help = true;
237                         }
238                         break;
239                 case 'l':
240                         if (disp_list)
241                                 break;
242                         if (list_app()) {
243                                 printf("Fail to display the list of "
244                                                 "installed applications\n");
245                                 return -1;
246                         }
247                         disp_list = true;
248                         break;
249                 case 'S':
250                         if (disp_run_list)
251                                 break;
252                         printf("\t appId (PID)\n");
253                         if (aul_app_get_running_app_info(__iterfunc_status,
254                                                 NULL)) {
255                                 printf("Fail to display the list of "
256                                                 "Running applications\n");
257                                 return -1;
258                         }
259                         disp_run_list = true;
260                         break;
261                 case 's':
262                 case 'k':
263                 case 'r':
264                         if (strlen(optarg) > 255) {
265                                 print_usage(argv[0]);
266                                 return -1;
267                         } else {
268                                 strcpy(args.appid, optarg);
269                         }
270                         op = next_opt;
271                         break;
272                 case 'd':
273                         args.flag_debug = 1;
274                         break;
275                 case '?':
276                         break;
277                 case -1:
278                         break;
279                 default:
280                         print_usage(argv[0]);
281                         break;
282                 }
283         } while (next_opt != -1);
284
285         if (argc == 1)
286                 print_usage(argv[0]);
287
288         if (optind < argc) {
289                 args.argc = argc - optind;
290                 args.argv = &argv[optind];
291         }
292         if ((op == 's') || (op == 'k') || (op == 'r')) {
293                 if (is_app_installed(args.appid) <= 0) {
294                         printf("The app with ID: %s is not avaible "
295                                         "for the user %d \n",
296                                         args.appid, getuid());
297                         return -1;
298                 }
299         }
300
301         if (op == 's') {
302                 if (strlen(args.appid) <= 0) {
303                         printf("result: %s\n", "failed");
304                         return -1;
305                 }
306                 aul_launch_init(NULL, NULL);
307                 g_idle_add(run_func, args.appid);
308                 mainloop = g_main_loop_new(NULL, FALSE);
309                 if (!mainloop) {
310                         printf("failed to create glib main loop\n");
311                         exit(EXIT_FAILURE);
312                 }
313                 g_main_loop_run(mainloop);
314                 return 0;
315         } else if (op == 'k') {
316                 is_running = aul_app_is_running(args.appid);
317                 if (true == is_running) {
318                         aul_app_get_running_app_info(__iterfunc_kill,
319                                         args.appid);
320                 } else {
321                         printf("result: %s\n", "App isn't running");
322                         return 1;
323                 }
324         } else if (op == 'r') {
325                 is_running = aul_app_is_running(args.appid);
326                 if (true == is_running) {
327                         printf("result: %s\n", "running");
328                         return 0;
329                 } else {
330                         printf("result: %s\n", "not running");
331                         return 1;
332                 }
333         }
334
335         return 0;
336 }