summaryrefslogtreecommitdiff
path: root/src/power/power-handler.c
blob: 6e63f5217b8ced897d8c447a4f4e3f1e2c21fee7 (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
/*
 * deviced
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * 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 <unistd.h>
#include <time.h>
#include <limits.h>
#include <fcntl.h>
#include <dirent.h>
#include <vconf.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <vconf.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <linux/reboot.h>
#include <sys/time.h>
#include <mntent.h>
#include <sys/mount.h>
#include <device-node.h>
#include <bundle.h>
#include <eventsystem.h>

#include "dd-deviced.h"
#include "core/log.h"
#include "core/launch.h"
#include "core/config-parser.h"
#include "core/device-notifier.h"
#include "core/device-idler.h"
#include "core/common.h"
#include "core/devices.h"
#include "core/launch.h"
#include "display/poll.h"
#include "display/setting.h"
#include "core/edbus-handler.h"
#include "display/core.h"
#include "power-handler.h"
#include "apps/apps.h"
#include "shared/deviced-systemd.h"
#include "boot.h"

#define POWEROFF_DURATION		2
#define MAX_RETRY			2
#define POWEROFF_WAIT_RESOURCED (0.5*1000) /* 0.5 seconds */

#define SIGNAL_POWEROFF_STATE	"ChangeState"

#define UMOUNT_RW_PATH "/opt/usr"

#define POWER_FLAG_POWEROFF "/run/"POWER_POWEROFF
#define POWER_FLAG_REBOOT   "/run/"POWER_REBOOT

#define POWER_CONF_FILE "/etc/deviced/power.conf"

struct power_flag {
	enum poweroff_type type;
	const char *path;
} power_flags[] = {
	{ POWER_OFF_DIRECT,  POWER_FLAG_POWEROFF },
	{ POWER_OFF_RESTART, POWER_FLAG_REBOOT   },
};

static struct timeval tv_start_poweroff;

static dd_list *poweroff_options;

static int get_poweroff_type_enum(char *type, enum poweroff_type *val)
{
	size_t len;

	if (!type || !val)
		return -EINVAL;

	len = strlen(type) + 1;

	if (!strncmp(POWER_POWEROFF, type, len)) {
		*val = POWER_OFF_DIRECT;
		return 0;
	}

	if (!strncmp(PWROFF_POPUP, type, len)) {
		*val = POWER_OFF_POPUP;
		return 0;
	}

	if (!strncmp(POWER_REBOOT, type, len)) {
		*val = POWER_OFF_RESTART;
		return 0;
	}

	_E("Invalid poweroff type (%s)", type);
	return -EINVAL;
}

static int find_poweroff_option(char *type, char *option, struct power_option **opt)
{
	int ret;
	dd_list *l;
	struct power_option *elem;
	enum poweroff_type type_e;

	if (!type || !opt)
		return -EINVAL;

	ret = get_poweroff_type_enum(type, &type_e);
	if (ret < 0) {
		_E("Failed to get type enum value (%d)", ret);
		return ret;
	}

	DD_LIST_FOREACH(poweroff_options, l, elem) {
		if (elem->type != type_e)
			continue;
		if (option) {
			if (elem->option == NULL)
				continue;
			if (strncmp(elem->option, option, strlen(elem->option)))
				continue;
		} else {
			if (elem->option != NULL)
				continue;
		}
		if (elem->option) {
			free(elem->option);
			elem->option = (option ? strdup(option) : NULL);
		}
		*opt = elem;
		return 0;
	}

	return -ENOENT;
}

static int telephony_off(struct power_option *opt)
{
	const struct device_ops *tele;

	tele = find_device("telephony");
	if (check_default(tele))
		return -ENODEV;

	return device_execute(tele, opt);
}

static int app_privileged(pid_t pid)
{
	char attr[64];
	int ret;
	size_t len;

	ret = get_privilege(pid, attr, sizeof(attr));
	if (ret < 0) {
		_E("Failed to get privilege of PID(%d)", pid);
		return 0;
	}

	len = strlen(attr);

	if (!strncmp("System::Privileged", attr, len + 1))
		return 1;

	if (strstr(attr, "User::Pkg::") == attr)
		return 1;

	return 0;
}

static int stop_systemd_journald(void)
{
	int ret;

	ret = deviced_systemd_stop_unit("systemd-journald.socket");
	if (ret < 0) {
		_E("failed to stop 'systemd-journald.socket'");
		return ret;
	}

	ret |= deviced_systemd_stop_unit("systemd-journald.service");
	if (ret < 0) {
		_E("failed to stop 'systemd-journald.service'");
		return ret;
	}

	return 0;
}

static void stop_systemd_service(void)
{
	_I("Request to stop systemd service to resourced");
	dbus_method_sync_timeout(RESOURCED_BUS_NAME,
			RESOURCED_PATH_PROCESS,
			RESOURCED_INTERFACE_PROCESS,
			"PrePoweroff",
			NULL, NULL, POWEROFF_WAIT_RESOURCED);
}

/* umount usr data partition */
static void unmount_rw_partition(void)
{
	umount_partition(UMOUNT_RW_PATH, true);
}

static void powerdown(void)
{
	static int wait;
	struct timeval now;
	int poweroff_duration = POWEROFF_DURATION;
	int check_duration = 0;
	char *buf;

	stop_systemd_journald();
	sync();

	buf = getenv("PWROFF_DUR");
	if (buf != NULL && strlen(buf) < 1024)
		poweroff_duration = atoi(buf);
	if (poweroff_duration < 0 || poweroff_duration > 60)
		poweroff_duration = POWEROFF_DURATION;
	gettimeofday(&now, NULL);
	check_duration = now.tv_sec - tv_start_poweroff.tv_sec;
	while (check_duration < poweroff_duration) {
		if (wait == 0) {
			_I("wait poweroff %d %d", check_duration, poweroff_duration);
			wait = 1;
		}
		usleep(100000);
		gettimeofday(&now, NULL);
		check_duration = now.tv_sec - tv_start_poweroff.tv_sec;
		if (check_duration < 0)
			break;
	}

	watchdog_notify();

	if (!is_emulator())
		unmount_rw_partition();
}

static void reboot_with_option(int cmd, char *option)
{
	if (!cmd)
		return;
	if (syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
				cmd, (void *)option) < 0)
		_E("Failed to reboot with option (%d)", errno);
}

static void poweroff(void)
{
	_I("Power off !!!");
	reboot_with_option(LINUX_REBOOT_CMD_POWER_OFF, NULL);
}

static void restart(char *option)
{
	if (option) {
		_I("Restart with option %s !!!", option);
		reboot_with_option(LINUX_REBOOT_CMD_RESTART2, option);
	} else {
		_I("Restart !!!");
		reboot_with_option(LINUX_REBOOT_CMD_RESTART, NULL);
	}
}

void powerdown_ap(struct power_option *opt)
{
	static int off;

	if (off)
		return;
	off = 1;

	_I("POWER DOWN: %s option(%s)", opt->name, opt->option);
	powerdown();

	if (opt->type == POWER_OFF_DIRECT) {
		poweroff();
		return;
	}

	if (opt->type == POWER_OFF_RESTART) {
		restart(opt->option);
		return ;
	}

	_E("Invalid type (%d)", opt->name);
}

static void system_shutdown_send_system_event(void)
{
	bundle *b;
	const char *str = EVT_VAL_SYSTEM_SHUTDOWN_TRUE;

	b = bundle_create();
	bundle_add_str(b, EVT_KEY_SYSTEM_SHUTDOWN, str);
	eventsystem_send_system_event(SYS_EVENT_SYSTEM_SHUTDOWN, b);
	bundle_free(b);
}

static void poweroff_start_animation(void)
{
	int ret;

	ret = deviced_systemd_start_unit("shutdown-animation.service");
	if (ret < 0)
		_E("Failed to start shutdown animation");

	gettimeofday(&tv_start_poweroff, NULL);
}

static int pwroff_popup(void)
{
	return launch_system_app(APP_POWERKEY,
			2, APP_KEY_TYPE, APP_POWERKEY);
}

static void disable_display(void)
{
	const struct device_ops *display_device_ops = NULL;
	FIND_DEVICE_VOID(display_device_ops, "display");
	pm_change_internal(INTERNAL_LOCK_POWEROFF, LCD_NORMAL);
	display_device_ops->exit(NULL);
}

static int poweroff_procedure(struct power_option *opt)
{
	int ret;

	if (!opt)
		return -EINVAL;

	disable_display();
	sync();

	watchdog_notify();
	ret = telephony_off(opt);
	if (ret < 0) {
		powerdown_ap(opt);
		return 0;
	}
	return ret;
}

static int booting_done(void *data)
{
	static int done;

	if (data == NULL)
		goto out;

	done = *(int *)data;
out:
	return done;
}

static void poweroff_send_broadcast(int status)
{
	static int old = 0;
	char *arr[1];
	char str_status[32];

	if (old == status)
		return;

	_D("broadcast poweroff %d", status);

	old = status;
	snprintf(str_status, sizeof(str_status), "%d", status);
	arr[0] = str_status;

	/* Need to notify to deviced-vibrator. deviced-vibrator receives ChangeState signal for POWER_OFF_DIRECT and POWER_OFF_RESTART */
	broadcast_edbus_signal(DEVICED_PATH_POWEROFF, DEVICED_INTERFACE_POWEROFF,
			SIGNAL_POWEROFF_STATE, "i", arr);
}

int check_power_flag(void)
{
	char buf[32] = { 0, };
	char *option = NULL;
	int i, index;
	int fd;
	ssize_t len;

	index = -1;
	for (i = 0 ; i < ARRAY_SIZE(power_flags) ; i++) {
		if (access(power_flags[i].path, F_OK) == 0) {
			index = i;
			break;
		}
	}
	if (index < 0)
		return 0;

	if (power_flags[i].type == POWER_OFF_DIRECT) {
		poweroff();
		return 1;
	}

	fd = open(power_flags[i].path, O_RDONLY);
	if (fd >= 0) {
		len = read(fd, buf, sizeof(buf) - 1);
		close(fd);
		while (len > 0 && buf[len - 1] == '\n')
			len--;
		buf[len] = '\0';
		_I("reboot option (%s)", buf);
	}

	if (strlen(buf) > 0)
		option = buf;

	restart(option);

	return 1;
}

static void make_power_flag(enum poweroff_type type, char *option)
{
	const char *path;
	int fd;
	ssize_t len;
	int i;

	path = NULL;
	for (i = 0 ; i < ARRAY_SIZE(power_flags) ; i++) {
		if (power_flags[i].type == type) {
			path = power_flags[i].path;
			break;
		}
	}
	if (!path)
		return;

	fd = open(path, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
	if (fd < 0) {
		_E("Failed to create (%s)", path);
		return;
	}

	if (option) {
		len = write(fd, option, strlen(option));
		if (len <= 0)
			_E("Failed to store option (%d)", len < 0 ? errno : len);
	}

	close(fd);
}

/*
 * Notice : Don't change sequence of poweroff_fixed_flow
 *          It will make blocking issue during poweroff
 */
static void poweroff_fixed_flow(enum poweroff_type type)
{
	int off = (int)type;

	system_shutdown_send_system_event();
	poweroff_send_broadcast(off);
	poweroff_start_animation();
	stop_systemd_service();
	device_notify(DEVICE_NOTIFIER_POWEROFF, &off);
}

static void poweroff_idler_cb(void *data)
{
	struct power_option *opt = data;
	int ret;

	if (!opt) {
		_E("power off option is NULL");
		return;
	}

	watchdog_notify();

	if (opt->type == POWER_OFF_POPUP) {
		vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, opt->type);
		pwroff_popup();
		goto out;
	}

	if (opt->type != POWER_OFF_DIRECT &&
		opt->type != POWER_OFF_RESTART) {
		_E("Invalid type (%d)", opt->type);
		return;
	}

	make_power_flag(opt->type, opt->option);

	pm_lock_internal(INTERNAL_LOCK_POWEROFF, LCD_OFF, STAY_CUR_STATE, 0);

	poweroff_fixed_flow(opt->type);

	/* TODO for notify. will be removed asap. */
	vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, opt->type);

	ret = poweroff_procedure(opt);
	if (ret < 0)
		_E("Failed to %s (%d)", opt->name, ret);

out:
	if (update_pm_setting)
		update_pm_setting(SETTING_POWEROFF, opt->type);
}

static int power_execute_pid(char *type, char *option, pid_t pid)
{
	int ret;
	struct power_option *opt;

	if (!type)
		return -EINVAL;

	ret = find_poweroff_option(type, option, &opt);
	if (ret < 0) {
		_E("Failed to find supported %s option %s (%d)", type, (option ? option : "NULL"), ret);
		return ret;
	}

	if (opt->type == POWER_OFF_RESTART) {
		if (!app_privileged(pid)) {
			_E("PID(%d) does not have the permission for reboot", pid);
			return -EPERM;
		}
	}

	ret = add_idle_request(poweroff_idler_cb, opt);
	if (ret < 0) {
		_E("fail to add poweroff idle request : %d", ret);
		return ret;
	}

	return 0;
}

static int power_execute(void *data)
{
	return power_execute_pid((char *)data, NULL, getpid());
}

static DBusMessage *dbus_power_handler(E_DBus_Object *obj, DBusMessage *msg)
{
	DBusError err;
	DBusMessageIter iter;
	DBusMessage *reply;
	pid_t pid;
	int ret;
	int argc;
	char *type_str;

	dbus_error_init(&err);

	if (!dbus_message_get_args(msg, &err,
		    DBUS_TYPE_STRING, &type_str,
		    DBUS_TYPE_INT32, &argc, DBUS_TYPE_INVALID)) {
		_E("there is no message");
		ret = -EINVAL;
		goto out;
	}

	if (argc < 0) {
		_E("message is invalid!");
		ret = -EINVAL;
		goto out;
	}

	pid = get_edbus_sender_pid(msg);
	if (kill(pid, 0) == -1) {
		_E("%d process does not exist, dbus ignored!", pid);
		ret = -ESRCH;
		goto out;
	}

	_I("poweroff PID(%d) requests %s", pid, type_str);
	ret = power_execute_pid(type_str, NULL, pid);

out:
	reply = dbus_message_new_method_return(msg);
	dbus_message_iter_init_append(reply, &iter);
	dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);

	return reply;
}

static DBusMessage *dbus_power_option_handler(E_DBus_Object *obj, DBusMessage *msg)
{
	DBusError err;
	pid_t pid;
	int ret;
	char *type, *option;

	dbus_error_init(&err);

	if (!dbus_message_get_args(msg, &err,
			DBUS_TYPE_STRING, &type,
			DBUS_TYPE_STRING, &option,
			DBUS_TYPE_INVALID)) {
		_E("there is no message");
		ret = -EINVAL;
		goto out;
	}

	pid = get_edbus_sender_pid(msg);
	if (kill(pid, 0) == -1) {
		_E("%d process does not exist, dbus ignored!", pid);
		ret = -ESRCH;
		goto out;
	}

	_I("poweroff PID(%d) requests %s %s", pid, type, option);
	ret = power_execute_pid(type, option, pid);

out:
	return make_reply_message(msg, ret);
}

static DBusMessage *request_reboot(E_DBus_Object *obj, DBusMessage *msg)
{
	DBusMessageIter iter;
	DBusMessage *reply;
	char *str;
	int ret;
	pid_t pid;

	if (!dbus_message_get_args(msg, NULL,
		    DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID)) {
		_E("there is no message");
		ret = -EINVAL;
		goto out;
	}

	pid = get_edbus_sender_pid(msg);

	_I("poweroff PID(%d) requests reboot with command : %s", pid, str);
	ret = power_execute_pid(POWER_REBOOT, NULL, pid);

out:
	reply = dbus_message_new_method_return(msg);
	dbus_message_iter_init_append(reply, &iter);
	dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
	return reply;
}

static const struct edbus_method edbus_methods[] = {
	{ POWER_REBOOT, "si", "i", dbus_power_handler },
	{ "RebootWithOption", "ss", "i", dbus_power_option_handler },
	/* Public API device_power_reboot() calls this dbus method. */
	{ "Reboot",      "s", "i", request_reboot },
	/* Add methods here */
};

static int add_poweroff_option(enum poweroff_type type, char *option)
{
	struct power_option *opt;
	static int index = 0;
	char *name;

	if (type == POWER_OFF_POPUP)
		name = PWROFF_POPUP;
	else if (type == POWER_OFF_DIRECT)
		name = POWER_POWEROFF;
	else if (type == POWER_OFF_RESTART)
		name = POWER_REBOOT;
	else {
		_E("Invalid type (%d)", type);
		return -EINVAL;
	}

	opt = calloc(1, sizeof(struct power_option));
	if (!opt) {
		_E("calloc() failed");
		return -ENOMEM;
	}

	opt->type = type;
	opt->name = strdup(name);
	opt->option = (option ? strdup(option) : NULL);
	opt->index = index++;

	DD_LIST_APPEND(poweroff_options, opt);

	_D("Add %s option (%s)", opt->name, opt->option);

	return 0;
}

static int load_config(struct parse_result *result, void *user_data)
{
	enum poweroff_type type;
	int ret;

	if (MATCH(result->section, "Poweroff"))
		type = POWER_OFF_DIRECT;
	else if (MATCH(result->section, "Reboot"))
		type = POWER_OFF_RESTART;
	else
		return 0;

	if (!MATCH(result->name, "Option"))
		return 0;

	ret = add_poweroff_option(type, result->value);
	if (ret < 0) {
		_E("Failed to add %s option (%s)", result->section, result->value);
		return ret;
	}

	return 0;
}

static void power_init(void *data)
{
	int ret;

	/* init dbus interface */
	ret = register_edbus_method(DEVICED_PATH_REBOOT,
			edbus_methods, ARRAY_SIZE(edbus_methods));
	if (ret < 0)
		_E("fail to init edbus method(%d)", ret);

	add_booting_done_handler(NULL);

	register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);

	add_poweroff_option(POWER_OFF_DIRECT, NULL);
	add_poweroff_option(POWER_OFF_RESTART, NULL);
	add_poweroff_option(POWER_OFF_POPUP, NULL);

	ret = config_parse(POWER_CONF_FILE, load_config, NULL);
	if (ret < 0)
		_E("Failed to load power off config (%d)", ret);
}

static const struct device_ops power_device_ops = {
	.name     = POWER_OPS_NAME,
	.init     = power_init,
	.execute  = power_execute,
};

DEVICE_OPS_REGISTER(&power_device_ops)