~ubuntu-branches/ubuntu/vivid/xfce4-session/vivid-proposed

« back to all changes in this revision

Viewing changes to xfce4-session/xfsm-systemd.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2013-07-01 19:35:39 UTC
  • mfrom: (4.2.15 sid)
  • Revision ID: package-import@ubuntu.com-20130701193539-tqwsq1uwaau0sxpi
Tags: 4.10.1-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - debian/patches:
    + xubuntu_ignore-gdm-lang.patch: do not set $LANG to $GDM_LANG, there's
      already an xsession script to do that, and $GDM_LANG might not contain
      a valid locale code.
    + xubuntu_set-xdg-current-desktop.patch: added. Taken from xfce4-utils,
      export XDG_CURRENT_DESKTOP=XFCE, can be useful with alacarte and
      gnome-menus. lp #927172

* debian/patches:
  - xfsm-startup-random-crash.patch: dropped, included in 4.10.1.
  - xubuntu_ignore-gdm-lang.patch: updated to apply cleanly.

* Check for logind support at runtime (lp: #1178373):
  - debian/patches/xubuntu_runtime-logind-support.patch: added.
  - debian/rules:
    + call xdt-autogen after patching
    + clean up autogenerated files modified by xdt-autogen.
  - debian/control:
    + add build-dep on libpolkit-gobject-1-dev (>= 0.100).
    + add build-dep on xfce4-dev-tools and libtool in order to run xdt-autogen
      after patching.
    + don't recommend consolekit anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * Copyright (C) 2012 Christian Hesse
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
17
 * MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <gio/gio.h>
 
23
#include <polkit/polkit.h>
 
24
#include <dbus/dbus-glib.h>
 
25
#include <dbus/dbus-glib-lowlevel.h>
 
26
 
 
27
#include <libxfsm/xfsm-util.h>
 
28
#include <xfce4-session/xfsm-systemd.h>
 
29
 
 
30
 
 
31
 
 
32
#define SYSTEMD_DBUS_NAME               "org.freedesktop.login1"
 
33
#define SYSTEMD_DBUS_PATH               "/org/freedesktop/login1"
 
34
#define SYSTEMD_DBUS_INTERFACE          "org.freedesktop.login1.Manager"
 
35
#define SYSTEMD_REBOOT_ACTION           "Reboot"
 
36
#define SYSTEMD_POWEROFF_ACTION         "PowerOff"
 
37
#define SYSTEMD_SUSPEND_ACTION          "Suspend"
 
38
#define SYSTEMD_HIBERNATE_ACTION        "Hibernate"
 
39
#define SYSTEMD_REBOOT_TEST             "org.freedesktop.login1.reboot"
 
40
#define SYSTEMD_POWEROFF_TEST           "org.freedesktop.login1.power-off"
 
41
#define SYSTEMD_SUSPEND_TEST            "org.freedesktop.login1.suspend"
 
42
#define SYSTEMD_HIBERNATE_TEST          "org.freedesktop.login1.hibernate"
 
43
 
 
44
 
 
45
 
 
46
static void     xfsm_systemd_finalize     (GObject         *object);
 
47
 
 
48
 
 
49
 
 
50
struct _XfsmSystemdClass
 
51
{
 
52
  GObjectClass __parent__;
 
53
};
 
54
 
 
55
struct _XfsmSystemd
 
56
{
 
57
  GObject __parent__;
 
58
 
 
59
  PolkitAuthority *authority;
 
60
  PolkitSubject   *subject;
 
61
};
 
62
 
 
63
 
 
64
 
 
65
G_DEFINE_TYPE (XfsmSystemd, xfsm_systemd, G_TYPE_OBJECT)
 
66
 
 
67
 
 
68
 
 
69
static void
 
70
xfsm_systemd_class_init (XfsmSystemdClass *klass)
 
71
{
 
72
  GObjectClass *gobject_class;
 
73
 
 
74
  gobject_class = G_OBJECT_CLASS (klass);
 
75
  gobject_class->finalize = xfsm_systemd_finalize;
 
76
}
 
77
 
 
78
 
 
79
 
 
80
static void
 
81
xfsm_systemd_init (XfsmSystemd *systemd)
 
82
{
 
83
  systemd->authority = polkit_authority_get_sync (NULL, NULL);
 
84
  systemd->subject = polkit_unix_process_new (getpid());
 
85
}
 
86
 
 
87
 
 
88
 
 
89
static void
 
90
xfsm_systemd_finalize (GObject *object)
 
91
{
 
92
  XfsmSystemd *systemd = XFSM_SYSTEMD (object);
 
93
 
 
94
  g_object_unref (G_OBJECT (systemd->authority));
 
95
  g_object_unref (G_OBJECT (systemd->subject));
 
96
 
 
97
  (*G_OBJECT_CLASS (xfsm_systemd_parent_class)->finalize) (object);
 
98
}
 
99
 
 
100
 
 
101
 
 
102
static gboolean
 
103
xfsm_systemd_lock_screen (GError **error)
 
104
{
 
105
  XfconfChannel *channel;
 
106
  gboolean       ret = TRUE;
 
107
 
 
108
  channel = xfsm_open_config ();
 
109
  if (xfconf_channel_get_bool (channel, "/shutdown/LockScreen", FALSE))
 
110
      ret = g_spawn_command_line_async ("xflock4", error);
 
111
 
 
112
  return ret;
 
113
}
 
114
 
 
115
 
 
116
 
 
117
static gboolean
 
118
xfsm_systemd_can_method (XfsmSystemd  *systemd,
 
119
                         gboolean     *can_method,
 
120
                         const gchar  *method,
 
121
                         GError      **error)
 
122
{
 
123
  PolkitAuthorizationResult *res;
 
124
  GError                    *local_error = NULL;
 
125
 
 
126
  *can_method = FALSE;
 
127
 
 
128
  res = polkit_authority_check_authorization_sync (systemd->authority,
 
129
                                                   systemd->subject,
 
130
                                                   method,
 
131
                                                   NULL,
 
132
                                                   POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
 
133
                                                   NULL,
 
134
                                                   &local_error);
 
135
 
 
136
  if (res == NULL)
 
137
    {
 
138
      g_propagate_error (error, local_error);
 
139
      return FALSE;
 
140
    }
 
141
 
 
142
  *can_method = polkit_authorization_result_get_is_authorized (res)
 
143
                || polkit_authorization_result_get_is_challenge (res);
 
144
 
 
145
  g_object_unref (G_OBJECT (res));
 
146
 
 
147
  return TRUE;
 
148
}
 
149
 
 
150
 
 
151
 
 
152
static gboolean
 
153
xfsm_systemd_try_method (XfsmSystemd  *systemd,
 
154
                         const gchar  *method,
 
155
                         GError      **error)
 
156
{
 
157
  GDBusConnection *bus;
 
158
  GError          *local_error = NULL;
 
159
 
 
160
  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
 
161
  if (G_UNLIKELY (bus == NULL))
 
162
    return FALSE;
 
163
 
 
164
  g_dbus_connection_call_sync (bus,
 
165
                               SYSTEMD_DBUS_NAME,
 
166
                               SYSTEMD_DBUS_PATH,
 
167
                               SYSTEMD_DBUS_INTERFACE,
 
168
                               method,
 
169
                               g_variant_new ("(b)", TRUE),
 
170
                               NULL, 0, G_MAXINT, NULL,
 
171
                               &local_error);
 
172
 
 
173
  g_object_unref (G_OBJECT (bus));
 
174
 
 
175
  if (local_error != NULL)
 
176
    {
 
177
      g_propagate_error (error, local_error);
 
178
      return FALSE;
 
179
    }
 
180
 
 
181
  return TRUE;
 
182
}
 
183
 
 
184
 
 
185
 
 
186
XfsmSystemd *
 
187
xfsm_systemd_get (void)
 
188
{
 
189
  static XfsmSystemd *object = NULL;
 
190
 
 
191
  if (G_LIKELY (object != NULL))
 
192
    {
 
193
      g_object_ref (G_OBJECT (object));
 
194
    }
 
195
  else
 
196
    {
 
197
      object = g_object_new (XFSM_TYPE_SYSTEMD, NULL);
 
198
      g_object_add_weak_pointer (G_OBJECT (object), (gpointer) &object);
 
199
    }
 
200
 
 
201
  return object;
 
202
}
 
203
 
 
204
 
 
205
 
 
206
gboolean
 
207
xfsm_systemd_try_restart (XfsmSystemd  *systemd,
 
208
                          GError      **error)
 
209
{
 
210
  return xfsm_systemd_try_method (systemd,
 
211
                                  SYSTEMD_REBOOT_ACTION,
 
212
                                  error);
 
213
}
 
214
 
 
215
 
 
216
 
 
217
gboolean
 
218
xfsm_systemd_try_shutdown (XfsmSystemd  *systemd,
 
219
                           GError      **error)
 
220
{
 
221
  return xfsm_systemd_try_method (systemd,
 
222
                                  SYSTEMD_POWEROFF_ACTION,
 
223
                                  error);
 
224
}
 
225
 
 
226
 
 
227
 
 
228
gboolean
 
229
xfsm_systemd_try_suspend (XfsmSystemd  *systemd,
 
230
                          GError      **error)
 
231
{
 
232
  if (!xfsm_systemd_lock_screen (error))
 
233
    return FALSE;
 
234
 
 
235
  return xfsm_systemd_try_method (systemd,
 
236
                                  SYSTEMD_SUSPEND_ACTION,
 
237
                                  error);
 
238
}
 
239
 
 
240
 
 
241
 
 
242
gboolean
 
243
xfsm_systemd_try_hibernate (XfsmSystemd  *systemd,
 
244
                            GError      **error)
 
245
{
 
246
  if (!xfsm_systemd_lock_screen (error))
 
247
    return FALSE;
 
248
 
 
249
  return xfsm_systemd_try_method (systemd,
 
250
                                  SYSTEMD_HIBERNATE_ACTION,
 
251
                                  error);
 
252
}
 
253
 
 
254
 
 
255
 
 
256
gboolean
 
257
xfsm_systemd_can_restart (XfsmSystemd  *systemd,
 
258
                          gboolean     *can_restart,
 
259
                          GError      **error)
 
260
{
 
261
  return xfsm_systemd_can_method (systemd,
 
262
                                  can_restart,
 
263
                                  SYSTEMD_REBOOT_TEST,
 
264
                                  error);
 
265
}
 
266
 
 
267
 
 
268
 
 
269
gboolean
 
270
xfsm_systemd_can_shutdown (XfsmSystemd  *systemd,
 
271
                           gboolean     *can_shutdown,
 
272
                           GError      **error)
 
273
{
 
274
  return xfsm_systemd_can_method (systemd,
 
275
                                  can_shutdown,
 
276
                                  SYSTEMD_POWEROFF_TEST,
 
277
                                  error);
 
278
}
 
279
 
 
280
 
 
281
 
 
282
gboolean
 
283
xfsm_systemd_can_suspend (XfsmSystemd  *systemd,
 
284
                          gboolean     *can_suspend,
 
285
                          gboolean     *auth_suspend,
 
286
                          GError      **error)
 
287
{
 
288
  gboolean ret = FALSE;
 
289
 
 
290
  ret = xfsm_systemd_can_method (systemd,
 
291
                                 can_suspend,
 
292
                                 SYSTEMD_SUSPEND_TEST,
 
293
                                 error);
 
294
  *auth_suspend = *can_suspend;
 
295
  return ret;
 
296
}
 
297
 
 
298
 
 
299
 
 
300
gboolean
 
301
xfsm_systemd_can_hibernate (XfsmSystemd  *systemd,
 
302
                            gboolean     *can_hibernate,
 
303
                            gboolean     *auth_hibernate,
 
304
                            GError      **error)
 
305
{
 
306
  gboolean ret = FALSE;
 
307
 
 
308
  ret = xfsm_systemd_can_method (systemd,
 
309
                                 can_hibernate,
 
310
                                 SYSTEMD_HIBERNATE_TEST,
 
311
                                 error);
 
312
  *auth_hibernate = *can_hibernate;
 
313
  return ret;
 
314
}