diff VBoxFrontend/ProgramLoader.cs @ 0:e1ec7bf71313

Initial commit
author Ivo Smits
date Wed, 04 May 2011 00:59:43 +0200
parents
children b0033e69105a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VBoxFrontend/ProgramLoader.cs	Wed May 04 00:59:43 2011 +0200
@@ -0,0 +1,36 @@
+using System;
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+namespace ConsoleApplication1 {
+	class ProgramLoader {
+		[DllImport("/usr/lib/virtualbox/VBoxXPCOMC.so")]
+		private static extern Int32 RTR3InitAndSUPLib();
+
+		static unsafe int Main(string[] args) {
+			if (Environment.OSVersion.Platform == PlatformID.Unix) {
+				Console.WriteLine("UNIX based OS detected, doing some magic...");
+				int init = RTR3InitAndSUPLib();
+				if (init != 0) throw new COMException("Failed to initialize VirtualBox driver", init);
+				Unix_setuid();
+				Assembly.LoadFile("Interop.VirtualBox.XPCOM.dll");
+			}
+			return Main2(args);
+		}
+
+		static int Main2(String[] args) {
+			ProgramRun p = new ProgramRun();
+			return p.Run(args);
+		}
+
+		private static void Unix_setuid() {
+			uint uid = Mono.Unix.Native.Syscall.getuid();
+			uint euid = Mono.Unix.Native.Syscall.geteuid();
+			Console.WriteLine("Running as user {0} effective {1}", uid, euid);
+			if (uid != euid) {
+				Console.WriteLine("Set user ID: {0}", uid);
+				Mono.Unix.Native.Syscall.setreuid(uid, uid);
+			}
+		}
+	}
+}
\ No newline at end of file