* tools/beagle-crawl-system.in: Use MONO_SHARED_DIR to point to a
[beagle.git] / Util / SafeProcess.cs
blobc9dac7fec19128972eb4a11754b5de26a0cd24e1
1 //
2 // SafeProcess.cs
3 //
4 // Copyright (C) 2006 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Runtime.InteropServices;
30 using Mono.Unix;
31 using GLib;
33 namespace Beagle.Util {
35 public class SafeProcess {
37 private bool redirect_stdin, redirect_stdout, redirect_stderr;
38 private int stdin, stdout, stderr;
39 private string[] args;
40 private UnixStream stdin_stream, stdout_stream, stderr_stream;
42 public string[] Arguments {
43 get { return args; }
44 set { args = value; }
47 public bool RedirectStandardInput {
48 get { return redirect_stdin; }
49 set { redirect_stdin = value; }
52 public bool RedirectStandardOutput {
53 get { return redirect_stdout; }
54 set { redirect_stdout = value; }
57 public bool RedirectStandardError {
58 get { return redirect_stderr; }
59 set { redirect_stderr = value; }
62 public Stream StandardInput {
63 get { return stdin_stream; }
66 public Stream StandardOutput {
67 get { return stdout_stream; }
70 public Stream StandardError {
71 get { return stderr_stream; }
74 [DllImport ("libglib-2.0.so.0")]
75 static extern bool g_spawn_async_with_pipes (string working_directory,
76 string[] argv,
77 string[] envp,
78 int flags,
79 IntPtr child_setup,
80 IntPtr child_data,
81 IntPtr pid,
82 ref int standard_input,
83 ref int standard_output,
84 ref int standard_error,
85 out IntPtr error);
87 public void Start ()
89 if (args == null)
90 throw new ArgumentException ("Arguments cannot be empty");
92 IntPtr error;
94 if (args [args.Length - 1] != null) {
95 // Need to null-terminate the array.
96 string[] tmp_args = new string [args.Length + 1];
97 Array.Copy (args, tmp_args, args.Length);
98 args = tmp_args;
101 g_spawn_async_with_pipes (null, args, null,
102 1 << 2, // G_SPAWN_SEARCH_PATH
103 IntPtr.Zero, IntPtr.Zero, IntPtr.Zero,
104 ref stdin, ref stdout, ref stderr, out error);
106 if (error != IntPtr.Zero)
107 throw new SafeProcessException (new GException (error));
109 if (! RedirectStandardInput)
110 Mono.Unix.Native.Syscall.close (stdin);
111 else
112 stdin_stream = new UnixStream (stdin);
114 if (! RedirectStandardOutput)
115 Mono.Unix.Native.Syscall.close (stdout);
116 else
117 stdout_stream = new UnixStream (stdout);
119 if (! RedirectStandardError)
120 Mono.Unix.Native.Syscall.close (stderr);
121 else
122 stderr_stream = new UnixStream (stderr);
125 public void Close ()
127 if (stdin_stream != null)
128 stdin_stream.Close ();
130 if (stdout_stream != null)
131 stdout_stream.Close ();
133 if (stderr_stream != null)
134 stderr_stream.Close ();
138 public class SafeProcessException : Exception {
140 internal SafeProcessException (GException gexception) : base (gexception.Message) { }