Monday, August 23, 2010

How to simulate CAPS LOCK press in Xlib

#include "stdio.h"
#include "X11/Xlib.h"
#include "X11/keysym.h"
#include "time.h"

Display *display=NULL;
unsigned int keycode;

int main ()
{
display = XOpenDisplay(NULL);
keycode = XKeysymToKeycode(display, XK_Caps_Lock);

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

/* type something here */

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

return 0;
}


Replace " " -s with < > for header files.
and compile using
gcc main.c -lX11 -lXtst

Retrieving CAPS LOCK info using Xlib

#include
#include

int main() {
Display * d = XOpenDisplay((char*)0);
bool caps_state = false;
if (d) {
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1;
}
std::cout << "caps_state = " << std::endl;
return 0;
}

Compile above code using

g++ main.cpp -L/usr/X11R6/lib -lX11


Thursday, August 19, 2010

DeviceKit

DeviceKit is a modular hardware abstraction layer designed for use in Linux systems that is designed to simplify device management and replace the current monolithic Linux HAL. DeviceKit includes the ability to enumerate system devices and send notifications when hardware is added or removed from the computer system.

DBus Interfaces provided by DeviceKit are following

  • org.freedesktop.DeviceKit.Power - Power interface
  • org.freedesktop.DeviceKit.Power.Device - Device interface
  • org.freedesktop.DeviceKit.Power.QoS- QoS interface

Samples using python

Enumerate all power objects on the system

Running ths script in virtual machine will show only one device AC. Running on real device will show other devices as well e.g. battery etc.

import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
lst = iface.EnumerateDevices()
print str(lst)

Retrieving battery state

State is numberical value with following assignments

  • 0: Unknown
  • 1: Charging
  • 2: Discharging
  • 3: Empty
  • 4: Fully charged
  • 5: Pending charge
  • 6: Pending discharge
    import dbus
    def printPropValue(prop):
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
    iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
    lst = iface.EnumerateDevices()
    id = len(lst) - 1
    proxy1 = bus.get_object('org.freedesktop.DeviceKit.Power', lst\[id\])
    iface1 = dbus.Interface(proxy1, 'org.freedesktop.DBus.Properties')
    val = iface1.Get(lst\[id\], prop)
    print prop + '\t' + str(val)
    printPropValue('State')

Getting Battery Percentage

That to get the battery percentage we need to read "Percentage" parameter of "org.freedesktop.DeviceKit.Power" interface.

Use the same function above passing "Percentage" argument

printPropValue('Percentage')

Help

http://people.freedesktop.org/~hughsient/DeviceKit-power/Device.html

http://people.freedesktop.org/~hughsient/DeviceKit-power/ref-dbus.html

http://hal.freedesktop.org/docs/DeviceKit/

Wednesday, August 18, 2010

How to extract .rpm package content

Install rpmdevtools package
  • sudo yum install rpmdevtools
Then extract
  • rpm2cpio somefile.rpm | cpio -idmv

Thursday, August 12, 2010

PolicyKit

PolicyKit is an operating system component for controlling system-wide privileges in Unix-like operating systems. It provides an roganized way for non-privileged processes to communicate with privileged ones. In contrast to systems such as sudo, it does not grant root permission to an entire process, but rather allows a finer level of control of centralized system policy.

Files shipped with PolicyKit and 3rd party packages (e.g. under package manager control) typically have comments (such as “DO NOT EDIT THIS FILE, it will be overwritten on update”) telling the system administrator that changes will be overwritten on update.

Configuring PolicyKit

Configuration for the Local Authority is read from files in the /etc/polkit-1/localauthority.conf.d directory.

The Local Authority reads files with .pkla extension from all directories located inside the /etc/polkit-1/localauthority and /var/lib/polkit-1/localauthority directories. By default, the following sub-directories are installed.

10-vendor.d Intended for use by the OS vendor.
20-org.d Intended for the organization deploying the OS.
30-site.d Intended for the site deploying the system.
50-local.d Intended for local usage.
90-mandatory.d Intended for the organization deploying the OS.

New direcrtories and can be added or removed. The configuration files are the .pkla files. A .pkla file must be named by using a scheme to ensure that the name is unique, e.g. reverse DNS notation or similar. For example com.mycompany.packagekit.pkla .

Setup .pkla files

Each group in a .pkla file must have a name that is unique within the file it belongs to.

The following keys are are recognized:

Identity

  • A semi-colon separated list of globs to match identities. Each glob should start with unix-user: or unix-group: to specify whether to match on a UNIX user name or a UNIX group name.

Action

  • A semi-colon separated list of globs to match action identifiers.

ResultActive

  • The result to return for subjects in an active local session that matches one or more of the given identities. Allowed values are similar to what can be used in the defaults section of .policy files used to define actions, e.g. yes, no, auth_self, auth_self_keep, auth_admin and auth_admin_keep.

ResultInactive

  • Like ResultActive but instead applies to subjects in inactive local sessions.

ResultAny

  • Like ResultActive but instead applies to any subject.

ReturnValue

  • A semi-colon separated list of key/value pairs (of the form key=value) that are added to the details of authorization result on positive matches.

Sample .pkla file

# Allow "standard users" to do some things without being interrupted by
# password dialogs (TODO: not complete)
#
[Desktop User Permissions]
Identity=unix-group:wetab
Action=org.freedesktop.packagekit.package-install;org.freedesktop.packagekit.package-remove;
ResultAny=no
ResultInactive=no
ResultActive=yes

This configuration file allows packagekit to install and remove packages.

Non secure way of configuring

The policies files are located in /usr/share/polkit-1/actions/"DBUS application name" .

Issue

Anything in /usr isn't considered 'configuration', so any time the PolicyKit is updated modifications will be wiped out and the modifications should be done again. Configuration should be set /etc or /var .

References

http://hal.freedesktop.org/docs/polkit/pklocalauthority.8.html

$ man pklocalauthority

PackageKit

Packagekit is an open source and free suite of software applications designed to provide a consistent and high-level front end for a number of different package management systems. PackageKit itself is a system activated daemon called packagekitd, that abstracts out differences between the different systems. The actual nuts-and-bolts distro tool (yum, apt, conary, etc) is used by PackageKit using compiled and scripted helpers. Those tools (yum, apt, conary, etc) are called the backend for PackageKit. PackageKit isn't meant to replace these tools, instead providing a common set of abstractions that can be used by standard GUI and text mode package managers. It provides C++, python APIs which actually at this currently are not well documented and everything presented here is derived from my studying of the code.

Installation

The recent version of PackageKit is 0.6.6, this is not available in repositories and it should be compiled from sources, which can be found here http://www.packagekit.org/releases/PackageKit-0.6.6.tar.gz . Please check if newer versions are available. While configuring PackageKit dont forget to configure it with following flags

./configure --with-default-backend=yum --enable-yum --prefix=/usr --sysconfdir=/etc --disable-static --disable-local
make
sudo make install

PackageKit daemon is a system activated daemon so you have to point dbus the location of the packagekit's daemon by configuring following file

/usr/share/dbus-1/system-services/org.freedesktop.PackageKit.service

Restart dbus service

sudo /etc/init.d/messagebus restart

PackageKit daemon location is

/usr/libexec/packagekitd

Issue

The attached script failed installing packages with "wetab" user privileges, the reason is not clear yet. That to run script successfully launch it with "sudo".

Raw Dbus API

Issues

The DBus API is defined by PackageKit but it's up to the backends themselves to do the right thing, and support all the different ways of using it. Depending on platform/packagekit version Dbus API may differ, for instance Dbus API in Ubuntu for the same version of PackageKit differed from the Dbus API under MeeGo, as authorof PackageKit said "Ubuntu really needs to update PackageKit to something more up to date."

API

Object path : /org/freedesktop/PackageKit

Interfaces :

  • org.freedesktop.PackageKit
  • org.freedesktop.PackageKit.Transaction

API description can be found here

The latest interface is available in the source tree or on-line.

Help

That to see Dbus API on current platform install Dbus debugger called "d-feet". If after launching "d-feet" does not show /org/freedom/PackageKit object path then launch packagekitd daemon manually with super user privilages: sudo /usr/sbin/packagekitd

Connect to "unix:path=/var/run/dbus/system_bus_socket" to debug SystemBus

High Level Python API

There an python class called "PackageKitClient" located in /usr/lib/python2.6/site-packages/packagekit/client.py file, which provides high level interface for package management. Unfortunatelly the class is not well documented and some functions are not working OK. Because of this the packagekit-wrapper-meego.py was implemented, which contains PackageKitClient wrapper class similiar to PackageKitClient implemented in /usr/lib/python2.6/site-packages/packagekit/client.py file. The packagekit-wrapper-meego.py is modifcation/addition of Canonicals packagekit wrapper for MeeGo. Run script with superuser privilegies as currently there is an issue decribed in PackageKit installation section.

The PackageKitClient (Attached) Class is written in python and contains methods described below. Also it contains the usage of all described functionalities.

The PackageKitClient class provides following methods:

Resolve(filter, package) : returns list of matched packages

GetDetails(package) : returns the details of the packages

SearchName(filter, name) : searches and returnes availlable packages

InstallPackage(package, )

RemovePackage(package, )

The packagekit-wrapper-meego.py is attached.

The mechanisms that are used in class for implementing the methods are same for implementing the same methods in C++ as well.

Sample of retrieving Transaction's status using python:

import dbus

try:
bus = dbus.SystemBus()
except dbus.DBusException, e:
print 'Unable to connect to dbus: %s' % str(e)
sys.exit()
try:
proxy = bus.get_object('org.freedesktop.PackageKit', '/org/freedesktop/PackageKit')
iface = dbus.Interface(proxy, 'org.freedesktop.PackageKit')
tid = iface.GetTid()

proxy1 = bus.get_object('org.freedesktop.PackageKit', str(tid))
iface1 = dbus.Interface(proxy1, 'org.freedesktop.DBus.Properties')
lst = iface1.Get('org.freedesktop.PackageKit.Transaction', 'Status')
print str(lst)
except dbus.DBusException, e:
print 'Unable to use PackageKit: %s' % str(e)

High Level C++ API, using libpackagekit

The libpackagekit gobject librarywraps the DBUS interface in a nice glib-style API. This makes designing programs that use libpackagekit can concentrate on core functionality rather that the DBUS and PackageKit internals. PkClient in libpackagekit can be used as easily as:

Code sample

PkClient *client;
client = pk_client_new ();
pk_client_install_package (client, "openoffice-clipart");
g_object_unref (client);

More info can be found here http://packagekit.org/gtk-doc/PkClient.html

Mechanism of retrieving progress information

The org.freedesktop.PackageKit.Transaction interface containes transaction properties (status, percentage, etc) and signal Changed() which is invoked if one of the parameters is changed. So that to get installation/removal progres just connect to the Changed() signal and read off the properties.

PolicyKit and PackageKit

Running DBus methods like InstallPacakges, RemovePackages, UpdateSystem and others requires authorization with root account. That to change authorization policies for this and other DBus methods PolicyKit should be configured. How to configure PolicyKit can be found in this blog. That to set policy the action name for the appropriate name should be known for example "InstallPackages" methods action name is "org.freedesktop.packagekit.install-untrusted".

Action names can be found the packagekit DBus API description here http://www.packagekit.org/gtk-doc/api-reference.html

Issue

Depending on configured policies, platform there might be need to set addtional policies for example that authorize SystemUpdate without root password "sudo" also "org.freedesktop.packagekit.install-untrusted" action should be setResultActive=yes .

Solution

That to see the required action names install package "polkit-gnome" which is available in meego repositories. Polkit-gnome will launch a pop-up window if the application requries the autorization.

Under details you can see the action which required authorization.

Here is a sample which shows action name for package removal

References

http://packagekit.org/

http://packagekit.org/gtk-doc/