July 27, 2011

Converting paths on a linux filesystem to WINE relative paths

Wine is a windows software emulator for Linux. Wine generally installs itself to ~/.wine and the C:\ Drive for the corresponding emulated windows environment is ~/.wine/drive_c/ . Wine sees the actually filesystem as mounted under a emulated Z:\ drive, so for example /mnt on the linux filesystem becomes Z:\mnt. To automatically run windows programs which require files from the linux filesystem, it is nifty if the linux filesystem path for a file can be converted to the emulated version starting with Z:\ , here is a small snippet of code which does that for the current working directory :

$ echo Z:`pwd` | sed 's/\//\\/g'

It gets the absolute path for the current working directory and converts all forward slashes ('/') to backward slashes ('\') and prepends the drive letter (Z:) to it. Now adapting the above snippet to get the wine path for a normal file is quite easy, and is left for those who are interested

Setting up password less authentication between two linux machines

Setting up password less authentication between two machines is a common requirement for many distributed software to work, some examples being hadoop, MPI etc ... however it is sometimes also handy if you are a system administrator and frequently login to a remote machine. Now the ssh-copy-id utility already exists to copy the ssh public key from the current machine to the remote machine. But if you have to copy the key from the remote machine to the local machine you have to first login to the remote machine, and then use ssh-copy-id (or some other method) from there. Here is a handy bash script to do the same.


#!/bin/bash
set -x
if [ $# -eq 0 ]; then
echo "Usage : setup_ssh <local_interface> <remote_ip> [<remote_user>]"
echo "Example : setup_ssh wlan0 192.168.0.10"
exit 1
fi
interface=$1
remote_ip=$2
my_ip=$(ifconfig $interface | grep "inet addr" | sed 's/inet addr://g' | awk '{print $1}')
if [ $? -ne 0 ]; then
echo "Could not get the current machines ip"
exit 1
fi
if [ $# -eq 3 ];then 
user=$3
else
user=$USER
fi
if [ "$user" = "root" ]; then
remote_home="/root/.ssh"
else
remote_home="/home/$user/.ssh"
fi
echo "Checking for remote copy program .."
which scp
if [ $? -eq 0 ]; then
remote_copy="scp"
else
which rsync
if [ $? -eq 0 ]; then
remote_copy="rsync"
else
echo "Could not find a remote copy program, quitting !"
exit 1
fi
fi
which ssh-copy-id
if [ $? -ne 0 ]; then
echo "Could not find ssh-copy-id "
exit 1
fi
if ! [ -f ~/.ssh/id_rsa.pub ]; then
echo "Could not find the default public key : ~/.ssh/id_rsa.pub"
exit 1
fi
echo "ssh-copy-id $user@$remote_ip"
ssh-copy-id $user@$remote_ip
$remote_copy $user@$remote_ip:$remote_home/*.pub ./
cat *.pub >> ~/.ssh/authorized_keys
rm -rf *.pub
if [ $? -ne 0 ];then
echo "Copying the public keys failed ... quitting "
exit 1
else
echo "Successfull"
exit 0
fi

Note that the script only works in one of the following two formats :

$ ssh-setup interface remote_ip remote_user 
                                 or
$ ssh-setup interface remote_ip 


The remote ip need not only be an ip address, as you would have already understood from the script, it can also be a hostname or some handle which can be resolved to an ip address.

A sample execution would look like  :


$ ssh-setup wlan0 192.168.0.21 root


Note : This script has only been tested on a linux machine (Fedora 14 ) and I do not assure you of its functionality, use at your own risk.

July 15, 2011

Modifying SWT widgets from outside the event thread / Using a single display with SWT on linux

Like most GUI frameworks , SWT ( which itself is a wrapper over the native windowing system) has an event loop, more over it has other restrictions on the way the widgets/elements within the GUI can be modified. The main item of importance in a SWT based GUI is the display, created like :

Display display = new Display()

now, on linux (x64 at least to the extent I have tested), SWT does not support multiple displays. So you have to use one display entity to drive all your GUI components, this can especially get really troublesome if your GUI has multiple components and windows. Logically making the code modular (splitting the code for each window into may be different classes imposes its own issues) will result in problems.
This is because, SWT allows the GUI widgets to be accessed/modified only from the thread in which the display was created. If you try to access it from outside this thread, you will end up with a SWTException : Invalid thread access.  Here are the possible fixes for this :


  • Declare the display as a static variable so that you have access to it based on the class name
  • Now whenever you need to change something in the GUI, assume you have a function changeWidgetStyle( Params ) which changes the widget style, then you can call the function as : 
                                 <Class Name>.display.asyncExec(new Runnable(){ 
                                                    public void run(){
                                                                  changeWidgetStyle( Params );
                                                    }
                                    });

  • Now the code inside the changeWidgetStyle function can access and modify any components of the GUI. I guess SWT imposes this restriction to make the event thread as responsive as possible.
  • Use the display.syncExec() method with exactly the same syntax if you want to wait for the execution of the function to complete.
I will try to post a code example soon ( as I find some more time). !

Configuring java using the alternatives system (on Fedora/Centos/Redhat)

On most red-hat based systems, alternative versions of the same software (from say different vendors) are managed using the "alternatives" system. Here is an example :

$ which java | xargs file
/usr/bin/java: symbolic link to `/etc/alternatives/java'
$ file /etc/alternatives/java
/etc/alternatives/java: symbolic link to `/usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java'

Here in this case we are looking at the java pointing to the one bundled by openjdk. Say if you have installed the Sun (or Oracle rather :|) JDK and if the path to that is /opt/java/bin , then you can add the sun version of java to the alternatives system using : 

$ alternatives --install /usr/bin/java java /opt/java/bin/java 100000 

The last number there is a priority, set it high so that any priority based overrides are ineffective. Now by doing : 

$ alternatives --config java 

you should be able to switch between the alternative versions of java !

July 13, 2011

The number of f**ks and sh**ts in the linux kernel source !

I was getting bored with my work and started trying to write out a fast parser in C which given a directory prints out the number of occurrences of fu*cks and sh**ts in files inside that directory. The program is here . I ran this program on the current stable linux kernel 2.6.29.3. Here are the interesting results ( I wrapped up the results from the C program to convert them to links to the source on the web using a simple python script, so that anyone interested can verify the count for themselves). It took the program a little more than 6 seconds (~ 6.3 secs) to complete parsing the entire kernel source.

Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/mmc/host/sdhci.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/media/video/bt819.c
Found 9 shits in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/media/video/zoran/zr36050.c
Found 9 shits in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/media/video/zoran/zr36060.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/media/video/zoran/zr36016.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/scsi/qlogicpti.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/scsi/qlogicpti.h
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/block/ub.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/mtd/mtd_blkdevs.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/declance.c
Found 2 shits in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/niu.h
Found 2 fucks in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/sunhme.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/sunhme.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/wan/z85230.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/wireless/iwlegacy/iwl3945-base.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/net/sunlance.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/video/aty/radeon_pm.c
Found 2 shits in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/video/sis/sis_main.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/macintosh/adb.c
Found 2 shits in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/staging/slicoss/slicoss.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/ata/sata_via.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=drivers/ide/cmd640.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=sound/oss/uart6850.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=sound/pci/cs46xx/dsp_spos_scb_lib.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=sound/pci/ac97/ac97_patch.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=Documentation/ManagementStyle
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=Documentation/DocBook/kernel-locking.tmpl
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=Documentation/DocBook/kernel-hacking.tmpl
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=include/linux/fb.h
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=lib/vsprintf.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=net/ipv4/tcp_input.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=net/ipv4/netfilter/nf_nat_snmp_basic.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=net/core/skbuff.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=fs/notify/inotify/inotify_user.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=fs/jffs2/dir.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=fs/isofs/inode.c
Found 2 fucks in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=fs/xfs/xfs_btree.h
Found 24 fucks in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/mips/pci/ops-bridge.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/mips/kernel/genex.S
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/mips/include/asm/mipsprom.h
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/mips/sgi-ip22/ip22-setup.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/m68k/include/asm/sun3ints.h
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/x86/kernel/cpu/cpufreq/powernow-k7.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/x86/platform/visws/visws_quirks.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/mm/ultra.S
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/mm/srmmu.c
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/kernel/head_32.S
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/kernel/traps_64.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/kernel/pcic.c
Found a shit in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/sparc/lib/checksum_32.S
Found a fuck in http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.39.y.git;a=blob;f=arch/parisc/kernel/sys_parisc.c
Found 42 f words and 53 s words !

Note : Don't go by the run time of the program, my machine has a i7 and is pretty fast !

EDIT : Considering the attention this post has been getting, the words matched are : *fuck* and *shit~[a-z], the program is of course not case sensitive, and as is obvious , it is not a perfect matching program, it was just a half an hour worth coding experiment !