refactor: extract common logic
[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 <dirent.h>
20 #include <dlog.h>
21 #include <errno.h>
22 #include <glib.h>
23 #include <pkgmgr-info.h>
24 #include <pkgmgr_installer_info.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/xattr.h>
32 #include <unistd.h>
33
34 #define WORKER "/etc/package-manager/parserlib/worker.sh"
35
36 #define TZDATA_UPDATE_PARSER_TAG "TZDATA_UPDATE_PARSER"
37
38 #define DEBUG_LOG(frmt, args...)                                     \
39   do {                                                               \
40     SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,                        \
41          "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
42   } while (0)
43
44 #define DEBUG_WARNING(frmt, args...)                                 \
45   do {                                                               \
46     SLOG(LOG_WARN, TZDATA_UPDATE_PARSER_TAG,                         \
47          "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
48   } while (0)
49
50 #define DEBUG_ERROR(frmt, args...)                                   \
51   do {                                                               \
52     SLOG(LOG_ERROR, TZDATA_UPDATE_PARSER_TAG,                        \
53          "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
54   } while (0)
55
56 #define TRACE_BEGIN                                      \
57   do {                                                   \
58     SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,            \
59          "[ENTER tzdata_update_parser] %s\n", __func__); \
60   } while (0)
61
62 #define TRACE_END                                        \
63   do {                                                   \
64     SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,            \
65          "[LEAVE tzdata_update_parser] %s\n", __func__); \
66   } while (0)
67
68 static void alt_system() {
69   pid_t pid;
70   int status;
71   pid_t ret = -10000;
72   // char *const args[2] = {"/home/mjpark",  NULL};
73   // char **env;
74   // extern char **environ;
75   /* ... Sanitize arguments ... */
76   pid = fork();
77   if (pid == -1) {
78     /* Handle error */
79   } else if (pid != 0) {
80     while ((ret = waitpid(pid, &status, 0)) == -1) {
81       if (errno != EINTR) {
82         /* Handle error */
83         break;
84       }
85     }
86     if ((ret != -1) && (!WIFEXITED(status) || !WEXITSTATUS(status))) {
87       /* Report unexpected child status */
88     }
89     DEBUG_LOG(" ----> parent ? : %d ", ret);
90   } else {
91     /* ... Initialize env as a sanitized copy of environ ... */
92     // ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0);
93     // if (execve("/bin/ls", args, env) == -1) {
94     // ret = execl("/bin/ls", "-1", (char *)0);
95     DEBUG_LOG(" ------------> run execl: %s ", WORKER);
96     ret = execl("/bin/bash", "bash", "-c", WORKER, (char *)NULL);
97     if (ret) {
98       /* Handle error */
99       DEBUG_LOG("error occured : %s", WORKER);
100       printf("error occured\n");
101       _Exit(127);
102     } else {
103       DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
104     }
105   }
106 }
107
108 static int _get_pkgmgrinfo_handle(const char *pkgid,
109                                   pkgmgrinfo_pkginfo_h handle) {
110   int ret = 0;
111   uid_t uid = 0;
112
113   pkgmgr_installer_info_get_target_uid(&uid);
114   ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
115
116   if (ret < 0) {
117     DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
118     return -1;
119   }
120   return ret;
121 }
122
123 static int _pkgmgr_plugin_install(const char *pkgid, const char *appid,
124                                   GList *list, int upgrade_flag) {
125   TRACE_BEGIN;
126   int ret = 0;
127   pkgmgrinfo_pkginfo_h handle = NULL;
128   const char *app_root_path = NULL;
129   // const char* dest_path = NULL;
130   uid_t uid = 0;
131
132   if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1;
133
134   ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
135   if (ret < 0) {
136     DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
137     goto FAIL;
138   }
139
140   if (appid == NULL) {
141     DEBUG_ERROR("appid is NULL \n");
142     goto FAIL;
143   }
144
145   if (upgrade_flag == 1) {
146     DEBUG_LOG("-----------------------------------------------------");
147     DEBUG_LOG("pkgid : %s ", pkgid);
148     DEBUG_LOG("APP root path : %s ", app_root_path);
149     DEBUG_LOG("-----------------------------------------------------");
150     // RUN SCRIPT HERE
151     alt_system();
152     DEBUG_LOG("-----------------------------------------------------");
153   }
154
155   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
156   TRACE_END;
157   return ret;
158
159 FAIL:
160   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
161   TRACE_END;
162   return -1;
163 }
164
165 // DEBUG_ERROR("srcdir length is not available \n");
166 // DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
167 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid,
168                                  GList *list) {
169   return _pkgmgr_plugin_install(pkgid, appid, list, 0);
170 }
171
172 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
173                                  GList *list) {
174   return _pkgmgr_plugin_install(pkgid, appid, list, 1);
175 }
176
177 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid,
178                                    GList *list) {
179   TRACE_BEGIN;
180   pkgmgrinfo_pkginfo_h handle = NULL;
181   const char *app_root_path = NULL;
182   // const char *dest_path = NULL;
183   int ret;
184   uid_t uid = 0;
185
186   if (appid == NULL) {
187     DEBUG_ERROR("appid is NULL \n");
188     return -1;
189   }
190
191   if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1;
192
193   ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
194   if (ret < 0) {
195     DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
196     goto FAIL;
197   }
198
199   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
200   TRACE_END;
201   return ret;
202
203 FAIL:
204   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
205   TRACE_END;
206   return -1;
207 }
208
209 #if USE_META_DATA
210 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid,
211                                    GList *list) {
212   TRACE_BEGIN;
213   return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
214 }
215
216 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
217                                    GList *list) {
218   TRACE_BEGIN;
219   return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
220 }
221
222 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid,
223                                      GList *list) {
224   TRACE_BEGIN;
225   return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
226 }
227 #endif
228
229 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid,
230                                           GList *list) {
231   TRACE_BEGIN;
232   return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
233 }
234
235 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
236                                           GList *list) {
237   TRACE_BEGIN;
238   return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
239 }
240
241 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid,
242                                             const char *appid, GList *list) {
243   TRACE_BEGIN;
244   return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
245 }
246
247 /* End of a file */