win-pvdrivers

changeset 583:10147bb21470

remove old .NET shutdownmon
author James Harper <james.harper@bendigoit.com.au>
date Sun Jun 07 20:18:43 2009 +1000 (2009-06-07)
parents 3fb323edeea9
children 40a40d4aa88a
files ShutdownMon/Program.cs ShutdownMon/ShutdownMon.csproj
line diff
     1.1 --- a/ShutdownMon/Program.cs	Tue Jun 02 22:50:25 2009 +1000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,366 +0,0 @@
     1.4 -using System;
     1.5 -using System.Collections;
     1.6 -using System.Configuration.Install;
     1.7 -using System.ComponentModel;
     1.8 -using System.IO;
     1.9 -using System.Runtime.InteropServices;
    1.10 -using System.Reflection;
    1.11 -using System.ServiceProcess;
    1.12 -using System.Text;
    1.13 -using System.Threading;
    1.14 -using Microsoft.Win32.SafeHandles;
    1.15 -
    1.16 -
    1.17 -namespace ShutdownMon
    1.18 -{
    1.19 -    class Program : ServiceBase
    1.20 -    {
    1.21 -        /* 
    1.22 -        * Shutdown definition see here: http://msdn2.microsoft.com/en-us/library/aa376868.aspx
    1.23 -         * http://msdn2.microsoft.com/en-us/library/aa376873(VS.85).aspx
    1.24 -        */
    1.25 -
    1.26 -        internal enum ShutdownReason : uint
    1.27 -        {
    1.28 -            MajorApplication = 0x00040000,
    1.29 -            MajorHardware = 0x00010000,
    1.30 -            MajorLegacyApi = 0x00070000,
    1.31 -            MajorOperatingSystem = 0x00020000,
    1.32 -            MajorOther = 0x00000000,
    1.33 -            MajorPower = 0x00060000,
    1.34 -            MajorSoftware = 0x00030000,
    1.35 -            MajorSystem = 0x00050000,
    1.36 -
    1.37 -            MinorBlueScreen = 0x0000000F,
    1.38 -            MinorCordUnplugged = 0x0000000b,
    1.39 -            MinorDisk = 0x00000007,
    1.40 -            MinorEnvironment = 0x0000000c,
    1.41 -            MinorHardwareDriver = 0x0000000d,
    1.42 -            MinorHotfix = 0x00000011,
    1.43 -            MinorHung = 0x00000005,
    1.44 -            MinorInstallation = 0x00000002,
    1.45 -            MinorMaintenance = 0x00000001,
    1.46 -            MinorMMC = 0x00000019,
    1.47 -            MinorNetworkConnectivity = 0x00000014,
    1.48 -            MinorNetworkCard = 0x00000009,
    1.49 -            MinorOther = 0x00000000,
    1.50 -            MinorOtherDriver = 0x0000000e,
    1.51 -            MinorPowerSupply = 0x0000000a,
    1.52 -            MinorProcessor = 0x00000008,
    1.53 -            MinorReconfig = 0x00000004,
    1.54 -            MinorSecurity = 0x00000013,
    1.55 -            MinorSecurityFix = 0x00000012,
    1.56 -            MinorSecurityFixUninstall = 0x00000018,
    1.57 -            MinorServicePack = 0x00000010,
    1.58 -            MinorServicePackUninstall = 0x00000016,
    1.59 -            MinorTermSrv = 0x00000020,
    1.60 -            MinorUnstable = 0x00000006,
    1.61 -            MinorUpgrade = 0x00000003,
    1.62 -            MinorWMI = 0x00000015,
    1.63 -
    1.64 -            FlagUserDefined = 0x40000000,
    1.65 -            FlagPlanned = 0x80000000
    1.66 -        }
    1.67 -
    1.68 -        const UInt32 READ_CONTROL = 0x00020000;
    1.69 -        const UInt32 STANDARD_RIGHTS_READ = READ_CONTROL;
    1.70 -        const UInt32 FILE_READ_DATA = 0x0001;
    1.71 -        const UInt32 FILE_READ_ATTRIBUTES = 0x0080;
    1.72 -        const UInt32 FILE_READ_EA = 0x0008;
    1.73 -        const UInt32 SYNCHRONIZE = 0x00100000;
    1.74 -        const UInt32 OPEN_EXISTING = 3;
    1.75 -        const UInt32 FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE);
    1.76 -        const UInt32 FILE_ATTRIBUTE_NORMAL = 0x0080;
    1.77 -
    1.78 -        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    1.79 -        internal static extern SafeFileHandle CreateFile(
    1.80 -            String Filename,
    1.81 -            UInt32 DesiredAccess,
    1.82 -            UInt32 ShareMode,
    1.83 -            IntPtr Attributes,
    1.84 -            UInt32 CreationDisposition,
    1.85 -            UInt32 FlagsAndAttributes,
    1.86 -            IntPtr TemplateFile);
    1.87 -
    1.88 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
    1.89 -        internal struct TokPriv1Luid
    1.90 -        {
    1.91 -            public int Count;
    1.92 -            public long Luid;
    1.93 -            public int Attr;
    1.94 -        }
    1.95 -
    1.96 -        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    1.97 -        internal static extern IntPtr GetCurrentProcess();
    1.98 -
    1.99 -        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   1.100 -        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
   1.101 -
   1.102 -        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   1.103 -        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
   1.104 -
   1.105 -        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   1.106 -        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
   1.107 -
   1.108 -        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   1.109 -        internal static extern bool InitiateSystemShutdownEx(string lpMachineName, string lpMessage, int dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown, ShutdownReason dwReason);
   1.110 -
   1.111 -//        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   1.112 -               
   1.113 -        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
   1.114 -        internal const int TOKEN_QUERY = 0x00000008;
   1.115 -        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
   1.116 -        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
   1.117 -
   1.118 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
   1.119 -        protected struct SP_DEVICE_INTERFACE_DATA
   1.120 -        {
   1.121 -            public UInt32 cbSize;
   1.122 -            public Guid InterfaceClassGuid;
   1.123 -            public UInt32 Flags;
   1.124 -            public IntPtr Reserved;
   1.125 -        };
   1.126 -        /*
   1.127 -        [StructLayout(LayoutKind.Sequential, Pack = 1)]
   1.128 -        public struct SP_DEVICE_INTERFACE_DETAIL_DATA
   1.129 -        {
   1.130 -            public int Size;
   1.131 -            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
   1.132 -            public string DevicePath;
   1.133 -        }
   1.134 -        */
   1.135 -        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
   1.136 -        private static extern IntPtr SetupDiGetClassDevsA(
   1.137 -            ref Guid ClassGuid,
   1.138 -            UInt32 Enumerator,
   1.139 -            IntPtr hwndParent,
   1.140 -            UInt32 Flags);
   1.141 -
   1.142 -        [DllImport("setupapi.dll", SetLastError = true)]
   1.143 -        private static extern Boolean SetupDiEnumDeviceInterfaces(
   1.144 -            IntPtr DeviceInfoSet,
   1.145 -            IntPtr DeviceInfoData,
   1.146 -            ref Guid InterfaceClassGuid,
   1.147 -            int MemberIndex,
   1.148 -            out SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
   1.149 -
   1.150 -        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
   1.151 -        private static extern Boolean SetupDiGetDeviceInterfaceDetail(
   1.152 -            IntPtr DeviceInfoSet,
   1.153 -            ref SP_DEVICE_INTERFACE_DATA DeviceIntefaceData,
   1.154 -            IntPtr DeviceInterfaceDetailData,
   1.155 -            UInt32 DeviceInterfacedetailDatasize,
   1.156 -            ref UInt32 DeviceInterfacedetaildataSize,
   1.157 -            IntPtr DeviceInfoData);
   1.158 -
   1.159 -        const int DIGCF_PRESENT = 0x00000002;
   1.160 -        const int DIGCF_DEVICEINTERFACE = 0x00000010;
   1.161 -
   1.162 -        private string GetXenInterfacePath()
   1.163 -        {
   1.164 -            Guid XenInterface = new Guid("{5c568ac5-9ddf-4fa5-a94a-39d67077819c}");
   1.165 -            IntPtr handle = SetupDiGetClassDevsA(ref XenInterface, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
   1.166 -            SP_DEVICE_INTERFACE_DATA sdid = new SP_DEVICE_INTERFACE_DATA();
   1.167 -
   1.168 -            sdid.cbSize = (UInt32)Marshal.SizeOf(sdid);
   1.169 -            sdid.InterfaceClassGuid = System.Guid.Empty;
   1.170 -            sdid.Flags = 0;
   1.171 -            sdid.Reserved = IntPtr.Zero;
   1.172 -
   1.173 -            if (!SetupDiEnumDeviceInterfaces(handle, IntPtr.Zero, ref XenInterface, 0, out sdid))
   1.174 -                throw new Win32Exception(Marshal.GetLastWin32Error());
   1.175 -            Console.WriteLine("Got interface");
   1.176 -            UInt32 buflen = 1024;
   1.177 -            IntPtr buf = Marshal.AllocHGlobal((int)buflen);
   1.178 -            if (Marshal.SizeOf(buf) == 4)
   1.179 -                Marshal.WriteInt32(buf, 6); // 32 bit systems align it this way...
   1.180 -            else
   1.181 -                Marshal.WriteInt32(buf, 8); // and 64 bit systems align it that...
   1.182 -            if (!SetupDiGetDeviceInterfaceDetail(handle, ref sdid, buf, buflen, ref buflen, IntPtr.Zero))
   1.183 -                throw new Win32Exception(Marshal.GetLastWin32Error());
   1.184 -            Console.WriteLine("Got detail");
   1.185 -            string filename = Marshal.PtrToStringUni(new IntPtr(buf.ToInt64() + 4));
   1.186 -            Console.WriteLine("interface path = " + filename);
   1.187 -            // free stuff here
   1.188 -            return filename;
   1.189 -        }
   1.190 -
   1.191 -        private static void DoExitWin(bool bForceAppsClosed, bool bRebootAfterShutdown)
   1.192 -        {
   1.193 -            bool ok;
   1.194 -            TokPriv1Luid tp;
   1.195 -            IntPtr hproc = GetCurrentProcess();
   1.196 -            IntPtr htok = IntPtr.Zero;
   1.197 -            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
   1.198 -            tp.Count = 1;
   1.199 -            tp.Luid = 0;
   1.200 -            tp.Attr = SE_PRIVILEGE_ENABLED;
   1.201 -            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
   1.202 -            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
   1.203 -            ok = InitiateSystemShutdownEx(null, null, 0, bForceAppsClosed, bRebootAfterShutdown, ShutdownReason.MajorOther & ShutdownReason.MinorOther);
   1.204 -        }
   1.205 -        
   1.206 -        const string MyServiceName = "XenShutdownMon";
   1.207 -        const string MyDisplayName = "Xen Shutdown Monitor Service";
   1.208 -        const string MyServiceDescription = "Monitors the kernel driver and shuts down Windows when directed";
   1.209 -
   1.210 -        private static Installer MakeServiceInstaller()
   1.211 -        {
   1.212 -            Installer i = new Installer();
   1.213 -            i.Context = new InstallContext();
   1.214 -            i.Context.Parameters.Add("AssemblyPath", Assembly.GetExecutingAssembly().Location);
   1.215 -            i.Context.Parameters.Add("LogToConsole", "false");
   1.216 -            i.Context.Parameters.Add("Silent", "true");
   1.217 -
   1.218 -            ServiceProcessInstaller spi = new ServiceProcessInstaller();
   1.219 -            spi.Account = ServiceAccount.LocalSystem;
   1.220 -            spi.Username = "";
   1.221 -            spi.Password = "";
   1.222 -            i.Installers.Add(spi);
   1.223 -
   1.224 -            ServiceInstaller si = new ServiceInstaller();
   1.225 -            si.ServiceName = MyServiceName;
   1.226 -            si.DisplayName = MyDisplayName;
   1.227 -            si.Description = MyServiceDescription;
   1.228 -            si.StartType = ServiceStartMode.Automatic;
   1.229 -            i.Installers.Add(si);
   1.230 -
   1.231 -            return i;
   1.232 -        }
   1.233 -
   1.234 -        static void Main(string[] args)
   1.235 -        {
   1.236 -            int argNo = 0;
   1.237 -
   1.238 -            while (argNo < args.Length)
   1.239 -            {
   1.240 -                string arg = args[argNo++];
   1.241 -                switch (arg)
   1.242 -                {
   1.243 -                    case "-i":
   1.244 -                        {
   1.245 -                            IDictionary mySavedState = new Hashtable();
   1.246 -                            Installer i = MakeServiceInstaller();
   1.247 -                            try
   1.248 -                            {
   1.249 -                                i.Install(mySavedState);
   1.250 -
   1.251 -                                Microsoft.Win32.RegistryKey config;
   1.252 -                                config = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System").OpenSubKey("CurrentControlSet").OpenSubKey("Services").OpenSubKey(MyServiceName, true);
   1.253 -                                if (args.Length > 1)
   1.254 -                                {
   1.255 -                                    config.SetValue("ImagePath", config.GetValue("ImagePath") + " -s " + string.Join(" ", args, argNo, args.Length - argNo - 1));
   1.256 -                                }
   1.257 -                                else
   1.258 -                                {
   1.259 -                                    config.SetValue("ImagePath", config.GetValue("ImagePath") + " -s");
   1.260 -                                }
   1.261 -
   1.262 -                                Console.WriteLine("Service installed successfully.");
   1.263 -                            }
   1.264 -                            catch (Win32Exception exWin32Exception)
   1.265 -                            {
   1.266 -                                if (exWin32Exception.NativeErrorCode == 1073)
   1.267 -                                {
   1.268 -                                    Console.WriteLine("Service already exists.");
   1.269 -                                    return;
   1.270 -                                }
   1.271 -                                else
   1.272 -                                    throw exWin32Exception;
   1.273 -                            }
   1.274 -                            return;
   1.275 -                        }
   1.276 -                    case "-u":
   1.277 -                        {
   1.278 -                            IDictionary mySavedState = new Hashtable();
   1.279 -                            Installer i = MakeServiceInstaller();
   1.280 -
   1.281 -                            try
   1.282 -                            {
   1.283 -                                i.Uninstall(null);
   1.284 -                                Console.WriteLine("Service uninstalled successfully.");
   1.285 -                            }
   1.286 -                            catch (InstallException exInstallException)
   1.287 -                            {
   1.288 -                                if (exInstallException.InnerException is Win32Exception)
   1.289 -                                {
   1.290 -                                    Win32Exception exWin32Exception = (Win32Exception)exInstallException.InnerException;
   1.291 -                                    if (exWin32Exception.NativeErrorCode == 1060)
   1.292 -                                    {
   1.293 -                                        Console.WriteLine("Service does not exist.");
   1.294 -                                        return;
   1.295 -                                    }
   1.296 -                                }
   1.297 -                                throw exInstallException;
   1.298 -                            }
   1.299 -                            return;
   1.300 -                        }
   1.301 -                    case "-s": // run as service
   1.302 -                        ServiceBase.Run(new Program());
   1.303 -                        return;
   1.304 -                }
   1.305 -            }
   1.306 -            Console.WriteLine("Connecting to kernel driver...");
   1.307 -            new Program().Run();
   1.308 -        }
   1.309 -
   1.310 -        public Program()
   1.311 -        {
   1.312 -            this.ServiceName = MyServiceName;
   1.313 -            this.CanHandlePowerEvent = false;
   1.314 -            this.CanHandleSessionChangeEvent = false;
   1.315 -            this.CanPauseAndContinue = false;
   1.316 -            this.CanShutdown = false;
   1.317 -            this.CanStop = true;
   1.318 -        }
   1.319 -
   1.320 -        private Thread workerThread = null;
   1.321 -
   1.322 -        protected override void OnStart(string[] args)
   1.323 -        {
   1.324 -            workerThread = new Thread(new ThreadStart(Run));
   1.325 -            workerThread.Start();
   1.326 -        }
   1.327 -
   1.328 -        protected override void OnStop()
   1.329 -        {
   1.330 -            workerThread.Abort();
   1.331 -            workerThread.Join();
   1.332 -        }
   1.333 -
   1.334 -        protected void Run()
   1.335 -        {
   1.336 -            SafeFileHandle handle;
   1.337 -            byte[] buf = new byte[128];
   1.338 -
   1.339 -            handle = CreateFile(GetXenInterfacePath(), FILE_GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
   1.340 -            FileStream fs = new FileStream(handle, FileAccess.Read);
   1.341 -            StreamReader sr = new StreamReader(fs);
   1.342 -            Console.WriteLine("Opened");
   1.343 -            while (true)
   1.344 -            {
   1.345 -                string command = sr.ReadLine();
   1.346 -
   1.347 -                Console.WriteLine("Command = " + command);
   1.348 -
   1.349 -                switch (command)
   1.350 -                {
   1.351 -                    case "":
   1.352 -                        break;
   1.353 -                    case "reboot":
   1.354 -                        //DoExitWin(EWX_REBOOT | EWX_FORCE);
   1.355 -                        //DoExitWin(ExitWindows.Reboot | ExitWindows.ForceIfHung);
   1.356 -                        DoExitWin(true, true);
   1.357 -                        break;
   1.358 -                    case "poweroff":
   1.359 -                    case "halt":
   1.360 -                    default:
   1.361 -                        //DoExitWin(EWX_POWEROFF | EWX_FORCE);
   1.362 -                        //DoExitWin(ExitWindows.PowerOff | ExitWindows.ForceIfHung);
   1.363 -                        DoExitWin(true, false);
   1.364 -                        break;
   1.365 -                }
   1.366 -            }
   1.367 -        }
   1.368 -    }
   1.369 -}
     2.1 --- a/ShutdownMon/ShutdownMon.csproj	Tue Jun 02 22:50:25 2009 +1000
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,66 +0,0 @@
     2.4 -<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
     2.5 -  <PropertyGroup>
     2.6 -    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     2.7 -    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     2.8 -    <ProductVersion>9.0.21022</ProductVersion>
     2.9 -    <SchemaVersion>2.0</SchemaVersion>
    2.10 -    <ProjectGuid>{26FF5B06-2383-454E-821F-306186171677}</ProjectGuid>
    2.11 -    <OutputType>Exe</OutputType>
    2.12 -    <AppDesignerFolder>Properties</AppDesignerFolder>
    2.13 -    <RootNamespace>ShutdownMon</RootNamespace>
    2.14 -    <AssemblyName>ShutdownMon</AssemblyName>
    2.15 -    <StartupObject>
    2.16 -    </StartupObject>
    2.17 -    <FileUpgradeFlags>
    2.18 -    </FileUpgradeFlags>
    2.19 -    <OldToolsVersion>2.0</OldToolsVersion>
    2.20 -    <UpgradeBackupLocation>
    2.21 -    </UpgradeBackupLocation>
    2.22 -  </PropertyGroup>
    2.23 -  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    2.24 -    <DebugSymbols>true</DebugSymbols>
    2.25 -    <DebugType>full</DebugType>
    2.26 -    <Optimize>false</Optimize>
    2.27 -    <OutputPath>..\target\</OutputPath>
    2.28 -    <DefineConstants>DEBUG;TRACE</DefineConstants>
    2.29 -    <ErrorReport>prompt</ErrorReport>
    2.30 -    <WarningLevel>4</WarningLevel>
    2.31 -  </PropertyGroup>
    2.32 -  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    2.33 -    <DebugType>pdbonly</DebugType>
    2.34 -    <Optimize>true</Optimize>
    2.35 -    <OutputPath>..\target\</OutputPath>
    2.36 -    <DefineConstants>TRACE</DefineConstants>
    2.37 -    <ErrorReport>prompt</ErrorReport>
    2.38 -    <WarningLevel>4</WarningLevel>
    2.39 -  </PropertyGroup>
    2.40 -  <ItemGroup>
    2.41 -    <Reference Include="System" />
    2.42 -    <Reference Include="System.Configuration.Install" />
    2.43 -    <Reference Include="System.Data" />
    2.44 -    <Reference Include="System.ServiceProcess" />
    2.45 -    <Reference Include="System.Xml" />
    2.46 -  </ItemGroup>
    2.47 -  <ItemGroup>
    2.48 -    <Compile Include="Program.cs">
    2.49 -      <SubType>Component</SubType>
    2.50 -    </Compile>
    2.51 -    <Compile Include="Properties\AssemblyInfo.cs" />
    2.52 -  </ItemGroup>
    2.53 -  <ItemGroup>
    2.54 -    <None Include="makefile" />
    2.55 -    <None Include="sources" />
    2.56 -  </ItemGroup>
    2.57 -  <ItemGroup>
    2.58 -    <Content Include="makefile.inc" />
    2.59 -    <Content Include="shutdownmon.c" />
    2.60 -  </ItemGroup>
    2.61 -  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    2.62 -  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
    2.63 -       Other similar extension points exist, see Microsoft.Common.targets.
    2.64 -  <Target Name="BeforeBuild">
    2.65 -  </Target>
    2.66 -  <Target Name="AfterBuild">
    2.67 -  </Target>
    2.68 -  -->
    2.69 -</Project>
    2.70 \ No newline at end of file