2005-09-22 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixProcess.cs
1 //
2 // Mono.Unix/UnixProcess.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004 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.
27 //
28
29 using System;
30 using Mono.Unix;
31
32 namespace Mono.Unix {
33
34         public sealed class UnixProcess
35         {
36                 private int pid;
37
38                 internal UnixProcess (int pid)
39                 {
40                         this.pid = pid;
41                 }
42
43                 public int Id {
44                         get {return pid;}
45                 }
46
47                 public bool HasExited {
48                         get {
49                                 int status = GetProcessStatus ();
50                                 return Syscall.WIFEXITED (status);
51                         }
52                 }
53
54                 private int GetProcessStatus ()
55                 {
56                         int status;
57                         int r = Syscall.waitpid (pid, out status, 
58                                         WaitOptions.WNOHANG | WaitOptions.WUNTRACED);
59                         UnixMarshal.ThrowExceptionForLastErrorIf (r);
60                         return r;
61                 }
62
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 Syscall.WEXITSTATUS (status);
70                         }
71                 }
72
73                 public bool HasSignaled {
74                         get {
75                                 int status = GetProcessStatus ();
76                                 return Syscall.WIFSIGNALED (status);
77                         }
78                 }
79
80                 [CLSCompliant (false)]
81                 [Obsolete ("The type of this property will change in the next release.")]
82                 public Signum TerminationSignal {
83                         get {
84                                 if (!HasSignaled)
85                                         throw new InvalidOperationException (
86                                                         Locale.GetText ("Process wasn't terminated by a signal"));
87                                 int status = GetProcessStatus ();
88                                 return Syscall.WTERMSIG (status);
89                         }
90                 }
91
92                 public bool HasStopped {
93                         get {
94                                 int status = GetProcessStatus ();
95                                 return Syscall.WIFSTOPPED (status);
96                         }
97                 }
98
99                 [CLSCompliant (false)]
100                 [Obsolete ("The type of this property will change in the next release.")]
101                 public Signum StopSignal {
102                         get {
103                                 if (!HasStopped)
104                                         throw new InvalidOperationException (
105                                                         Locale.GetText ("Process isn't stopped"));
106                                 int status = GetProcessStatus ();
107                                 return Syscall.WSTOPSIG (status);
108                         }
109                 }
110
111                 public int ProcessGroupId {
112                         get {return Syscall.getpgid (pid);}
113                         set {
114                                 int r = Syscall.setpgid (pid, value);
115                                 UnixMarshal.ThrowExceptionForLastErrorIf (r);
116                         }
117                 }
118
119                 public int SessionId {
120                         get {
121                                 int r = Syscall.getsid (pid);
122                                 UnixMarshal.ThrowExceptionForLastErrorIf (r);
123                                 return r;
124                         }
125                 }
126
127                 public static UnixProcess GetCurrentProcess ()
128                 {
129                         return new UnixProcess (GetCurrentProcessId ());
130                 }
131
132                 public static int GetCurrentProcessId ()
133                 {
134                         return Syscall.getpid ();
135                 }
136
137                 public void Kill ()
138                 {
139                         Signal (Signum.SIGKILL);
140                 }
141
142                 [CLSCompliant (false)]
143                 [Obsolete ("Use Signal (Mono.Unix.Native.Signum)")]
144                 public void Signal (Signum signal)
145                 {
146                         int r = Syscall.kill (pid, signal);
147                         UnixMarshal.ThrowExceptionForLastErrorIf (r);
148                 }
149
150                 [CLSCompliant (false)]
151                 public void Signal (Native.Signum signal)
152                 {
153                         int r = Native.Syscall.kill (pid, signal);
154                         UnixMarshal.ThrowExceptionForLastErrorIf (r);
155                 }
156
157                 public void WaitForExit ()
158                 {
159                         int status;
160                         int r;
161                         do {
162                                 r = Syscall.waitpid (pid, out status, (WaitOptions) 0);
163                         } while (UnixMarshal.ShouldRetrySyscall (r));
164                         UnixMarshal.ThrowExceptionForLastErrorIf (r);
165                 }
166         }
167 }
168
169 // vim: noexpandtab