summaryrefslogtreecommitdiff
path: root/src/launchpad_lib.c
blob: f655ed6be729c694827d6e420a1d40544033c404 (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
/*
 * Copyright (c) 2015 - 2016 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 <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <malloc.h>
#ifdef _APPFW_FEATURE_LOADER_PRIORITY
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <bundle_internal.h>
#include <aul.h>
#include <security-manager.h>

#include "launchpad_common.h"
#include "launchpad.h"
#include "preexec.h"

#ifndef API
#define API __attribute__ ((visibility("default")))
#endif

#define AUL_PR_NAME 16
#define LOWEST_PRIO 20

static loader_lifecycle_callback_s *__loader_callbacks;
static loader_adapter_s *__loader_adapter;
static void *__loader_user_data;
static int __argc;
static char **__argv;
static bundle *__bundle;
static char *__appid;
static char *__pkgid;
static char *__root_path;
static int __loader_type = LAUNCHPAD_TYPE_UNSUPPORTED;
static int __loader_id;

static void __at_exit_to_release_bundle()
{
	if (__bundle)
		bundle_free(__bundle);
}

static void __release_at_exit(void)
{
	if (__appid != NULL)
		free(__appid);

	if (__pkgid != NULL)
		free(__pkgid);

	if (__root_path != NULL)
		free(__root_path);
}

static int __set_access(const char *appid, const char *pkg_type,
		const char *app_path)
{
	return security_manager_prepare_app(appid);
}

static int __prepare_exec(const char *appid, const char *app_path,
			const char *pkg_type, int type)
{
	const char *file_name = NULL;
	char process_name[AUL_PR_NAME] = { 0, };
	int ret;

	__preexec_run(pkg_type, appid, app_path);

	/* SET PRIVILEGES*/
	SECURE_LOGD("[candidata] appid : %s / pkg_type : %s / app_path : %s",
		appid, pkg_type, app_path);
	ret = __set_access(appid, pkg_type, app_path);
	if (ret < 0) {
		_D("fail to set privileges - check your package's credential: "
				"%d\n", ret);
		return -1;
	}

	/*
	 * SET DUMPABLE - for coredump
	 * This dumpable flag should be set after
	 * calling perm_app_set_privilege().
	 */
	prctl(PR_SET_DUMPABLE, 1);

	/* SET PROCESS NAME*/
	if (app_path == NULL) {
		_D("app_path should not be NULL - check menu db");
		return -1;
	}

	file_name = strrchr(app_path, '/') + 1;
	if (file_name == NULL) {
		_D("can't locate file name to execute");
		return -1;
	}
	memset(process_name, '\0', AUL_PR_NAME);
	snprintf(process_name, AUL_PR_NAME, "%s", file_name);
	prctl(PR_SET_NAME, process_name);

	return 0;
}

static int __default_launch_cb(bundle *kb, const char *appid,
		const char *app_path, const char *pkg_type, int loader_type)
{
	char err_str[MAX_LOCAL_BUFSZ] = { 0, };
#ifdef _APPFW_FEATURE_PRIORITY_CHANGE
	int res;
	const char *high_priority = bundle_get_val(kb, AUL_K_HIGHPRIORITY);

	_D("high_priority: %s", high_priority);
	if (strncmp(high_priority, "true", 4) == 0) {
		res = setpriority(PRIO_PROCESS, 0, -10);
		if (res == -1) {
			SECURE_LOGE("Setting process (%d) priority "
				"to -10 failed, errno: %d (%s)",
				getpid(), errno,
				strerror_r(errno, err_str, sizeof(err_str)));
		}
	}
	bundle_del(kb, AUL_K_HIGHPRIORITY);
#endif

	if (__prepare_exec(appid, app_path, pkg_type, loader_type) < 0) {
		_E("__candidate_process_prepare_exec() failed");
		if (access(app_path, F_OK | R_OK)) {
			SECURE_LOGE("access() failed for file: \"%s\", "
				"error: %d (%s)", app_path, errno,
				strerror_r(errno, err_str, sizeof(err_str)));
		}
		exit(-1);
	}

	return 0;
}

static int __candidate_process_launchpad_main_loop(app_pkt_t *pkt,
		char *out_app_path, int *out_argc, char ***out_argv, int type)
{
	bundle *kb;
	appinfo_t *menu_info = NULL;
	const char *app_path = NULL;
	int tmp_argc = 0;
	char **tmp_argv = NULL;
	int ret = -1;
	int i;

	kb = bundle_decode(pkt->data, pkt->len);
	if (!kb) {
		_E("bundle decode error");
		exit(-1);
	}

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

	__bundle = kb;
	atexit(__at_exit_to_release_bundle);
	atexit(__release_at_exit);

	menu_info = _appinfo_create(kb);
	if (menu_info == NULL) {
		_D("such pkg no found");
		exit(-1);
	}

	if (menu_info->appid == NULL) {
		_E("Unable to get app_id");
		exit(-1);
	}

	if (type < 0) {
		_E("Invalid launchpad type: %d", type);
		exit(-1);
	}

	SECURE_LOGD("app id: %s, launchpad type: %d", menu_info->appid, type);

	app_path = _appinfo_get_app_path(menu_info);
	if (app_path == NULL) {
		_E("app_path is NULL");
		exit(-1);
	}

	if (app_path[0] != '/') {
		_E("app_path is not absolute path");
		exit(-1);
	}

	_modify_bundle(kb, /*cr.pid - unused parameter*/ 0, menu_info,
			pkt->cmd);

	__appid = strdup(menu_info->appid);
	if (__appid == NULL) {
		_E("Out of memory");
		exit(-1);
	}
	aul_set_preinit_appid(__appid);

	if (menu_info->pkgid == NULL) {
		_E("unable to get pkg_id from menu_info");
		exit(-1);
	}
	SECURE_LOGD("pkg id: %s", menu_info->pkgid);

	__pkgid = strdup(menu_info->pkgid);
	if (__pkgid == NULL) {
		_E("Out of memory");
		exit(-1);
	}
	aul_set_preinit_pkgid(__pkgid);

	__root_path = strdup(menu_info->root_path);
	if (__root_path == NULL) {
		_E("Out of memory");
		exit(-1);
	}
	aul_set_preinit_root_path(__root_path);

	tmp_argv = _create_argc_argv(kb, &tmp_argc);

	__default_launch_cb(kb, __appid, app_path, menu_info->pkg_type, type);

	if (__loader_callbacks->launch) {
		ret = __loader_callbacks->launch(tmp_argc, tmp_argv, app_path,
				__appid, __pkgid, menu_info->pkg_type,
				__loader_user_data);
	}

	/* SET ENVIROMENT*/
	_set_env(menu_info, kb);

	if (out_app_path != NULL && out_argc != NULL && out_argv != NULL) {
		memset(out_app_path, '\0', strlen(out_app_path));
		snprintf(out_app_path, LOADER_ARG_LEN, "%s", app_path);

		*out_argv = tmp_argv;
		*out_argc = tmp_argc;
		(*out_argv)[LOADER_ARG_PATH] = out_app_path;

		for (i = 0; i < *out_argc; i++) {
			SECURE_LOGD("input argument %d : %s##", i,
					(*out_argv)[i]);
		}
	} else {
		exit(-1);
	}

	if (menu_info != NULL)
		_appinfo_free(menu_info);

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

	return ret;
}

static void __receiver_cb(int fd)
{
	int ret = -1;
	int recv_ret;
	app_pkt_t *pkt;

	_D("[candidate] ECORE_FD_READ");
	pkt = (app_pkt_t *)malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
	if (!pkt) {
		_D("[candidate] out of memory1");
		exit(-1);
	}
	memset(pkt, 0, AUL_SOCK_MAXBUFF);

	recv_ret = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
	if (recv_ret == -1) {
		_D("[condidate] recv error!");
		close(fd);
		free(pkt);
		exit(-1);
	}
	_D("[candidate] recv_ret: %d, pkt->len: %d", recv_ret, pkt->len);

	__loader_adapter->remove_fd(__loader_user_data, fd);
	close(fd);
	ret = __candidate_process_launchpad_main_loop(pkt,
			__argv[LOADER_ARG_PATH], &__argc, &__argv,
			__loader_type);
	SECURE_LOGD("[candidate] real app argv[0]: %s, real app argc: %d",
			__argv[LOADER_ARG_PATH], __argc);
	free(pkt);

	if (ret >= 0) {
		__loader_adapter->loop_quit(__loader_user_data);
		_D("[candidate] ecore main loop quit");
	}
}

static int __before_loop(int argc, char **argv)
{
	int client_fd;
	int ret = -1;
	bundle *extra = NULL;
#ifdef _APPFW_FEATURE_LOADER_PRIORITY
	char err_str[MAX_LOCAL_BUFSZ] = { 0, };
	int res = setpriority(PRIO_PROCESS, 0, LOWEST_PRIO);

	if (res == -1) {
		SECURE_LOGE("Setting process (%d) priority to %d failed, "
				"errno: %d (%s)", getpid(), LOWEST_PRIO, errno,
				strerror_r(errno, err_str, sizeof(err_str)));
	}
#endif
	__preexec_init(argc, argv);

	/* Set new session ID & new process group ID*/
	/* In linux, child can set new session ID without check permission */
	/* TODO : should be add to check permission in the kernel*/
	setsid();

	if (argc > 3) {
		extra = bundle_decode((bundle_raw *)argv[LOADER_ARG_EXTRA],
				strlen(argv[LOADER_ARG_EXTRA]));
	}

	if (__loader_callbacks->create) {
		__loader_callbacks->create(extra, __loader_type,
				__loader_user_data);
		ret = 0;
	}

	if (extra)
		bundle_free(extra);

	malloc_trim(0);
#ifdef _APPFW_FEATURE_LOADER_PRIORITY
	res = setpriority(PRIO_PGRP, 0, 0);
	if (res == -1) {
		SECURE_LOGE("Setting process (%d) priority to 0 failed, "
				"errno: %d (%s)", getpid(), errno,
				strerror_r(errno, err_str, sizeof(err_str)));
	}
#endif
	client_fd = _connect_to_launchpad(__loader_type, __loader_id);
	if (client_fd == -1) {
		_D("Connecting to candidate process was failed.");
		return -1;
	}

	__loader_adapter->add_fd(__loader_user_data, client_fd, __receiver_cb);

	return ret;
}

static int __after_loop(void)
{
	if (__loader_callbacks->terminate) {
		return __loader_callbacks->terminate(__argc, __argv,
				__loader_user_data);
	}

	return -1;
}

bundle *launchpad_loader_get_bundle()
{
	return __bundle;
}

API int launchpad_loader_main(int argc, char **argv,
		loader_lifecycle_callback_s *callbacks,
		loader_adapter_s *adapter, void *user_data)
{
	if (argc < 3) {
		_E("too few argument.");
		return -1;
	}

	__loader_type = argv[LOADER_ARG_TYPE][0] - '0';
	if (__loader_type < 0 || __loader_type >= LAUNCHPAD_TYPE_MAX) {
		_E("invalid argument. (type: %d)", __loader_type);
		return -1;
	}

	__loader_id = atoi(argv[LOADER_ARG_ID]);

	if (callbacks == NULL) {
		_E("invalid argument. callback is null");
		return -1;
	}

	if (adapter == NULL) {
		_E("invalid argument. adapter is null");
		return -1;
	}

	if (adapter->loop_begin == NULL || adapter->loop_quit == NULL
		|| adapter->add_fd == NULL || adapter->remove_fd == NULL) {
		_E("invalid argument. adapter callback is null");
		return -1;
	}

	__loader_callbacks = callbacks;
	__loader_adapter = adapter;
	__loader_user_data = user_data;
	__argc = argc;
	__argv = argv;

	if (__before_loop(argc, argv) != 0)
		return -1;

	_D("[candidate] ecore main loop begin");
	__loader_adapter->loop_begin(__loader_user_data);

	return __after_loop();
}