summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/glib-sharp/Spawn.cs
blob: 8b70b192f70b577171d6b7f27cbe1ea89d00c184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// glib/Spawn.cs : Spawn g_spawn API wrapper
//
// Author: Mike Kestner  <mkestner@novell.com>
//
// Copyright (c) 2007 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General 
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


namespace Gst.GLib {

	using System;
	using System.Runtime.InteropServices;
	
	public enum SpawnError {
		Fork, 
		Read,
		Chdir,
		Acces,
		Perm, 
		TooBig,
		NoExec,
		NameTooLong,
		NoEnt,     
		NoMem,    
		NotDir,  
		Loop,   
		TxtBusy,   
		IO,       
		NFile,   
		MFile,  
		Inval, 
		IsDir,
		LibBad,   
		Failed,  
	}

	[Flags]
	public enum SpawnFlags {
		LeaveDescriptorsOpen = 1 << 0,
		DoNotReapChild         = 1 << 1,
		SearchPath             = 1 << 2,
		StdoutToDevNull        = 1 << 3,
		StderrToDevNull        = 1 << 4,
		ChildInheritsStdin     = 1 << 5,
		FileAndArgvZero        = 1 << 6,
	}

	public delegate void SpawnChildSetupFunc ();

	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate void SpawnChildSetupFuncNative (IntPtr gch);

	internal class SpawnChildSetupWrapper {

		SpawnChildSetupFunc handler;

		public SpawnChildSetupWrapper (SpawnChildSetupFunc handler)
		{
			if (handler == null)
				return;

			this.handler = handler;
			Data = (IntPtr) GCHandle.Alloc (this);
			NativeCallback = new SpawnChildSetupFuncNative (InvokeHandler);
		}

		public IntPtr Data;
		public SpawnChildSetupFuncNative NativeCallback;

		static void InvokeHandler (IntPtr data)
		{
			if (data == IntPtr.Zero)
				return;
			GCHandle gch = (GCHandle) data;
			(gch.Target as SpawnChildSetupWrapper).handler ();
			gch.Free ();
		}
	}

	public class Process {

		public const int IgnorePipe = Int32.MaxValue;
		public const int RequestPipe = 0;

		long pid;

		private Process (int pid) 
		{
			this.pid = pid;
		}

		[DllImport ("libglib-2.0-0.dll")]
		static extern void g_spawn_close_pid (int pid);

		public void Close ()
		{
			g_spawn_close_pid ((int) pid);
		}

		[DllImport ("libglib-2.0-0.dll")]
		static extern bool g_spawn_async (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, out IntPtr error);

		public static bool SpawnAsync (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out Process child_process)
		{
			int pid;
			IntPtr error;
			IntPtr native_dir = Marshaller.StringToPtrGStrdup (working_directory);
			IntPtr[] native_argv = Marshaller.StringArrayToNullTermPointer (argv);
			IntPtr[] native_envp = Marshaller.StringArrayToNullTermPointer (envp);
			SpawnChildSetupWrapper wrapper = new SpawnChildSetupWrapper (child_setup);
			bool result = g_spawn_async (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, out error);
			child_process = new Process (pid);
			Marshaller.Free (native_dir);
			Marshaller.Free (native_argv);
			Marshaller.Free (native_envp);
			if (error != IntPtr.Zero) throw new Gst.GLib.GException (error);
			return result;
		}  

		[DllImport ("libglib-2.0-0.dll")]
		static extern bool g_spawn_async_with_pipes (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, IntPtr stdin, IntPtr stdout, IntPtr stderr, out IntPtr error);

		public static bool SpawnAsyncWithPipes (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out Process child_process, ref int stdin, ref int stdout, ref int stderr)
		{
			int pid;
			IntPtr error;
			IntPtr native_dir = Marshaller.StringToPtrGStrdup (working_directory);
			IntPtr[] native_argv = Marshaller.StringArrayToNullTermPointer (argv);
			IntPtr[] native_envp = Marshaller.StringArrayToNullTermPointer (envp);
			SpawnChildSetupWrapper wrapper = new SpawnChildSetupWrapper (child_setup);
			IntPtr in_ptr = stdin == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
			IntPtr out_ptr = stdout == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
			IntPtr err_ptr = stderr == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
			bool result = g_spawn_async_with_pipes (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, in_ptr, out_ptr, err_ptr, out error);
			child_process = new Process (pid);
			if (in_ptr != IntPtr.Zero) {
				stdin = Marshal.ReadInt32 (in_ptr);
				Marshal.FreeHGlobal (in_ptr);
			}
			if (out_ptr != IntPtr.Zero) {
				stdout = Marshal.ReadInt32 (out_ptr);
				Marshal.FreeHGlobal (out_ptr);
			}
			if (err_ptr != IntPtr.Zero) {
				stderr = Marshal.ReadInt32 (err_ptr);
				Marshal.FreeHGlobal (err_ptr);
			}
			Marshaller.Free (native_dir);
			Marshaller.Free (native_argv);
			Marshaller.Free (native_envp);
			if (error != IntPtr.Zero) throw new Gst.GLib.GException (error);
			return result;
		}  

		[DllImport ("libglib-2.0-0.dll")]
		static extern bool g_spawn_sync (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

		public static bool SpawnSync (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out string stdout, out string stderr, out int exit_status)
		{
			IntPtr native_stdout, native_stderr, error;
			IntPtr native_dir = Marshaller.StringToPtrGStrdup (working_directory);
			IntPtr[] native_argv = Marshaller.StringArrayToNullTermPointer (argv);
			IntPtr[] native_envp = Marshaller.StringArrayToNullTermPointer (envp);
			SpawnChildSetupWrapper wrapper = new SpawnChildSetupWrapper (child_setup);
			bool result = g_spawn_sync (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out native_stdout, out native_stderr, out exit_status, out error);
			Marshaller.Free (native_dir);
			Marshaller.Free (native_argv);
			Marshaller.Free (native_envp);
			stdout = Marshaller.PtrToStringGFree (native_stdout);
			stderr = Marshaller.PtrToStringGFree (native_stderr);
			if (error != IntPtr.Zero) throw new Gst.GLib.GException (error);
			return result;
		}  

		[DllImport ("libglib-2.0-0.dll")]
		static extern bool g_spawn_command_line_async (IntPtr cmdline, out IntPtr error);

		public static bool SpawnCommandLineAsync (string command_line)
		{
			IntPtr error;
			IntPtr native_cmd = Marshaller.StringToPtrGStrdup (command_line);
			bool result = g_spawn_command_line_async (native_cmd, out error);
			Marshaller.Free (native_cmd);
			if (error != IntPtr.Zero) throw new Gst.GLib.GException (error);
			return result;
		}

		[DllImport ("libglib-2.0-0.dll")]
		static extern bool g_spawn_command_line_sync (IntPtr cmdline, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

		public static bool SpawnCommandLineSync (string command_line, out string stdout, out string stderr, out int exit_status)
		{
			IntPtr error, native_stdout, native_stderr;
			IntPtr native_cmd = Marshaller.StringToPtrGStrdup (command_line);
			bool result = g_spawn_command_line_sync (native_cmd, out native_stdout, out native_stderr, out exit_status, out error);
			Marshaller.Free (native_cmd);
			stdout = Marshaller.PtrToStringGFree (native_stdout);
			stderr = Marshaller.PtrToStringGFree (native_stderr);
			if (error != IntPtr.Zero) throw new Gst.GLib.GException (error);
			return result;
		}
	}
}