Monday, 29 December 2008

Change the Process Name in Programs executed by Mono under Linux (or other *nix OS)

I try to divide my free time by diving into open source world or trying to port some closed source .NET projects, commissioned by clients, designed to work under Windows to Linux.
I've recently updated a little tool written in C#, upgrading it to a stable state: cptree 1.1. If you don't remember what is for, please (re)read this post. When you execute the program with one or no arguments, it simply list file and folders traversing all the tree from the current (or specified) directory. So a day, working on my Ubuntu Linux pc, I've opened the Process Monitor (the one Windows users call Task Manager), with the will to kill cptree, committed to list a very deep directory. But what a surprise! No cptree or cptree.exe process exists: I was able to find only a process called mono eating a little more of cpu in relation of other processes...

Hence how to set the process name in a .NET application executed by Mono? I causually found the solution inspecting Banshee sources, which I know is a .NET application designed for Linux (forums and blogs are full of compliments about internal architecture of this application - and I can confirm it!). I found the solution in a file called PlatformHacks.cs. Anyway this file contains more than you need, so I suggest you to use my version (it contains just the code needed to accomplish this post intent).

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace CopyDirectoryTree
{
static class PlatformHacks
{
[DllImport("libc")] // Linux
private static extern int prctl(int option, byte[] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);

[DllImport("libc")] // BSD
private static extern void setproctitle(byte[] fmt, byte[] str_arg);

public static void SetProcessName(string name)
{
if (Environment.OSVersion.Platform != PlatformID.Unix)
{
return;
}

try
{
if (prctl(15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes(name + "\0"),
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0)
{

throw new ApplicationException("Error setting process name: " +
Mono.Unix.Native.Stdlib.GetLastError());
}
}

catch (EntryPointNotFoundException)
{

setproctitle(Encoding.ASCII.GetBytes("%s\0"),
Encoding.ASCII.GetBytes(name + "\0"));
}
}

public static void TrySetProcessName(string name)
{
try
{
SetProcessName(name);
}
catch
{
}
}
}
}

The use is straightforward, place the following call as first instruction in the entry point of your program:
PlatformHacks.TrySetProcessName("your-proc-name");

A big post for a little detail? To the readers the last word: feel free to add comments.

Bye and good platform hacking!

Sunday, 21 December 2008

Command Line Library 1.4 Stable, available!

This new version has some internal refactoring and an architectural change that allows the code to work better with components containers.

I think it's not useful repeat myself here, please jump to release page at CodePlex. As always here you can download source code, binary and help files (in CHM format).

All suggestions, bug reports, comments and feature requests will be really keep in high degree of consideration. So please write me (my new email is: gsscoder@ymail.com).

Bye!

Monday, 8 December 2008

Cache Reloaded, a caching service for Linux

A caching service is a middle-ware application used as a transient persistence storage. This one, Cache Reloaded, is a fork I've created starting from a stable open source .NET project: Merge System Shared Cache.
The first reason that convinced me to start this adventure, is the will to execute their software on Linux under Mono. Anyway this is only the first step and the code need some more testing. The decision to fork a new project instead of creating a patch can be found in my will to perform refactoring on various part of the code base.

Cache Reloaded can store CLR objects in memory and (for the moment) it accepts only .NET/Mono clients. It also has distributed networking capabilities inherited from its parent project: take a look to their web site.

If you can't wait download and test it by yourself, take a look at Cache Reloaded working inside a Linux.








This other screen shot shows you Cache Reloaded receiving requests from a Windows Client.








But if you're really interested, download and test it (I'll try to help you if you meet some problem).

Bye!

Wednesday, 3 December 2008

More on USB support on VirtualBox

If you upgraded your system to Ubuntu 8.04, maybe you can't locate the correct rule to modify in the udev configuration file specified in last post (/etc/udev/rules.d/40-permissions.rules).
So open your /etc/rc.local script as root and add the following line to allow VirtualBox attach to your USB:

#USB Support for VirtualBox
VBOX=$(grep vboxusers /etc/group | sed 's/vboxusers:x:\(.*\):.*/\1/')
mount -t usbfs -o devgid=$VBOX,devmode=664,nodev,noexec,nosuid none /proc/bus/usb


Add these lines before the exit 0 command.

I hope this post may help someone,
bye!

Sunday, 30 November 2008

VirtualBox and USB Problems

I had some problem using a Windows guest in a Linux (Ubuntu) host about the use of USB. The virtualization software I use is VirtualBox, recently acquired by Sun Microsystems.
The first thing is to use the latest stable release available. Drop your distribution installation and download the right package from their web site (at the moment of writing the latest version is 2.0.6).
In Ubuntu, at least in 7.10, you need to remove comments from a part of this script: /etc/init.d/mountdevsubfs.sh. The comment part inside the do_start() routine:

#
# Magic to make /proc/bus/usb work
#
#mkdir -p /dev/bus/usb/.usbfs
#domount usbfs "" /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listmode=0644
#ln -s .usbfs/devices /dev/bus/usb/devices
#mount --rbind /dev/bus/usb /proc/bus/usb


Modify the script with root privileges and execute it as root passing the word start as first and unique argument.

Now open this other file: /etc/udev/rules.d/40-permissions.rules.
Locate this lines:

# USB devices (usbfs replacement)
SUBSYSTEM=="usb_device", MODE="0664"

Copy the SUBSYSTEM line, comment it and change the new one with MODE value set to 0666:

#SUBSYSTEM=="usb_device", MODE="0664"
SUBSYSTEM=="usb_device", MODE="0666"


Also this file must be modified as root (in Ubuntu you can use sudo nano -w filename).

Restart the system and test if USB works on your guest operating system.

Bye!

Tuesday, 25 November 2008

Write code with your brain, ever! (Especially in Italy)

I'm not here to undress my analyst/programmer clothes and wear a Jedi tunic, symbol of masters of life!
I just want to give you a little suggestion, that worked for me.

If you are a freelance who works at a customer or employed in some kind of organization, it doesn't matter... Do not ask detailed technical advice at people not belonging to IT services (or worse... IT service people with knowledge limited to pre-relational era). Even if somehow you have to respond directly to these people, they will tell you that you are losing time. Only if your diversion from the main stream will be successful, you will receive a bonus.

So use your brain, code with your brain, don't ask (at people described above), do what you think is good: things will go well and you will save your liver! I hope this is not an international rule... But you can be sure that in Italy it works this way: meritocracy; there is this word in Italian vocabulary? Of course, but only in the vocabulary: meritocrazia.

Well, a lot of luck to all! Tanta fortuna!

Thursday, 9 October 2008

Decouple Software in Services

Good practices of software engineering tell us to decouple software components as much is possible (without overtake a reasonable limit!).
If you give a read to my first blog article (excluding greetings...), you can see my interest about this argument. In few words I thing that last generation OO-platforms like .NET and Java are of great help in slicing software in components viewed as services.
Two of most important design patterns behind this principles are Inversion of Control and Dependency Injection (I suggest you to read Martin Fowler article).

For .NET there are two interesting libraries that help you implement this kind of stuff:
Castle Project and Unity Application Block (both open source).
For Java take a look to PicoContainer.

All of these are large and powerful libraries that give you a lot more of what told in this post. For this reason I decided to write a simple Inversion of Control/Dependency Injection Container (as soon as possible on CodePlex).

Good slicing!

Tuesday, 5 August 2008

New releases for all CodePlex hosted projects

All projects hosted on CodePlex has recently upgraded to a new release:
Command Line Parser Library -> 1.3.7 release candidate 0
Common Utility Library -> 1.2.0.5 July 2008
Copy Directory Tree Utility -> 1.0.50.7 beta

Enjoy!

Monday, 28 April 2008

Command Line Parser Library,1.3.1 beta released!

Minor updates for types not involved in parsing; these types are now under CommandLine.Text namespace.

Download here.

Monday, 21 April 2008

The importance of documentation

I have always published relatively small projects to an audience of programmers. For that reason (but mainly for lack of time) I have neglected this aspect of the projects.
With an audience of programmers, I thought that unit-test sources were enough to learn the use of a class library. About that please read my post of 14 June 2007.

The reality is that if you want people to use your software (in source or binary form), you have to provide good documentation.

For now, I'm trying to improve the documentation of Command Line Parser Library:
  • adding more info directly on CodePlex
  • adding a API reference in HTML and CHM (MSDN-like) format
  • adding documentation comments to public members
    • this will help advanced IDE while using the binary
    • or the people interested directly in source code
As always, any suggestion is welcome!

Friday, 11 April 2008

cptree 1.0 alfa: Add value to your file system folders!

I wrote a simple program to replicate a directory structure, excluding files, in another point. In this way you can back up important folders like the download or home directories.

The program is built using Command Line Parser Library 1.3 and like this project Copy Directory Tree Utility (cptree) is open source and can be downloaded from CodePlex. Here you can read a lot more about it.

Have fun!

Tuesday, 1 April 2008

Command Line Library, 1.3 production released!

This is the first production release result of deep testing and used with success in other projects. Release 1.3 features documentation comments in publicly visible members.

Download it!

Tuesday, 18 March 2008

Vista Fonts on Ubuntu Linux

If you want continue to edit MS Office 2007 documents that uses fonts like Calibri, Cambria, etc under Ubuntu Linux without problems, check out this very useful instructions.
(I think the script can run with little or no modifications in other distributions).
The article author suggest you to create and execute the script (vista-fonts-installer.sh) posted as link in the article page. I suggest you to read the article and users comments, but let me list the fundamentals steps:
  • create a directory named .fonts in your home
    • mkdir ~/.fonts
  • download the file into your home directory vista-fonts-installer.sh (in the home, not in ~/.fonts!)
  • make the script executable
    • chmod a+x ~/vista-fonts-installer.sh
  • modify the header of the script from #!/bin/sh to #!/bin/bash (see users comments)
  • finally, execute the script
    • ./vista-fonts-installer.sh
I hope this info were useful also for you.

Sunday, 16 March 2008

Try Vector Linux

Are you searching for a fast, lightweight and simple to install linux? A linux distro with good defaults and pre-configured nice gui settings? Then try Vector Linux.
The installation program is ncurses based (graphical text gui) but is very simple to use (at least for people with some linux experience). For now I've installed this distro in a virtual machine with 128MB of ram and a small hard disk (10GB total; >3GB used installing "can't live without" software).
After the first boot; I can surf the web, play a Mario's clone and write a Sci-Fi story with AbiWord!

In next days (I hope...) I'll try this wonderful linux in a real old pc. Stay tuned!