summaryrefslogtreecommitdiff
path: root/src/status.c
blob: 42d3c51dd7eebf1513f2792e97eb68ed675e3ddb (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
/*
 * 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"

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

static int (*_aul_status_listen_handler)(const char *appid, const char *pkgid,
		int pid, int status, int is_sub_app, void *data);
static void *_aul_status_listen_data;

static app_status_cb_info_t *app_status_cb;
static int app_status = STATUS_LAUNCHING;
extern int aul_launch_fini();

API int aul_status_update(int status)
{
	int ret;
	app_status_cb_info_t *cb = app_status_cb;

	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) {
		while (cb) {
			if (cb->handler) {
				if (cb->handler(app_status, cb->data) < 0)
					aul_remove_status_local_cb(cb->handler, cb->data);
			}

			cb = cb->next;
		}
	}

	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;

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

	ret = aul_sock_send_raw(AUL_UTIL_PID, uid, APP_GET_STATUS,
			(unsigned char *)&pid, sizeof(pid), AUL_SOCK_NONE);

	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;

	if (appid == NULL)
		return AUL_R_EINVAL;

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

	bundle_add(kb, AUL_K_APPID, appid);
	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 = app_status_cb;

	if (func == NULL)
		return -1;

	/* check known callback */
	while (cb) {
		if (cb && cb->handler == func && cb->data == data) {
			/* already in list */
			return 0;
		}
		cb = cb->next;
	}

	cb = (app_status_cb_info_t *)malloc(sizeof(app_status_cb_info_t));
	if (cb == NULL)
		return -1;

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

	cb->next = app_status_cb;
	app_status_cb = cb;

	return 0;
}

API int aul_remove_status_local_cb(int (*func)(int status, void *data), void *data)
{
	app_status_cb_info_t *cb = app_status_cb;
	app_status_cb_info_t *tmp = NULL;

	if (app_status_cb
		 && app_status_cb->handler == func
		 && app_status_cb->data == data) {
		cb = app_status_cb->next;
		free(app_status_cb);
		app_status_cb = cb;
		return 0;
	}

	while (cb) {
		if (cb->next
			 && cb->next->handler == func
			 && cb->next->data == data) {
			tmp = cb->next->next;
			free(cb->next);
			cb->next = tmp;
			return 0;
		}

		cb = cb->next;
	}

	return -1;
}

API int aul_invoke_status_local_cb(int status)
{
	app_status_cb_info_t *cb = app_status_cb;

	while (cb) {
		if (cb->handler) {
			if (cb->handler(status, cb->data) < 0)
				aul_remove_status_local_cb(cb->handler, cb->data);
		}

		cb = cb->next;
	}

	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;
}

void app_status_event(bundle *kb)
{
	const char *appid;
	const char *pkgid;
	const char *val;
	int pid = -1;
	int status = -1;
	int is_subapp = -1;

	if (kb == NULL) {
		_E("Invalid parameter");
		return;
	}

	appid = bundle_get_val(kb, AUL_K_APPID);
	pkgid = bundle_get_val(kb, AUL_K_PKGID);
	val = bundle_get_val(kb, AUL_K_PID);
	if (val)
		pid = atoi(val);
	val = bundle_get_val(kb, AUL_K_STATUS);
	if (val)
		status = atoi(val);
	val = bundle_get_val(kb, AUL_K_IS_SUBAPP);
	if (val)
		is_subapp = atoi(val);

	if (appid == NULL || pkgid == NULL ||
			pid == -1 || status == -1 || is_subapp == -1) {
		_E("Failed to get app status info");
		return;
	}

	if (_aul_status_listen_handler) {
		_aul_status_listen_handler(appid, pkgid, pid, status,
				is_subapp, _aul_status_listen_data);
	}
}

API int aul_listen_app_status(const char *appid,
		int (*aul_handler)(const char *appid, const char *pkgid,
			int pid, int status, int is_subapp, void *data),
		void *data)
{
	int ret;
	bundle *kb;
	int initialized = 0;

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

	if (!aul_is_initialized()) {
		if (aul_launch_init(NULL, NULL) < 0)
			return AUL_R_ENOINIT;
		initialized = 1;
	}

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

	bundle_add(kb, AUL_K_APPID, appid);
	ret = app_send_cmd(AUL_UTIL_PID, APP_LISTEN_STATUS, kb);
	bundle_free(kb);

	_aul_status_listen_handler = aul_handler;
	_aul_status_listen_data = data;

	return ret;
}