81559d75a0326051fe186b9768a7eec41da1ddad
[platform/core/base/tzdata-update-parser.git] / tzdata-parser / src / tzdata-update-parser.c
1 /*
2  * Copyright 2013 Samsung Electronics Co., Ltd
3  *
4  *
5  * Contact: Minsu Seo <minsu15.seo@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/xattr.h>
23
24 #include <sys/wait.h>
25 #include <errno.h>
26 #include <stdlib.h>
27
28 #include <unistd.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <glib.h>
32 #include <dlog.h>
33 #include <pkgmgr-info.h>
34 #include <pkgmgr_installer_info.h>
35
36 #define WORKER "/etc/package-manager/parserlib/worker.sh"
37
38
39 #define TZDATA_UPDATE_PARSER_TAG                "TZDATA_UPDATE_PARSER"
40
41 #define DEBUG_LOG(frmt, args...)                \
42         do { SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
43         __func__, ##args);} while (0)
44
45 #define DEBUG_WARNING(frmt, args...)            \
46         do { SLOG(LOG_WARN,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
47         __func__, ##args);} while (0)
48
49 #define DEBUG_ERROR(frmt, args...)              \
50         do { SLOG(LOG_ERROR,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
51         __func__, ##args);} while (0)
52
53 #define TRACE_BEGIN do {\
54         SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[ENTER tzdata_update_parser] %s\n",\
55         __func__);} while (0)
56
57 #define TRACE_END  do {\
58         SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[LEAVE tzdata_update_parser] %s\n",\
59         __func__);} while (0)
60
61
62 static void alt_system()
63 {
64         pid_t pid;
65         int status;
66         pid_t ret = -10000;
67         //char *const args[2] = {"/home/mjpark",  NULL};
68         //char **env;
69         //extern char **environ;
70         /* ... Sanitize arguments ... */
71         pid = fork();
72         if (pid == -1) {
73                 /* Handle error */
74         } else if (pid != 0) {
75                 while ((ret = waitpid(pid, &status, 0)) == -1) {
76                         if (errno != EINTR) {
77                                 /* Handle error */
78                                 break;
79                         }
80                 }
81                 if ((ret != -1) &&
82                                 (!WIFEXITED(status) || !WEXITSTATUS(status)) ) {
83                         /* Report unexpected child status */
84                 }
85                 DEBUG_LOG(" ----> parent ? : %d ", ret);
86
87         } else {
88                 /* ... Initialize env as a sanitized copy of environ ... */
89                 //ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0);
90                 //if (execve("/bin/ls", args, env) == -1) {
91                 //ret = execl("/bin/ls", "-1", (char *)0);
92                 DEBUG_LOG(" ------------> run execl: %s ", WORKER);
93                 ret = execl("/bin/bash", "bash", "-c",  WORKER, (char *) NULL);
94                 if (ret) {
95                         /* Handle error */
96                         DEBUG_LOG("error occured : %s", WORKER);
97                         printf("error occured\n");
98                         _Exit(127);
99                 } else {
100                         DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
101                 }
102         }
103 }
104
105 //DEBUG_ERROR("srcdir length is not available \n");
106 //DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
107 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
108 {
109         TRACE_BEGIN;
110         int ret;
111         pkgmgrinfo_pkginfo_h handle = NULL;
112         const char *app_root_path = NULL;
113         //const char *dest_path = NULL;
114         uid_t uid = 0;
115
116         pkgmgr_installer_info_get_target_uid(&uid);
117         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
118
119         if (ret < 0)
120         {
121                 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
122                 return -1;
123         }
124
125         ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
126         if (ret < 0)
127         {
128                 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
129                 goto FAIL;
130         }
131
132         if (appid == NULL)
133         {
134                 DEBUG_ERROR("appid is NULL \n");
135                 goto FAIL;
136         }
137
138
139         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
140         TRACE_END;
141         return ret;
142
143 FAIL:
144         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
145         TRACE_END;
146         return -1;
147 }
148
149
150 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
151 {
152         TRACE_BEGIN;
153         int ret = 0;
154         pkgmgrinfo_pkginfo_h handle = NULL;
155         const char* app_root_path = NULL;
156         //const char* dest_path = NULL;
157         uid_t uid = 0;
158
159         pkgmgr_installer_info_get_target_uid(&uid);
160         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
161
162         if (ret < 0)
163         {
164                 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
165                 return -1;
166         }
167
168         ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
169         if (ret < 0)
170         {
171                 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
172                 goto FAIL;
173         }
174
175         if (appid == NULL)
176         {
177                 DEBUG_ERROR("appid is NULL \n");
178                 goto FAIL;
179         }
180
181         DEBUG_LOG("-----------------------------------------------------");
182         DEBUG_LOG("pkgid : %s ", pkgid);
183         DEBUG_LOG("APP root path : %s ", app_root_path);
184         DEBUG_LOG("-----------------------------------------------------");
185         // RUN SCRIPT HERE
186         alt_system();
187         DEBUG_LOG("-----------------------------------------------------");
188
189         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
190         TRACE_END;
191         return ret;
192
193 FAIL:
194         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
195         TRACE_END;
196         return -1;
197 }
198
199
200
201 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
202 {
203         TRACE_BEGIN;
204         pkgmgrinfo_pkginfo_h handle = NULL;
205         const char* app_root_path = NULL;
206         //const char *dest_path = NULL;
207         int ret;
208         uid_t uid = 0;
209
210         if (appid == NULL)
211         {
212                 DEBUG_ERROR("appid is NULL \n");
213                 return -1;
214         }
215
216         pkgmgr_installer_info_get_target_uid(&uid);
217         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
218
219         if (ret < 0)
220         {
221                 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
222                 return -1;
223         }
224
225         ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
226         if (ret < 0)
227         {
228                 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
229                 goto FAIL;
230         }
231
232         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
233         TRACE_END;
234         return ret;
235
236 FAIL:
237         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
238         TRACE_END;
239         return -1;
240 }
241
242 #if USE_META_DATA
243 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
244 {
245         TRACE_BEGIN;
246         return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
247 }
248
249 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
250 {
251         TRACE_BEGIN;
252         return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
253 }
254
255 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
256 {
257         TRACE_BEGIN;
258         return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
259 }
260 #endif
261
262 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
263 {
264         TRACE_BEGIN;
265         return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
266 }
267
268 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
269 {
270         TRACE_BEGIN;
271         return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
272 }
273
274 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
275 {
276         TRACE_BEGIN;
277         return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
278 }
279
280
281 /* End of a file */