~ubuntu-branches/ubuntu/trusty/xfce4-power-manager/trusty-proposed

« back to all changes in this revision

Viewing changes to src/xfpm-systemd.c

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2014-01-20 07:04:09 UTC
  • Revision ID: package-import@ubuntu.com-20140120070409-x4f1ulv0sz2f2742
Tags: 1.2.0-3ubuntu2
* Add patches from debian svn branch to fix systemd. LP: #1222021
* Force run of xdt-autogen to continue to allow ppc64el to build

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * * Copyright (C) 2009-2011 Ali <aliov@xfce.org>
 
3
 * * Copyright (C) 2013 Andreas Müller <schnitzeltony@googlemail.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
 
 
30
#include <dbus/dbus-glib.h>
 
31
#include <polkit/polkit.h>
 
32
 
 
33
#include "xfpm-systemd.h"
 
34
 
 
35
 
 
36
static void xfpm_systemd_finalize   (GObject *object);
 
37
 
 
38
static void xfpm_systemd_get_property (GObject *object,
 
39
                                       guint prop_id,
 
40
                                       GValue *value,
 
41
                                       GParamSpec *pspec);
 
42
 
 
43
#define XFPM_SYSTEMD_GET_PRIVATE(o) \
 
44
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_SYSTEMD, XfpmSystemdPrivate))
 
45
 
 
46
struct XfpmSystemdPrivate
 
47
{
 
48
    gboolean         can_shutdown;
 
49
    gboolean         can_restart;
 
50
 
 
51
    PolkitAuthority *authority;
 
52
    PolkitSubject   *subject;
 
53
};
 
54
 
 
55
enum
 
56
{
 
57
    PROP_0,
 
58
    PROP_CAN_RESTART,
 
59
    PROP_CAN_SHUTDOWN
 
60
};
 
61
 
 
62
G_DEFINE_TYPE (XfpmSystemd, xfpm_systemd, G_TYPE_OBJECT)
 
63
 
 
64
#define SYSTEMD_DBUS_NAME               "org.freedesktop.login1"
 
65
#define SYSTEMD_DBUS_PATH               "/org/freedesktop/login1"
 
66
#define SYSTEMD_DBUS_INTERFACE          "org.freedesktop.login1.Manager"
 
67
#define SYSTEMD_REBOOT_ACTION           "Reboot"
 
68
#define SYSTEMD_POWEROFF_ACTION         "PowerOff"
 
69
#define SYSTEMD_REBOOT_TEST             "org.freedesktop.login1.reboot"
 
70
#define SYSTEMD_POWEROFF_TEST           "org.freedesktop.login1.power-off"
 
71
 
 
72
static void
 
73
xfpm_systemd_class_init (XfpmSystemdClass *klass)
 
74
{
 
75
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
76
 
 
77
    object_class->finalize = xfpm_systemd_finalize;
 
78
 
 
79
    object_class->get_property = xfpm_systemd_get_property;
 
80
 
 
81
    g_object_class_install_property (object_class,
 
82
                                     PROP_CAN_RESTART,
 
83
                                     g_param_spec_boolean ("can-restart",
 
84
                                                           NULL, NULL,
 
85
                                                           FALSE,
 
86
                                                           G_PARAM_READABLE));
 
87
 
 
88
    g_object_class_install_property (object_class,
 
89
                                     PROP_CAN_SHUTDOWN,
 
90
                                     g_param_spec_boolean ("can-shutdown",
 
91
                                                           NULL, NULL,
 
92
                                                           FALSE,
 
93
                                                           G_PARAM_READABLE));
 
94
 
 
95
    g_type_class_add_private (klass, sizeof (XfpmSystemdPrivate));
 
96
}
 
97
 
 
98
static gboolean
 
99
xfpm_systemd_can_method (XfpmSystemd  *systemd,
 
100
                         gboolean     *can_method,
 
101
                         const gchar  *method)
 
102
{
 
103
    PolkitAuthorizationResult *res;
 
104
    GError                    *local_error = NULL;
 
105
 
 
106
    *can_method = FALSE;
 
107
    res = polkit_authority_check_authorization_sync (systemd->priv->authority,
 
108
                                                     systemd->priv->subject,
 
109
                                                     method,
 
110
                                                     NULL,
 
111
                                                     POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
 
112
                                                     NULL,
 
113
                                                     &local_error);
 
114
    if ( local_error )
 
115
    {
 
116
        g_critical ("Unable to get %s : %s", method, local_error->message);
 
117
        g_error_free (local_error);
 
118
    }
 
119
 
 
120
    if(res)
 
121
    {
 
122
        *can_method = polkit_authorization_result_get_is_authorized (res) ||
 
123
                      polkit_authorization_result_get_is_challenge (res);
 
124
        g_object_unref (G_OBJECT (res));
 
125
    }
 
126
 
 
127
    return TRUE;
 
128
}
 
129
 
 
130
static void
 
131
xfpm_systemd_init (XfpmSystemd *systemd)
 
132
{
 
133
    systemd->priv = XFPM_SYSTEMD_GET_PRIVATE (systemd);
 
134
    systemd->priv->authority = polkit_authority_get_sync (NULL, NULL);
 
135
    systemd->priv->subject = polkit_unix_process_new (getpid());
 
136
    systemd->priv->can_shutdown = FALSE;
 
137
    systemd->priv->can_restart  = FALSE;
 
138
 
 
139
    if(systemd->priv->authority && systemd->priv->subject)
 
140
    {
 
141
        xfpm_systemd_can_method (systemd,
 
142
                                 &systemd->priv->can_shutdown,
 
143
                                 SYSTEMD_POWEROFF_TEST);
 
144
        xfpm_systemd_can_method (systemd,
 
145
                                 &systemd->priv->can_restart,
 
146
                                 SYSTEMD_REBOOT_TEST);
 
147
    }
 
148
}
 
149
 
 
150
static void xfpm_systemd_get_property (GObject *object,
 
151
                                           guint prop_id,
 
152
                                           GValue *value,
 
153
                                           GParamSpec *pspec)
 
154
{
 
155
    XfpmSystemd *systemd;
 
156
    systemd = XFPM_SYSTEMD (object);
 
157
 
 
158
    switch (prop_id)
 
159
    {
 
160
        case PROP_CAN_SHUTDOWN:
 
161
            g_value_set_boolean (value, systemd->priv->can_shutdown);
 
162
            break;
 
163
        case PROP_CAN_RESTART:
 
164
            g_value_set_boolean (value, systemd->priv->can_restart);
 
165
            break;
 
166
        default:
 
167
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
168
            break;
 
169
    }
 
170
}
 
171
 
 
172
static void
 
173
xfpm_systemd_finalize (GObject *object)
 
174
{
 
175
    XfpmSystemd *systemd;
 
176
 
 
177
    systemd = XFPM_SYSTEMD (object);
 
178
 
 
179
    if(systemd->priv->authority)
 
180
        g_object_unref (G_OBJECT (systemd->priv->authority));
 
181
    if(systemd->priv->subject)
 
182
        g_object_unref (G_OBJECT (systemd->priv->subject));
 
183
 
 
184
    G_OBJECT_CLASS (xfpm_systemd_parent_class)->finalize (object);
 
185
}
 
186
 
 
187
XfpmSystemd *
 
188
xfpm_systemd_new (void)
 
189
{
 
190
    static gpointer systemd_obj = NULL;
 
191
 
 
192
    if ( G_LIKELY (systemd_obj != NULL ) )
 
193
    {
 
194
        g_object_ref (systemd_obj);
 
195
    }
 
196
    else
 
197
    {
 
198
        systemd_obj = g_object_new (XFPM_TYPE_SYSTEMD, NULL);
 
199
        g_object_add_weak_pointer (systemd_obj, &systemd_obj);
 
200
    }
 
201
 
 
202
    return XFPM_SYSTEMD (systemd_obj);
 
203
}
 
204
 
 
205
static void
 
206
xfpm_systemd_try_method (XfpmSystemd  *systemd,
 
207
                         const gchar  *method,
 
208
                         GError      **error)
 
209
{
 
210
    GDBusConnection *bus;
 
211
    GError          *local_error = NULL;
 
212
 
 
213
    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
 
214
    if (G_LIKELY (bus != NULL))
 
215
    {
 
216
        g_dbus_connection_call_sync (bus,
 
217
                                     SYSTEMD_DBUS_NAME,
 
218
                                     SYSTEMD_DBUS_PATH,
 
219
                                     SYSTEMD_DBUS_INTERFACE,
 
220
                                     method,
 
221
                                     g_variant_new ("(b)", TRUE),
 
222
                                     NULL, 0, G_MAXINT, NULL,
 
223
                                     &local_error);
 
224
        g_object_unref (G_OBJECT (bus));
 
225
 
 
226
        if (local_error != NULL)
 
227
        {
 
228
            g_propagate_error (error, local_error);
 
229
        }
 
230
    }
 
231
}
 
232
 
 
233
void xfpm_systemd_shutdown (XfpmSystemd *systemd, GError **error)
 
234
{
 
235
    xfpm_systemd_try_method (systemd,
 
236
                             SYSTEMD_POWEROFF_ACTION,
 
237
                             error);
 
238
}
 
239
 
 
240
void xfpm_systemd_reboot (XfpmSystemd *systemd, GError **error)
 
241
{
 
242
    xfpm_systemd_try_method (systemd,
 
243
                             SYSTEMD_REBOOT_ACTION,
 
244
                             error);
 
245
}