tizen beta release
[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(val_array[j] = strtok(NULL,","))
52                         {
53                                 j++;
54                         }
55                         if(j==1)
56                                 bundle_add(kb, gargv[i], gargv[i + 1]);
57                         else if(j>1)
58                                 bundle_add_str_array(kb, gargv[i], val_array, j);
59                 }
60         }
61
62         return kb;
63 }
64
65 int launch()
66 {
67         bundle *kb = NULL;
68         FILE *fp;
69         int ret = -1;
70         int pid = -1;
71
72         kb = create_internal_bundle(2);
73         if (NULL == kb) {
74                 printf("bundle creation fail\n");
75                 return -1;
76         }
77
78         pid = aul_launch_app(gargv[1], kb);
79
80         if (kb) {
81                 bundle_free(kb);
82                 kb = NULL;
83         }
84         /* Write the package name to TMP_FILE*/
85         fp = fopen(TMP_FILE, "w");
86         if (fp == NULL)
87                 return -1;
88         ret = fprintf(fp, "%d", pid);
89         fclose(fp);
90         if (ret < 0)
91                 return -1;
92
93         return pid;
94 }
95
96 void print_usage(char *progname)
97 {
98         printf("[usage] %s <pkgname> <key1> <val1> <key2> <val2> ...\n",
99                progname);
100 }
101
102 static Eina_Bool run_func(void *data)
103 {
104         if (launch() > 0) {
105                 printf("... successfully launched\n");
106         } else {
107                 printf("... launch failed\n");
108         }
109
110         ecore_main_loop_quit();
111
112         return 0;
113 }
114
115 int main(int argc, char **argv)
116 {
117
118         /* Checking the User ID*/
119         if (getuid() != ROOT_UID) {
120                 fprintf(stderr, "permission error\n");
121                 exit(EXIT_FAILURE);
122         }
123
124         if (argc < 2) {
125                 print_usage(argv[0]);
126                 exit(EXIT_FAILURE);
127         }
128
129         ecore_init();
130
131         gargc = argc;
132         gargv = argv;
133
134         aul_launch_init(NULL, NULL);
135
136         ecore_idler_add(run_func, NULL);
137
138         ecore_main_loop_begin();
139
140         return 0;
141 }
142