3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this source distribution.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
using System.Runtime.InteropServices;
24
using Mono.Unix.Native;
29
public static class Util
32
[DllImport ("libc")] // Linux
33
private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
35
private static int prctl (int option, byte [] arg2)
37
return prctl (option, arg2, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
40
private static int prctl (int option, string arg2)
42
return prctl (option, Encoding.ASCII.GetBytes (arg2 + "\0"));
45
[DllImport ("libc")] // BSD
46
private static extern void setproctitle (byte [] fmt, byte [] name);
48
private static void setproctitle (string fmt, string name)
50
setproctitle (Encoding.ASCII.GetBytes (fmt + "\0"), Encoding.ASCII.GetBytes (name + "\0"));
54
/// Sets the name of the current process on Linux. Throws EntryPointNotFoundException
55
/// if we are not on Linux.
57
/// <param name="name">
58
/// A <see cref="System.String"/> name to set the process name to.
61
/// A <see cref="System.Boolean"/> indicating whether the set was successful.
63
private static bool SetLinuxProcessName (string name)
65
return prctl (15 /* PR_SET_NAME */, name) == 0;
69
/// Sets the name of the current process on BSD. Throws EntryPointNotFoundException
70
/// if we are not on BSD.
72
/// <param name="name">
73
/// A <see cref="System.String"/> name to set the process name to.
76
/// A <see cref="System.Boolean"/> indicating whether the set was successful.
78
private static void SetBSDProcessName (string name)
80
setproctitle ("%s", name);
84
/// Sets the name of the current process.
86
/// <param name="name">
87
/// A <see cref="System.String"/> name to set the process name to.
89
public static void SetProcessName (string name)
92
if (!SetLinuxProcessName (name))
93
throw new ApplicationException ("Error setting process name: " + Stdlib.GetLastError ());
94
} catch (EntryPointNotFoundException) {
95
SetBSDProcessName (name);