2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Unix / UnixProcess.cs
blob3360ef25fe97ad4ed25660646eb8517d73ba95ce
1 //
2 // Mono.Unix/UnixProcess.cs
3 //
4 // Authors:
5 // Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004-2005 Jonathan Pryor
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using Mono.Unix;
32 namespace Mono.Unix {
34 public sealed class UnixProcess
36 private int pid;
38 internal UnixProcess (int pid)
40 this.pid = pid;
43 public int Id {
44 get {return pid;}
47 public bool HasExited {
48 get {
49 int status = GetProcessStatus ();
50 return Native.Syscall.WIFEXITED (status);
54 private int GetProcessStatus ()
56 int status;
57 int r = Native.Syscall.waitpid (pid, out status,
58 Native.WaitOptions.WNOHANG | Native.WaitOptions.WUNTRACED);
59 UnixMarshal.ThrowExceptionForLastErrorIf (r);
60 return r;
63 public int ExitCode {
64 get {
65 if (!HasExited)
66 throw new InvalidOperationException (
67 Locale.GetText ("Process hasn't exited"));
68 int status = GetProcessStatus ();
69 return Native.Syscall.WEXITSTATUS (status);
73 public bool HasSignaled {
74 get {
75 int status = GetProcessStatus ();
76 return Native.Syscall.WIFSIGNALED (status);
80 public Native.Signum TerminationSignal {
81 get {
82 if (!HasSignaled)
83 throw new InvalidOperationException (
84 Locale.GetText ("Process wasn't terminated by a signal"));
85 int status = GetProcessStatus ();
86 return Native.Syscall.WTERMSIG (status);
90 public bool HasStopped {
91 get {
92 int status = GetProcessStatus ();
93 return Native.Syscall.WIFSTOPPED (status);
97 public Native.Signum StopSignal {
98 get {
99 if (!HasStopped)
100 throw new InvalidOperationException (
101 Locale.GetText ("Process isn't stopped"));
102 int status = GetProcessStatus ();
103 return Native.Syscall.WSTOPSIG (status);
107 public int ProcessGroupId {
108 get {return Native.Syscall.getpgid (pid);}
109 set {
110 int r = Native.Syscall.setpgid (pid, value);
111 UnixMarshal.ThrowExceptionForLastErrorIf (r);
115 public int SessionId {
116 get {
117 int r = Native.Syscall.getsid (pid);
118 UnixMarshal.ThrowExceptionForLastErrorIf (r);
119 return r;
123 public static UnixProcess GetCurrentProcess ()
125 return new UnixProcess (GetCurrentProcessId ());
128 public static int GetCurrentProcessId ()
130 return Native.Syscall.getpid ();
133 public void Kill ()
135 Signal (Native.Signum.SIGKILL);
138 [CLSCompliant (false)]
139 public void Signal (Native.Signum signal)
141 int r = Native.Syscall.kill (pid, signal);
142 UnixMarshal.ThrowExceptionForLastErrorIf (r);
145 public void WaitForExit ()
147 int status;
148 int r;
149 do {
150 r = Native.Syscall.waitpid (pid, out status, (Native.WaitOptions) 0);
151 } while (UnixMarshal.ShouldRetrySyscall (r));
152 UnixMarshal.ThrowExceptionForLastErrorIf (r);
157 // vim: noexpandtab