Changing process name in Mono

Banshee 0.10.5 supports changing the process name from “mono” to “banshee” using the prctl call. This makes “killall banshee” work. While this is nothing to marvel over, I have been a little taken aback by the amount of inquiry over this feature. In the past few days, a large number of people have asked me, “how did you do this.” Here’s a brief description of “how.”

This had been discussed over private email a few months ago, but was recently touched on again as a bug filed against Beagle.

The short answer is, if you want your mono app to be “killall-able,” then do this:

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

...

[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)
{
    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"));
    }
}

...

try {
    SetProcessName ("banshee");
} catch (Exception e) {
    Console.Error.WriteLine ("Failed to set process name: {0}", e.Message);
}

Now “killall banshee” works. Also consider using “exec -a banshee ...” in your Bash wrapper to make process listings look better. There are more details provided in the bug, as well as some implications you should be aware of if you do this with your Mono app.

Code snippet updated on 2007-11-12 to show how to support BSD systems and reflect a known fix for 64 bit systems. The above code is known to be bug free and tested in many production applications.

This entry was posted in uncategorized and tagged , , . Bookmark the permalink.

14 Responses to Changing process name in Mono

  1. Sven says:

    Great, was looking for that, thanks :-)

  2. Anónimo says:

    Good, but is a WTF that we have to killall our music player :)

  3. Of course “killall banshee” should never be needed :) Nonetheless, this is just one more way to make mono apps feel more native.

  4. Stephen Thorne says:

    Awesome, I had a python module that would change sys.argv, but that only modifies /proc//cmdline, while killall looks at /proc//stat. This will allow me to stop having to use pkill -f, and go back to using killall :).

    This feature was only added in September ’04, and is a linux kernel feature. I think that’s an important thing to mention, because boxes running kernels without this feature may fall over on your media player, if you don’t catch and handle that exception nicely. :)

  5. Right, I should have mentioned prctl is Linux-only. Other systems support similar calls however, and when we get this in Mono itself, it will account for that.

    As for Banshee… yes, of course I handle a possible exception. However, Banshee is Linux only as well right now, mainly because of HAL. Banshee relies on other features that are mostly available on modern Linux systems, so chances are good you have a 2.6 kernel that post-dates Sept ’04.

  6. anonymono says:

    Could this be done with only managed code? If not, how could you do it multiplatform way? I suppose using #ifdef, am I right?

  7. No, you have to call something low-level, and that something is system-specific. Ideally the low-level, system-specific calls will be implemented in the Mono runtime in C, with a managed layer on top.

  8. anonymono says:

    Ideally the low-level, system-specific calls will be implemented in the Mono runtime in C, with a managed layer on top.

    If there is already a bug in bugzilla about this particular feature request or you open a new one, post the URL here please.

  9. anonymono says:

    BTW: wouldn’t it be nice to patch Mono so as to behave like this: by default, use this system call to put the name of the EXE file that holds the program that is running.

  10. Mike Kestner says:

    It is pretty much _never_ correct to use ulong or long in a PInvoke signature. I know you are just passing zeros, but on a 32 bit machine you are overflowing the expected parameter passing space for the method by 12 bytes. YMMV.

    The conventional way to pass a ulong parameter to the native side is to use an IntPtr which will vary in size the same way a long does on the native side.

  11. Mirco Bauer says:

    You can do the process rename much simpler with a shell script (which is neded anyhow for user friendly invokation):

    exec -a myname mono foo.exe
    echo done.

    the script is not really portable which is not important since on windows you don’t need this rename hack anyhow.

  12. Mirco Bauer says:

    ups, didn’t check that killall read /proc/foo/status instead of /proc/foo/cmdline., so ignore my previous comment ;)

  13. Mike: ah, yes. I remember doing that [improperly] for some signature a while ago. Thanks ;)

    Mirco: that’s what I mean by a combination of ‘exec -a’ and prctl. ‘exec -a’ does the argv[0] rename, which top, ps, gnome system monitor, and others will pick up and show in listings, while prctl actually renames the process, which killall and others will pick up. You have to do both to get the best coverage.

  14. srini says:

    How abt for Mac ????

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>