summaryrefslogtreecommitdiff
path: root/service/gtop-dbus-service.c
blob: 062cef4b5189e7c075fa56dc2a1b519ed03cd57f (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
#include <gio/gio.h>
#include <stdlib.h>
#include <stdio.h>
#include <config.h>
#include <glib.h>

#include "gtop-dbus-service.h"

// required for renice
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>

static const gchar object_name[] = "/org/gnome/gtopServer";
static const gchar processes_object_name[] = "/org/gnome/gtopServer/Processes";

#define VERBOSE 1
/* ---------------------------------------------------------------------------------------------------- */

static GDBusNodeInfo *introspection_data = NULL;

/* Introspection data for the service we are exporting */
static const gchar introspection_xml[] =
  "<node name='/org/gnome/gtopServer'>"
  "  <interface name='org.gnome.gtop'>"
  "    <property type='s' name='Version' access='read'/>"
  "  </interface>"
  "  <interface name='org.gnome.gtop.Processes'>"
  "    <method name='SendSignal'>"
  "      <arg name='pid' type='i' direction='in' />"
  "      <arg name='signal' type='i' direction='in' />"
  "    </method>"
  "    <method name='Renice'>"
  "      <arg name='pid' type='i' direction='in' />"
  "      <arg name='nice' type='i' direction='in' />"
  "    </method>"
  "    <method name='GetProperties'>"
  "      <arg name='pid' type='i' direction='in' />"
  "      <arg name='propertyList' type='as' direction='in' />"
  "      <arg name='properties' type='a{sv}' direction='out' />"
  "    </method>"
  "  </interface>"
  "</node>";

/** 
 * Handle a call to a method.  (Parameters should be obvious.)
 */
static void
handle_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
#ifdef VERBOSE
    gchar *paramstr = g_variant_print (parameters, TRUE);
    fprintf (stderr, MSG_PREFIX
             "handle_method_call (%p,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",(invocation),%p)\n",
             connection, sender, object_path, interface_name, method_name, 
             paramstr, user_data);
    g_free (paramstr);
#endif
    if (g_strcmp0 (method_name, "Renice") == 0)
    {
        int pid, prio;
        g_variant_get (parameters, "(ii)", &pid, &prio);
        gint renice_result = setpriority (PRIO_PROCESS, pid, CLAMP(prio, PRIO_MIN, PRIO_MAX));
#ifdef VERBOSE
        fprintf (stderr, MSG_PREFIX"renicing '%d' to '%d' priority returned %d", pid, prio, renice_result);
#endif      
        if (renice_result == 0) 
            g_dbus_method_invocation_return_value (invocation, NULL);
        else {
            switch (errno) {
                case ESRCH: g_dbus_method_invocation_return_error (invocation, 
                                                                   G_IO_ERROR,
                                                                   G_IO_ERROR_NOT_FOUND,
                                                                   MSG_PREFIX "Process '%d' not found",
                                                                   pid);
                break;
                case EACCES:
                case EPERM: g_dbus_method_invocation_return_error (invocation, 
                                                                   G_IO_ERROR,
                                                                   G_IO_ERROR_PERMISSION_DENIED,
                                                                   MSG_PREFIX "Permission denied to change priority of process '%d' to '%d'",
                                                                   pid, prio);
                break;
            }
        }
            
    }

    // Anything else is an error.
    else
    {
  // Default: No such method
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_INVALID_ARGUMENT,
                                             MSG_PREFIX "Invalid method: '%s' on '%s'",
                                             method_name, interface_name);
    }
} // handle_method_call

/**
 * Handle a request to set a property.
 */
static gboolean
handle_set_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GVariant         *value,
                     GError          **error,
                     gpointer          user_data)
{
    // Print an optional log message
#ifdef VERBOSE
    gchar *valstr = g_variant_print (value, TRUE);
    fprintf (stderr, MSG_PREFIX
             "handle_set_property (%p,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",(error),%p)\n",
             connection, sender, object_path, interface_name, property_name, 
             valstr, user_data);
    g_free (valstr);
#endif

    g_set_error (error,
                 G_IO_ERROR,
                 G_IO_ERROR_FAILED,
                 MSG_PREFIX "No such property: '%s'",
                 property_name);
    return 0;
} // handle_set_property

/**
 * Handle a request for a property.  (Parameters should be obvious.)
 */
static GVariant *
handle_get_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GError          **error,
                     gpointer          user_data)
{
  // Print an optional log message
#ifdef VERBOSE
    fprintf (stderr, MSG_PREFIX
             "handle_get_property (%p,\"%s\",\"%s\",\"%s\",\"%s\",(error),%p)\n",
             connection, sender, object_path, interface_name, property_name, 
             user_data);
#endif

    if (g_strcmp0 (property_name, "Version") == 0)
    {
        return g_variant_new_string (LIBGTOP_VERSION);
    }

    // Anything else is an error.
    else
    {
        g_set_error (error,
                     G_IO_ERROR,
                     G_IO_ERROR_FAILED,
                     MSG_PREFIX "Invalid property '%s'",
                     property_name);
        return NULL;
    } // unknown property
} // handle_get_property

/**
 * What to do when the bus gets acquired.
 */
static void
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer        user_data)
{
    static GDBusInterfaceVTable interface_vtable =
      {
        handle_method_call,
        handle_get_property,
        handle_set_property
      };

    guint registration_id;
    GError *error = NULL;

  // A bit of (optional) notification
#ifdef VERBOSE
    fprintf (stderr, MSG_PREFIX "on_bus_acquired (%p, \"%s\", %p)\n", 
             connection, name, user_data);
#endif

    registration_id = 
      g_dbus_connection_register_object (connection,
                                         object_name,
                                         introspection_data->interfaces[0],
                                         &interface_vtable,
                                         NULL,    // Optional user data
                                         NULL,    // Func. for freeing user data
                                         &error);
                                         
                                         

    registration_id = 
      g_dbus_connection_register_object (connection,
                                         processes_object_name,
                                         introspection_data->interfaces[1],
                                         &interface_vtable,
                                         NULL,    // Optional user data
                                         NULL,    // Func. for freeing user data
                                         &error);
                                         
} // on_bus_acquired

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
  // A bit of (optional) notification
#ifdef VERBOSE
    fprintf (stderr, MSG_PREFIX "on_name_acquired (%p, \"%s\", %p)\n", 
             connection, name, user_data);
#endif
} // on_name_acquired

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
  // A bit of (optional) notification
#ifdef VERBOSE
    fprintf (stderr, MSG_PREFIX "on_name_lost (%p, \"%s\", %p)\n", 
             connection, name, user_data);
#endif
  // Things seem to have gone badly wrong, so give up
    exit (1);
} // on_name_lost

int main ( int argc, char ** argv ) {
    guint owner_id;
    GMainLoop *loop;
    
    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
    
    owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                               GTOP_SERVER,
                               G_BUS_NAME_OWNER_FLAGS_NONE,
                               on_bus_acquired,
                               on_name_acquired,
                               on_name_lost,
                               NULL,
                               NULL);
                               
    loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (loop);
    
    // Tell the bus that we're done with the name
    g_bus_unown_name (owner_id);

    // Clean up after ourselves
    g_dbus_node_info_unref (introspection_data);
  
    return 0;
}