summaryrefslogtreecommitdiff
path: root/am_daemon/amd_appinfo.c
blob: 0c520b8fad01b914cd11d0eba617cb31c1f36111 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <glib.h>
#include <dirent.h>

#include <pkgmgr-info.h>
#include <vconf.h>
#include "amd_config.h"
#include "simple_util.h"
#include "amd_appinfo.h"


#define SERVICE_GROUP "Service"

struct appinfomgr {
	GHashTable *tbl; /* key is filename, value is struct appinfo */
};

enum _appinfo_idx {
	_AI_FILE = 0, /* service filename */
	_AI_NAME,
	_AI_COMP,
	_AI_EXEC,
	_AI_TYPE,
	_AI_ONBOOT,
	_AI_RESTART,
	_AI_MULTI,
	_AI_HWACC,
	_AI_PERM,
	_AI_PKGID,
	_AI_PRELOAD,
	_AI_MAX,
};
#define _AI_START _AI_NAME /* start index */

struct appinfo_t {
	char *name;
	enum appinfo_type type;
};

static struct appinfo_t _appinfos[] = {
	[_AI_NAME] = { "Name", AIT_NAME, },
	[_AI_COMP] = { "Component", AIT_COMP, },
	[_AI_EXEC] = { "Exec", AIT_EXEC, },
	[_AI_TYPE] = { "PkgType", AIT_TYPE, },
	[_AI_ONBOOT] = { "StartOnBoot", AIT_ONBOOT, },
	[_AI_RESTART] = { "AutoRestart", AIT_RESTART, },
	[_AI_MULTI] = { "Multiple", AIT_MULTI, },
	[_AI_HWACC] = { "Hwacceleration", AIT_HWACC, },
	[_AI_PERM] = { "PermissionType", AIT_PERM, },
	[_AI_PKGID] = { "PackageId", AIT_PKGID, },
	[_AI_PRELOAD] = { "Preload", AIT_PRELOAD, },
};

struct appinfo {
	char *val[_AI_MAX];
};

int gles = 1;

static void _free_appinfo(gpointer data)
{
	struct appinfo *c = data;
	int i;

	if (!c)
		return;

	for (i = 0; i < sizeof(c->val)/sizeof(c->val[0]); i++)
		free(c->val[i]);

	free(c);
}

static void _fini(struct appinfomgr *cf)
{
	assert(cf);

	g_hash_table_destroy(cf->tbl);
	free(cf);
}

static int __app_info_insert_handler (const pkgmgrinfo_appinfo_h handle, void *data)
{
	struct appinfo *c;
	struct appinfomgr *cf = (struct appinfomgr *)data;
	gboolean r;
	char *exec;
	char *type;
	char *appid;
	char *pkgid;
	bool multiple;
	bool onboot;
	bool restart;
	pkgmgrinfo_app_hwacceleration hwacc;
	pkgmgrinfo_app_component component;
	pkgmgrinfo_permission_type permission;
	int ret = -1;
	bool preload;

	if (!handle) {
		_E("null app handle");
		return -1;
	}
	ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
	if (ret < 0) {
		_E("fail to get appinfo");
		return -1;
	}

	g_hash_table_remove(cf->tbl, appid);

	c = calloc(1, sizeof(*c));
	if (!c) {
		_E("create appinfo: %s", strerror(errno));
		return -1;
	}

	memset(c, 0, sizeof(struct appinfo));

	c->val[_AI_FILE] = strdup(appid);
	if (!c->val[_AI_FILE]) {
		_E("create appinfo: %s", strerror(errno));
		_free_appinfo(c);
		return -1;
	}

	c->val[_AI_NAME] = strdup(appid); //TODO :

	pkgmgrinfo_appinfo_get_component(handle, &component);
	if(component == PMINFO_UI_APP) {
		c->val[_AI_COMP] = strdup("ui"); //TODO :

		r = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
		if(multiple == true)
			c->val[_AI_MULTI] = strdup("true");
		else c->val[_AI_MULTI] = strdup("false");

		r = pkgmgrinfo_appinfo_is_preload(handle, &preload);
		if (preload == false) {
			c->val[_AI_PRELOAD] = strdup("false");
		} else {
			c->val[_AI_PRELOAD] = strdup("true");
		}

		if(gles == 0) {
			c->val[_AI_HWACC] = strdup("NOT_USE");
		} else {

			r = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
			if (hwacc == PMINFO_HWACCELERATION_USE_GL) {
				c->val[_AI_HWACC] = strdup("USE");
			} else if (hwacc == PMINFO_HWACCELERATION_USE_SYSTEM_SETTING) {
				c->val[_AI_HWACC] = strdup("SYS");
			} else {
				c->val[_AI_HWACC] = strdup("NOT_USE");
			}
		}
	} else {
		c->val[_AI_COMP] = strdup("svc");

		r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
		if(onboot == true)
			c->val[_AI_ONBOOT] = strdup("true");
		else c->val[_AI_ONBOOT] = strdup("false");

		r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
		if(restart == true)
			c->val[_AI_RESTART] = strdup("true");
		else c->val[_AI_RESTART] = strdup("false");
	}

	r = pkgmgrinfo_appinfo_get_exec(handle, &exec);
	c->val[_AI_EXEC] = strdup(exec);

	r = pkgmgrinfo_appinfo_get_apptype(handle, &type);
	if(strncmp(type, "capp", 4) == 0 ) {
		c->val[_AI_TYPE] = strdup("rpm");
	} else if (strncmp(type, "c++app", 6) == 0 || strncmp(type, "ospapp", 6) == 0) {
		c->val[_AI_TYPE] = strdup("tpk");
	} else if (strncmp(type, "webapp", 6) == 0) {
		c->val[_AI_TYPE] = strdup("wgt");
	}

	r = pkgmgrinfo_appinfo_get_permission_type(handle, &permission);
	if (permission == PMINFO_PERMISSION_SIGNATURE) {
		c->val[_AI_PERM] = strdup("signature");
	} else if (permission == PMINFO_PERMISSION_PRIVILEGE) {
		c->val[_AI_PERM] = strdup("privilege");
	} else {
		c->val[_AI_PERM] = strdup("normal");
	}

	r = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
	c->val[_AI_PKGID] = strdup(pkgid);

	SECURE_LOGD("%s : %s : %s", c->val[_AI_FILE], c->val[_AI_COMP], c->val[_AI_TYPE]);

	g_hash_table_insert(cf->tbl, c->val[_AI_FILE], c);

	return 0;
}

static int __app_info_delete_handler (const pkgmgrinfo_appinfo_h handle, void *data)
{
	struct appinfomgr *cf = (struct appinfomgr *)data;
	char *appid;

	pkgmgrinfo_appinfo_get_appid(handle, &appid);

	g_hash_table_remove(cf->tbl, appid);

	return 0;
}

static int _read_pkg_info(struct appinfomgr *cf, uid_t uid)
{
	int r;
	if(uid != GLOBAL_USER)
		r = pkgmgrinfo_appinfo_get_usr_install_list(__app_info_insert_handler, uid, cf);
	else
		r = pkgmgrinfo_appinfo_get_install_list(__app_info_insert_handler, cf);
	return r;
}

static struct appinfomgr *_init()
{
	struct appinfomgr *cf;

	cf = calloc(1, sizeof(*cf));
	if (!cf) {
		_E("appinfo init: %s", strerror(errno));
		return NULL;
	}

	cf->tbl = g_hash_table_new_full(g_str_hash, g_str_equal,
			NULL, _free_appinfo);

	return cf;
}

static void __vconf_cb(keynode_t *key, void *data)
{
	char *noti_string;
	char *type_string;
	char *appid;
	char *uid_string;
	uid_t uid;
	char *saveptr;
	pkgmgrinfo_appinfo_h handle;
	struct appinfomgr *cf = (struct appinfomgr *)data;
	int ret;

	noti_string = vconf_keynode_get_str(key);
	if( noti_string == NULL ) {
		return;
	}

	SECURE_LOGD("noti_string : %s",noti_string);
	type_string = strtok_r(noti_string, ":", &saveptr);
	appid = strtok_r(NULL, ":", &saveptr);
	uid_string = strtok_r(NULL, ":", &saveptr);
	uid = atoi(uid_string);

	_D("type_string: [%s]\n", type_string);
	_D("appid: [%s]\n", appid);
	_D("uid: %d\n", uid);
	if ( strncmp(type_string, "create", 6) == 0) {
		//is_admin
		if (uid != GLOBAL_USER)
		  ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
		else
		  ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
		if(ret < 0) {
			_E("pkgmgrinfo_appinfo_get_appinfo fail");
		}

		SECURE_LOGD("appid : %s /handle : %x", appid, handle);

		__app_info_insert_handler(handle, data);

		pkgmgrinfo_appinfo_destroy_appinfo(handle);
	} else if ( strncmp(type_string, "delete", 6) == 0) {
		g_hash_table_remove(cf->tbl, appid);
	} else if (strncmp(type_string, "update", 6) == 0){
		/*REMOVE EXISTING ENTRY & CREATE AGAIN*/
		if (g_hash_table_remove(cf->tbl, appid)){
			if (pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle) == PMINFO_R_OK){
				__app_info_insert_handler(handle, data);
				pkgmgrinfo_appinfo_destroy_appinfo(handle);
			}
		}
	}
}

int app_func(pkgmgrinfo_appinfo_h handle, void *user_data)
{
	char *appid = NULL;
	struct appinfomgr *cf = (struct appinfomgr *)user_data;
	int r;

	pkgmgrinfo_appinfo_get_appid(handle, &appid);
	r = g_hash_table_remove(cf->tbl, appid);
	SECURE_LOGD("upgrading... (%s)", appid);

	return 0;
}

static int __cb(int req_id, const char *pkg_type,
		       const char *pkgid, const char *key, const char *val,
		       const void *pmsg, void *user_data)
{
	int ret = 0;
	pkgmgrinfo_pkginfo_h handle;

	SECURE_LOGD("appid(%s), key(%s), value(%s)", pkgid, key, val);

	if((strncmp(key,"start", 5) == 0) && (strncmp(val, "update", 6) == 0) ) {
		ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
		if (ret != PMINFO_R_OK)
			return -1;
		ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, app_func, user_data);
		if (ret != PMINFO_R_OK) {
			pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
			return -1;
		}
		pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
	} else if (strncmp(key,"end", 3) == 0) {
			ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
		if (ret != PMINFO_R_OK)
			return -1;
		ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_insert_handler, user_data);
		if (ret != PMINFO_R_OK) {
			pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
			return -1;
		}
		pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
	}
	return ret;
}

int appinfo_init(struct appinfomgr **cf)
{
	struct appinfomgr *_cf;
	int r;
	FILE *fp = NULL;
	char buf[4096] = {0,};
	char *tmp = NULL;

	if (!cf) {
		errno = EINVAL;
		_E("appinfo init: %s", strerror(errno));
		return -1;
	}

	fp = fopen("/proc/cmdline", "r");
	if (fp == NULL){
		_E("appinfo init failed: %s", strerror(errno));
		return -1;
	}
	r = fgets(buf, sizeof(buf), fp);
	tmp = strstr(buf, "gles");
	if(tmp != NULL) {
		sscanf(tmp,"gles=%d", &gles);
	}
	fclose(fp);

	_cf = _init();
	if (!_cf)
		return -1;

	r = _read_pkg_info(_cf, getuid());
	if (r != PMINFO_R_OK) {
		_fini(_cf);
		return -1;
	}

	r = vconf_notify_key_changed(VCONFKEY_MENUSCREEN_DESKTOP, __vconf_cb, _cf);
	if (r < 0)
		_E("Unable to register vconf notification callback for VCONFKEY_MENUSCREEN_DESKTOP\n");

	int event_type = PMINFO_CLIENT_STATUS_UPGRADE;
	pkgmgrinfo_client *pc = NULL;
	pc = pkgmgrinfo_client_new(PMINFO_REQUEST);
	pkgmgrinfo_client_set_status_type(pc, event_type);
	pkgmgrinfo_client_listen_status(pc, __cb , _cf);

	*cf = _cf;

	return 0;
}

void appinfo_fini(struct appinfomgr **cf)
{
	if (!cf || !*cf)
		return;

	_fini(*cf);
	*cf = NULL;
}

const struct appinfo *appinfo_insert(struct appinfomgr *cf, const char *pkg_name)
{
	pkgmgrinfo_pkginfo_h handle;
	if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) == PMINFO_R_OK){
		pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_insert_handler, cf);
		pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_insert_handler, cf);
		pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
	}
	return cf;
}

void appinfo_delete(struct appinfomgr *cf, const char *pkg_name)
{
	pkgmgrinfo_pkginfo_h handle;
	if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) != PMINFO_R_OK)
		return;
	pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_delete_handler, cf);
	pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_delete_handler, cf);
	pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
}

const struct appinfo *appinfo_find(uid_t caller_uid, const char *appid)
{
	struct appinfo *res;
	gboolean r;
	bool multiple;
	bool preload;
	bool onboot;
	char *exec;
	bool restart;
	char *pkgid;
	char *type;
	pkgmgrinfo_app_hwacceleration hwacc;
	pkgmgrinfo_appinfo_h handle;
	pkgmgrinfo_app_component component;
	pkgmgrinfo_permission_type permission;
	int ret;

	ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
	if (ret != PMINFO_R_OK){
		ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid , caller_uid , &handle);
		if (ret != PMINFO_R_OK){
		  _E("appid is not found in DB %s", appid);
		  return NULL;
		}
	}

	res = calloc(1, sizeof(*res));
	if (!res) {
		_E("create appinfo: %s", strerror(errno));
		return NULL;
	}

	memset(res, 0, sizeof(struct appinfo));

	res->val[_AI_FILE] = strdup(appid);
	if (!res->val[_AI_FILE]) {
		_E("create appinfo: %s", strerror(errno));
		_free_appinfo(res);
		 return NULL;
	}

	res->val[_AI_NAME] = strdup(appid); //TODO :

	pkgmgrinfo_appinfo_get_component(handle, &component);
	if(component == PMINFO_UI_APP) {
		res->val[_AI_COMP] = strdup("ui"); //TODO :

		r = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
		if(multiple == true)
			res->val[_AI_MULTI] = strdup("true");
		else res->val[_AI_MULTI] = strdup("false");

		r = pkgmgrinfo_appinfo_is_preload(handle, &preload);
		if (preload == false) {
			res->val[_AI_PRELOAD] = strdup("false");
		} else {
			res->val[_AI_PRELOAD] = strdup("true");
		}

		if(gles == 0) {
			res->val[_AI_HWACC] = strdup("NOT_USE");
		} else {

			r = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
			if (hwacc == PMINFO_HWACCELERATION_USE_GL) {
				res->val[_AI_HWACC] = strdup("USE");
			} else if (hwacc == PMINFO_HWACCELERATION_USE_SYSTEM_SETTING) {
				res->val[_AI_HWACC] = strdup("SYS");
			} else {
				res->val[_AI_HWACC] = strdup("NOT_USE");
			}
		}
	} else {
		res->val[_AI_COMP] = strdup("svc");

		r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
		if(onboot == true)
			res->val[_AI_ONBOOT] = strdup("true");
		else res->val[_AI_ONBOOT] = strdup("false");

		r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
		if(restart == true)
			res->val[_AI_RESTART] = strdup("true");
		else res->val[_AI_RESTART] = strdup("false");
	}

	r = pkgmgrinfo_appinfo_get_exec(handle, &exec);
	res->val[_AI_EXEC] = strdup(exec);

	r = pkgmgrinfo_appinfo_get_apptype(handle, &type);
	if(strncmp(type, "capp", 4) == 0 ) {
		res->val[_AI_TYPE] = strdup("rpm");
	} else if (strncmp(type, "c++app", 6) == 0 || strncmp(type, "ospapp", 6) == 0) {
		res->val[_AI_TYPE] = strdup("tpk");
	} else if (strncmp(type, "webapp", 6) == 0) {
		res->val[_AI_TYPE] = strdup("wgt");
	}

	r = pkgmgrinfo_appinfo_get_permission_type(handle, &permission);
	if (permission == PMINFO_PERMISSION_SIGNATURE) {
		res->val[_AI_PERM] = strdup("signature");
	} else if (permission == PMINFO_PERMISSION_PRIVILEGE) {
		res->val[_AI_PERM] = strdup("privilege");
	} else {
		res->val[_AI_PERM] = strdup("normal");
	}

	r = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
	res->val[_AI_PKGID] = strdup(pkgid);

	SECURE_LOGD("%s : %s : %s", res->val[_AI_FILE], res->val[_AI_COMP], res->val[_AI_TYPE]);

	return res;
}

const char *appinfo_get_value(const struct appinfo *c, enum appinfo_type type)
{
	enum _appinfo_idx i;

	if (!c) {
		errno = EINVAL;
		_E("appinfo get value: %s", strerror(errno));
		return NULL;
	}

	for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
		if (type == _appinfos[i].type)
			return c->val[i];
	}

	errno = ENOENT;
	_E("appinfo get value: %s", strerror(errno));

	return NULL;
}

const char *appinfo_get_filename(const struct appinfo *c)
{
	if (!c) {
		errno = EINVAL;
		SECURE_LOGE("appinfo get filename: %s", strerror(errno));
		return NULL;
	}

	return c->val[_AI_FILE];
}

struct _cbinfo {
	appinfo_iter_callback cb;
	void *cb_data;
};

static void _iter_cb(gpointer key, gpointer value, gpointer user_data)
{
	struct _cbinfo *cbi = user_data;

	assert(cbi);

	cbi->cb(cbi->cb_data, key, value);
}

void appinfo_foreach(struct appinfomgr *cf, appinfo_iter_callback cb, void *user_data)
{
	struct _cbinfo cbi;

	if (!cf || !cb) {
		errno = EINVAL;
		_E("appinfo foreach: %s", strerror(errno));
		return;
	}

	cbi.cb = cb;
	cbi.cb_data = user_data;

	g_hash_table_foreach(cf->tbl, _iter_cb, &cbi);
}

int appinfo_get_boolean(const struct appinfo *c, enum appinfo_type type)
{
	const char *v;

	v = appinfo_get_value(c, type);
	if (!v)
		return -1;

	if (!strcmp(v, "1") || !strcasecmp(v, "true"))
		return 1;

	if (!strcmp(v, "0") || !strcasecmp(v, "false"))
		return 0;

	errno = EFAULT;

	return -1;
}