~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do/src/Util.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 05:57:47 UTC
  • Revision ID: ootz0rz@gmail.com-20090623055747-3srobsuq3q8wbn81
initial adding of Do core stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Util.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.Text;
 
22
using System.Runtime.InteropServices;
 
23
 
 
24
using Mono.Unix.Native;
 
25
 
 
26
namespace Do
 
27
{
 
28
 
 
29
        public static class Util
 
30
        {
 
31
                
 
32
                [DllImport ("libc")] // Linux
 
33
                private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
 
34
                
 
35
                private static int prctl (int option, byte [] arg2)
 
36
                {
 
37
                        return prctl (option, arg2, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
 
38
                }
 
39
 
 
40
                private static int prctl (int option, string arg2)
 
41
                {
 
42
                        return prctl (option, Encoding.ASCII.GetBytes (arg2 + "\0"));
 
43
                }
 
44
 
 
45
                [DllImport ("libc")] // BSD
 
46
                private static extern void setproctitle (byte [] fmt, byte [] name);
 
47
 
 
48
                private static void setproctitle (string fmt, string name)
 
49
                {
 
50
                        setproctitle (Encoding.ASCII.GetBytes (fmt + "\0"), Encoding.ASCII.GetBytes (name + "\0"));
 
51
                }
 
52
 
 
53
                /// <summary>
 
54
                /// Sets the name of the current process on Linux. Throws EntryPointNotFoundException
 
55
                /// if we are not on Linux.
 
56
                /// </summary>
 
57
                /// <param name="name">
 
58
                /// A <see cref="System.String"/> name to set the process name to.
 
59
                /// </param>
 
60
                /// <returns>
 
61
                /// A <see cref="System.Boolean"/> indicating whether the set was successful.
 
62
                /// </returns>
 
63
                private static bool SetLinuxProcessName (string name)
 
64
                {
 
65
                        return prctl (15 /* PR_SET_NAME */, name) == 0;
 
66
                }
 
67
 
 
68
                /// <summary>
 
69
                /// Sets the name of the current process on BSD. Throws EntryPointNotFoundException
 
70
                /// if we are not on BSD.
 
71
                /// </summary>
 
72
                /// <param name="name">
 
73
                /// A <see cref="System.String"/> name to set the process name to.
 
74
                /// </param>
 
75
                /// <returns>
 
76
                /// A <see cref="System.Boolean"/> indicating whether the set was successful.
 
77
                /// </returns>
 
78
                private static void SetBSDProcessName (string name)
 
79
                {
 
80
                        setproctitle ("%s", name);
 
81
                }
 
82
 
 
83
                /// <summary>
 
84
                /// Sets the name of the current process.
 
85
                /// </summary>
 
86
                /// <param name="name">
 
87
                /// A <see cref="System.String"/> name to set the process name to.
 
88
                /// </param>
 
89
                public static void SetProcessName (string name)
 
90
                {
 
91
                        try {
 
92
                                if (!SetLinuxProcessName (name))
 
93
                                        throw new ApplicationException ("Error setting process name: " + Stdlib.GetLastError ());
 
94
                        } catch (EntryPointNotFoundException) {
 
95
                                SetBSDProcessName (name);
 
96
                        }
 
97
                }
 
98
        }
 
99
}