Cogs and Levers A blog full of technical stuff

Watching the File System with INotify in Haskell

Introduction

Some applications that you write from time to time may require you to study changes that occur on the file system. These changes could be that a file arrives, is modified, is closed, etc. Your program can then respond accordingly to these interesting events.

In today’s post, I’ll show you how to monitor the file system for changes using the hinotify package.

What is inotify?

inotify is short for inode notify. It’s a piece of the Linux Kernel that adds notifications at the filesystem level so that userspace programs can take advantage of these events. The Wikipedia page on inotify has a good explanation for further reading.

The Code

There are 3 main jobs that we need to take care of here:

  • Create awareness with INotify
  • Register your interest in changes
  • Respond to the changes
module Main where

import Control.Concurrent (threadDelay)
import System.INotify

main :: IO ()
main = do
  -- the paths that we'll monitor
  let paths = [ "/tmp", "/home/user" ]
  
  -- setup INotify
  withINotify $ \n -> do
    -- monitor each predefined path, and respond using printEvent
    mapM_ (\f -> addWatch n [Modify, CloseWrite] f (printEvent f)) paths
    
    -- this gives "addWatch" some time to collect some data
    threadDelay 10000000
    
  where
    -- print the file and event to the console
    printEvent :: FilePath -> Event -> IO ()
    printEvent f e = putStrLn (f ++ ": " ++ show e)

I’ve tried to comment this code as best I can to show you what’s going on. It’s all pretty straight forward. Delaying the main thread may seem unintuitive, however without this call being made the program will finish execution without collecting data (because INotify doesn’t block!).

Nifty.

Making Sys-V Init Scripts for Debian

Introduction

Sometimes it’s necessary to run programs (applications or daemons) in the background at different stages of a machine’s up time. The most common use case of which centralises around starting something at boot and stopping something at shutdown.

In today’s post, I’ll be doing a write up on preparing init scripts for Sys-V style systems.

How it works

Debian’s Sys-V style init system relies on the scripts under /etc/init.d to instruct the operating system on how to start or stop particular programs. A header that sits at the top of these scripts informs the init system under what conditions this script should start and stop.

Here’s an example of this header from an excerpt taken from /etc/init.d/README

All init.d scripts are expected to have a LSB style header documenting dependencies and default runlevel settings. The header look like this (not all fields are required):

### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $portmap
# Should-Stop:       $portmap
# X-Start-Before:    nis
# X-Stop-After:      nis
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

The insserv man page will take you further in-depth as to what each of these means for your init script.

A full example of an init script is given on the Debian administration website here. It doesn’t use the header above - which is acceptable as these fields aren’t required by the script.

#! /bin/sh
# /etc/init.d/blah
#

# Some things that run always
touch /var/lock/blah

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script blah "
    echo "Could do more here"
    ;;
  stop)
    echo "Stopping script blah"
    echo "Could do more here"
    ;;
  *)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac

exit 0

Putting this script into a text file (into your home directory) and applying execute permissions on the file, allows you to test it at the console. I’ve put this script into a file called “blah”.

$ chmod 755 blah

$ ./blah
Usage: /etc/init.d/blah {start|stop}

$ ./blah start
Starting script blah
Could do more here

$ ./blah stop
Stopping script blah
Could do more here

The script implements a “start” and “stop” instruction.

Installation

Now that you’ve developed your script and are happy with its operation (after testing it at the console), you can install it on your system.

The first step is to copy your script (in this case “blah”) up into /etc/init.d/. This makes it available to the init system to use. It won’t actually use it though until you establish some symlinks between the script and the runlevel script sets.

You can check that it’s available to your system using the “service” command, like so:

$ sudo service --status-all
 [ + ]  acpid
 [ + ]  atd
 [ ? ]  blah
 [ - ]  bootlogs
 [ ? ]  bootmisc.sh
 . . .
 . . .
 . . .
 . . .

You can see that “blah” is being registered here as an init script and as such can also be started and stopped using the “service” command:

$ sudo service blah start
Starting script blah
Could do more here

$ sudo service blah stop
Stopping script blah
Could do more here

Now we’ll attach this init script to the startup and shut down of the computer. update-rc.d will help with this process. You can get the script installed with the following command:

$ sudo update-rc.d blah defaults

If you no longer want the script to execute on your system, you can remove it from the script sets with the following command:

$ update-rc.d -f blah remove

After removing it, you’ll still have your script in /etc/init.d/, just in case you want to set it up again.

That’s it for today.

Textfiles.com

I had to bookmark this site here. I’ve just burnt the past 2 hours looking through it, fondly remembering the 90’s.

http://www.textfiles.com/programming/

Setting up Bochs

Introduction

Today’s post is just a short one on the installation of the virtual machine Bochs within a Debian environment.

Procedure

# Install Bochs using your package manager
sudo apt-get install bochs

# Install the X11 and sdl plugin for Bochs
sudo apt-get install bochs-x bochs-sdl

Finally, make sure that your machines are using SDL as the display library by adding this line to your .bochsrc

files:display_library: sdl

That’s it for today.

Project setup with Maven at the Command Line

Introduction

A few utilities exist to manage your build, dependencies, test running for Java projects. One that I’ve seen that is quite intuitive (once you wrap your head around the xml structure) is Maven. According to the website, Maven is a “software project management and comprehension tool”.

The main benefit I’ve seen already is how the developer’s work-cycle is managed using the “POM” (project object model). The POM is just an XML file that accompanies your project to describe to Maven what your requirements are to build, test & package your software unit.

An excellent, short post can be found on the Maven website called “Maven in 5 minutes”.

Today’s post will focus on Maven installation and getting a “Hello, world” project running.

Installation

I’m on a Debian-flavored Linux distribution, so you may need to translate slightly between package managers. To get Maven installed, issue the following command at the prompt:

sudo apt-get install maven

Check that everything has installed correctly with the following command:

mvn --version

You should see some output not unlike what I’ve got here:

Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_AU, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-4-amd64", arch: "amd64", family: "unix"

If you’re seeing output like what I’ve got above - that’s it. You’re installed now.

First Project

Getting your first application together is pretty easy. A “quick start” approach is to use the quick start templates to generate a project structure like so:

cd ~/Source
mvn archetype:generate -DgroupId=org.temp -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Maven will then go out and grab all that it needs from the web to get your project setup. It’s now generated a project structure for you (in a directory called “hello”) that looks like this:

.
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── temp
    └── test
        └── java
        	└── org
        		└── temp

Editing the file that they put into the source folder for you (at src/main/java/org/temp/App.java), you can see that your job is already done:

package org.temp;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) {
            System.out.println( "Hello World!" );
    }
}

Build it and give it a run!

mvn compile
mvn exec:java -Dexec.mainClass="org.temp.App"

You should see some output like this:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ hello >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ hello <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ hello ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.331s
[INFO] Finished at: Thu Jan 30 12:46:28 EST 2014
[INFO] Final Memory: 7M/30M
[INFO] ------------------------------------------------------------------------

Most important line there is “Hello World!”.

There is so much more that you can do with Maven for your projects. Check out the documentation - that’s it for today.