summaryrefslogtreecommitdiff
path: root/src/status.c
blob: 4439ff3cfacd1a5ad3fcc9c7e6a1504790d10319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

#include <aul.h>
#include <bundle.h>
#include <bundle_internal.h>

#include "aul_util.h"
#include "aul_sock.h"
#include "aul_api.h"
#include "launch.h"
#include "aul_app_com.h"

typedef struct _app_status_cb_info_t {
	int (*handler)(int status, void *data);
	void *data;
} app_status_cb_info_t;

struct status_listen_s {
	aul_app_com_connection_h conn;
	app_status_cb callback;
	void *user_data;
};

static int app_status = STATUS_LAUNCHING;
static GList *app_status_cb_list;

API int aul_status_update(int status)
{
	int ret;
	app_status_cb_info_t *cb;
	GList *iter;

	app_status = status;

	ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(), APP_STATUS_UPDATE,
		(unsigned char *)&status, sizeof(status), AUL_SOCK_NOREPLY);

	if (!ret) {
		iter = g_list_first(app_status_cb_list);
		while (iter) {
			cb = (app_status_cb_info_t *)iter->data;
			iter = g_list_next(iter);
			if (cb && cb->handler) {
				if (cb->handler(app_status, cb->data) < 0) {
					app_status_cb_list = g_list_remove(
							app_status_cb_list, cb);
					free(cb);
				}
			}
		}
	}

	return ret;
}

API int aul_app_get_status_bypid(int pid)
{
	return aul_app_get_status_bypid_for_uid(pid, getuid());
}

API int aul_app_get_status_bypid_for_uid(int pid, uid_t uid)
{
	int ret;
	char buf[MAX_PID_STR_BUFSZ];
	bundle *b;

	if (pid == getpid())
		return app_status;

	b = bundle_create();
	if (b == NULL) {
		_E("out of memory");
		return AUL_R_ERROR;
	}

	snprintf(buf, sizeof(buf), "%d", pid);
	bundle_add(b, AUL_K_PID, buf);
	snprintf(buf, sizeof(buf), "%d", uid);
	bundle_add(b, AUL_K_TARGET_UID, buf);

	ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_STATUS,
			b, AUL_SOCK_NONE);
	bundle_free(b);

	return ret;
}

API int aul_app_get_status(const char *appid)
{
	return aul_app_get_status_for_uid(appid, getuid());
}

API int aul_app_get_status_for_uid(const char *appid, uid_t uid)
{
	int ret;
	bundle *kb;
	char buf[MAX_PID_STR_BUFSZ];

	if (appid == NULL)
		return AUL_R_EINVAL;

	kb = bundle_create();
	if (kb == NULL) {
		_E("out of memory");
		return AUL_R_ERROR;
	}

	snprintf(buf, sizeof(buf), "%d", uid);
	bundle_add(kb, AUL_K_APPID, appid);
	bundle_add(kb, AUL_K_TARGET_UID, buf);
	ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid,
			APP_GET_STATUS_BY_APPID, kb);
	bundle_free(kb);

	return ret;
}

API int aul_add_status_local_cb(int (*func)(int status, void *data), void *data)
{
	app_status_cb_info_t *cb;
	GList *iter;

	if (func == NULL)
		return -1;

	iter = g_list_first(app_status_cb_list);
	while (iter) {
		cb = (app_status_cb_info_t *)iter->data;
		if (cb && cb->handler == func && cb->data == data) {
			_D("Already exists");
			return 0;
		}

		iter = g_list_next(iter);
	}

	cb = (app_status_cb_info_t *)malloc(sizeof(app_status_cb_info_t));
	if (cb == NULL) {
		_E("out of memory");
		return -1;
	}

	cb->handler = func;
	cb->data = data;

	app_status_cb_list = g_list_append(app_status_cb_list, cb);

	return 0;
}

API int aul_remove_status_local_cb(int (*func)(int status, void *data), void *data)
{
	app_status_cb_info_t *cb;
	GList *iter;

	iter = g_list_first(app_status_cb_list);
	while (iter) {
		cb = (app_status_cb_info_t *)iter->data;
		iter = g_list_next(iter);
		if (cb && cb->handler == func && cb->data == data) {
			app_status_cb_list = g_list_remove(app_status_cb_list, cb);
			free(cb);
			return 0;
		}
	}

	return -1;
}

API int aul_invoke_status_local_cb(int status)
{
	app_status_cb_info_t *cb;
	GList *iter;

	iter = g_list_first(app_status_cb_list);
	while (iter) {
		cb = (app_status_cb_info_t *)iter->data;
		iter = g_list_next(iter);
		if (cb && cb->handler) {
			if (cb->handler(status, cb->data) < 0) {
				app_status_cb_list = g_list_remove(
						app_status_cb_list, cb);
				free(cb);
			}
		}
	}

	return 0;
}

API int aul_running_list_update(char *appid, char *app_path, char *pid)
{
	int ret;
	bundle *kb;

	kb = bundle_create();

	bundle_add(kb, AUL_K_APPID, appid);
	bundle_add(kb, AUL_K_EXEC, app_path);
	bundle_add(kb, AUL_K_PID, pid);

	ret = app_send_cmd(AUL_UTIL_PID, APP_RUNNING_LIST_UPDATE, kb);

	if (kb != NULL)
			bundle_free(kb);

	return ret;
}

API int aul_set_process_group(int owner_pid, int child_pid)
{
	int ret = -1;
	bundle *kb = NULL;
	char pid_buf[MAX_PID_STR_BUFSZ] = {0,};

	kb = bundle_create();

	if (kb == NULL)
		return -1;

	snprintf(pid_buf, MAX_PID_STR_BUFSZ, "%d", owner_pid);
	bundle_add(kb, AUL_K_OWNER_PID, pid_buf);
	snprintf(pid_buf, MAX_PID_STR_BUFSZ, "%d", child_pid);
	bundle_add(kb, AUL_K_CHILD_PID, pid_buf);
	ret = app_send_cmd(AUL_UTIL_PID, APP_SET_PROCESS_GROUP, kb);
	bundle_free(kb);

	return ret;
}

static int __app_status_event_cb(const char *endpoint, aul_app_com_result_e res,
		bundle *envelope, void *user_data)
{
	struct status_listen_s *listen = (struct status_listen_s *)user_data;
	aul_app_info app_info = { 0, };
	const char *val;
	int context_status;

	if (listen == NULL) {
		_E("Critical error");
		return -1;
	}

	bundle_get_str(envelope, AUL_K_APPID, &app_info.appid);
	if (app_info.appid == NULL) {
		_E("Failed to get application id");
		return -1;
	}

	bundle_get_str(envelope, AUL_K_PKGID, &app_info.pkgid);
	if (app_info.pkgid == NULL) {
		_E("Failed to get package id");
		return -1;
	}

	bundle_get_str(envelope, AUL_K_EXEC, &app_info.app_path);
	if (app_info.app_path == NULL) {
		_E("Failed to get app path");
		return -1;
	}

	bundle_get_str(envelope, AUL_K_INSTANCE_ID, &app_info.instance_id);

	val = bundle_get_val(envelope, AUL_K_PID);
	if (val == NULL) {
		_E("Failed to get pid");
		return -1;
	}
	app_info.pid = atoi(val);

	val = bundle_get_val(envelope, AUL_K_STATUS);
	if (val == NULL) {
		_E("Failed to get status");
		return -1;
	}
	app_info.status = atoi(val);

	val = bundle_get_val(envelope, AUL_K_IS_SUBAPP);
	if (val == NULL) {
		_E("Failed to get is_subapp");
		return -1;
	}
	app_info.is_sub_app = atoi(val);

	val = bundle_get_val(envelope, "__CONTEXT_STATUS__");
	if (val == NULL) {
		_E("Failed to get context status");
		return -1;
	}
	context_status = atoi(val);

	listen->callback(&app_info, context_status, listen->user_data);

	return 0;
}

API int aul_listen_app_status(const char *appid, app_status_cb callback,
		void *data, status_listen_h *handle)
{
	struct status_listen_s *listen;
	char endpoint[128];

	if (appid == NULL || callback == NULL || handle == NULL) {
		_E("Invalid parameter");
		return AUL_R_EINVAL;
	}

	listen = calloc(1, sizeof(struct status_listen_s));
	if (listen == NULL) {
		_E("Out of memory");
		return AUL_R_ERROR;
	}

	snprintf(endpoint, sizeof(endpoint), "app_status_event:%s:%d",
			appid, getuid());
	aul_app_com_create(endpoint, NULL, __app_status_event_cb,
			listen, &listen->conn);
	if (listen->conn == NULL) {
		_E("Failed to create app com");
		free(listen);
		return AUL_R_ERROR;
	}

	listen->callback = callback;
	listen->user_data = data;

	*handle = listen;

	return AUL_R_OK;
}