Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / aul_running_context.c
1 /*
2  * Copyright (c) 2019 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 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <bundle_internal.h>
22 #include <glib.h>
23
24 #include "aul_api.h"
25 #include "aul_cmd.h"
26 #include "aul_error.h"
27 #include "aul_running_context.h"
28 #include "aul_sock.h"
29 #include "aul_util.h"
30
31 struct aul_running_context_s {
32         char *app_id;
33         char *comp_id;
34         char *inst_id;
35         int pid;
36 };
37
38 static void __destroy_running_context(gpointer data)
39 {
40         struct aul_running_context_s *context = data;
41
42         free(context->inst_id);
43         free(context->comp_id);
44         free(context->app_id);
45         free(context);
46 }
47
48 static struct aul_running_context_s *__create_running_context(bundle *b)
49 {
50         struct aul_running_context_s *context;
51         const char *val;
52
53         context = calloc(1, sizeof(struct aul_running_context_s));
54         if (!context) {
55                 _E("Out of memory");
56                 return NULL;
57         }
58
59         val = bundle_get_val(b, AUL_K_APPID);
60         if (val)
61                 context->app_id = strdup(val);
62
63         val = bundle_get_val(b, AUL_K_COMPONENT_ID);
64         if (val)
65                 context->comp_id = strdup(val);
66
67         val = bundle_get_val(b, AUL_K_INSTANCE_ID);
68         if (val)
69                 context->inst_id = strdup(val);
70
71         val = bundle_get_val(b, AUL_K_PID);
72         if (val)
73                 context->pid = atoi(val);
74
75         return context;
76 }
77
78 static bundle *__create_bundle(const char *app_id,
79                 const char *comp_id,
80                 const char *inst_id)
81 {
82         bundle *b;
83
84         b = bundle_create();
85         if (!b) {
86                 _E("Out of memory");
87                 return NULL;
88         }
89
90         bundle_add(b, AUL_K_APPID, app_id);
91
92         if (comp_id)
93                 bundle_add(b, AUL_K_COMPONENT_ID, comp_id);
94
95         if (inst_id)
96                 bundle_add(b, AUL_K_INSTANCE_ID, inst_id);
97
98         return b;
99 }
100
101 API int aul_running_context_create(const char *app_id,
102                 const char *comp_id,
103                 const char *inst_id,
104                 aul_running_context_h *handle)
105 {
106         struct aul_running_context_s *context;
107         app_pkt_t *pkt = NULL;
108         bundle *b;
109         int ret;
110         int fd;
111
112         if (!app_id || !handle) {
113                 _E("Invalid parameter");
114                 return AUL_R_EINVAL;
115         }
116
117         b = __create_bundle(app_id, comp_id, inst_id);
118         if (!b)
119                 return AUL_R_ENOMEM;
120
121         fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
122                         APP_GET_RUNNING_CONTEXT,
123                         b, AUL_SOCK_ASYNC);
124         bundle_free(b);
125         if (fd <= 0) {
126                 _E("Failed to send the request. error(%d)", fd);
127                 return aul_error_convert(fd);
128         }
129
130         ret = aul_sock_recv_reply_pkt(fd, &pkt);
131         if (ret < 0 || pkt == NULL) {
132                 _E("Failed to get reply. error(%d)", ret);
133                 return aul_error_convert(ret);
134         }
135
136         if (pkt->cmd != APP_GET_INFO_OK) {
137                 _E("Failed to get running context. error(%d)", pkt->cmd);
138                 ret = aul_error_convert(pkt->cmd);
139                 free(pkt);
140                 return ret;
141         }
142
143         if (!(pkt->opt & AUL_SOCK_BUNDLE)) {
144                 _E("Invalid protocol");
145                 free(pkt);
146                 return AUL_R_ERROR;
147         }
148
149         b = bundle_decode(pkt->data, pkt->len);
150         free(pkt);
151         if (!b) {
152                 _E("Failed to decode bundle data");
153                 return AUL_R_ERROR;
154         }
155
156         context = __create_running_context(b);
157         bundle_free(b);
158         if (!context) {
159                 _E("Failed to create running context");
160                 return AUL_R_ENOMEM;
161         }
162
163         *handle = context;
164
165         return AUL_R_OK;
166 }
167
168 API int aul_running_context_destroy(aul_running_context_h handle)
169 {
170         if (!handle) {
171                 _E("Invalid parameter");
172                 return AUL_R_EINVAL;
173         }
174
175         __destroy_running_context(handle);
176
177         return AUL_R_OK;
178 }
179
180 API int aul_running_context_get_app_id(aul_running_context_h handle,
181                 const char **app_id)
182 {
183         if (!handle || !app_id) {
184                 _E("Invalid parameter");
185                 return AUL_R_EINVAL;
186         }
187
188         *app_id = handle->app_id;
189
190         return AUL_R_OK;
191 }
192
193 API int aul_running_context_get_comp_id(aul_running_context_h handle,
194                 const char **comp_id)
195 {
196         if (!handle || !comp_id) {
197                 _E("Invalid parameter");
198                 return AUL_R_EINVAL;
199         }
200
201         *comp_id = handle->comp_id;
202
203         return AUL_R_OK;
204 }
205
206 API int aul_running_context_get_inst_id(aul_running_context_h handle,
207                 const char **inst_id)
208 {
209         if (!handle || !inst_id) {
210                 _E("Invalid parameter");
211                 return AUL_R_EINVAL;
212         }
213
214         *inst_id = handle->inst_id;
215
216         return AUL_R_OK;
217 }
218
219 API int aul_running_context_get_pid(aul_running_context_h handle,
220                 int *pid)
221 {
222         if (!handle || !pid) {
223                 _E("Invalid parameter");
224                 return AUL_R_EINVAL;
225         }
226
227         *pid = handle->pid;
228
229         return AUL_R_OK;
230 }