1 From d79c28572185335bd7c0d4f8bbce63d3904d1d6c Mon Sep 17 00:00:00 2001
2 From: Eric Koegel <eric.koegel@gmail.com>
3 Date: Thu, 9 Oct 2014 19:30:59 +0300
4 Subject: [PATCH] Start adding the Suspend/Hibernate DBUS API
6 The initial work in progress to add in suspend/hibernate and the
9 data/org.freedesktop.consolekit.policy | 35 +++++
10 src/ck-log-event.h | 2 +
11 src/ck-manager.c | 218 +++++++++++++++++++++++++++++
12 src/ck-manager.h | 10 ++
13 src/org.freedesktop.ConsoleKit.Manager.xml | 28 ++++
14 5 files changed, 293 insertions(+)
16 diff --git a/data/org.freedesktop.consolekit.policy b/data/org.freedesktop.consolekit.policy
17 index a709b39..76a6cc8 100644
18 --- a/data/org.freedesktop.consolekit.policy
19 +++ b/data/org.freedesktop.consolekit.policy
20 @@ -45,4 +45,39 @@ Policy definitions for ConsoleKit
24 + <action id="org.freedesktop.consolekit.system.suspend">
25 + <description>Suspend the system</description>
26 + <message>System policy prevents suspending the system</message>
28 + <allow_inactive>no</allow_inactive>
29 + <allow_active>yes</allow_active>
33 + <action id="org.freedesktop.consolekit.system.suspend-multiple-users">
34 + <description>Suspend the system when multiple users are logged in</description>
35 + <message>System policy prevents suspending the system when other users are logged in</message>
37 + <allow_inactive>no</allow_inactive>
38 + <allow_active>auth_admin_keep</allow_active>
42 + <action id="org.freedesktop.consolekit.system.hibernate">
43 + <description>Hibernate the system</description>
44 + <message>System policy prevents hibernating the system</message>
46 + <allow_inactive>no</allow_inactive>
47 + <allow_active>yes</allow_active>
51 + <action id="org.freedesktop.consolekit.system.hibernate-multiple-users">
52 + <description>Hibernate the system when multiple users are logged in</description>
53 + <message>System policy prevents hibernating the system when other users are logged in</message>
55 + <allow_inactive>no</allow_inactive>
56 + <allow_active>auth_admin_keep</allow_active>
60 diff --git a/src/ck-log-event.h b/src/ck-log-event.h
61 index 65571f0..dd307f7 100644
62 --- a/src/ck-log-event.h
63 +++ b/src/ck-log-event.h
64 @@ -33,6 +33,8 @@ typedef enum
65 CK_LOG_EVENT_SYSTEM_START,
66 CK_LOG_EVENT_SYSTEM_STOP,
67 CK_LOG_EVENT_SYSTEM_RESTART,
68 + CK_LOG_EVENT_SYSTEM_SUSPEND,
69 + CK_LOG_EVENT_SYSTEM_HIBERNATE,
70 CK_LOG_EVENT_SYSTEM_RUNLEVEL_CHANGED,
71 CK_LOG_EVENT_SEAT_ADDED,
72 CK_LOG_EVENT_SEAT_REMOVED,
73 diff --git a/src/ck-manager.c b/src/ck-manager.c
74 index c6713b8..eff383c 100644
75 --- a/src/ck-manager.c
76 +++ b/src/ck-manager.c
81 +log_system_suspend_event (CkManager *manager)
87 + memset (&event, 0, sizeof (CkLogEvent));
89 + event.type = CK_LOG_EVENT_SYSTEM_SUSPEND;
90 + g_get_current_time (&event.timestamp);
93 + res = ck_event_logger_queue_event (manager->priv->logger, &event, &error);
95 + g_debug ("Unable to log event: %s", error->message);
96 + g_error_free (error);
99 + /* FIXME: in this case we should block and wait for log to flush */
103 +log_system_hibernate_event (CkManager *manager)
109 + memset (&event, 0, sizeof (CkLogEvent));
111 + event.type = CK_LOG_EVENT_SYSTEM_HIBERNATE;
112 + g_get_current_time (&event.timestamp);
115 + res = ck_event_logger_queue_event (manager->priv->logger, &event, &error);
117 + g_debug ("Unable to log event: %s", error->message);
118 + g_error_free (error);
121 + /* FIXME: in this case we should block and wait for log to flush */
125 log_seat_session_added_event (CkManager *manager,
128 @@ -1502,6 +1547,182 @@
133 +do_suspend (CkManager *manager,
134 + DBusGMethodInvocation *context,
140 + g_debug ("ConsoleKit preforming Suspend");
142 + log_system_suspend_event (manager);
145 + res = g_spawn_command_line_async (PREFIX "/lib/ConsoleKit/scripts/ck-system-suspend",
150 + g_warning ("Unable to suspend system: %s", error->message);
152 + new_error = g_error_new (CK_MANAGER_ERROR,
153 + CK_MANAGER_ERROR_GENERAL,
154 + "Unable to suspend system: %s", error->message);
155 + dbus_g_method_return_error (context, new_error);
156 + g_error_free (new_error);
158 + g_error_free (error);
160 + dbus_g_method_return (context);
166 + dbus-send --system --dest=org.freedesktop.ConsoleKit \
167 + --type=method_call --print-reply --reply-timeout=2000 \
168 + /org/freedesktop/ConsoleKit/Manager \
169 + org.freedesktop.ConsoleKit.Manager.Suspend
172 +ck_manager_suspend (CkManager *manager,
173 + DBusGMethodInvocation *context)
175 + const char *action;
177 + if (get_system_num_users (manager) > 1) {
178 + action = "org.freedesktop.consolekit.system.suspend-multiple-users";
180 + action = "org.freedesktop.consolekit.system.suspend";
183 + g_debug ("ConsoleKit Suspend: %s", action);
185 +#if defined HAVE_POLKIT
186 + check_polkit_permissions (manager, context, action, do_suspend);
187 +#elif defined ENABLE_RBAC_SHUTDOWN
188 + check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY, do_suspend, NULL);
190 + g_warning ("Compiled without PolicyKit or RBAC support!");
191 + do_suspend(manager, context, NULL);
198 +ck_manager_can_suspend (CkManager *manager,
199 + DBusGMethodInvocation *context)
202 + const char *action;
204 + action = "org.freedesktop.consolekit.system.suspend";
206 +#if defined HAVE_POLKIT
207 + get_polkit_permissions (manager, action, context);
208 +#elif defined ENABLE_RBAC_SHUTDOWN
209 + if (check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY,
211 + dbus_g_method_return (context, TRUE);
213 + dbus_g_method_return (context, FALSE);
221 +do_hibernate (CkManager *manager,
222 + DBusGMethodInvocation *context,
228 + g_debug ("ConsoleKit preforming Hibernate");
230 + log_system_hibernate_event (manager);
233 + res = g_spawn_command_line_async (PREFIX "/lib/ConsoleKit/scripts/ck-system-hibernate",
238 + g_warning ("Unable to hibernate system: %s", error->message);
240 + new_error = g_error_new (CK_MANAGER_ERROR,
241 + CK_MANAGER_ERROR_GENERAL,
242 + "Unable to hibernate system: %s", error->message);
243 + dbus_g_method_return_error (context, new_error);
244 + g_error_free (new_error);
246 + g_error_free (error);
248 + dbus_g_method_return (context);
254 + dbus-send --system --dest=org.freedesktop.ConsoleKit \
255 + --type=method_call --print-reply --reply-timeout=2000 \
256 + /org/freedesktop/ConsoleKit/Manager \
257 + org.freedesktop.ConsoleKit.Manager.Hibernate
260 +ck_manager_hibernate (CkManager *manager,
261 + DBusGMethodInvocation *context)
263 + const char *action;
265 + if (get_system_num_users (manager) > 1) {
266 + action = "org.freedesktop.consolekit.system.hibernate-multiple-users";
268 + action = "org.freedesktop.consolekit.system.hibernate";
271 + g_debug ("ConsoleKit Hibernate: %s", action);
273 +#if defined HAVE_POLKIT
274 + check_polkit_permissions (manager, context, action, do_hibernate);
275 +#elif defined ENABLE_RBAC_SHUTDOWN
276 + check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY, do_hibernate, NULL);
278 + g_warning ("Compiled without PolicyKit or RBAC support!");
279 + do_hibernate(manager, context, NULL);
286 +ck_manager_can_hibernate (CkManager *manager,
287 + DBusGMethodInvocation *context)
290 + const char *action;
292 + action = "org.freedesktop.consolekit.system.hibernate";
294 +#if defined HAVE_POLKIT
295 + get_polkit_permissions (manager, action, context);
296 +#elif defined ENABLE_RBAC_SHUTDOWN
297 + if (check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY,
299 + dbus_g_method_return (context, TRUE);
301 + dbus_g_method_return (context, FALSE);
309 on_seat_active_session_changed_full (CkSeat *seat,
310 CkSession *old_session,
311 diff --git a/src/ck-manager.h b/src/ck-manager.h
312 index 4bd56e8..bfa2c01 100644
313 --- a/src/ck-manager.h
314 +++ b/src/ck-manager.h
316 gboolean ck_manager_restart (CkManager *manager,
317 DBusGMethodInvocation *context);
319 +gboolean ck_manager_suspend (CkManager *manager,
320 + DBusGMethodInvocation *context);
322 +gboolean ck_manager_hibernate (CkManager *manager,
323 + DBusGMethodInvocation *context);
325 gboolean ck_manager_can_stop (CkManager *manager,
326 DBusGMethodInvocation *context);
327 gboolean ck_manager_can_restart (CkManager *manager,
328 DBusGMethodInvocation *context);
330 +gboolean ck_manager_can_suspend (CkManager *manager,
331 + DBusGMethodInvocation *context);
332 +gboolean ck_manager_can_hibernate (CkManager *manager,
333 + DBusGMethodInvocation *context);
335 gboolean ck_manager_get_available_operating_systems (CkManager *manager,
336 DBusGMethodInvocation *context);
337 gboolean ck_manager_restart_with_parameters (CkManager *manager,
338 diff --git a/src/org.freedesktop.ConsoleKit.Manager.xml b/src/org.freedesktop.ConsoleKit.Manager.xml
339 index f903b55..ae6a90c 100644
340 --- a/src/org.freedesktop.ConsoleKit.Manager.xml
341 +++ b/src/org.freedesktop.ConsoleKit.Manager.xml
346 + <method name="Suspend">
347 + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
350 + <doc:para>This method initiates a request to suspend the computer system.</doc:para>
355 + <method name="CanSuspend">
356 + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
357 + <arg name="can_suspend" type="b" direction="out"/>
360 + <method name="Hibernate">
361 + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
364 + <doc:para>This method initiates a request to hibernate the computer system.</doc:para>
369 + <method name="CanHibernate">
370 + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
371 + <arg name="can_hibernate" type="b" direction="out"/>
374 <method name="OpenSession">
375 <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
376 <arg name="cookie" direction="out" type="s">