Adjust some log levels
[platform/core/appfw/pkgmgr-server.git] / src / signal.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21
22 #include <systemd/sd-login.h>
23
24 #include <pkgmgr_installer.h>
25 #include <tzplatform_config.h>
26
27 #include "pkgmgr-server.h"
28
29 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
30
31 static int __get_uid_list(uid_t target_uid, uid_t **uids)
32 {
33         int n;
34
35         if (target_uid != GLOBAL_USER) {
36                 *uids = malloc(sizeof(uid_t));
37                 if (*uids == NULL) {
38                         ERR("out of memory");
39                         return 0;
40                 }
41                 (*uids)[0] = target_uid;
42                 n = 1;
43         } else {
44                 n = sd_get_uids(uids);
45                 if (n < 0) {
46                         ERR("cannot get login user list");
47                         return 0;
48                 }
49         }
50
51         return n;
52 }
53
54 static void __free_uid_list(uid_t *uids)
55 {
56         free(uids);
57 }
58
59 void _send_app_signal(uid_t uid, const char *req_id,
60                 const char *pkgid, const char *appid,
61                 const char *key, const char *val, int req_type)
62 {
63         pkgmgr_installer *pi;
64         uid_t *uids = NULL;
65         int n;
66         int i;
67
68         pi = pkgmgr_installer_new();
69         if (!pi) {
70                 ERR("Failure in creating the pkgmgr_installer object");
71                 return;
72         }
73
74         if (pkgmgr_installer_set_uid(pi, uid))
75                 goto catch;
76
77         switch (req_type) {
78         case REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID:
79         case REQUEST_TYPE_ENABLE_APP:
80                 if (pkgmgr_installer_set_request_type(pi,
81                                         PKGMGR_REQ_ENABLE_APP))
82                         goto catch;
83                 break;
84         case REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID:
85         case REQUEST_TYPE_DISABLE_APP:
86                 if (pkgmgr_installer_set_request_type(pi,
87                                         PKGMGR_REQ_DISABLE_APP))
88                         goto catch;
89                 break;
90         case REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN:
91                 if (pkgmgr_installer_set_request_type(pi,
92                                         PKGMGR_REQ_ENABLE_APP_SPLASH_SCREEN))
93                         goto catch;
94                 break;
95         case REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN:
96                 if (pkgmgr_installer_set_request_type(pi,
97                                         PKGMGR_REQ_DISABLE_APP_SPLASH_SCREEN))
98                         goto catch;
99                 break;
100         default:
101                 ERR("Unsupported req_type[%d]", req_type);
102                 goto catch;
103         }
104
105         if (pkgmgr_installer_set_session_id(pi, req_id))
106                 goto catch;
107         pkgmgr_installer_send_app_signal(pi, "app", pkgid, appid, key, val);
108         n = __get_uid_list(uid, &uids);
109         for (i = 0; i < n; i++)
110                 pkgmgr_installer_send_app_signal_for_uid(pi, uids[i], "app",
111                                 pkgid, appid, key, val);
112
113 catch:
114         __free_uid_list(uids);
115         pkgmgr_installer_free(pi);
116
117         return;
118 }
119
120 void _send_fail_signal(struct backend_job *job)
121 {
122         int req_type;
123         pkgmgr_installer *pi;
124         uid_t *uids = NULL;
125         int n;
126         int i;
127
128         pi = pkgmgr_installer_new();
129         if (!pi) {
130                 ERR("Failure in creating the pkgmgr_installer object");
131                 return;
132         }
133         pkgmgr_installer_set_session_id(pi, job->req_id);
134         switch (job->req_type) {
135         case REQUEST_TYPE_INSTALL:
136         case REQUEST_TYPE_MOUNT_INSTALL:
137         case REQUEST_TYPE_REINSTALL:
138                 req_type = PKGMGR_REQ_INSTALL;
139                 break;
140         case REQUEST_TYPE_UNINSTALL:
141                 req_type = PKGMGR_REQ_UNINSTALL;
142                 break;
143         case REQUEST_TYPE_MOVE:
144                 req_type = PKGMGR_REQ_MOVE;
145                 break;
146         case REQUEST_TYPE_GETSIZE:
147                 req_type = PKGMGR_REQ_GETSIZE;
148                 break;
149         case REQUEST_TYPE_DISABLE_PKG:
150                 req_type = PKGMGR_REQ_DISABLE_PKG;
151                 break;
152         case REQUEST_TYPE_ENABLE_PKG:
153                 req_type = PKGMGR_REQ_ENABLE_PKG;
154                 break;
155         default:
156                 req_type = PKGMGR_REQ_INVALID;
157                 break;
158         }
159         pkgmgr_installer_set_request_type(pi, req_type);
160         pkgmgr_installer_send_signal(pi, job->backend_type, job->pkgid,
161                         "end", "fail");
162         n = __get_uid_list(job->target_uid, &uids);
163         for (i = 0; i < n; i++)
164                 pkgmgr_installer_send_signal_for_uid(pi, uids[i],
165                                 job->backend_type, job->pkgid, "end", "fail");
166
167         __free_uid_list(uids);
168         pkgmgr_installer_free(pi);
169         return;
170 }