aboutsummaryrefslogtreecommitdiffstats
path: root/meta-xfce/recipes-xfce/xfce4-session/xfce4-session/0001-Add-shutdown-reboot-functionality-for-systemd-bug-87.patch
blob: ca5c3622297018b1ed8e1153640ab4a6a33f9b15 (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
From ae28aef315a7a6b90f1649ce6d1f30b842791cbf Mon Sep 17 00:00:00 2001
From: Nick Schermer <nick@xfce.org>
Date: Sun, 4 Nov 2012 21:23:36 +0100
Subject: [PATCH] Add shutdown/reboot functionality for systemd (bug #8729).

Based on patch by Christian Hesse and Evangelos Foutras.

Upstream-Status: backport
---
 configure.in                  |    4 +
 xfce4-session/Makefile.am     |   14 ++-
 xfce4-session/xfsm-shutdown.c |   35 ++++++-
 xfce4-session/xfsm-systemd.c  |  229 +++++++++++++++++++++++++++++++++++++++++
 xfce4-session/xfsm-systemd.h  |   55 ++++++++++
 5 files changed, 333 insertions(+), 4 deletions(-)
 create mode 100644 xfce4-session/xfsm-systemd.c
 create mode 100644 xfce4-session/xfsm-systemd.h

diff --git a/configure.in b/configure.in
index 9aa4f8f..134d0f7 100644
--- a/configure.in
+++ b/configure.in
@@ -96,6 +96,10 @@ XDT_CHECK_PACKAGE([DBUS], [dbus-1], [1.1.0])
 XDT_CHECK_PACKAGE([DBUS_GLIB], [dbus-glib-1], [0.84])
 XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [4.9.0])
 
+dnl Check for polkit / systemd integration
+XDT_CHECK_OPTIONAL_PACKAGE([SYSTEMD], [polkit-gobject-1], [0.100],
+                           [systemd], [Systemd support (through polit)])
+
 dnl Check for debugging support
 XDT_FEATURE_DEBUG([xfsm_debug_default])
 
diff --git a/xfce4-session/Makefile.am b/xfce4-session/Makefile.am
index c015154..f748b2b 100644
--- a/xfce4-session/Makefile.am
+++ b/xfce4-session/Makefile.am
@@ -38,8 +38,6 @@ xfce4_session_SOURCES =							\
 	xfsm-compat-gnome.h						\
 	xfsm-compat-kde.c						\
 	xfsm-compat-kde.h						\
-	xfsm-consolekit.c						\
-	xfsm-consolekit.h						\
 	xfsm-dns.c							\
 	xfsm-dns.h							\
 	xfsm-error.c							\
@@ -65,6 +63,16 @@ xfce4_session_SOURCES =							\
 	xfsm-upower.c							\
 	xfsm-upower.h
 
+if HAVE_SYSTEMD
+xfce4_session_SOURCES +=						\
+	xfsm-systemd.c							\
+	xfsm-systemd.h
+else
+xfce4_session_SOURCES +=						\
+	xfsm-consolekit.c						\
+	xfsm-consolekit.h
+endif
+
 xfce4_session_CFLAGS =							\
 	$(LIBSM_CFLAGS)							\
 	$(LIBX11_CFLAGS)						\
@@ -72,6 +80,7 @@ xfce4_session_CFLAGS =							\
 	$(DBUS_CFLAGS)							\
 	$(DBUS_GLIB_CFLAGS)						\
 	$(LIBWNCK_CFLAGS)						\
+	$(SYSTEMD_CFLAGS)						\
 	$(XFCONF_CFLAGS)						\
 	$(GMODULE_CFLAGS)						\
 	$(PLATFORM_CFLAGS)
@@ -91,6 +100,7 @@ xfce4_session_LDADD =							\
 	$(DBUS_LIBS)							\
 	$(DBUS_GLIB_LIBS)						\
 	$(LIBWNCK_LIBS)							\
+	$(SYSTEMD_LIBS)							\
 	$(XFCONF_LIBS)							\
 	-lm
 
diff --git a/xfce4-session/xfsm-shutdown.c b/xfce4-session/xfsm-shutdown.c
index d8757a8..4c483a7 100644
--- a/xfce4-session/xfsm-shutdown.c
+++ b/xfce4-session/xfsm-shutdown.c
@@ -66,10 +66,13 @@
 #include <xfce4-session/xfsm-fadeout.h>
 #include <xfce4-session/xfsm-global.h>
 #include <xfce4-session/xfsm-legacy.h>
-#include <xfce4-session/xfsm-consolekit.h>
 #include <xfce4-session/xfsm-upower.h>
 
-
+#ifdef HAVE_SYSTEMD
+#include <xfce4-session/xfsm-systemd.h>
+#else
+#include <xfce4-session/xfsm-consolekit.h>
+#endif
 
 static void xfsm_shutdown_finalize  (GObject      *object);
 static void xfsm_shutdown_sudo_free (XfsmShutdown *shutdown);
@@ -93,7 +96,11 @@ struct _XfsmShutdown
 {
   GObject __parent__;
 
+#ifdef HAVE_SYSTEMD
+  XfsmSystemd    *systemd;
+#else
   XfsmConsolekit *consolekit;
+#endif
   XfsmUPower     *upower;
 
   /* kiosk settings */
@@ -131,7 +138,11 @@ xfsm_shutdown_init (XfsmShutdown *shutdown)
 {
   XfceKiosk *kiosk;
 
+#ifdef HAVE_SYSTEMD
+  shutdown->systemd = xfsm_systemd_get ();
+#else
   shutdown->consolekit = xfsm_consolekit_get ();
+#endif
   shutdown->upower = xfsm_upower_get ();
   shutdown->helper_state = SUDO_NOT_INITIAZED;
   shutdown->helper_require_password = FALSE;
@@ -150,7 +161,11 @@ xfsm_shutdown_finalize (GObject *object)
 {
   XfsmShutdown *shutdown = XFSM_SHUTDOWN (object);
 
+#ifdef HAVE_SYSTEMD
+  g_object_unref (G_OBJECT (shutdown->systemd));
+#else
   g_object_unref (G_OBJECT (shutdown->consolekit));
+#endif
   g_object_unref (G_OBJECT (shutdown->upower));
 
   /* close down helper */
@@ -641,7 +656,11 @@ xfsm_shutdown_try_restart (XfsmShutdown  *shutdown,
   if (shutdown->helper_state == SUDO_AVAILABLE)
     return xfsm_shutdown_sudo_try_action (shutdown, XFSM_SHUTDOWN_RESTART, error);
   else
+#ifdef HAVE_SYSTEMD
+    return xfsm_systemd_try_restart (shutdown->systemd, error);
+#else
     return xfsm_consolekit_try_restart (shutdown->consolekit, error);
+#endif
 }
 
 
@@ -658,7 +677,11 @@ xfsm_shutdown_try_shutdown (XfsmShutdown  *shutdown,
   if (shutdown->helper_state == SUDO_AVAILABLE)
     return xfsm_shutdown_sudo_try_action (shutdown, XFSM_SHUTDOWN_SHUTDOWN, error);
   else
+#ifdef HAVE_SYSTEMD
+    return xfsm_systemd_try_shutdown (shutdown->systemd, error);
+#else
     return xfsm_consolekit_try_shutdown (shutdown->consolekit, error);
+#endif
 }
 
 
@@ -698,7 +721,11 @@ xfsm_shutdown_can_restart (XfsmShutdown  *shutdown,
       return TRUE;
     }
 
+#ifdef HAVE_SYSTEMD
+  if (xfsm_systemd_can_restart (shutdown->systemd, can_restart, error))
+#else
   if (xfsm_consolekit_can_restart (shutdown->consolekit, can_restart, error))
+#endif
     return TRUE;
 
   if (xfsm_shutdown_sudo_init (shutdown, error))
@@ -725,7 +752,11 @@ xfsm_shutdown_can_shutdown (XfsmShutdown  *shutdown,
       return TRUE;
     }
 
+#ifdef HAVE_SYSTEMD
+  if (xfsm_systemd_can_shutdown (shutdown->systemd, can_shutdown, error))
+#else
   if (xfsm_consolekit_can_shutdown (shutdown->consolekit, can_shutdown, error))
+#endif
     return TRUE;
 
   if (xfsm_shutdown_sudo_init (shutdown, error))
diff --git a/xfce4-session/xfsm-systemd.c b/xfce4-session/xfsm-systemd.c
new file mode 100644
index 0000000..7bdd39d
--- /dev/null
+++ b/xfce4-session/xfsm-systemd.c
@@ -0,0 +1,229 @@
+/*-
+ * Copyright (C) 2012 Christian Hesse
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <gio/gio.h>
+#include <polkit/polkit.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <xfce4-session/xfsm-systemd.h>
+
+
+
+#define SYSTEMD_DBUS_NAME               "org.freedesktop.login1"
+#define SYSTEMD_DBUS_PATH               "/org/freedesktop/login1"
+#define SYSTEMD_DBUS_INTERFACE          "org.freedesktop.login1.Manager"
+#define SYSTEMD_REBOOT_ACTION           "Reboot"
+#define SYSTEMD_POWEROFF_ACTION         "PowerOff"
+#define SYSTEMD_REBOOT_TEST             "org.freedesktop.login1.reboot"
+#define SYSTEMD_POWEROFF_TEST           "org.freedesktop.login1.power-off"
+
+
+
+static void     xfsm_systemd_finalize     (GObject         *object);
+
+
+
+struct _XfsmSystemdClass
+{
+  GObjectClass __parent__;
+};
+
+struct _XfsmSystemd
+{
+  GObject __parent__;
+
+  PolkitAuthority *authority;
+  PolkitSubject   *subject;
+};
+
+
+
+G_DEFINE_TYPE (XfsmSystemd, xfsm_systemd, G_TYPE_OBJECT)
+
+
+
+static void
+xfsm_systemd_class_init (XfsmSystemdClass *klass)
+{
+  GObjectClass *gobject_class;
+
+  gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->finalize = xfsm_systemd_finalize;
+}
+
+
+
+static void
+xfsm_systemd_init (XfsmSystemd *systemd)
+{
+  systemd->authority = polkit_authority_get_sync (NULL, NULL);
+  systemd->subject = polkit_unix_process_new (getpid());
+}
+
+
+
+static void
+xfsm_systemd_finalize (GObject *object)
+{
+  XfsmSystemd *systemd = XFSM_SYSTEMD (object);
+
+  g_object_unref (G_OBJECT (systemd->authority));
+  g_object_unref (G_OBJECT (systemd->subject));
+
+  (*G_OBJECT_CLASS (xfsm_systemd_parent_class)->finalize) (object);
+}
+
+
+static gboolean
+xfsm_systemd_can_method (XfsmSystemd  *systemd,
+                         gboolean     *can_method,
+                         const gchar  *method,
+                         GError      **error)
+{
+  PolkitAuthorizationResult *res;
+  GError                    *local_error = NULL;
+
+  *can_method = FALSE;
+
+  res = polkit_authority_check_authorization_sync (systemd->authority,
+                                                   systemd->subject,
+                                                   method,
+                                                   NULL,
+                                                   POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
+                                                   NULL,
+                                                   &local_error);
+
+  if (res == NULL)
+    {
+      g_propagate_error (error, local_error);
+      return FALSE;
+    }
+
+  *can_method = polkit_authorization_result_get_is_authorized (res)
+                || polkit_authorization_result_get_is_challenge (res);
+
+  g_object_unref (G_OBJECT (res));
+
+  return TRUE;
+}
+
+
+
+static gboolean
+xfsm_systemd_try_method (XfsmSystemd  *systemd,
+                         const gchar  *method,
+                         GError      **error)
+{
+  GDBusConnection *bus;
+  GError          *local_error = NULL;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
+  if (G_UNLIKELY (bus == NULL))
+    return FALSE;
+
+  g_dbus_connection_call_sync (bus,
+                               SYSTEMD_DBUS_NAME,
+                               SYSTEMD_DBUS_PATH,
+                               SYSTEMD_DBUS_INTERFACE,
+                               method,
+                               g_variant_new ("(b)", TRUE),
+                               NULL, 0, G_MAXINT, NULL,
+                               &local_error);
+
+  g_object_unref (G_OBJECT (bus));
+
+  if (local_error != NULL)
+    {
+      g_propagate_error (error, local_error);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+
+
+XfsmSystemd *
+xfsm_systemd_get (void)
+{
+  static XfsmSystemd *object = NULL;
+
+  if (G_LIKELY (object != NULL))
+    {
+      g_object_ref (G_OBJECT (object));
+    }
+  else
+    {
+      object = g_object_new (XFSM_TYPE_SYSTEMD, NULL);
+      g_object_add_weak_pointer (G_OBJECT (object), (gpointer) &object);
+    }
+
+  return object;
+}
+
+
+
+gboolean
+xfsm_systemd_try_restart (XfsmSystemd  *systemd,
+                          GError      **error)
+{
+  return xfsm_systemd_try_method (systemd,
+                                  SYSTEMD_REBOOT_ACTION,
+                                  error);
+}
+
+
+
+gboolean
+xfsm_systemd_try_shutdown (XfsmSystemd  *systemd,
+                           GError      **error)
+{
+  return xfsm_systemd_try_method (systemd,
+                                  SYSTEMD_POWEROFF_ACTION,
+                                  error);
+}
+
+
+
+gboolean
+xfsm_systemd_can_restart (XfsmSystemd  *systemd,
+                          gboolean     *can_restart,
+                          GError      **error)
+{
+  return xfsm_systemd_can_method (systemd,
+                                  can_restart,
+                                  SYSTEMD_REBOOT_TEST,
+                                  error);
+}
+
+
+
+gboolean
+xfsm_systemd_can_shutdown (XfsmSystemd  *systemd,
+                           gboolean     *can_shutdown,
+                           GError      **error)
+{
+  return xfsm_systemd_can_method (systemd,
+                                  can_shutdown,
+                                  SYSTEMD_POWEROFF_TEST,
+                                  error);
+}
diff --git a/xfce4-session/xfsm-systemd.h b/xfce4-session/xfsm-systemd.h
new file mode 100644
index 0000000..8223622
--- /dev/null
+++ b/xfce4-session/xfsm-systemd.h
@@ -0,0 +1,55 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2012 Christian Hesse
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __XFSM_SYSTEMD_H__
+#define __XFSM_SYSTEMD_H__
+
+typedef struct _XfsmSystemdClass XfsmSystemdClass;
+typedef struct _XfsmSystemd      XfsmSystemd;
+
+#define XFSM_TYPE_SYSTEMD            (xfsm_systemd_get_type ())
+#define XFSM_SYSTEMD(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), XFSM_TYPE_SYSTEMD, XfsmSystemd))
+#define XFSM_SYSTEMD_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), XFSM_TYPE_SYSTEMD, XfsmSystemdClass))
+#define XFSM_IS_SYSTEMD(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XFSM_TYPE_SYSTEMD))
+#define XFSM_IS_SYSTEMD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), XFSM_TYPE_SYSTEMD))
+#define XFSM_SYSTEMD_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), XFSM_TYPE_SYSTEMD, XfsmSystemdClass))
+
+GType           xfsm_systemd_get_type     (void) G_GNUC_CONST;
+
+XfsmSystemd *xfsm_systemd_get          (void);
+
+gboolean     xfsm_systemd_try_restart  (XfsmSystemd  *systemd,
+                                        GError      **error);
+
+gboolean     xfsm_systemd_try_shutdown (XfsmSystemd  *systemd,
+                                        GError      **error);
+
+gboolean     xfsm_systemd_can_restart  (XfsmSystemd  *systemd,
+                                        gboolean     *can_restart,
+                                        GError      **error);
+
+gboolean     xfsm_systemd_can_shutdown (XfsmSystemd  *systemd,
+                                        gboolean     *can_shutdown,
+                                        GError      **error);
+
+G_END_DECLS
+
+#endif  /* __XFSM_SYSTEMD_H__ */
-- 
1.7.4.4