4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
28 #include <pkgmgr_installer.h>
29 #include <security-server.h>
30 #include <tzplatform_config.h>
31 #include "rpm-frontend.h"
32 #include "rpm-installer-util.h"
33 #include "rpm-installer.h"
34 #include "coretpk-installer.h"
37 extern struct appdata ad;
39 extern pkgmgr_installer *pi;
40 ri_frontend_data front_data;
41 char scrolllabel[256];
44 #define OTP_USR_APPS tzplatform_getenv(TZ_USER_APP)
46 static void __ri_show_usage(char **arg);
47 static int __ri_process_request(ri_frontend_cmdline_arg *fdata);
49 static void __ri_show_usage(char **arg)
55 while (arg[i] != NULL) {
56 snprintf(buffer, 256, "%s %s", buff, arg[i]);
57 strncpy(buff, buffer, 255);
61 _LOGD("%s\n", buffer);
63 "\nrpm-backend usage\n rpm-backend -k <keyid> <command> <pkgid | pkg_path> [-q] \n\n");
64 _LOGD("<Commands> \n");
66 "\t -i <package file path> : install package file \n");
68 "\t -k <keyid> : key id file \n");
70 "\t -r : (recover). Must ignore specific package name or path \n");
72 "\t -d <package name> : delete a package with package name \n");
74 "\t -q : (quiet) run in background without any user interaction \n");
76 "\t -s : (smack) apply smack rule and set smack label\n");
79 int _ri_parse_hybrid(int argc, char **argv)
83 if (argv[1] != NULL) {
84 if (!strcmp(argv[1], "-iv")) {
85 _LOGE("Hybrid Installation start\n");
87 for (i = 0; i < argc; i++) {
88 const char* arg_str = argv[i];
90 _LOGE("argv[%d] = [%s]\n", i, arg_str);
93 if (_coretpk_installer_request_hybrid(argv[1][1], argv[2], atoi(argv[4])) == 0) {
94 return RPM_INSTALLER_SUCCESS;
96 return RPM_INSTALLER_ERR_INTERNAL;
98 } else if (!strcmp(argv[1], "-uv")) {
99 _LOGE("Hybrid Uninstallation start\n");
100 return RPM_INSTALLER_SUCCESS;
104 return RPM_INSTALLER_ERR_WRONG_PARAM;
107 int _ri_parse_cmdline(int argc, char **argv, ri_frontend_cmdline_arg *data)
109 int req_cmd = INVALID_CMD;
110 const char *pkgid = NULL;
111 const char *pkeyid = NULL;
114 pi = pkgmgr_installer_new();
117 "Failure in creating the pkgmgr_installer object \n");
118 return RPM_INSTALLER_ERR_WRONG_PARAM;
120 ret = pkgmgr_installer_receive_request(pi, argc, argv);
122 _LOGE("pkgmgr_installer_receive_request failed \n");
123 return RPM_INSTALLER_ERR_WRONG_PARAM;
125 ret = pkgmgr_installer_get_request_type(pi);
127 case PKGMGR_REQ_INSTALL:
128 req_cmd = INSTALL_CMD;
130 case PKGMGR_REQ_REINSTALL:
131 req_cmd = CORETPK_REINSTALL_CMD;
133 case PKGMGR_REQ_UNINSTALL:
134 req_cmd = DELETE_CMD;
136 case PKGMGR_REQ_RECOVER:
137 req_cmd = RECOVER_CMD;
139 case PKGMGR_REQ_CLEAR:
140 req_cmd = CLEARDATA_CMD;
142 case PKGMGR_REQ_MOVE:
145 case PKGMGR_REQ_SMACK:
148 case PKGMGR_REQ_PERM:
150 case PKGMGR_REQ_INVALID:
151 req_cmd = INVALID_CMD;
156 if (req_cmd != RECOVER_CMD) {
157 pkgid = pkgmgr_installer_get_request_info(pi);
160 "pkgmgr_installer_get_request_info failed \n");
161 return RPM_INSTALLER_ERR_WRONG_PARAM;
163 pkeyid = pkgmgr_installer_get_session_id(pi);
165 _LOGE("pkgmgr_installer_get_session_id failed \n");
166 return RPM_INSTALLER_ERR_WRONG_PARAM;
168 move_type = pkgmgr_installer_get_move_type(pi);
170 //Logically dead code,the value of req_cmd never satisfies the condition
172 if ((req_cmd < INSTALL_CMD) ||(req_cmd > RPM_CMD_MAX)) {
173 _LOGE("invalid command \n");
177 data->req_cmd = req_cmd;
178 data->pkgid = (char *)pkgid;
179 data->keyid = (char *)pkeyid;
180 data->move_type = move_type;
181 data->clientid = (char *)pkgmgr_installer_get_caller_pkgid(pi);
183 return RPM_INSTALLER_SUCCESS;
186 _LOGE("Error in parsing input parameter\n");
187 __ri_show_usage(argv);
188 return RPM_INSTALLER_ERR_WRONG_PARAM;
192 static int __ri_is_core_tpk_app(char *pkgid)
194 char pkgpath[BUF_SIZE] = {'\0'};
196 snprintf(pkgpath, BUF_SIZE, "%s/%s/tizen-manifest.xml", OTP_USR_APPS, pkgid);
198 if (access(pkgpath, R_OK) == 0) {
199 _LOGE("This is a core tpk app.");
202 _LOGE("This is not a core tpk app.");
207 static int __ri_process_request(ri_frontend_cmdline_arg *data)
211 return RPM_INSTALLER_ERR_WRONG_PARAM;
214 if (data->req_cmd != RECOVER_CMD) {
215 pkgid = strdup(data->pkgid);
216 if (PM_UNLIKELY(pkgid == NULL)) {
217 _LOGE("strdup failed\n");
218 return RPM_INSTALLER_ERR_WRONG_PARAM;
220 keyid = strdup(data->keyid);
221 if (PM_UNLIKELY(keyid == NULL)) {
222 _LOGE("strdup failed\n");
225 return RPM_INSTALLER_ERR_WRONG_PARAM;
230 _LOGE("pkgid is null\n");
234 switch (data->req_cmd) {
236 _LOGD("rpm-backend -i %s\n", pkgid);
237 ret = _rpm_backend_interface(keyid, pkgid, "install", data->clientid);
240 if (__ri_is_core_tpk_app(pkgid) == 0) {
241 _LOGD("------------------------------------------------");
242 _LOGD("uninstallation: tpk, pkgid=[%s]", pkgid);
243 _LOGD("------------------------------------------------");
244 ret = _coretpk_backend_interface("coretpk-uninstall", data);
246 _LOGD("uninstallation for rpm [%s]", pkgid);
247 ret = _rpm_backend_interface(keyid, pkgid, "remove", NULL);
251 _LOGD("rpm-backend -c %s\n", pkgid);
252 ret = _rpm_backend_interface(keyid, pkgid, "cleardata", NULL);
255 if (__ri_is_core_tpk_app(pkgid) == 0) {
256 _LOGD("coretpk-move %s\n", pkgid);
257 ret = _coretpk_backend_interface("coretpk-move", data);
259 _LOGD("rpm-backend -m %s -t %d\n", pkgid, data->move_type);
260 move_type = data->move_type;
261 ret = _rpm_backend_interface(keyid, pkgid, "move", NULL);
265 _LOGD("rpm-backend -r \n");
266 ret = _rpm_backend_interface(keyid, pkgid, "recover", NULL);
269 _LOGD("rpm-backend -s %s", pkgid);
270 ret = _rpm_backend_interface(keyid, pkgid, "smack", NULL);
272 case EFLWGT_INSTALL_CMD:
273 _LOGD("eflwgt-install %s\n", pkgid);
274 ret = _rpm_backend_interface(keyid, pkgid, "eflwgt-install", data->clientid);
276 case CORETPK_INSTALL_CMD:
277 _LOGD("------------------------------------------------");
278 _LOGD("installation: tpk, arg=[%s]", pkgid);
279 _LOGD("------------------------------------------------");
280 ret = _coretpk_backend_interface("coretpk-install", data);
282 case CORETPK_REINSTALL_CMD:
283 _LOGD("coretpk-reinstall %s\n", pkgid);
284 ret = _coretpk_backend_interface("coretpk-reinstall", data);
286 case CORETPK_DIRECTORY_INSTALL_CMD:
287 _LOGD("coretpk-directory_install %s\n", pkgid);
288 ret = _coretpk_backend_interface("coretpk-directory-install", data);
291 _LOGD("rpm enable %s\n", pkgid);
292 ret = _rpm_backend_interface(keyid, pkgid, "rpm-enable", NULL);
295 _LOGD("rpm disable %s\n", pkgid);
296 ret = _rpm_backend_interface(keyid, pkgid, "rpm-disable", NULL);
299 _LOGE("Error Never Come Here as Error is already checked\n");
314 void _ri_stat_cb(const char *pkgid, const char *key, const char *val)
317 if (NULL == pkgid || NULL == key || NULL == val) {
318 _LOGE("Either pkgid/key/val is NULL\n");
319 return; /*TODO: handle error. */
322 char pkgid_modified[PATH_MAX] = {0};
325 char *pkgid_tmp = NULL;
326 char *saveptr = NULL;
328 memcpy(pkgid_modified, pkgid, strlen(pkgid));
330 result = strtok_r(pkgid_modified, delims, &saveptr);
331 while (result != NULL) {
333 result = strtok_r(NULL, delims, &saveptr);
336 if (strcmp(key, "install_percent") == 0) {
338 } else if (strcmp(key, "error") == 0) {
339 /* Store the error to be display to the user */
340 front_data.error = strdup(val);
341 } else if (strcmp(key, "end") == 0) {
343 char requesttype[32];
344 switch (front_data.args->req_cmd) {
346 snprintf(requesttype, sizeof(requesttype),
350 snprintf(requesttype, sizeof(requesttype), "deletion");
353 snprintf(requesttype, sizeof(requesttype),
357 snprintf(requesttype, sizeof(requesttype),
361 snprintf(requesttype, sizeof(requesttype), "recovery");
365 if (front_data.error) {
367 snprintf(scrolllabel, sizeof(scrolllabel),
368 "%s :: %s:: %s:: %s", requesttype, pkgid_tmp,
371 _LOGE("%s\n", scrolllabel);
372 ret_val = _ri_string_to_error_no(front_data.error);
373 _LOGE("%d\n", ret_val);
376 snprintf(scrolllabel, sizeof(scrolllabel),
377 " %s :: %s :: %s", requesttype, pkgid_tmp,
379 _LOGD("%s\n", scrolllabel);
385 int _ri_cmdline_process(ri_frontend_data *data)
388 ri_frontend_cmdline_arg *fdata = data->args;
389 /*rpm-installer is invoked by pkgmgr-server hence server should do cookie validation*/
390 ret = __ri_process_request(fdata);
391 if (ret != RPM_INSTALLER_SUCCESS) {
392 _LOGE("__ri_process_request: Error\n");
395 return RPM_INSTALLER_SUCCESS;
398 int _ri_cmdline_destroy(ri_frontend_data *data)
403 if (data->security_cookie){
404 free(data->security_cookie);
405 data->security_cookie = NULL;