Added initial processes Dbus object implementation with renice method

This commit is contained in:
Robert Roth
2013-09-03 23:39:56 +03:00
parent 009949a773
commit 6b264b847b
3 changed files with 87 additions and 16 deletions

View File

@@ -4,5 +4,5 @@ noinst_PROGRAMS = gtop-dbus-service
AM_CPPFLAGS= ${GTOP_DBUS_CFLAGS} AM_CPPFLAGS= ${GTOP_DBUS_CFLAGS}
gtop_dbus_service_SOURCES = gtop-dbus-service.c gtop_dbus_service_SOURCES = gtop-dbus-service.h gtop-dbus-service.c
gtop_dbus_service_LDADD = $(GTOP_DBUS_LIBS) gtop_dbus_service_LDADD = $(GTOP_DBUS_LIBS)

View File

@@ -1,23 +1,45 @@
#include <gio/gio.h> #include <gio/gio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <config.h> #include <config.h>
#include <glib.h> #include <glib.h>
#define MSG_PREFIX "[libgtop dbus server] " #include "gtop-dbus-service.h"
static const gchar service[] = "org.gnome.gtopServer"; // 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 object_name[] = "/org/gnome/gtopServer";
static const gchar processes_object_name[] = "/org/gnome/gtopServer/Processes";
#define VERBOSE 1
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
static GDBusNodeInfo *introspection_data = NULL; static GDBusNodeInfo *introspection_data = NULL;
/* Introspection data for the service we are exporting */ /* Introspection data for the service we are exporting */
static const gchar introspection_xml[] = static const gchar introspection_xml[] =
"<node>" "<node name='/org/gnome/gtopServer'>"
" <interface name='org.gnome.gtop'>" " <interface name='org.gnome.gtop'>"
" <property type='s' name='Version' access='read'/>" " <property type='s' name='Version' access='read'/>"
" </interface>" " </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>"; "</node>";
/** /**
@@ -41,13 +63,46 @@ handle_method_call (GDBusConnection *connection,
paramstr, user_data); paramstr, user_data);
g_free (paramstr); g_free (paramstr);
#endif #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 // Default: No such method
g_dbus_method_invocation_return_error (invocation, g_dbus_method_invocation_return_error (invocation,
G_IO_ERROR, G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT, G_IO_ERROR_INVALID_ARGUMENT,
MSG_PREFIX "Invalid method: '%s'", MSG_PREFIX "Invalid method: '%s' on '%s'",
method_name); method_name, interface_name);
}
} // handle_method_call } // handle_method_call
/** /**
@@ -151,6 +206,17 @@ on_bus_acquired (GDBusConnection *connection,
NULL, // Func. for freeing user data NULL, // Func. for freeing user data
&error); &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 } // on_bus_acquired
static void static void
@@ -186,7 +252,7 @@ int main ( int argc, char ** argv ) {
introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL); introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
service, GTOP_SERVER,
G_BUS_NAME_OWNER_FLAGS_NONE, G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired, on_bus_acquired,
on_name_acquired, on_name_acquired,

View File

@@ -0,0 +1,5 @@
#include <glib.h>
#define MSG_PREFIX "[libgtop dbus server] "
static const gchar GTOP_SERVER[] = "org.gnome.gtopServer";