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 "rpm-frontend.h"
31 #include "rpm-installer-util.h"
32 #include "rpm-installer.h"
33 #include "coretpk-installer.h"
36 extern struct appdata ad;
38 extern pkgmgr_installer *pi;
39 ri_frontend_data front_data;
40 char scrolllabel[256];
43 #define OTP_USR_APPS "/opt/usr/apps"
45 static void __ri_show_usage(char **arg);
46 static int __ri_process_request(ri_frontend_cmdline_arg *fdata);
48 static void __ri_show_usage(char **arg)
54 while (arg[i] != NULL) {
55 snprintf(buffer, 256, "%s %s", buff, arg[i]);
56 strncpy(buff, buffer, 255);
60 _LOGD("%s\n", buffer);
62 "\nrpm-backend usage\n rpm-backend -k <keyid> <command> <pkgid | pkg_path> [-q] \n\n");
63 _LOGD("<Commands> \n");
65 "\t -i <package file path> : install package file \n");
67 "\t -k <keyid> : key id file \n");
69 "\t -r : (recover). Must ignore specific package name or path \n");
71 "\t -d <package name> : delete a package with package name \n");
73 "\t -q : (quiet) run in background without any user interaction \n");
75 "\t -s : (smack) apply smack rule and set smack label\n");
78 int _ri_parse_hybrid(int argc, char **argv)
82 if (argv[1] != NULL) {
83 if (!strcmp(argv[1], "-iv")) {
84 _LOGE("Hybrid Installation start\n");
86 for (i = 0; i < argc; i++) {
87 const char* arg_str = argv[i];
89 _LOGE("argv[%d] = [%s]\n", i, arg_str);
92 if (_coretpk_installer_request_hybrid(argv[1][1], argv[2], atoi(argv[4])) == 0) {
93 return RPM_INSTALLER_SUCCESS;
95 return RPM_INSTALLER_ERR_INTERNAL;
97 } else if (!strcmp(argv[1], "-uv")) {
98 _LOGE("Hybrid Uninstallation start\n");
99 return RPM_INSTALLER_SUCCESS;
103 return RPM_INSTALLER_ERR_WRONG_PARAM;
106 int _ri_parse_cmdline(int argc, char **argv, ri_frontend_cmdline_arg *data)
108 int req_cmd = INVALID_CMD;
109 const char *pkgid = NULL;
110 const char *pkeyid = NULL;
113 pi = pkgmgr_installer_new();
116 "Failure in creating the pkgmgr_installer object \n");
117 return RPM_INSTALLER_ERR_WRONG_PARAM;
119 ret = pkgmgr_installer_receive_request(pi, argc, argv);
121 _LOGE("pkgmgr_installer_receive_request failed \n");
122 return RPM_INSTALLER_ERR_WRONG_PARAM;
124 ret = pkgmgr_installer_get_request_type(pi);
126 case PKGMGR_REQ_INSTALL:
127 req_cmd = INSTALL_CMD;
129 case PKGMGR_REQ_REINSTALL:
130 req_cmd = CORETPK_REINSTALL_CMD;
132 case PKGMGR_REQ_UNINSTALL:
133 req_cmd = DELETE_CMD;
135 case PKGMGR_REQ_RECOVER:
136 req_cmd = RECOVER_CMD;
138 case PKGMGR_REQ_CLEAR:
139 req_cmd = CLEARDATA_CMD;
141 case PKGMGR_REQ_MOVE:
144 case PKGMGR_REQ_SMACK:
147 case PKGMGR_REQ_PERM:
149 case PKGMGR_REQ_INVALID:
150 req_cmd = INVALID_CMD;
155 if (req_cmd != RECOVER_CMD) {
156 pkgid = pkgmgr_installer_get_request_info(pi);
159 "pkgmgr_installer_get_request_info failed \n");
160 return RPM_INSTALLER_ERR_WRONG_PARAM;
162 pkeyid = pkgmgr_installer_get_session_id(pi);
164 _LOGE("pkgmgr_installer_get_session_id failed \n");
165 return RPM_INSTALLER_ERR_WRONG_PARAM;
167 move_type = pkgmgr_installer_get_move_type(pi);
169 //Logically dead code,the value of req_cmd never satisfies the condition
171 if ((req_cmd < INSTALL_CMD) ||(req_cmd > RPM_CMD_MAX)) {
172 _LOGE("invalid command \n");
176 data->req_cmd = req_cmd;
177 data->pkgid = (char *)pkgid;
178 data->keyid = (char *)pkeyid;
179 data->move_type = move_type;
180 data->clientid = (char *)pkgmgr_installer_get_caller_pkgid(pi);
182 return RPM_INSTALLER_SUCCESS;
185 _LOGE("Error in parsing input parameter\n");
186 __ri_show_usage(argv);
187 return RPM_INSTALLER_ERR_WRONG_PARAM;
191 static int __ri_is_core_tpk_app(char *pkgid)
193 char pkgpath[BUF_SIZE] = {'\0'};
195 snprintf(pkgpath, BUF_SIZE, "%s/%s/tizen-manifest.xml", OTP_USR_APPS, pkgid);
197 if (access(pkgpath, R_OK) == 0) {
198 _LOGE("This is a core tpk app.");
201 _LOGE("This is not a core tpk app.");
206 static int __ri_process_request(ri_frontend_cmdline_arg *data)
210 return RPM_INSTALLER_ERR_WRONG_PARAM;
213 if (data->req_cmd != RECOVER_CMD) {
214 pkgid = strdup(data->pkgid);
215 if (PM_UNLIKELY(pkgid == NULL)) {
216 _LOGE("strdup failed\n");
217 return RPM_INSTALLER_ERR_WRONG_PARAM;
219 keyid = strdup(data->keyid);
220 if (PM_UNLIKELY(keyid == NULL)) {
221 _LOGE("strdup failed\n");
224 return RPM_INSTALLER_ERR_WRONG_PARAM;
229 _LOGE("pkgid is null\n");
233 switch (data->req_cmd) {
235 _LOGD("rpm-backend -i %s\n", pkgid);
236 ret = _rpm_backend_interface(keyid, pkgid, "install", data->clientid);
239 if (__ri_is_core_tpk_app(pkgid) == 0) {
240 _LOGD("------------------------------------------------");
241 _LOGD("uninstallation: tpk, pkgid=[%s]", pkgid);
242 _LOGD("------------------------------------------------");
243 ret = _coretpk_backend_interface("coretpk-uninstall", data);
245 _LOGD("uninstallation for rpm [%s]", pkgid);
246 ret = _rpm_backend_interface(keyid, pkgid, "remove", NULL);
250 _LOGD("rpm-backend -c %s\n", pkgid);
251 ret = _rpm_backend_interface(keyid, pkgid, "cleardata", NULL);
254 if (__ri_is_core_tpk_app(pkgid) == 0) {
255 _LOGD("coretpk-move %s\n", pkgid);
256 ret = _coretpk_backend_interface("coretpk-move", data);
258 _LOGD("rpm-backend -m %s -t %d\n", pkgid, data->move_type);
259 move_type = data->move_type;
260 ret = _rpm_backend_interface(keyid, pkgid, "move", NULL);
264 _LOGD("rpm-backend -r \n");
265 ret = _rpm_backend_interface(keyid, pkgid, "recover", NULL);
268 _LOGD("rpm-backend -s %s", pkgid);
269 ret = _rpm_backend_interface(keyid, pkgid, "smack", NULL);
271 case EFLWGT_INSTALL_CMD:
272 _LOGD("eflwgt-install %s\n", pkgid);
273 ret = _rpm_backend_interface(keyid, pkgid, "eflwgt-install", data->clientid);
275 case CORETPK_INSTALL_CMD:
276 _LOGD("------------------------------------------------");
277 _LOGD("installation: tpk, arg=[%s]", pkgid);
278 _LOGD("------------------------------------------------");
279 ret = _coretpk_backend_interface("coretpk-install", data);
281 case CORETPK_REINSTALL_CMD:
282 _LOGD("coretpk-reinstall %s\n", pkgid);
283 ret = _coretpk_backend_interface("coretpk-reinstall", data);
285 case CORETPK_DIRECTORY_INSTALL_CMD:
286 _LOGD("coretpk-directory_install %s\n", pkgid);
287 ret = _coretpk_backend_interface("coretpk-directory-install", data);
290 _LOGD("rpm enable %s\n", pkgid);
291 ret = _rpm_backend_interface(keyid, pkgid, "rpm-enable", NULL);
294 _LOGD("rpm disable %s\n", pkgid);
295 ret = _rpm_backend_interface(keyid, pkgid, "rpm-disable", NULL);
298 _LOGE("Error Never Come Here as Error is already checked\n");
313 void _ri_stat_cb(const char *pkgid, const char *key, const char *val)
316 if (NULL == pkgid || NULL == key || NULL == val) {
317 _LOGE("Either pkgid/key/val is NULL\n");
318 return; /*TODO: handle error. */
321 char pkgid_modified[PATH_MAX] = {0};
324 char *pkgid_tmp = NULL;
325 char *saveptr = NULL;
327 memcpy(pkgid_modified, pkgid, strlen(pkgid));
329 result = strtok_r(pkgid_modified, delims, &saveptr);
330 while (result != NULL) {
332 result = strtok_r(NULL, delims, &saveptr);
335 if (strcmp(key, "install_percent") == 0) {
337 } else if (strcmp(key, "error") == 0) {
338 /* Store the error to be display to the user */
339 front_data.error = strdup(val);
340 } else if (strcmp(key, "end") == 0) {
342 char requesttype[32];
343 switch (front_data.args->req_cmd) {
345 snprintf(requesttype, sizeof(requesttype),
349 snprintf(requesttype, sizeof(requesttype), "deletion");
352 snprintf(requesttype, sizeof(requesttype),
356 snprintf(requesttype, sizeof(requesttype),
360 snprintf(requesttype, sizeof(requesttype), "recovery");
364 if (front_data.error) {
366 snprintf(scrolllabel, sizeof(scrolllabel),
367 "%s :: %s:: %s:: %s", requesttype, pkgid_tmp,
370 _LOGE("%s\n", scrolllabel);
371 ret_val = _ri_string_to_error_no(front_data.error);
372 _LOGE("%d\n", ret_val);
375 snprintf(scrolllabel, sizeof(scrolllabel),
376 " %s :: %s :: %s", requesttype, pkgid_tmp,
378 _LOGD("%s\n", scrolllabel);
384 int _ri_cmdline_process(ri_frontend_data *data)
387 ri_frontend_cmdline_arg *fdata = data->args;
388 /*rpm-installer is invoked by pkgmgr-server hence server should do cookie validation*/
389 ret = __ri_process_request(fdata);
390 if (ret != RPM_INSTALLER_SUCCESS) {
391 _LOGE("__ri_process_request: Error\n");
394 return RPM_INSTALLER_SUCCESS;
397 int _ri_cmdline_destroy(ri_frontend_data *data)
402 if (data->security_cookie){
403 free(data->security_cookie);
404 data->security_cookie = NULL;