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!