Update source from tizen 2.3
[platform/core/base/rpm-installer.git] / backend / src / rpm / rpm-installer-tool.c
1 /*
2  * rpm-installer
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>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <rpmlib.h>
32 #include <header.h>
33 #include <rpmts.h>
34 #include <rpmdb.h>
35 #include <pkgmgr_installer.h>
36
37 #include "rpm-installer.h"
38 #include "rpm-frontend.h"
39 #include "rpm-installer-type.h"
40
41 char *gpkgname = NULL;
42 extern char scrolllabel[256];
43 extern int move_type;
44 enum optionsflags {
45         INVALID_OPTIONS = 0,
46         FORCE_OVERWITE = 1,
47         IGNORE_DEPENDS = 2,
48 };
49
50 struct ri_backend_data_t {
51         int req_cmd;
52         char *cmd_string;
53         char *pkgid;
54         int force_overwrite;
55 };
56
57 typedef struct ri_backend_data_t ri_backend_data;
58 static int __ri_native_recovery(int lastbackstate);
59 static int __ri_uninstall_package(char *pkgid);
60 static int __ri_clear_private_data(char *pkgid);
61 static int __ri_move_package(char *pkgid, int move_type);
62 static inline int __ri_read_proc(const char *path, char *buf, int size);
63 static inline int __ri_find_pid_by_cmdline(const char *dname,
64                                            const char *cmdline,
65                                            const char *priv);
66 static bool __ri_is_another_instance_running(const char *exepath);
67
68 static int __ri_uninstall_package(char *pkgid)
69 {
70
71         if (pkgid == NULL)
72                 return RPM_INSTALLER_ERR_WRONG_PARAM;
73         int ret = 0;
74         ret = _rpm_installer_package_uninstall(pkgid);
75         if (ret == RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED) {
76                 _LOGE("[__ri_uninstall_package]%s "
77                        "not installed\n", pkgid);
78         } else if (ret != 0) {
79                 _LOGE(
80                        "[__ri_uninstall_package]%s uninstall failed(%d)\n",
81                        pkgid, ret);
82         } else {
83                 _LOGE(
84                        "[__ri_uninstall_package]%s successfully uninstalled\n",
85                        pkgid);
86         }
87         return ret;
88 }
89
90 static int __ri_clear_private_data(char *pkgid)
91 {
92         if (pkgid == NULL)
93                 return RPM_INSTALLER_ERR_WRONG_PARAM;
94         int ret = 0;
95         ret = _rpm_installer_clear_private_data(pkgid);
96         if (ret == RPM_INSTALLER_SUCCESS) {
97                 _LOGE(
98                        "[__clear_private_data]%s clear data successful\n",
99                        pkgid);
100         } else {
101                 _LOGE(
102                        "[__clear_private_data]%s clear data failed(%d)\n",
103                        pkgid, ret);
104         }
105         return ret;
106 }
107
108 static int __ri_move_package(char *pkgid, int move_type)
109 {
110         if (pkgid == NULL)
111                 return RPM_INSTALLER_ERR_WRONG_PARAM;
112         int ret = 0;
113         if(gpkgname){
114                 free(gpkgname);
115                 gpkgname = NULL;
116         }
117         gpkgname = strdup(pkgid);
118         if(!gpkgname){
119                 _LOGE("Malloc failed!!");
120                 return RPM_INSTALLER_ERR_INTERNAL;
121         }
122         ret = _rpm_move_pkg(pkgid, move_type);
123         if (ret == RPM_INSTALLER_SUCCESS) {
124                 _LOGE(
125                        "[__ri_move_package]%s move successful\n",
126                        pkgid);
127         } else {
128                 _LOGE(
129                        "[__ri_move_package]%s move failed(%d)\n",
130                        pkgid, ret);
131         }
132         return ret;
133 }
134
135 static inline int __ri_read_proc(const char *path, char *buf, int size)
136 {
137         int fd;
138         int ret;
139
140         if (buf == NULL || path == NULL)
141                 return -1;
142
143         fd = open(path, O_RDONLY);
144         if (fd < 0)
145                 return -1;
146
147         ret = read(fd, buf, size - 1);
148         if (ret <= 0) {
149                 close(fd);
150                 return -1;
151         } else
152                 buf[ret] = 0;
153
154         close(fd);
155
156         return ret;
157 }
158
159 static inline int __ri_find_pid_by_cmdline(const char *dname,
160                                            const char *cmdline,
161                                            const char *priv)
162 {
163         int pid = 0;
164         if (strncmp(cmdline, priv, strlen(RPM)) == 0) {
165                 pid = atoi(dname);
166                 if (pid != getpgid(pid))
167                         pid = 0;
168                 if (pid == getpid())
169                         pid = 0;
170         }
171
172         return pid;
173 }
174
175 static bool __ri_is_another_instance_running(const char *exepath)
176 {
177         DIR *dp;
178         struct dirent *dentry;
179         int pid;
180         int ret;
181         char buf[256] = { 0, };
182         char buf1[256] = { 0, };
183         dp = opendir("/proc");
184         if (dp == NULL) {
185                 return 0;
186         }
187         while ((dentry = readdir(dp)) != NULL) {
188                 if (!isdigit(dentry->d_name[0]))
189                         continue;
190                 snprintf(buf, sizeof(buf), "/proc/%s/cmdline", dentry->d_name);
191                 ret = __ri_read_proc(buf, buf1, sizeof(buf));
192                 if (ret <= 0)
193                         continue;
194                 pid = __ri_find_pid_by_cmdline(dentry->d_name, buf1, exepath);
195                 if (pid > 0) {
196                         closedir(dp);
197                         return 1;
198                 }
199         }
200
201         closedir(dp);
202         return 0;
203
204 }
205
206 static int __ri_native_recovery(int lastbackstate)
207 {
208         char *pn = NULL;
209         int lreq;
210         int opt;
211         int err = 0;
212
213         _LOGD("Rpm Installer Recovery Entry \n");
214
215         /* which package it was installing and what was state at that time */
216         _ri_get_last_input_info(&pn, &lreq, &opt);
217
218         switch (lastbackstate) {
219         case REQUEST_ACCEPTED:
220         case GOT_PACKAGE_INFO_SUCCESSFULLY:
221                 /*
222                  * restart the last operation
223                  */
224                 _LOGD(
225                               "Rpm Installer Recovery started. state=%d \n", lastbackstate);
226                 switch (lreq) {
227                 case INSTALL_CMD:
228                         err =
229                             _rpm_installer_package_install(pn, true, "--force", NULL);
230                         if (err)
231                                 goto RECOVERYERROR;
232                         break;
233
234                 case DELETE_CMD:
235                         err = _rpm_installer_package_uninstall(pn);
236                         if (err)
237                                 goto RECOVERYERROR;
238                         break;
239                 case EFLWGT_INSTALL_CMD:
240                         err = _rpm_installer_package_uninstall(pn);
241                         if(err)
242                                 goto RECOVERYERROR;
243                         break;
244
245                 case CLEARDATA_CMD:
246                 case MOVE_CMD:
247                 case RECOVER_CMD:
248                         /*TODO*/
249                         _LOGD(
250                                         "Recovery of command(%d) is to be implemented\n", lreq);
251                         if(pn) free(pn);
252                         return 0;
253                 }
254                 _LOGD(
255                               " Rpm Installer Recovery Ended \n");
256                 break;
257         case REQUEST_COMPLETED:
258                 _LOGD(
259                               " Rpm Installer Recovery. Nothing To Be Done\n");
260                 _ri_set_backend_state_info(REQUEST_COMPLETED);
261                 break;
262
263         case REQUEST_PENDING:
264                 _LOGD(
265                                 "Rpm Installer Recovery started. state=%d\n", lastbackstate);
266                 /*Only package downgradation can be the case*/
267                 err = _rpm_installer_package_install(pn, true, "--force", NULL);
268                 if (err != RPM_INSTALLER_SUCCESS &&
269                         err != RPM_INSTALLER_ERR_NEED_USER_CONFIRMATION) {
270                         goto RECOVERYERROR;
271                 }
272                 _LOGD(
273                               " Rpm Installer Recovery ended \n");
274                 _ri_set_backend_state_info(REQUEST_COMPLETED);
275                 break;
276
277         default:
278                 /*
279                  * Unknown state
280                  * No need to recover
281                  */
282                 _LOGD(
283                               " Rpm Installer Recovery Default state \n");
284                 break;
285
286         }
287         if(pn) free(pn);
288         return 0;
289
290  RECOVERYERROR:
291         _LOGE("Error in Recovery error number = (%d)\n",
292                       err);
293         if(pn) free(pn);
294         return err;
295
296 }
297
298 static int __ri_check_root_path(const char *pkgid)
299 {
300         char dirpath[BUF_SIZE] = {'\0'};
301         struct stat stFileInfo;
302
303         snprintf(dirpath, BUF_SIZE, "/usr/apps/%s", pkgid);
304
305         (void)stat(dirpath, &stFileInfo);
306
307         if (S_ISDIR(stFileInfo.st_mode)) {
308                 return 0;       //it menas "/usr/apps/pkgid"
309         }
310         return 1;               //it menas "/opt/usr/apps/pkgid"
311 }
312
313 void __ri_make_directory(const char *pkgid)
314 {
315         char usr_pkg[BUF_SIZE] = {'\0'};
316         char opt_pkg[BUF_SIZE] = {'\0'};
317         int ret = 0;
318
319         snprintf(usr_pkg, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
320         snprintf(opt_pkg, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
321
322         // check author signature
323         if ((access(opt_pkg, R_OK) == 0) || (access(usr_pkg, R_OK) == 0)) {
324                 _LOGE("pkgid[%s] has author-signature",pkgid);
325
326                 // root path
327                 memset(opt_pkg, '\0', BUF_SIZE);
328                 snprintf(opt_pkg, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
329                 if (access(opt_pkg, R_OK) != 0) {
330                         _LOGE("dont have [%s]\n",opt_pkg);
331                         ret = mkdir(opt_pkg, DIRECTORY_PERMISSION_755);
332                         if (ret < 0) {
333                                 _LOGE("directory making is failed.\n");
334                         }else{
335                                 _LOGE("directory[%s] is created", opt_pkg);
336                         }
337                 }
338
339                 // shared
340                 memset(opt_pkg, '\0', BUF_SIZE);
341                 snprintf(opt_pkg, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgid);
342                 if (access(opt_pkg, R_OK) != 0) {
343                         _LOGE("dont have [%s]\n",opt_pkg);
344                         ret = mkdir(opt_pkg, DIRECTORY_PERMISSION_755);
345                         if (ret < 0) {
346                                 _LOGE("directory making is failed.\n");
347                         }else{
348                                 _LOGE("directory[%s] is created", opt_pkg);
349                         }
350                 }
351
352                 // shared/data
353                 memset(opt_pkg, '\0', BUF_SIZE);
354                 snprintf(opt_pkg, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
355                 if (access(opt_pkg, R_OK) != 0) {
356                         _LOGE("dont have [%s]\n",opt_pkg);
357                         ret = mkdir(opt_pkg, DIRECTORY_PERMISSION_755);
358                         if (ret < 0) {
359                                 _LOGE("directory making is failed.\n");
360                         }else{
361                                 _LOGE("directory[%s] is created", opt_pkg);
362                         }
363                 }
364
365                 // shared/trusted
366                 memset(opt_pkg, '\0', BUF_SIZE);
367                 snprintf(opt_pkg, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
368                 if (access(opt_pkg, R_OK) != 0) {
369                         _LOGE("dont have [%s],\n",opt_pkg);
370                         ret = mkdir(opt_pkg, DIRECTORY_PERMISSION_755);
371                         if (ret < 0) {
372                                 _LOGE("directory making is failed.\n");
373                         }else{
374                                 _LOGE("directory[%s] is created", opt_pkg);
375                         }
376                 }
377
378         }
379 }
380
381 static int __ri_process_smack(char *keyid, char *pkgid)
382 {
383         int ret = 0;
384
385         /*apply smack for ug*/
386         if(strcmp(keyid,"ug-smack")==0) {
387                 _LOGD("only apply smack for ug\n");
388                 const char *perm[] = {"http://tizen.org/privilege/appsetting", NULL};
389                 _ri_apply_smack(pkgid,__ri_check_root_path(pkgid));
390                 _ri_privilege_enable_permissions(pkgid, 1, perm, 1);
391         /*apply smack for rpm package*/
392         } else if (strcmp(keyid,"rpm-smack")==0) {
393                 _LOGD("apply smack for rpm");
394                 __ri_make_directory(pkgid);
395                 _ri_apply_smack(pkgid,__ri_check_root_path(pkgid));
396
397         /*soft-reset for rpm package*/
398         } else if (strcmp(keyid,"soft-reset")==0) {
399                 _LOGD("soft-reset\n");
400                 _ri_soft_reset(pkgid);
401
402         /*register xml to db, call pkgmgr parser*/
403         } else if (strcmp(keyid,"core-xml")==0) {
404                 _LOGD("install corexml");
405                 ret = _rpm_installer_corexml_install(pkgid);
406                 if (ret != 0) {
407                         _LOGE("corexml_install failed with err(%d)\n", ret);
408                 } else {
409                         _LOGD("manifest is installed successfully");
410                 }
411         /*apply privilege for rpm package*/
412         } else if (strcmp(keyid,"rpm-perm")==0) {
413                 _LOGD("apply privileges for rpm");
414                 ret = _ri_apply_privilege(pkgid, 0);
415                 if (ret != 0) {
416                         _LOGE("apply privileges failed with err(%d)", ret);
417                 } else {
418                         _LOGD("apply privileges success");
419                 }
420         /*check csc xml*/
421         } else if (strcmp(keyid,"csc-xml")==0) {
422                 _LOGD("csc xml for rpm\n");
423                 ret = _rpm_process_cscxml(pkgid);
424                 if (ret != 0) {
425                         _LOGE("install csc xml failed with err(%d)\n", ret);
426                 } else {
427                         _LOGD("install csc xml success\n");
428                 }
429
430         /*check csc coretpk*/
431         } else if (strcmp(keyid,"csc-core")==0) {
432                 _LOGD("csc for coretpk\n");
433                 ret = _rpm_process_csc_coretpk(pkgid);
434                 if (ret != 0) {
435                         _LOGE("install coretpk csc failed with err(%d)\n", ret);
436                 } else {
437                         _LOGD("install coretpk csc success\n");
438                 }
439
440         /*check fota*/
441         } else if (strcmp(keyid,"rpm-fota")==0) {
442                 _LOGD("fota process for rpm\n");
443                 ret = _rpm_process_fota(pkgid);
444                 if (ret != 0) {
445                         _LOGE("fota process failed with err(%d)\n", ret);
446                 } else {
447                         _LOGD("fota process success\n");
448                 }
449         /*check fota*/
450         } else if (strcmp(keyid,"rpm-rw-fota")==0) {
451                 _LOGD("rw fota process for rpm\n");
452                 ret = _rpm_process_fota_for_rw(pkgid);
453                 if (ret != 0) {
454                         _LOGE("rw fota process failed with err(%d)\n", ret);
455                 } else {
456                         _LOGD("rw fota process success\n");
457                 }
458         } else {
459                 _LOGE("smack cmd error\n");
460                 ret = -1;
461         }
462
463         return ret;
464 }
465
466 int _rpm_backend_interface(char *keyid, char *pkgid, char *reqcommand, char *clientid)
467 {
468         int ret = -1;
469         ri_backend_data data = { 0 };
470         int backendstate;
471         rpmRC rc;
472         if (reqcommand == NULL) {
473                 _LOGE("reqcommand is NULL\n");
474                 return RPM_INSTALLER_ERR_WRONG_PARAM;
475         }
476         if (keyid == NULL || pkgid == NULL) {
477                 if (strncmp(reqcommand, "recover", strlen("recover"))) {
478                         _LOGE(" Either keyid/pkgid is NULL\n");
479                         return RPM_INSTALLER_ERR_WRONG_PARAM;
480                 }
481                 _LOGE(" Either keyid/pkgid is NULL\n");
482                 return RPM_INSTALLER_ERR_WRONG_PARAM;
483         }
484
485         if (strncmp(reqcommand, "install", strlen("install")) == 0) {
486                 data.req_cmd = INSTALL_CMD;
487                 data.cmd_string = strdup("install");
488                 if (data.cmd_string == NULL) {
489                         _LOGE(
490                                "strdup failed due to insufficient memory\n");
491                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
492                 }
493         } else if (strncmp(reqcommand, "remove", strlen("remove")) == 0) {
494                 data.req_cmd = DELETE_CMD;
495                 data.cmd_string = strdup("uninstall");
496                 if (data.cmd_string == NULL) {
497                         _LOGE(
498                                "strdup failed due to insufficient memory\n");
499                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
500                 }
501         } else if (strncmp(reqcommand, "recover", strlen("recover")) == 0) {
502                 data.req_cmd = RECOVER_CMD;
503                 data.cmd_string = strdup("recover");
504                 if (data.cmd_string == NULL) {
505                         _LOGE(
506                                "strdup failed due to insufficient memory\n");
507                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
508                 }
509         } else if (strncmp(reqcommand, "cleardata", strlen("cleardata")) == 0) {
510                 data.req_cmd = CLEARDATA_CMD;
511                 data.cmd_string = strdup("cleardata");
512                 if (data.cmd_string == NULL) {
513                         _LOGE(
514                                "strdup failed due to insufficient memory\n");
515                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
516                 }
517         } else if (strncmp(reqcommand, "move", strlen("move")) == 0) {
518                 data.req_cmd = MOVE_CMD;
519                 data.cmd_string = strdup("move");
520                 if (data.cmd_string == NULL) {
521                         _LOGE(
522                                "strdup failed due to insufficient memory\n");
523                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
524                 }
525         } else if (strncmp(reqcommand, "smack", strlen("smack")) == 0) {
526                 return __ri_process_smack(keyid, pkgid);
527         } else if (strncmp(reqcommand, "eflwgt-install", strlen("eflwgt-install")) == 0) {
528                 data.req_cmd = EFLWGT_INSTALL_CMD;
529                 data.cmd_string = strdup("eflwgt-install");
530                 if (data.cmd_string == NULL) {
531                         _LOGE(
532                                 "strdup failed due to insufficient memory\n");
533                         return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
534                 }
535         } else if (strncmp(reqcommand, "rpm-enable", strlen("rpm-enable")) == 0) {
536                 if (strstr(pkgid, ":") == NULL)
537                         ret = _rpm_process_enable(pkgid);
538                 else
539                         ret = _rpm_process_enabled_list(pkgid);
540                 return ret;
541         } else if (strncmp(reqcommand, "rpm-disable", strlen("rpm-disable")) == 0) {
542                 if (strstr(pkgid, ":") == NULL)
543                         ret = _rpm_process_disable(pkgid);
544                 else
545                         ret = _rpm_process_disabled_list(pkgid);
546                 return ret;
547         } else {
548                 _LOGD("wrong input parameter\n");
549                 _LOGD("%d\n", RPM_INSTALLER_ERR_WRONG_PARAM);
550                 return RPM_INSTALLER_ERR_WRONG_PARAM;
551         }
552
553         data.pkgid = pkgid;
554         backendstate = _ri_get_backend_state();
555
556         rc = rpmReadConfigFiles(NULL, NULL);
557         if (rc == RPMRC_OK) {
558                 _LOGD("Successfully read rpm configuration\n");
559         } else {
560                 _LOGE("Unable to read RPM configuration.\n");
561                 if (data.cmd_string) {
562                         free(data.cmd_string);
563                         data.cmd_string = NULL;
564                 }
565                 return RPM_INSTALLER_ERR_INTERNAL;
566         }
567
568         if (RECOVER_CMD == data.req_cmd) {
569                 if (0 == backendstate) {
570                         int lastbackstate;
571
572                         /* check the current state of backend */
573                         lastbackstate = _ri_get_backend_state_info();
574
575                         if (REQUEST_COMPLETED == lastbackstate) {
576                                 _LOGD(
577                                        " Rpm Installer recovery is in REQUEST_COMPLETED  \n");
578                                 snprintf(scrolllabel, sizeof(scrolllabel),
579                                          "No Recovery Needed");
580                         } else{
581                                 ret = __ri_native_recovery(lastbackstate);
582                                 if (ret == 0)
583                                         snprintf(scrolllabel, sizeof(scrolllabel),
584                                                  "Recovery Success");
585                                 else
586                                         snprintf(scrolllabel, sizeof(scrolllabel),
587                                                 "Recovery Failed");
588                         }
589                         /* set the backend state as completed */
590                         _ri_set_backend_state(1);
591                 } else {
592                         /* nothing to recover */
593                         _LOGD(
594                                " Rpm Installer recovery Nothing to be done\n");
595                         ret = 0;
596                         snprintf(scrolllabel, sizeof(scrolllabel),
597                                  "No Recovery Needed");
598                 }
599                 _LOGD("%d\n", ret);
600                 if (data.cmd_string) {
601                         free(data.cmd_string);
602                         data.cmd_string = NULL;
603                 }
604                 return ret;
605
606         }
607         if (backendstate == 0) {
608
609                 /* Non Recovery case
610                  *
611                  * Another Instance may be running
612                  * or something went wrong in last execution
613                  * Check for it
614                  */
615                 if (__ri_is_another_instance_running(RPM)) {
616                         if (data.pkgid) {
617                                 _ri_broadcast_status_notification
618                                     (data.pkgid, "rpm", "error",
619                                      "Another Instance Running");
620                                 _ri_stat_cb(data.pkgid, "error",
621                                             "Another Instance Running");
622                                 _ri_broadcast_status_notification
623                                     (data.pkgid, "rpm", "end", "fail");
624                                 _ri_stat_cb(data.pkgid, "end",
625                                             "fail");
626                         } else {
627                                 _ri_broadcast_status_notification
628                                     ("unknown", "unknown", "error",
629                                      "Another Instance Running");
630                                 _ri_stat_cb("unknown", "error",
631                                             "Another Instance Running");
632                                 _ri_broadcast_status_notification
633                                     ("unknown", "unknown", "end", "fail");
634                                 _ri_stat_cb("unknown", "end", "fail");
635                         }
636                         _LOGD(
637                                "Request Failed as "
638                                "Another Instance is running \n");
639                         ret = RPM_INSTALLER_ERR_RESOURCE_BUSY;
640                         if (data.cmd_string) {
641                                 free(data.cmd_string);
642                                 data.cmd_string = NULL;
643                         }
644                         return ret;
645                 } else {
646                         int lastbackstate;
647
648                         /* check the current state of backend */
649                         lastbackstate = _ri_get_backend_state_info();
650
651                         /* Publish Notification that backend has started */
652 //                      _ri_broadcast_status_notification(data.pkgid, "rpm", "start", data.cmd_string);
653 //                      _ri_broadcast_status_notification(data.pkgid, "rpm", "command", data.cmd_string);
654
655                         if (REQUEST_COMPLETED == lastbackstate) {
656                                 _LOGD(
657                                        " Rpm Installer recovery"
658                                        " is in REQUEST_COMPLETED  \n");
659                                 ret = 0;
660                         } else
661                                 ret = __ri_native_recovery(lastbackstate);
662                         if (ret != 0) {
663                                 _LOGD(
664                                         "recovery of last request failed\n");
665                         } else {
666                                 _LOGD(
667                                        "recovery of last request success\n");
668                         }
669
670                         /* set the backend state as completed */
671                         _ri_set_backend_state(1);
672                 }
673         }
674
675         /* set the backend state as started for the current request*/
676         _ri_set_backend_state(0);
677
678 #ifdef SEND_PKGPATH
679         gpkgname = strdup(data.pkgid);
680
681         /* Publish Notification that backend has started */
682         if (data.pkgid)
683                 _ri_broadcast_status_notification(data.pkgid, "rpm", "start",
684                                                   data.cmd_string);
685         else
686                 _ri_broadcast_status_notification("unknown", "start",
687                                                   data.cmd_string);
688 #endif
689
690         _ri_set_backend_state_info(REQUEST_ACCEPTED);
691
692         /* Set the input request info */
693         if(data.pkgid == NULL)
694                 return RPM_INSTALLER_ERR_PKG_NOT_FOUND;
695         _ri_save_last_input_info(data.pkgid, data.req_cmd,
696                                          data.force_overwrite);
697
698         switch (data.req_cmd) {
699         case INSTALL_CMD:
700                 {
701                         _LOGD("[%s] --install %s\n",
702                                "backend", data.pkgid);
703 #ifdef SEND_PKGPATH
704                         _ri_broadcast_status_notification(data.pkgid, "rpm",
705                                                           "command", "Install");
706 #endif
707                         if (data.force_overwrite == FORCE_OVERWITE) {
708                                 _LOGD(
709                                        "[%s] --install %s --force-overwrite\n",
710                                        "backend", data.pkgid);
711                                 ret =
712                                     _rpm_installer_package_install
713                                     (data.pkgid, true, "--force", clientid);
714                         } else {
715                                 if(data.pkgid == NULL) {
716                                         _LOGE("pkgid is null");
717                                         break;
718                                 }
719                                 _LOGD("[%s] --install %s\n",
720                                        "backend", data.pkgid);
721                                 ret =
722                                     _rpm_installer_package_install
723                                     (data.pkgid, false, NULL, clientid);
724                         }
725                 }
726                 break;
727         case DELETE_CMD:
728                 {
729                         _LOGD("[%s] uninstall %s\n",
730                                "backend", data.pkgid);
731 #ifdef SEND_PKGPATH
732                         _ri_broadcast_status_notification(data.pkgid, "rpm",
733                                                           "command", "Remove");
734 #endif
735                         ret = __ri_uninstall_package(data.pkgid);
736                         if (ret != 0) {
737                                 _LOGD("remove fail\n");
738                         } else {
739                                 _LOGD("remove success\n");
740                         }
741                 }
742                 break;
743         case CLEARDATA_CMD:
744                 {
745                         _LOGD("[%s] clear data %s\n",
746                                "backend", data.pkgid);
747 #ifdef SEND_PKGPATH
748                         _ri_broadcast_status_notification(data.pkgid, "rpm",
749                                                           "command", "clear");
750 #endif
751                         ret = __ri_clear_private_data(data.pkgid);
752                         if (ret != 0) {
753                                 char *errstr = NULL;
754                                 _ri_error_no_to_string(ret, &errstr);
755                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
756                                                                   "error",
757                                                                   errstr);
758                                 _ri_stat_cb(data.pkgid, "error", errstr);
759                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
760                                                                   "end",
761                                                                   "fail");
762                                 _ri_stat_cb(data.pkgid, "end", "fail");
763                                 _LOGE(
764                                        "clear data failed with err(%d) (%s)\n",
765                                        ret, errstr);
766                         } else {
767                                 _LOGD("clear data success\n");
768                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
769                                                                   "end", "ok");
770                                 _ri_stat_cb(data.pkgid, "end", "ok");
771                         }
772                         break;
773                 }
774         case MOVE_CMD:
775                 {
776                         _LOGD("[%s] move %s\n",
777                                "backend", data.pkgid);
778 #ifdef SEND_PKGPATH
779                         _ri_broadcast_status_notification(data.pkgid, "rpm",
780                                                           "command", "move");
781 #endif
782                         ret = __ri_move_package(data.pkgid, move_type);
783                         if (ret != 0) {
784                                 char *errstr = NULL;
785                                 _ri_error_no_to_string(ret, &errstr);
786                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
787                                                                   "error",
788                                                                   errstr);
789                                 _ri_stat_cb(data.pkgid, "error", errstr);
790                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
791                                                                   "end",
792                                                                   "fail");
793                                 _ri_stat_cb(data.pkgid, "end", "fail");
794                                 _LOGE(
795                                        "move failed with err(%d) (%s)\n",
796                                        ret, errstr);
797                         } else {
798                                 _LOGD("move success\n");
799                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
800                                                                   "end", "ok");
801                                 _ri_stat_cb(data.pkgid, "end", "ok");
802                         }
803                         break;
804                 }
805                 case EFLWGT_INSTALL_CMD:
806                         {
807
808                         _LOGD("[%s] eflwgt-install %s\n",
809                                                    "backend", data.pkgid);
810 #ifdef SEND_PKGPATH
811                         _ri_broadcast_status_notification(data.pkgid, "rpm",
812                                                                   "command", "eflwgt-install");
813 #endif
814                         ret = _rpm_installer_package_install_with_dbpath(data.pkgid, clientid);
815                         if (ret != 0) {
816                                 char *errstr = NULL;
817                                 _ri_error_no_to_string(ret, &errstr);
818                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
819                                                                           "error",
820                                                                           errstr);
821                                 _ri_stat_cb(data.pkgid, "error", errstr);
822                                 sleep(2);
823                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
824                                                                   "end",
825                                                                   "fail");
826                                 _ri_stat_cb(data.pkgid, "end", "fail");
827                                 _LOGE("eflwgt-install failed with err(%d) (%s)\n",
828                                                    ret, errstr);
829                         } else {
830                                 _LOGD("eflwgt-install success\n");
831                                 _ri_broadcast_status_notification(data.pkgid, "rpm",
832                                                                   "end", "ok");
833                                 _ri_stat_cb(data.pkgid, "end", "ok");
834                         }
835                                 _ri_remove_wgt_unzip_dir();
836                                 break;
837                         }
838
839         default:
840                 {
841                         _ri_broadcast_status_notification("unknown", "unknown",
842                                                           "command",
843                                                           "unknown");
844                         _ri_broadcast_status_notification("unknown", "unknown",
845                                                           "error",
846                                                           "not supported");
847                         _ri_stat_cb("unknown", "error", "not supported");
848                         _ri_broadcast_status_notification("unknown", "unknown",
849                                                           "end", "fail");
850                         _ri_stat_cb("unknown", "end", "fail");
851                         _LOGE("unknown command \n");
852                         ret = RPM_INSTALLER_ERR_WRONG_PARAM;
853                 }
854         }
855
856         if (gpkgname) {
857                 free(gpkgname);
858                 gpkgname = NULL;
859         }
860
861         if (data.cmd_string) {
862                 free(data.cmd_string);
863                 data.cmd_string = NULL;
864         }
865
866         if (_ri_get_backend_state_info() != REQUEST_PENDING) {
867                 _ri_set_backend_state_info(REQUEST_COMPLETED);
868                 /* set the backend state as completed */
869                 _ri_set_backend_state(1);
870                 _LOGD("%d\n", ret);
871         }
872         return ret;
873 }