Add some conditions for app disable/enable 61/117061/1
authorJunghyun Yeon <jungh.yeon@samsung.com>
Mon, 26 Dec 2016 11:42:42 +0000 (20:42 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 2 Mar 2017 12:27:14 +0000 (21:27 +0900)
- Codes will check certain app is installed as global app or not
and call internal api with different target uid
- Add usage about enable/disable pkg/app at help message

Change-Id: Ic1353e0468d661b3e78f05bab3ca54c638dee0be
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/pkg_cmd.c

index 9e8ee3b..50c4cda 100644 (file)
@@ -56,6 +56,7 @@
 
 #define OPTVAL_GLOBAL 1000
 #define OPTVAL_CLEAR_ALL 1001
+#define OPTVAL_UID 1002
 
 static int __process_request(uid_t uid);
 static void __print_usage();
@@ -101,6 +102,7 @@ const struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"debug-mode", 0, NULL, 'G'},
        {"getsizeinfo", 0, NULL, 'x'},
+       {"uid", 1, NULL, OPTVAL_UID},
        {0, 0, 0, 0}            /* sentinel */
 };
 
@@ -145,6 +147,7 @@ struct pm_tool_args_t {
        int global;
        int type;
        int result;
+       int uid;
        bool debug_mode;
 };
 typedef struct pm_tool_args_t pm_tool_args;
@@ -419,9 +422,12 @@ static void __print_usage()
        printf("-t, --package-type      provide package type\n");
        printf("-T, --move-type         provide move type [0: move to internal / 1: move to external]\n");
        printf("    --global            Global Mode [Warning: user should be privilegied to use this mode]\n");
+       printf("    --uid               Specify target user's id. This only affect app activation/deactivation.\n");
        printf("-e, --tep-path          provide TEP package path\n");
        printf("-M, --tep-move          decide move/copy of TEP package [0: copy TEP package / 1: move TEP package]\n");
        printf("-G, --debug-mode        install the package with debug mode for sdk\n");
+       printf("-D, --deactivate        disable package or app\n");
+       printf("-A, --activate          enable package or app\n");
        printf("-h, --help              print this help\n");
 
        printf("\n");
@@ -439,6 +445,7 @@ static void __print_usage()
        printf("pkgcmd -k -n <pkgid>\n");
        printf("pkgcmd --clear-all (-t <pkg type>)\n");
        printf("pkgcmd -X <old_pkg> -Y <new_pkg> -Z <delta_pkg>\n");
+       printf("pkgcmd -D -t <pkg type> -n <pkgid> (--global) (--uid <uid>)\n");
 
        printf("\n");
        printf("Example:\n");
@@ -456,6 +463,8 @@ static void __print_usage()
        printf("pkgcmd -l\n");
        printf("pkgcmd -l -t tpk\n");
        printf("pkgcmd -g -T 0 -n org.example.hello\n");
+       printf("pkgcmd -D -t tpk -n org.example.hellopkg\n");
+       printf("pkgcmd -D -t app -n org.example.helloapp --global\n");
 
        printf("\n");
        exit(0);
@@ -591,6 +600,7 @@ static int __process_request(uid_t target_uid)
        int ret = -1;
        pkgmgr_client *pc = NULL;
        pkgmgr_client *listen_pc = NULL;
+       pkgmgrinfo_appinfo_h appinfo_h;
        char buf[1024] = {'\0'};
        int pid = -1;
        char pkg_old[PATH_MAX] = {0, };
@@ -872,13 +882,40 @@ static int __process_request(uid_t target_uid)
                }
 
                if (strcmp(data.pkg_type, "app") == 0) {
-                       if (data.global)
-                               /* enable global app for this user only */
-                               ret = pkgmgr_client_activate_global_app_for_uid(pc, data.pkgid,
-                                       __app_return_cb, NULL, __get_current_user_id());
-                       else
-                               /* enable app which belongs to this user */
-                               ret = pkgmgr_client_usr_activate_app(pc, data.pkgid, __app_return_cb, NULL, target_uid);
+                       if (data.global && data.uid != -1) {
+                               if (data.uid != __get_current_user_id()) {
+                                       printf("Invalid uid : %d\n", data.uid);
+                                       ret = -1;
+                                       break;
+                               } else {
+                                       target_uid = data.uid;
+                               }
+                       }
+                       ret = pkgmgrinfo_appinfo_get_usr_disabled_appinfo(data.pkgid,
+                                       target_uid, &appinfo_h);
+                       if (ret != PMINFO_R_OK) {
+                               printf("Failed to get appinfo[%s]\n", data.pkgid);
+                               ret = -1;
+                               break;
+                       }
+
+                       if (data.global) {
+                               if (data.uid != -1)
+                                       /* enable global app for this user only */
+                                       ret = pkgmgr_client_activate_global_app_for_uid(pc,
+                                                       data.pkgid, __app_return_cb, NULL,
+                                                       __get_current_user_id());
+                               else
+                                       /* enable global app for all user */
+                                       ret = pkgmgr_client_usr_activate_app(pc, data.pkgid,
+                                                       __app_return_cb, NULL, GLOBAL_USER);
+                       } else {
+                               /* enable local app */
+                               ret = pkgmgr_client_usr_activate_app(pc, data.pkgid,
+                                               __app_return_cb, NULL, target_uid);
+                       }
+
+                       pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
                } else {
                        listen_pc = pkgmgr_client_new(PC_LISTENING);
                        if (listen_pc == NULL) {
@@ -891,7 +928,8 @@ static int __process_request(uid_t target_uid)
                                printf("Failed to set callback[%d]\n", ret);
                                break;
                        }
-                       ret = pkgmgr_client_usr_activate(pc, data.pkg_type, data.pkgid, target_uid);
+                       ret = pkgmgr_client_usr_activate(pc, data.pkg_type, data.pkgid,
+                                       target_uid);
                }
                if (ret < 0)
                        break;
@@ -917,13 +955,37 @@ static int __process_request(uid_t target_uid)
                }
 
                if (strcmp(data.pkg_type, "app") == 0) {
-                       if (data.global)
-                               /* disable global app for this user only*/
-                               ret = pkgmgr_client_deactivate_global_app_for_uid(pc, data.pkgid,
-                                       __app_return_cb, NULL, __get_current_user_id());
-                       else
-                               /* disable app which belongs to this user */
-                               ret = pkgmgr_client_usr_deactivate_app(pc, data.pkgid, __app_return_cb, NULL, target_uid);
+                       ret = pkgmgrinfo_appinfo_get_usr_appinfo(data.pkgid,
+                                       target_uid, &appinfo_h);
+                       if (ret != PMINFO_R_OK) {
+                               printf("Failed to get appinfo[%s]\n", data.pkgid);
+                               ret = -1;
+                               break;
+                       }
+
+                       if (data.global) {
+                               if (data.uid != -1 && data.uid != getuid()) {
+                                       printf("Invalid uid : %d\n", data.uid);
+                                       pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
+                                       ret = -1;
+                                       break;
+                               }
+                               if (data.uid != -1)
+                                       /* disable global app for this user only */
+                                       ret = pkgmgr_client_deactivate_global_app_for_uid(pc,
+                                                       data.pkgid, __app_return_cb, NULL,
+                                                       __get_current_user_id());
+                               else
+                                       /* disable global app for all user */
+                                       ret = pkgmgr_client_usr_deactivate_app(pc, data.pkgid,
+                                                       __app_return_cb, NULL, GLOBAL_USER);
+                       } else {
+                               /* disable local app */
+                               ret = pkgmgr_client_usr_deactivate_app(pc, data.pkgid,
+                                               __app_return_cb, NULL, target_uid);
+                       }
+
+                       pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
                } else {
                        listen_pc = pkgmgr_client_new(PC_LISTENING);
                        if (listen_pc == NULL) {
@@ -936,7 +998,8 @@ static int __process_request(uid_t target_uid)
                                printf("Failed to set callback[%d]\n", ret);
                                break;
                        }
-                       ret = pkgmgr_client_usr_deactivate(pc, data.pkg_type, data.pkgid, target_uid);
+                       ret = pkgmgr_client_usr_deactivate(pc, data.pkg_type, data.pkgid,
+                                       target_uid);
                }
                if (ret < 0)
                        break;
@@ -1211,6 +1274,7 @@ int main(int argc, char *argv[])
        data.global = 0; /* By default pkg_cmd will manage for the current user */
        data.result = 0;
        data.type = -1;
+       data.uid = -1;
        while (1) {
                c = getopt_long(argc, argv, short_options, long_options,
                                &opt_idx);
@@ -1398,6 +1462,10 @@ int main(int argc, char *argv[])
                case ':':  /* */
                        break;
 
+               case OPTVAL_UID: /* specify target user id */
+                       data.uid = atoi(optarg);
+                       break;
+
                default:
                        break;