2 // Mono.Posix/PosixProcess.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004 Jonathan Pryor
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 namespace Mono
.Posix
{
34 public sealed class PosixProcess
38 private PosixProcess (int pid
)
47 public bool HasExited
{
49 int status
= GetProcessStatus ();
50 return Syscall
.WIFEXITED (status
);
54 private int GetProcessStatus ()
57 int r
= Syscall
.waitpid (pid
, out status
,
58 WaitOptions
.WNOHANG
| WaitOptions
.WUNTRACED
);
59 PosixMarshal
.ThrowExceptionForLastErrorIf (r
);
66 throw new InvalidOperationException (
67 Locale
.GetText ("Process hasn't exited"));
68 int status
= GetProcessStatus ();
69 return Syscall
.WEXITSTATUS (status
);
73 public bool HasSignaled
{
75 int status
= GetProcessStatus ();
76 return Syscall
.WIFSIGNALED (status
);
80 public Signum TerminationSignal
{
83 throw new InvalidOperationException (
84 Locale
.GetText ("Process wasn't terminated by a signal"));
85 int status
= GetProcessStatus ();
86 return Syscall
.WTERMSIG (status
);
90 public bool HasStopped
{
92 int status
= GetProcessStatus ();
93 return Syscall
.WIFSTOPPED (status
);
97 public Signum StopSignal
{
100 throw new InvalidOperationException (
101 Locale
.GetText ("Process isn't stopped"));
102 int status
= GetProcessStatus ();
103 return Syscall
.WSTOPSIG (status
);
107 public int ProcessGroupId
{
108 get {return Syscall.getpgid (pid);}
110 int r
= Syscall
.setpgid (pid
, value);
111 PosixMarshal
.ThrowExceptionForLastErrorIf (r
);
115 public int SessionId
{
117 int r
= Syscall
.getsid (pid
);
118 PosixMarshal
.ThrowExceptionForLastErrorIf (r
);
123 public static PosixProcess
GetCurrentProcess ()
125 return new PosixProcess (GetCurrentProcessId ());
128 public static int GetCurrentProcessId ()
130 return Syscall
.getpid ();
135 Signal (Signum
.SIGKILL
);
138 public void Signal (Signum signal
)
140 int r
= Syscall
.kill (pid
, signal
);
141 PosixMarshal
.ThrowExceptionForLastErrorIf (r
);
144 public void WaitForExit ()
150 r
= Syscall
.waitpid (pid
, out status
, (WaitOptions
) 0);
151 } while (r
== -1 && (e
= Syscall
.GetLastError()) == Error
.EINTR
);
152 PosixMarshal
.ThrowExceptionForLastErrorIf (r
);