upload tizen1.0 source
[framework/appfw/aul-1.git] / test / launch_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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26
27 #include <Ecore.h>
28 #include "aul.h"
29
30 #define ROOT_UID 0
31 #define TMP_FILE "/tmp/.testpkg"
32
33 static char **gargv;
34 static int gargc;
35
36 static bundle *create_internal_bundle(int start)
37 {
38         bundle *kb;
39         int i;
40         char arg[1024];
41         char* val_array[128];
42
43         kb = bundle_create();
44         for (i = start; i < gargc - 1; i++) {
45                 if ((i + 1) > gargc - 1)
46                         bundle_add(kb, gargv[i], " ");
47                 else {
48                         int j = 1;
49                         strncpy(arg, gargv[i + 1], 1024);
50                         val_array[0] = strtok(arg,",");
51                         while(1)
52                         {
53                                 val_array[j] = strtok(NULL,",");
54                                 if(val_array[j] == NULL)
55                                         break;
56                                 j++;
57                         }
58                         if(j==1)
59                                 bundle_add(kb, gargv[i], gargv[i + 1]);
60                         else if(j>1)
61                                 bundle_add_str_array(kb, gargv[i],
62                                         (const char**)val_array, j);
63                 }
64         }
65
66         return kb;
67 }
68
69 int launch()
70 {
71         bundle *kb = NULL;
72         FILE *fp;
73         int ret = -1;
74         int pid = -1;
75
76         kb = create_internal_bundle(2);
77         if (NULL == kb) {
78                 printf("bundle creation fail\n");
79                 return -1;
80         }
81
82         pid = aul_launch_app(gargv[1], kb);
83
84         if (kb) {
85                 bundle_free(kb);
86                 kb = NULL;
87         }
88         /* Write the package name to TMP_FILE*/
89         fp = fopen(TMP_FILE, "w");
90         if (fp == NULL)
91                 return -1;
92         ret = fprintf(fp, "%d", pid);
93         fclose(fp);
94         if (ret < 0)
95                 return -1;
96
97         return pid;
98 }
99
100 void print_usage(char *progname)
101 {
102         printf("[usage] %s <pkgname> <key1> <val1> <key2> <val2> ...\n",
103                progname);
104 }
105
106 static Eina_Bool run_func(void *data)
107 {
108         if (launch() > 0) {
109                 printf("... successfully launched\n");
110         } else {
111                 printf("... launch failed\n");
112         }
113
114         ecore_main_loop_quit();
115
116         return 0;
117 }
118
119 int main(int argc, char **argv)
120 {
121
122         /* Checking the User ID*/
123         if (getuid() != ROOT_UID) {
124                 fprintf(stderr, "permission error\n");
125                 exit(EXIT_FAILURE);
126         }
127
128         if (argc < 2) {
129                 print_usage(argv[0]);
130                 exit(EXIT_FAILURE);
131         }
132
133         ecore_init();
134
135         gargc = argc;
136         gargv = argv;
137
138         aul_launch_init(NULL, NULL);
139
140         ecore_idler_add(run_func, NULL);
141
142         ecore_main_loop_begin();
143
144         return 0;
145 }
146