1 #include <libsyscommon/list.h>
4 #include "shared/log.h"
5 #include "shared/device-notifier.h"
6 #include "shared/common.h"
8 #include "sleep-wait.h"
10 #define MAX_WAIT_SECOND 5 /* second */
19 /* inactive: This list contains sleep waits added by AddSleepWait dbus method.
20 * active: All entries of inactive list are moved to this list when sleep is requested.
21 * Each one moves to inactive list on receiving ConfirmWait dbus method.
22 * Sleep will be triggered when it becomes empty. */
23 static GList *inactive;
26 static int max_wait_timer;
27 static int current_sleep_id;
29 static void switch_active_list(void)
34 n = g_list_length(active);
36 _W("Sleep wait remains in active list, %d", n);
43 static gboolean max_wait_expired_cb(void *data)
45 struct sleep_wait *sw;
46 GList *elem, *elem_next;
50 /* force confirmation */
51 if (g_list_length(active) != 0) {
52 SYS_G_LIST_FOREACH_SAFE(active, elem, elem_next, sw) {
53 if (kill(sw->pid, 0) != 0) {
54 _D("Remove not existing sleep wait of pid=%d(%s)", sw->pid, sw->comm);
55 active = g_list_remove(active, sw);
57 } else { /* move to inactive manually */
58 _W("pid=%d(%s) hasn't comfirmed sleep wait", sw->pid, sw->comm);
59 active = g_list_remove(active, sw);
60 inactive = g_list_append(inactive, sw);
65 device_notify(DEVICE_NOTIFIER_POWER_SLEEP_WAIT_DONE, NULL);
67 return G_SOURCE_REMOVE;
71 * 0 if sleep_wait is found in inactive list,
72 * -EBUSY if sleep_wait is found in active list,
73 * -ESRCH if sleep_wait is not found in both list */
74 static int find_sleep_wait(pid_t pid, struct sleep_wait **sw)
76 struct sleep_wait *ret = NULL;
82 SYS_G_LIST_FOREACH(inactive, elem, ret) {
83 if (ret->pid == pid) {
89 SYS_G_LIST_FOREACH(active, elem, ret) {
90 if (ret->pid == pid) {
100 int add_sleep_wait(pid_t pid)
102 struct sleep_wait *sw;
105 ret = find_sleep_wait(pid, &sw);
106 if (ret == -ESRCH) { /* new node */
107 sw = calloc(1, sizeof(struct sleep_wait));
112 get_command(pid, sw->comm, sizeof(sw->comm));
113 inactive = g_list_append(inactive, sw);
115 _D("pid=%d(%s) is added to sleep wait list", sw->pid, sw->comm);
116 } else if (ret == 0 || ret == -EBUSY) {
117 _D("pid=%d(%s) has already been added to sleep wait list", sw->pid, sw->comm);
123 void remove_sleep_wait(pid_t pid)
125 struct sleep_wait *sw = NULL;
128 retval = find_sleep_wait(pid, &sw);
129 if (retval == -ESRCH)
133 _D("pid=%d(%s) requested removing sleep wait", sw->pid, sw->comm);
135 /* remove from where it is contained */
136 active = g_list_remove(active, sw);
137 inactive = g_list_remove(inactive, sw);
141 /* wake unlock when active becomes empty by this operation
142 * during waiting sleep confirmations */
143 if (max_wait_timer != 0 && g_list_length(active) == 0) {
144 g_source_remove(max_wait_timer);
147 device_notify(DEVICE_NOTIFIER_POWER_SLEEP_WAIT_DONE, NULL);
152 int confirm_sleep_wait(pid_t pid, int id)
154 struct sleep_wait *sw;
157 ret = find_sleep_wait(pid, &sw);
159 _E("pid=%d hasn't been added to sleep wait list", pid);
163 /* Multiple confirmation or
164 * confirm request after MAX_WAIT_SECOND */
166 _D("pid=%d(%s) sleep wait has already been confirmed", sw->pid, sw->comm);
170 if (id != current_sleep_id) {
171 _E("Confirm mismatches sleep_id, current=%d, confirm=%d", current_sleep_id, id);
175 /* Move sw from active to inactive list.
176 * Wake unlock if active becomes empty. */
178 active = g_list_remove(active, sw);
179 inactive = g_list_append(inactive, sw);
180 _D("pid=%d(%s) confirmed sleep wait", sw->pid, sw->comm);
182 if (g_list_length(active) == 0) {
183 /* remove max_wait_timer and wake unlock immediately */
184 if (max_wait_timer) {
185 g_source_remove(max_wait_timer);
189 device_notify(DEVICE_NOTIFIER_POWER_SLEEP_WAIT_DONE, NULL);
196 _E("Failed to confirm sleep wait, %d", ret);
201 void start_sleep_wait(int id)
203 if (max_wait_timer) {
204 _E("Already sleep waiting");
208 switch_active_list();
210 /* no need to defer sleep. wake unlock */
211 if (g_list_length(active) == 0) {
212 device_notify(DEVICE_NOTIFIER_POWER_SLEEP_WAIT_DONE, NULL);
216 current_sleep_id = id;
218 _D("Sleep wait is triggered, maximum wait timeout=%ds", MAX_WAIT_SECOND);
219 max_wait_timer = g_timeout_add_seconds(MAX_WAIT_SECOND, max_wait_expired_cb, NULL);
222 void stop_sleep_wait(void)
224 struct sleep_wait *sw;
225 GList *elem, *elem_next;
227 if (max_wait_timer == 0)
230 /* cancel sleep wait */
231 g_source_remove(max_wait_timer);
234 _D("Wake locked during sleep wait, cancel all sleep waits");
236 SYS_G_LIST_FOREACH_SAFE(active, elem, elem_next, sw) {
237 if (kill(sw->pid, 0) != 0) {
238 _D("Remove not existing sleep wait of %d(%s)", sw->pid, sw->comm);
239 active = g_list_remove(active, sw);
241 } else { /* move to inactive manually */
242 active = g_list_remove(active, sw);
243 inactive = g_list_append(inactive, sw);