summaryrefslogtreecommitdiff
path: root/src/power/power-handler.c
blob: 89b6732fccc45bfaf791e68881a16fdb96f6d0da (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
/*
 * 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 <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 <stdbool.h>
#include <signal.h>
#include <systemd/sd-daemon.h>
#include <libsyscommon/dbus-system.h>
#include <libsyscommon/dbus-systemd.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 "display/core.h"
#include "display/display-ops.h"
#include "power-handler.h"
#include "apps/apps.h"
#include "boot.h"

#define POWEROFF_DURATION           4
#define POWEROFF_WAIT_RESOURCED     (0.5*1000) /* 0.5 seconds */
#define POWEROFF_WAIT_MAX           10 /* 10 seconds */
#define POWEROFF_WAIT_SYSTEMD_MS    (30 * 1000/*ms*/) /* 30 seconds */
#define SIGNAL_POWEROFF_STATE       "ChangeState"
#define POWER_CONF_FILE             "/etc/deviced/power.conf"

static struct timeval tv_start_poweroff;
static dd_list *poweroff_options;
static struct power_option poweroff_opt;
static dd_list *poweroff_handles;

static const char *poweroff_type_flagpaths[] = { // index denotes type
	[POWEROFF_TYPE_POWEROFF] = POWER_FLAG_POWEROFF,
	[POWEROFF_TYPE_REBOOT] = POWER_FLAG_REBOOT,
	[POWEROFF_TYPE_EXIT] = POWER_FLAG_EXIT,
};

static const char *poweroff_type_names[] = { // index denotes type
	[POWEROFF_TYPE_POWEROFF] = POWER_POWEROFF,
	[POWEROFF_TYPE_REBOOT] = POWER_REBOOT,
	[POWEROFF_TYPE_EXIT] = POWER_EXIT,
};

enum poweroff_stage {
	POWEROFF_DEFAULT,           /* Default stage, poweroff has not been triggered */
	POWEROFF_TRIGGERED,         /* Poweroff is triggered. Wait timer can be added up to this stage */
	POWEROFF_WAIT_OTHERS,       /* Wait for other processes to clean up their resources */
};

static enum poweroff_stage poweroff_stage;

static const char *poweroff_type_to_name(enum poweroff_type type)
{
	if (type <= 0 || type >= ARRAY_SIZE(poweroff_type_names))
		return NULL;

	return poweroff_type_names[type];
}

static enum poweroff_type poweroff_name_to_type(const char *name)
{
	if (!name)
		goto out;

	for (int i = 0; i < ARRAY_SIZE(poweroff_type_names); i++) {
		if (poweroff_type_names[i] && strcmp(poweroff_type_names[i], name) == 0)
			return i;
	}
out:
	return POWEROFF_TYPE_INVALID;
}

static void poweroff_start_animation(void)
{
	int ret;

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

	gettimeofday(&tv_start_poweroff, NULL);
}

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

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

static int disable_systemd_journald(void)
{
	int ret;

	ret = systemd_stop_unit_async("systemd-journald.socket", NULL);
	if (ret < 0) {
		_E("Failed to stop 'systemd-journald.socket'.");
		return ret;
	}

	ret = systemd_stop_unit_async("systemd-journald.service", NULL);
	if (ret < 0) {
		_E("Failed to stop 'systemd-journald.service'.");
		return ret;
	}
	return 0;
}

/* processes might start to fail abnormally after we kill them via
 * umount_partition_by_kill. Failing services can trigger coredump
 * handler, which might have trouble handling core in inconsistent
 * system state (eg. lack of /opt).  Due to this we disable the
 * coredump handler for the shutdown period.
 */
static bool disable_coredump_handler(void)
{
	int ret = 0;
	int fd = open("/proc/sys/kernel/core_pattern", O_WRONLY);
	if (fd >= 0) {
		ret = write(fd, "/dev/null", sizeof("/dev/null") - 1);
		close(fd);
	}

	bool is_ok = ret > 0;
	_I("Disabling core dumps %s.", is_ok  ? "succeeded" : "failed");

	return is_ok;
}

void poweroff_request_shutdown(void)
{
	const char *method;

	if (poweroff_opt.type == POWEROFF_TYPE_REBOOT)
		method = "Reboot";
	else if (poweroff_opt.type == POWEROFF_TYPE_POWEROFF)
		method = "PowerOff";
	else if (poweroff_opt.type == POWEROFF_TYPE_EXIT)
		method = "Exit";
	else {
		_E("Poweroff invalid type(%d).", poweroff_opt.type);
		return;
	}

	CRITICAL_LOG("Requested %s via systemd.", method);
	dbus_handle_method_sync_timeout(SYSTEMD_DBUS_DEST,
					SYSTEMD_DBUS_PATH,
					SYSTEMD_DBUS_IFACE_MANAGER,
					method,
					NULL,
					NULL,
					POWEROFF_WAIT_SYSTEMD_MS);

	raise(SIGUSR1);
}

static void poweroff_wait_for_seconds(void)
{
	static int wait;
	struct timeval now;
	int poweroff_duration = POWEROFF_DURATION;
	int check_duration = 0;

	watchdog_notify();

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

void poweroff_prepare(void)
{
	int off = poweroff_opt.type;

	if (off == POWEROFF_TYPE_POWEROFF)
		CRITICAL_LOG("Prepare PowerOff.");
	else if (off == POWEROFF_TYPE_REBOOT)
		CRITICAL_LOG("Prepare Reboot.");

	poweroff_notify_resourced();
	disable_systemd_journald();
	disable_coredump_handler();

	poweroff_wait_for_seconds();

	disable_display();

	device_notify(DEVICE_NOTIFIER_POWEROFF, &off);
}

int check_power_flag(void)
{
	for (int i = 0; i < ARRAY_SIZE(poweroff_type_flagpaths); i++) {
		if (access(poweroff_type_flagpaths[i], F_OK) == 0) {
			poweroff_opt.type = i;
			poweroff_opt.option = NULL;
			return 1;
		}
	}

	return 0;
}

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

	if (type <= 0 || type >= ARRAY_SIZE(poweroff_type_flagpaths))
		return;

	path = poweroff_type_flagpaths[type];
	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: %zd", len < 0 ? errno : len);
	}

	close(fd);
}

static void poweroff_remove_handle(pid_t pid)
{
	struct poweroff_handle *handle;
	dd_list *elem;

	DD_LIST_FOREACH(poweroff_handles, elem, handle) {
		if (handle->pid == pid)
			break;
	}

	assert(handle);

	_D("Remove handle pid=%d timeout=%d timeout_id=%d.", handle->pid, handle->timeout, handle->timeout_id);
	DD_LIST_REMOVE(poweroff_handles, handle);

	if (handle->timeout_id) {
		g_source_remove(handle->timeout_id);
		handle->timeout = 0;
		handle->timeout_id = 0;
	}

	free(handle);
}

static gboolean poweroff_wait_timeout_cb(void *data)
{
	char timeout[50] = {0,};
	pid_t pid = (pid_t)((intptr_t)data);

	poweroff_remove_handle(pid);

	/* All other processes finished cleanup. Poweroff is now on standby */
	if (poweroff_stage == POWEROFF_WAIT_OTHERS && DD_LIST_LENGTH(poweroff_handles) == 0) {
		_D("The last poweroff wait timer for pid %d is expired. Poweroff is now on standby.", pid);

		CRITICAL_LOG("Starting poweroff sequence.");

		// Watchdog timeout 90 -> 30 sec to reduce delay from unexpected poweroff failure.
		snprintf(timeout, sizeof(timeout), "WATCHDOG_USEC=%llu", (unsigned long long)POWEROFF_WAIT_SYSTEMD_MS*1000);
		sd_notify(0, timeout);

		make_power_flag(poweroff_opt.type, poweroff_opt.option);

		if (disp_plgn.pm_lock_internal)
			disp_plgn.pm_lock_internal(INTERNAL_LOCK_POWEROFF, LCD_OFF, STAY_CUR_STATE, 0);

		poweroff_prepare();
		poweroff_request_shutdown();

		if (disp_plgn.update_pm_setting)
			disp_plgn.update_pm_setting(SETTING_POWEROFF, poweroff_opt.type);
	} else {
		_D("Poweroff wait timer for pid %d is expired, but keep waiting for others...", pid);
	}

	return G_SOURCE_REMOVE;
}

static gboolean poweroff_start_timers(void *data)
{
	struct poweroff_handle *handle = NULL;
	dd_list *l;
	bool timer_exist = false;
	int pid_alive = 0;

	poweroff_stage = POWEROFF_WAIT_OTHERS;

	DD_LIST_FOREACH(poweroff_handles, l, handle) {
		pid_alive = kill(handle->pid, 0);
		if (pid_alive == -1) {
			_D("Pid(%d) is dead.", handle->pid);
			handle->timeout = 0;
		}

		_D("Run timer, pid=%d timeout=%d timeout_id=%d.", handle->pid, handle->timeout, handle->timeout_id);

		handle->timeout_id = g_timeout_add_seconds(handle->timeout,
				poweroff_wait_timeout_cb,
				(void *)((intptr_t)(handle->pid)));

		timer_exist = true;
	}

	if (timer_exist) {
		return G_SOURCE_REMOVE;
	} else {
		_D("Handle is NULL.");

		handle = (struct poweroff_handle *)malloc(sizeof(struct poweroff_handle));
		if (handle == NULL) {
			_E("Not enough memory.");
			return G_SOURCE_REMOVE;
		}

		handle->pid = getpid();
		handle->timeout = 0;
		handle->timeout_id = g_timeout_add_seconds(handle->timeout,
				poweroff_wait_timeout_cb,
				(void *)((intptr_t)(handle->pid)));

		if (!handle->timeout_id) {
			_E("Failed to timer_add.");
			free(handle);
			return G_SOURCE_REMOVE;
		}

		DD_LIST_APPEND(poweroff_handles, handle);
	}

	_D("Last Timer: timer_id=%d pid=%d timeout=%d", handle->timeout_id, handle->pid, handle->timeout);

	return G_SOURCE_REMOVE;
}

static void system_shutdown_send_system_event(void)
{
	bundle *b;

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

static int poweroff_option_valid(enum poweroff_type type_e, const char *option)
{
	dd_list *l;
	struct power_option *elem;

	DD_LIST_FOREACH(poweroff_options, l, elem) {
		if (elem->type != type_e)
			continue;

		/* Do not match option
		if (option) {
			if (elem->option == NULL)
				continue;
			if (strncmp(elem->option, option, strlen(elem->option)))
				continue;
		} else {
			if (elem->option != NULL)
				continue;
		} */
		return 1;
	}

	return 0;
}

static void poweroff_send_broadcast(int status)
{
	static int old = 0;
	int ret;

	if (old == status)
		return;

	_D("Broadcast poweroff %d.", status);

	old = status;

	/* Need to notify to deviced-vibrator. deviced-vibrator receives ChangeState signal for POWEROFF_TYPE_DIRECT and POWEROFF_TYPE_RESTART */
	ret = dbus_handle_emit_dbus_signal(NULL,
						DEVICED_PATH_POWEROFF,
						DEVICED_INTERFACE_POWEROFF,
						SIGNAL_POWEROFF_STATE,
						g_variant_new("(i)", status));
	if (ret < 0)
		_E("Failed to send dbus signal(%s)", SIGNAL_POWEROFF_STATE);
}

static int power_execute_pid(const char *typename, const char *option)
{
	int ret;

	if (poweroff_stage >= POWEROFF_TRIGGERED) {
		_E("Duplicate poweroff request. Poweroff was already triggered.");
		return -EINVAL;
	}

	enum poweroff_type type_e = poweroff_name_to_type(typename);
	if (type_e == POWEROFF_TYPE_INVALID) {
		_E("Failed to get type enum value(%d).", type_e);
		return -EINVAL;
	}

	if (poweroff_option_valid(type_e, option)) {
		poweroff_opt.type = type_e;
		free(poweroff_opt.option);
		poweroff_opt.option = NULL;
		if (option)
			poweroff_opt.option = strdup(option);
	} else {
		_E("Failed to find supported type(%s). option=%s", typename, (option ? option : "NULL"));
		return -EINVAL;
	}

	ret = vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, poweroff_opt.type);
	if (ret < 0)
		_E("Failed to set vconf value for power off status: %d", vconf_get_ext_errno());

	poweroff_stage = POWEROFF_TRIGGERED;

	/* Poweroff event broadcasting */
	system_shutdown_send_system_event();
	poweroff_send_broadcast(poweroff_opt.type);

	poweroff_start_animation();

	/* Spare time for AddPowerOffWait requests */
	g_timeout_add_seconds(1, poweroff_start_timers, NULL);

	return 0;
}

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

static int check_sender_process(GDBusConnection *conn, const char *sender)
{
	pid_t pid;

	if (sender == NULL || g_dbus_is_name(sender) == FALSE) {
		_E("Invalid sender");
		return -EINVAL;
	}

	pid = dbus_connection_get_sender_pid(conn, sender);
	if (pid == -1 || kill(pid, 0) == -1) {
		_E("Process(%d) does not exist, dbus ignored.", pid);
		return -ESRCH;
	}

	return pid;
}

static GVariant *dbus_power_handler(GDBusConnection *conn,
	const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
	GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
{
	int ret;
	char *type_str;

	g_variant_get(param, "(s)", &type_str);

	ret = check_sender_process(conn, sender);
	if (ret < 0)
		goto out;


	CRITICAL_LOG("Poweroff PID(%d) requests %s.", ret, type_str);
	ret = power_execute_pid(type_str, NULL);

out:
	g_free(type_str);
	return g_variant_new("(i)", ret);
}

static GVariant *dbus_power_option_handler(GDBusConnection *conn,
	const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
	GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
{
	int ret;
	char *type, *option;

	g_variant_get(param, "(ss)", &type, &option);

	ret = check_sender_process(conn, sender);
	if (ret < 0)
		goto out;


	CRITICAL_LOG("Poweroff PID(%d) requests type=%s option=%s.", ret, type, option);
	ret = power_execute_pid(type, option);

out:
	g_free(type);
	g_free(option);
	return g_variant_new("(i)", ret);
}

/* timer can be added before the stage POWEROFF_WAIT_OTHERS */
static GVariant *add_poweroff_time(GDBusConnection *conn,
	const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
	GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
{
	struct poweroff_handle *handle;
	int ret;
	pid_t pid;
	dd_list *l;

	if (poweroff_stage >= POWEROFF_WAIT_OTHERS) {
		_E("It's too late. Poweroff is already in waiting stage.");
		ret = -1;
		goto out;
	}

	ret = check_sender_process(conn, sender);
	if (ret < 0)
		goto out;

	pid = (pid_t)ret;

	CRITICAL_LOG("PID %d requested to a poweroff timer.", pid);

	DD_LIST_FOREACH(poweroff_handles, l, handle) {
		if (handle->pid == pid)
			break;
	}

	if (handle)
		poweroff_remove_handle(pid);

	_D("Make a new handle.");
	handle = (struct poweroff_handle *)malloc(sizeof(struct poweroff_handle));
	if (handle == NULL) {
		_E("Not enough memory.");
		ret = -1;
		goto out;
	}

	handle->pid = pid;
	handle->timeout_id = 0;
	handle->timeout = POWEROFF_WAIT_MAX;

	DD_LIST_APPEND(poweroff_handles, handle);
	_D("Add a new poweroff timer. pid=%d timeout=%d timeout_id=%d)", handle->pid, handle->timeout, handle->timeout_id);

	ret = 0;

out:
	return g_variant_new("(i)", ret);
}

static GVariant *remove_poweroff_time(GDBusConnection *conn,
	const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
	GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
{
	struct poweroff_handle *handle;
	dd_list *l;
	int ret = 0;
	pid_t pid;

	ret = check_sender_process(conn, sender);
	if (ret < 0)
		goto out;

	pid = (pid_t)ret;

	CRITICAL_LOG("PID %d requested to remove poweroff timer.", pid);

	DD_LIST_FOREACH(poweroff_handles, l, handle) {
		if (handle->pid == pid)
			break;
	}

	if (handle) {
		if (handle->timeout_id)
			g_source_remove(handle->timeout_id);
		handle->timeout = 0;
		handle->timeout_id = g_timeout_add_seconds(handle->timeout,
				poweroff_wait_timeout_cb,
				(void *)((intptr_t)(handle->pid)));
	} else {
		_E("Invalid pid(%d).", pid);
		ret = -1;
	}

out:
	return g_variant_new("(i)", ret);
}

static const dbus_method_s dbus_methods[] = {
	{ "PowerOff"          , "s" , "i", dbus_power_handler },
	{ "PowerOffWithOption", "ss", "i", dbus_power_option_handler },
	/* Public API device_power_reboot() calls this dbus method. */
	{ "AddPowerOffWait"   , NULL, "i", add_poweroff_time },
	{ "RemovePowerOffWait", NULL, "i", remove_poweroff_time },
	/* Add methods here */
};

static const dbus_interface_u dbus_interface = {
	.oh = NULL,
	.name = DEVICED_INTERFACE_POWEROFF,
	.methods = dbus_methods,
	.nr_methods = ARRAY_SIZE(dbus_methods),
};

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

	name = poweroff_type_to_name(type);
	if (!name) {
		_E("Invalid type(%d).", type);
		return -EINVAL;
	}

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

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

	DD_LIST_APPEND(poweroff_options, opt);

	_D("Add %s option=%s", 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 = POWEROFF_TYPE_DIRECT;
	else if (MATCH(result->section, "Reboot"))
		type = POWEROFF_TYPE_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 int booting_done(void *data)
{
	static int done;

	if (data == NULL)
		goto out;

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

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

	/* init dbus interface */
	ret = dbus_handle_add_dbus_object(NULL, DEVICED_PATH_POWEROFF, &dbus_interface);
	if (ret < 0)
		_E("Failed to init dbus method: %d", ret);

	add_booting_done_handler(NULL);

	register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);

	add_poweroff_option(POWEROFF_TYPE_POWEROFF, NULL);
	add_poweroff_option(POWEROFF_TYPE_RESTART, NULL);
	add_poweroff_option(POWEROFF_TYPE_EXIT, NULL);

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

	poweroff_stage = POWEROFF_DEFAULT;
}

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

DEVICE_OPS_REGISTER(&power_device_ops)