Get in touch:
01524 851 877
07718 896 553

Simple Sleeping Linux Kernel Thread

Posted on May 26 2009

I am currently implementing the timeout functionality for the NP++ kernel module. Each physical mapping has a function pointer defined in its descriptive struct (struct np_sw) as described here.

Until now this hasn’t actually been implemented but I wanted to create a kernel thread that simply sleeps for 500 milliseconds, then awakes and looks through all the physical mappings available on the system and calls their timeout function.

This is a fairly simple process but in order to achieve it we need to include a couple of new headers :

#include <linux/kthread.h>
#include <linux/delay.h>

The first of these is for the thread related functions needed and the second is to allow us to sleep the thread.

Now we have those included we need to declare a function that will be called when we start the thread as follows :

int timeout_thread(void *data)
Any kernel thread function needs ro return an int and take a void pointer as the only argument to conform with the function pointer defined the kthread_create() function in kthread.h which the kthread_run() macro is a wrapper for.

Now we need to actually create the thread. I have done this in the modules init_module() function as follows:

kthread_run(timeout_thread, NULL, "NP++ Timeout Thread");
Now this is all done we simply need to put some implementation into the timeout_thread() function to perform the sleep as follows:

//Some Code Here

This sets the state of the thread to TASK_RUNNING to tell the kernel that the thread is working. The code to call the mappings individual timeout functions will go here. Then we set the state to TASK_INTERRUPTIBLE to tell the kernel we aren’t doing anything at which point we call the msleep() function to sleep the thread for 500 milliseconds.

Compile the module and you have a nice thread that will run roughly every 500 milliseconds. If you need a longer delay that can be measured in seconds the delay.h header also defines the following function that takes the number of seconds as the only argument:

void ssleep(unsigned int seconds);

NP++ Linux Kernel Module Version 0.1

Posted on May 22 2009

The first public release of the NP++ Linux kernel module is now available for download here.

This is an extremely early version and doesn’t have the full functionality of the earlier version written in the FreeBSD kernel but it’s a good building block. It is being released due to requests from certain parties that wish to use it so it is a lot easier for me to put it up on the net rather than emailing copies to people. This also means there will be a better development cycle.

There is a guide to creating a physical mapping kernel module to run along side the main NP++ module here.

For anyone reading this that hasn’t heard of my work on NP++ (which is probably most people) there is an overview of the protocol here. Hopefully with a bit more debugging the userspace tools that compliment the main protocol will be getting released soon as well.

Ctrl-Alt-F1 In Vmware

Posted on May 22 2009

As anyone that uses Linux on a regular basis will know it is quite handy being able to change to a different console using the Ctrl, Alt and Function keys. In VMWare however this is a problem as the Ctrl-Alt combination releases control of the input devices to the host operating system.

Looking through the menus and there is no option immediately visible to allow this keystroke combination to be input. There is just the standard menu item to send Ctrl-Alt_Del. After a quick google it appears there is a relatively simple solution however.

By using the combination Ctrl-Alt-Spacebar, then releasing the space bar whilst keeping the other two buttons depressed you can then use Ctrl-Alt as normal. So in this case the key combination is Ctrl-Alt-Spacebar, release the space bar and hit the F1 key and voila.

Apache Mod Rewrite And HTTP GET

Posted on May 20 2009

Just a quick post about passing the HTTP GET parameters to pages when using Apache with Mod Rewrite. Obviously if you are using a rule that redirects a number of URL’s to a single script and you try to pass GET parameters they just get tagged on to the end of it which will usually break things. You need to append the GET parameters from within the rewrite rule as shown below to get it to work properly.

RewriteRule ^(.*)/$ index.php?page=$1&%{QUERY_STRING}
Simply appending ‘&%{QUERY_STRING}’ to the end of the rule tells Mod Rewrite to append the GET parameters.

Add Files To Gumstix Buildroot

Posted on May 18 2009

Whilst trying to fix an error in a gumstix buildoot I stumbled across a nice easy way to include files in your completed buildroot image that I was previously unaware of.

Basically the file system that will be placed on to the gumstix is contained in the ‘build_arm_nofpu/root/’ directory. Simply place the files that you want to be in your completed image somewhere in this file system tree and they will built into the image once you run make again.

Fix For “Missing Operating System” Message – Ubuntu On Flash Drive

Posted on May 17 2009

As described in my previous blog post I had a problem getting USB on a flash drive to boot due to the PC simply saying “Missing Operating System” when it tried to boot from the device.

I found a fix for this that basically involved wiping the flash disk using fdisk. Creating a new partition and using usb-create again. The solution is explained step by step below :

The first step is to load fdisk. To do this you need the following simple command :

sudo fdisk /dev/sdg
Obviously you need to point it to the correct dev file where the USB flash disk is located. In my case it was /dev/sdg.

Once fdisk has loaded you need to delete the partition that is on the disk. This can be done by typing ‘d’ and then hitting enter. (If you want to check at any time what the state of the partition table is on the disk you can type ‘p’ and hit enter.)

This hasn’t actually deleted the partition yet. In order to write the changes to the drive you need to type w and hit enter. (Beware this will effectively wipe the drive so make sure you definitely have the right disk…)

So now you have a nice blank USB drive to use. Of course it now needs a partition to be able to write the Ubuntu image in when you run usb-creator. Start up gparted and right click on the unallocated partition. (Again, make sure you have the right drive selected in the top right hand corner of gparted or you could wipe off a hard disk by mistake.) Click on new and select Fat32 for the file system type. Write these changes to the USB drive and you are are set.

Now all that is left to do is fire up usb-creator, write the Ubuntu system to teh flash drive and stick it into the PC you wish to load and boot it up. Simples…

Ubuntu On A USB Flash Drive

Posted on May 17 2009

Over the years I have played around a few times getting either a Linux distribution or FreeBSD to run from a flash drive for a number of reasons. Last night I had the need to do so again to install Ubuntu on to a machine without a CD drive of any kind installed.

Before I started messing around setting up the MBR etc I decided to do a google search to see if others had done this and written a nice tutorial. To my surprise I found that ubuntu has a package named ‘usb-creator’ available that automates the process for you.

All you have to do is download a Ubuntu iso and it can mount it, prepare your flash drive and install it for you.

I also found out there are a number of tools now available to automate the process for different Linux distributions as well as BSD. The ones I have come across so far are :

Live USB Creator (Shell Script)

Despite these tools however I installed Ubuntu to the stick using usb-creator and when I went to boot it I got “Missing Operating System” displayed on the host PC. I am in the process of trying to rectify this at the moment so if and when I find a work around I will post the results here.

Wget Entire Directory

Posted on May 15 2009

I had the need to grab a whole directory via wget earlier which surprisingly I hadn’t needed to do before. The solution is quite simple :

wget -r -l1 --no-parent http://host/dir/
This will grab the whole directory. It basically works by telling wget to recurse (-r) but sets the level of directory recursion to 1 (-l1). The –no-parent option tells it not to follow the parent directory link.

Another handy wget command along the same lines is :

wget -r -l1 --no-parent -A.gif http://host/dir/

This is useful for just getting files of a single type. In this case it will download *.gif from the directory

Compiling Linux Modules Dependant On Exported Symbols

Posted on May 12 2009

I have been playing around with the NP++ Linux Kernel module today and it all seems to be working as planned at present. However I was in the process of writing a sample physical mapping module that uses the API from the NP++ module and ran into a bit of a problem.

Within the NP++ kernel module I have exported a few symbols (functions and variables) using the EXPORT_SYMBOL() macro. I expected to be able to use them quite simply in any other modules I created by simply including the NP++ header file. I had declared them as ‘extern’ in the header file and used the EXPORT_SYMBOL macro in the .c files as per all the documentation I had come across.

When I came to compile the mapping module that was trying to use these exported symbols however I came across a problem that the function I was trying to use was being shown as undefined during the link stage. This had me a bit stumped as I had convinced myself that the symbol should be visible.

It turns out that understandably as I had just compiled the module the kernel symbols table didn’t actually know about the symbols I had exported so compiling the new module still couldn’t see them. It turns out that the simple solution to this problem is to copy the Module.symvers file from the directory of the NP++ module to the directory of the mapping module.

The moral of this rambling is that you need to copy the Module.symvars file from the directory of a module once you have compiled it to the directory of any other out-of-tree module you are writing that relies on exported symbols from the first.

NP++ Wireshark Dissectors Available

Posted on May 11 2009

I am uploading the dissectors I modified for use with Wireshark. Although the NP++ code and accompanying applications have not yet been released these dissectors will be needed to be able to view the contents of the NP++ Neighbour Discovery options that are used.

Links to the files are below.


These files were modified from the originals in the “wireshark 1.0.4 (SVN Rev 1)” version of Wireshark. The easiest way to use them is simply download the 1.0.4 version of Wireshark here and replace the 2 files in /path/to/source/epan/dissectors/. Then run the normal ./configure, make, make install process to install it.

I haven’t got around to moving the code to a later version of Wireshark yet but will do in due time. I will also make a full version of Wireshark available for download with the replaced files contained in it at some point.