816168c94e9b9cb58ae0776f64b226c4a52a4b8e
[framework/appfw/aul-1.git] / test / open_app.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include <Ecore.h>
29 #include <bundle_internal.h>
30 #include "aul.h"
31
32 #define ROOT_UID 0
33 #define TMP_FILE "/tmp/.testpkg"
34
35 static char **gargv;
36 static int gargc;
37 bundle *kb = NULL;
38
39
40 static bundle *create_internal_bundle(int start)
41 {
42         bundle *kb;
43         int i;
44         char arg[1024] = {0, };
45         char* val_array[128];
46
47         kb = bundle_create();
48         for (i = start; i < gargc - 1; i++) {
49                 if ((i + 1) > gargc - 1)
50                         bundle_add(kb, gargv[i], " ");
51                 else {
52                         int j = 1;
53                         strncpy(arg, gargv[i + 1], 1023);
54                         val_array[0] = strtok(arg,",");
55                         while(1)
56                         {
57                                 val_array[j] = strtok(NULL,",");
58                                 if(val_array[j] == NULL)
59                                         break;
60                                 j++;
61                         }
62                         if(j==1)
63                                 bundle_add(kb, gargv[i], gargv[i + 1]);
64                         else if(j>1)
65                                 bundle_add_str_array(kb, gargv[i],
66                                         (const char**)val_array, j);
67                 }
68         }
69
70         return kb;
71 }
72
73 int launch()
74 {
75         FILE *fp;
76         int ret = -1;
77         int pid = -1;
78
79         kb = create_internal_bundle(2);
80         if (NULL == kb) {
81                 printf("bundle creation fail\n");
82                 return -1;
83         }
84
85         pid = aul_open_app(gargv[1]);
86
87         /* Write the package name to TMP_FILE*/
88         fp = fopen(TMP_FILE, "w");
89         if (fp == NULL)
90                 return -1;
91         ret = fprintf(fp, "%d", pid);
92         fclose(fp);
93         if (ret < 0)
94                 return -1;
95
96         return pid;
97 }
98
99 void print_usage(char *progname)
100 {
101         printf("[usage] %s <appid>\n",
102                progname);
103 }
104
105 static int __launch_app_dead_handler(int pid, void *data)
106 {
107         int listen_pid = (int) data;
108
109         if(listen_pid == pid)
110                 ecore_main_loop_quit();
111
112         return 0;
113 }
114
115 static Eina_Bool run_func(void *data)
116 {
117         int pid = -1;
118         const char *str = NULL;
119         if ((pid = launch()) > 0) {
120                 printf("... successfully launched\n");
121         } else {
122                 printf("... launch failed\n");
123         }
124
125         str = bundle_get_val(kb, "__LAUNCH_APP_MODE__");
126
127         if( str && strcmp(str, "SYNC") == 0 ) {
128                 aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)pid);
129         } else {
130                 ecore_main_loop_quit();
131         }
132
133         if (kb) {
134                 bundle_free(kb);
135                 kb = NULL;
136         }
137
138         return 0;
139 }
140
141 int main(int argc, char **argv)
142 {
143         if (argc < 2) {
144                 print_usage(argv[0]);
145                 exit(EXIT_FAILURE);
146         }
147
148         ecore_init();
149
150         gargc = argc;
151         gargv = argv;
152
153         ecore_idler_add(run_func, NULL);
154
155         ecore_main_loop_begin();
156
157         return 0;
158 }
159