summaryrefslogtreecommitdiff
path: root/tool/app_launcher.c
blob: 45095aca5b0d2762e130e42e6d4eb15d29165f9f (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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
 *  app_launcher
 *
 * Copyright (c) 2014, Intel Corporation.
 *
 * Contact: Baptiste DURAND <baptiste.durand@open.eurogiciel.org>
 *
 * 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.
 *
 */

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/inotify.h>

#include <gio/gio.h>
#include <glib.h>
#include <pkgmgr-info.h>
#include <bundle.h>
#include <bundle_internal.h>
#include <tzplatform_config.h>

#include "aul.h"
#include "aul_svc.h"
#include "launch.h"

#define PATH_RUN "/run"
#define AMD_READY ".amd_ready"
#define LAUNCHPAD_PROCESS_POOL_SOCK ".launchpad-process-pool-sock"
#define APP_TYPE_UI "uiapp"
#define APP_TYPE_SERVICE "svcapp"
#define REGULAR_UID_MIN 5000
#define INOTIFY_BUF (1024 * ((sizeof(struct inotify_event)) + 16))
#define TIMEOUT_INTERVAL 1000

struct launch_arg {
	char appid[256];
	char **argv;
	int argc;
	int flag_debug;
	int flag_web;
	int sync;
	char op;
	int pid;
};

struct amd_watch {
	int fd;
	int wd;
	GIOChannel *io;
	guint wid;
	char *appid;
	int pid;
};

static GMainLoop *mainloop = NULL;
static uid_t uid;
static int monitoring_dead_signal;
static int monitoring_amd_ready;

static bundle *_create_internal_bundle(struct launch_arg *args)
{
	bundle *b;
	int i;

	if (!args->flag_debug && args->argc < 2)
		return NULL;

	b = bundle_create();
	if (args->flag_debug)
		bundle_add(b, AUL_K_DEBUG, "1");

	for (i = 0; i + 1 < args->argc; i += 2) {
		bundle_add(b, args->argv[i], args->argv[i + 1]);
		if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
				!strcmp(args->argv[i + 1], "SYNC"))
			args->sync = 1;
	}

	return b;
}

static int __launch_app_dead_handler(int pid, void *data)
{
	struct launch_arg *launch_arg_data = (struct launch_arg *)data;

	if (launch_arg_data->pid == pid) {
		if (monitoring_amd_ready)
			printf("... launch failed\n");

		g_main_loop_quit(mainloop);
	}

	return 0;
}

static gboolean __check_running(gpointer data)
{
	int ret;
	struct launch_arg *launch_arg_data = (struct launch_arg *)data;

	ret = aul_app_is_running(launch_arg_data->appid);
	if (ret == 0) {
		if (monitoring_amd_ready)
			return TRUE;
		else
			printf("... launch failed\n");

		g_main_loop_quit(mainloop);
	}

	return FALSE;
}

static void __reply_cb_func(bundle *b, int request_code, aul_svc_result_val result, void *data)
{
	char* port = NULL;
	const char* pid_str = NULL;
	int pid = 0;

	bundle_get_str(b, "port", &port);
	pid_str = bundle_get_val(b, AUL_K_CALLEE_PID);
	if (pid_str)
		pid = atoi(pid_str);

	if (port != NULL && strlen(port) > 0) {
		printf("... successfully launched pid = %d with debug 1 port: %s\n",
					pid, port);
	} else {
		printf("... launch failed\n");
	}

	g_main_loop_quit(mainloop);
}

static gboolean run_func(void *data)
{
	int pid;
	bundle *kb;
	struct launch_arg *launch_arg_data = (struct launch_arg *)data;

	kb = _create_internal_bundle(launch_arg_data);

	if (launch_arg_data->op == 'e') {
		if (kb == NULL)
			kb = bundle_create();
		aul_svc_set_loader_id(kb, PAD_LOADER_ID_DIRECT);
	}

	if (!launch_arg_data->flag_web) {
		pid = aul_launch_app_for_uid((char *)launch_arg_data->appid, kb, uid);

		if (kb) {
			bundle_free(kb);
			kb = NULL;
		}

		if (pid > 0) {
			printf("... successfully launched pid = %d with debug %d\n",
					pid, launch_arg_data->flag_debug);
			if (launch_arg_data->sync) {
				launch_arg_data->pid = pid;
				aul_listen_app_dead_signal(__launch_app_dead_handler, launch_arg_data);
				g_idle_add(__check_running, launch_arg_data);
				monitoring_dead_signal = 1;
				return FALSE;
			}
		} else {
			printf("... launch failed\n");
		}

		g_main_loop_quit(mainloop);
	} else {
		aul_svc_set_operation(kb, AUL_SVC_OPERATION_DEFAULT);
		aul_svc_set_appid(kb, (char *)launch_arg_data->appid);
		pid = aul_svc_run_service_for_uid(kb, 0, __reply_cb_func, (void*)NULL, uid);
		bundle_free(kb);
		if (pid <= 0) {
			printf("... launch failed\n");
			g_main_loop_quit(mainloop);
		}
	}

	return FALSE;
}

static int __get_gles(void)
{
	FILE *fp;
	char buf[PATH_MAX];
	char *tmp;
	int gles = 1;

	fp = fopen("/proc/cmdline", "r");
	if (fp == NULL)
		return gles;

	if (fgets(buf, sizeof(buf), fp) != NULL) {
		tmp = strstr(buf, "gles");
		if (tmp != NULL)
			sscanf(tmp, "gles=%d", &gles);
	}

	fclose(fp);

	return gles;
}

static int __set_appinfo_for_launchpad(bundle *kb, const char *appid)
{
	pkgmgrinfo_appinfo_h handle;
	int ret;
	char *pkgid = NULL;
	char *exec = NULL;
	char *apptype = NULL;
	char *pkgtype = NULL;
	char *component_type = NULL;
	char *root_path = NULL;
	char *api_version = NULL;
	pkgmgrinfo_app_hwacceleration hwacc = PMINFO_HWACCELERATION_OFF;
	const char *hwacc_str = "NOT_USE";
	bool process_pool = false;

	if (kb == NULL)
		return -1;

	ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
	if (ret != PMINFO_R_OK)
		return -1;

	ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_get_apptype(handle, &apptype);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_get_pkgtype(handle, &pkgtype);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
	if (ret != PMINFO_R_OK)
		goto end;

	if (!component_type || (strcmp(component_type, APP_TYPE_SERVICE) != 0
			&& strcmp(component_type, APP_TYPE_UI) != 0)) {
		ret = -1;
		goto end;
	}

	ret = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_is_process_pool(handle, &process_pool);
	if (ret != PMINFO_R_OK)
		goto end;

	if (__get_gles()) {
		if (hwacc == PMINFO_HWACCELERATION_OFF)
			hwacc_str = "NOT_USE";
		else if (hwacc == PMINFO_HWACCELERATION_ON)
			hwacc_str = "USE";
		else
			hwacc_str = "SYS";
	}

	ret = pkgmgrinfo_appinfo_get_root_path(handle, &root_path);
	if (ret != PMINFO_R_OK)
		goto end;

	ret = pkgmgrinfo_appinfo_get_api_version(handle, &api_version);
	if (ret != PMINFO_R_OK)
		goto end;

	bundle_add(kb, AUL_K_APPID, appid);
	bundle_add(kb, AUL_K_HWACC, hwacc_str);
	bundle_add(kb, AUL_K_EXEC, exec);
	bundle_add(kb, AUL_K_APP_TYPE, apptype);
	bundle_add(kb, AUL_K_PKGID, pkgid);
	bundle_add(kb, AUL_K_INTERNAL_POOL, process_pool ? "true" : "false");
	bundle_add(kb, AUL_K_COMP_TYPE, component_type);
	bundle_add(kb, AUL_K_PACKAGETYPE, pkgtype);
	bundle_add(kb, AUL_K_ROOT_PATH, root_path);
	bundle_add(kb, AUL_K_API_VERSION, api_version);

	aul_svc_set_loader_id(kb, PAD_LOADER_ID_DIRECT);

end:
	pkgmgrinfo_appinfo_destroy_appinfo(handle);

	return ret;
}

static gboolean __amd_monitor_cb(GIOChannel *io, GIOCondition cond,
		gpointer data)
{
	char buf[INOTIFY_BUF];
	ssize_t len = 0;
	int i = 0;
	struct inotify_event *event;
	char *p;
	int fd = g_io_channel_unix_get_fd(io);
	struct amd_watch *watch = (struct amd_watch *)data;

	len = read(fd, buf, sizeof(buf));
	if (len < 0)
		return TRUE;

	while (i < len) {
		event = (struct inotify_event *)&buf[i];
		if (event->len) {
			p = event->name;
			if (p && !strcmp(p, AMD_READY)) {
				aul_app_register_pid(watch->appid, watch->pid);
				monitoring_amd_ready = 0;
				printf("... successfully launched pid = %d\n",
						watch->pid);
				return FALSE;
			}
		}
		i += offsetof(struct inotify_event, name) + event->len;
		if (i >= INOTIFY_BUF)
			break;
	}

	return TRUE;
}

static void __watch_destroy_cb(gpointer data)
{
	struct amd_watch *watch = (struct amd_watch *)data;

	if (watch == NULL)
		return;

	g_io_channel_unref(watch->io);

	if (watch->appid)
		free(watch->appid);
	if (watch->wd)
		inotify_rm_watch(watch->fd, watch->wd);
	close(watch->fd);
	free(watch);

	if (!monitoring_dead_signal && !monitoring_amd_ready)
		g_main_loop_quit(mainloop);
}

static void __watch_amd_ready(const char *appid, int pid)
{
	struct amd_watch *watch;

	watch = (struct amd_watch *)calloc(1, sizeof(struct amd_watch));
	if (watch == NULL)
		return;

	watch->appid = strdup(appid);
	if (watch->appid == NULL) {
		free(watch);
		return;
	}

	watch->pid = pid;

	watch->fd = inotify_init();
	if (watch->fd < 0) {
		free(watch->appid);
		free(watch);
		return;
	}

	watch->wd = inotify_add_watch(watch->fd, PATH_RUN, IN_CREATE);
	if (watch->wd < 0) {
		close(watch->fd);
		free(watch->appid);
		free(watch);
		return;
	}

	watch->io = g_io_channel_unix_new(watch->fd);
	if (watch->io == 0) {
		inotify_rm_watch(watch->fd, watch->wd);
		close(watch->fd);
		free(watch->appid);
		free(watch);
		return;
	}

	watch->wid = g_io_add_watch_full(watch->io, G_PRIORITY_DEFAULT,
			G_IO_IN, __amd_monitor_cb, watch, __watch_destroy_cb);
	monitoring_amd_ready = 1;
}

static void __register_appinfo(const char *appid, int pid)
{
	char buf[PATH_MAX];

	snprintf(buf, sizeof(buf), "/run/%s", AMD_READY);
	if (access(buf, F_OK) == 0) {
		aul_app_register_pid(appid, pid);
		return;
	}

	__watch_amd_ready(appid, pid);
}

static gboolean __register_dead_signal(gpointer data)
{
	int ret;

	ret = aul_listen_app_dead_signal(__launch_app_dead_handler, data);
	if (ret == 0) {
		monitoring_dead_signal = 1;
		return FALSE;
	}

	return TRUE;
}

static gboolean fast_run_func(void *data)
{
	int pid;
	bundle *kb;
	struct launch_arg *launch_arg_data = (struct launch_arg *)data;
	int ret;

	kb = _create_internal_bundle(launch_arg_data);
	if (kb == NULL)
		kb = bundle_create();

	if (__set_appinfo_for_launchpad(kb, launch_arg_data->appid) < 0) {
		printf("failed to set appinfo\n");
		bundle_free(kb);
		return FALSE;
	}

	pid = app_send_cmd_to_launchpad(LAUNCHPAD_PROCESS_POOL_SOCK,
			uid, 0, kb);
	bundle_free(kb);
	if (pid > 0) {
		__register_appinfo(launch_arg_data->appid, pid);
		if (!monitoring_amd_ready)
			printf("... successfully launched pid = %d\n", pid);

		if (launch_arg_data->sync || monitoring_amd_ready) {
			launch_arg_data->pid = pid;
			ret = aul_listen_app_dead_signal(
					__launch_app_dead_handler,
					launch_arg_data);
			g_timeout_add(TIMEOUT_INTERVAL, __check_running,
					launch_arg_data);
			if (ret == 0) {
				monitoring_dead_signal = 1;
			} else {
				g_timeout_add(TIMEOUT_INTERVAL,
						__register_dead_signal,
						launch_arg_data);
				return FALSE;
			}
		}

		if (monitoring_dead_signal || monitoring_amd_ready)
			return FALSE;
	} else {
		printf("... launch failed\n");
	}

	g_main_loop_quit(mainloop);

	return FALSE;
}

static void print_usage(char *program)
{
	printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
	printf(
			"   -h                        --help              Display this usage information.\n"
			"   -l                        --list              Display installed apps list\n"
			"   -S                        --status            Display running apps list\n"
			"   -s [tizen application ID] --start             Launch widget with tizen application ID\n"
			"   -k [tizen application ID] --kill              Kill widget with tizen application ID\n"
			"   -t [tizen application ID] --terminate         Terminate widget with tizen application ID\n"
			"   -r [tizen application ID] --is-running        Check whether application is running by tizen application ID,\n"
			"                                                 If widget is running, 0(zero) will be returned.\n"
			"   -f [tizen application ID] --fast-start        Fast launch app with tizen application ID\n"
			"   -e [tizen application ID] --direct-start      Direct Launch app with tizen application ID\n"
			"   -d                        --debug             Activate debug mode\n"
			"   -w                        --web-debug         Activate web debug mode. Use only for webapps\n"
			"   -u [uid]                  --user              Specify user. Use with other commands.\n"
	      );
}

static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
{
	char *label;
	char *appid;

	if (pkgmgrinfo_appinfo_get_label(handle, &label))
		label = "";

	if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
		printf("Failed to get appid\n");
		return -1;
	}

	printf("\t'%s'\t '%s'\n", label, appid);

	return 0;
}

static int list_app(void)
{
	int ret = 0;

	printf("\tApplication List for user %lu\n", (long)uid);
	printf("\tUser's Application \n");
	printf("\t Name \t AppID \n");
	printf("\t=================================================\n");
	if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
				uid, NULL) != PMINFO_R_OK)
		ret = -1;
	printf("\t=================================================\n");
	return ret;
}

static int __iterfunc_status(const aul_app_info *info, void *data)
{
	printf("\t  %s (%d)\n", info->appid, info->pid);
	return 0;
}

static int __iterfunc_kill(const aul_app_info *info, void *data)
{
	if (!data)
		return 0;
	if (strcmp(info->appid, data) == 0) {
		aul_kill_pid(info->pid);
		printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
	}
	return 0;
}

static int __iterfunc_term(const aul_app_info *info, void *data)
{
	if (!data)
		return 0;
	if (strcmp(info->appid, data) == 0) {
		aul_terminate_pid_for_uid(info->pid, uid);
		printf("\t Terminate appId: %s (%d)\n", info->appid, info->pid);
	}
	return 0;
}

static int is_app_installed(char *appid)
{
	int is_installed = 0;
	pkgmgrinfo_appinfo_filter_h filter;

	if (pkgmgrinfo_appinfo_filter_create(&filter)) {
		printf("Failed to create filter\n");
		return -1;
	}

	if (pkgmgrinfo_appinfo_filter_add_string(filter,
				PMINFO_APPINFO_PROP_APP_ID, appid)) {
		printf("Failed to add filter string\n");
		pkgmgrinfo_appinfo_filter_destroy(filter);
		return -1;
	}

	if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
				uid)) {
		printf("Failed to get filter count\n");
		pkgmgrinfo_appinfo_filter_destroy(filter);
		return -1;
	}

	pkgmgrinfo_appinfo_filter_destroy(filter);

	return is_installed;
}

int main(int argc, char **argv)
{
	char path[PATH_MAX];
	bool disp_help = false;
	bool disp_list = false;
	bool disp_run_list = false;
	bool is_running;
	int next_opt;
	int opt_idx = 0;
	char op = '\0';
	struct launch_arg args;
	static struct option long_options[] = {
		{ "help", no_argument, 0, 'h' },
		{ "list", no_argument, 0, 'l' },
		{ "status", no_argument, 0, 'S' },
		{ "start", required_argument, 0, 's' },
		{ "args", required_argument, 0, 'a' },
		{ "kill", required_argument, 0, 'k' },
		{ "terminate", required_argument, 0, 't' },
		{ "is-running", required_argument, 0, 'r' },
		{ "fast-launch", required_argument, 0, 'f' },
		{ "direct-launch", required_argument, 0, 'e' },
		{ "debug", no_argument, 0, 'd' },
		{ "web-debug", no_argument, 0, 'w' },
		{ "user", required_argument, 0, 'u' },
		{ 0, 0, 0, 0 }
	};
	memset(&args, 0, sizeof(struct launch_arg));

	uid = getuid();

	do {
		next_opt = getopt_long(argc,
				argv,
				"hlSs:k:t:r:f:e:dwu:",
				long_options,
				&opt_idx);

		switch (next_opt) {
		case 'h':
			if (!disp_help) {
				print_usage(argv[0]);
				disp_help = true;
			}
			break;
		case 'l':
			if (disp_list)
				break;

			op = next_opt;
			disp_list = true;
			break;
		case 'S':
			if (disp_run_list)
				break;
			op = next_opt;
			disp_run_list = true;
			break;
		case 's':
		case 'k':
		case 't':
		case 'r':
		case 'f':
		case 'e':
			if (strlen(optarg) > 255) {
				print_usage(argv[0]);
				return -1;
			} else {
				strncpy(args.appid, optarg, sizeof(args.appid) - 1);
			}
			op = next_opt;
			break;
		case 'd':
			args.flag_debug = 1;
			break;
		case 'w':
			args.flag_debug = 1;
			args.flag_web = 1;
			break;
		case '?':
			break;
		case -1:
			break;
		case 'u':
			uid = atoi(optarg);
			break;
		default:
			print_usage(argv[0]);
			break;
		}
	} while (next_opt != -1);

	if (argc == 1)
		print_usage(argv[0]);

	if (op == 'S') {
		printf("\t appId (PID)\n");
		if (aul_app_get_running_app_info_for_uid(__iterfunc_status,
					NULL, uid)) {
			printf("Fail to display the list of "
					"Running applications\n");
			return -1;
		}
	} else if (op == 'l') {
		if (list_app()) {
			printf("Fail to display the list of "
					"installed applications\n");
			return -1;
		}
	}

	if (optind < argc) {
		args.argc = argc - optind;
		args.argv = &argv[optind];
	}
	if ((op == 's') || (op == 'k') || (op == 'r') || (op == 'f') || (op == 'e')) {
		if (is_app_installed(args.appid) <= 0) {
			printf("The app with ID: %s is not avaible "
					"for the user %d \n",
					args.appid, uid);
			return -1;
		}
	}

	if ((op == 's') || (op == 'e')) {
		if (strlen(args.appid) <= 0) {
			printf("result: %s\n", "failed");
			return -1;
		}
		args.op = op;
		g_idle_add(run_func, args.appid);
		mainloop = g_main_loop_new(NULL, FALSE);
		if (!mainloop) {
			printf("failed to create glib main loop\n");
			exit(EXIT_FAILURE);
		}
		g_main_loop_run(mainloop);
		return 0;
	} else if (op == 'k') {
		is_running = aul_app_is_running_for_uid(args.appid, uid);
		if (true == is_running) {
			aul_app_get_running_app_info_for_uid(__iterfunc_kill,
					args.appid, uid);
		} else {
			printf("result: %s\n", "App isn't running");
			return 1;
		}
	} else if (op == 't') {
		is_running = aul_app_is_running_for_uid(args.appid, uid);
		if (true == is_running) {
			aul_app_get_running_app_info_for_uid(__iterfunc_term,
					args.appid, uid);
		} else {
			printf("result: %s\n", "App isn't running");
			return 1;
		}
	} else if (op == 'r') {
		is_running = aul_app_is_running_for_uid(args.appid, uid);
		if (true == is_running) {
			printf("result: %s\n", "running");
			return 0;
		} else {
			printf("result: %s\n", "not running");
			return 1;
		}
	} else if (op == 'f') {
		if (strlen(args.appid) <= 0) {
			printf("result: failed\n");
			return -1;
		}

		snprintf(path, sizeof(path), "/run/%s", AMD_READY);
		if (access(path, F_OK) == 0)
			g_idle_add(run_func, args.appid);
		else
			g_idle_add(fast_run_func, args.appid);

		mainloop = g_main_loop_new(NULL, FALSE);
		if (!mainloop) {
			printf("failed to create glib main loop\n");
			exit(EXIT_FAILURE);
		}
		g_main_loop_run(mainloop);
	}

	return 0;
}