win-pvdrivers

changeset 623:b79b67b46d0c

Added hibernate response to shutdownmon (need to manually write 'hibernate' to /local/domain/<domid>/control/shutdown)
author James Harper <james.harper@bendigoit.com.au>
date Sat Aug 08 20:55:53 2009 +1000 (2009-08-08)
parents f905eb3f0545
children 0b55299418ce
files shutdownmon/shutdownmon.c shutdownmon/sources
line diff
     1.1 --- a/shutdownmon/shutdownmon.c	Wed Aug 05 19:09:55 2009 +1000
     1.2 +++ b/shutdownmon/shutdownmon.c	Sat Aug 08 20:55:53 2009 +1000
     1.3 @@ -9,6 +9,7 @@
     1.4  #include <winioctl.h>
     1.5  #include <setupapi.h>
     1.6  #include <ctype.h>
     1.7 +#include <powrprof.h>
     1.8  
     1.9  #define SERVICE_ID "ShutdownMon"
    1.10  #define SERVICE_NAME "Xen Shutdown Monitor"
    1.11 @@ -169,6 +170,46 @@ remove_service()
    1.12  }
    1.13  
    1.14  static void
    1.15 +do_hibernate()
    1.16 +{
    1.17 +  HANDLE proc_handle = GetCurrentProcess();
    1.18 +  TOKEN_PRIVILEGES *tp;
    1.19 +  HANDLE token_handle;
    1.20 +
    1.21 +  printf("proc_handle = %p\n", proc_handle);
    1.22 +
    1.23 +  if (!OpenProcessToken(proc_handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token_handle))
    1.24 +  {
    1.25 +    printf("OpenProcessToken failed\n");
    1.26 +    return;
    1.27 +  }
    1.28 +  printf("token_handle = %p\n", token_handle);
    1.29 +
    1.30 +  tp = malloc(sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES));
    1.31 +  tp->PrivilegeCount = 1;
    1.32 +  if (!LookupPrivilegeValueA(NULL, SE_SHUTDOWN_NAME, &tp->Privileges[0].Luid))
    1.33 +  {
    1.34 +    printf("LookupPrivilegeValue failed\n");
    1.35 +    CloseHandle(token_handle);
    1.36 +    return;
    1.37 +  }
    1.38 +
    1.39 +  tp->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    1.40 +  if (!AdjustTokenPrivileges(token_handle, FALSE, tp, 0, NULL, NULL))
    1.41 +  {
    1.42 +    CloseHandle(token_handle);
    1.43 +    return;
    1.44 +  }
    1.45 +
    1.46 +  if (!SetSuspendState(TRUE, FALSE, FALSE))
    1.47 +  {
    1.48 +    printf("hibernate failed\n");
    1.49 +  }
    1.50 +
    1.51 +  CloseHandle(token_handle);
    1.52 +}
    1.53 +
    1.54 +static void
    1.55  do_shutdown(BOOL bRebootAfterShutdown)
    1.56  {
    1.57    HANDLE proc_handle = GetCurrentProcess();
    1.58 @@ -372,6 +413,10 @@ do_monitoring()
    1.59      {
    1.60        do_shutdown(TRUE);
    1.61      } 
    1.62 +    else if (strcmp("hibernate", buf) == 0)
    1.63 +    {
    1.64 +      do_hibernate();
    1.65 +    } 
    1.66    }
    1.67  }
    1.68  
     2.1 --- a/shutdownmon/sources	Wed Aug 05 19:09:55 2009 +1000
     2.2 +++ b/shutdownmon/sources	Sat Aug 08 20:55:53 2009 +1000
     2.3 @@ -1,7 +1,7 @@
     2.4  !INCLUDE ..\common.inc
     2.5  TARGETNAME=shutdownmon
     2.6  TARGETTYPE=PROGRAM
     2.7 -TARGETLIBS=$(TARGETLIBS) $(DDK_LIB_PATH)\setupapi.lib
     2.8 +TARGETLIBS=$(TARGETLIBS) $(DDK_LIB_PATH)\setupapi.lib $(DDK_LIB_PATH)\powrprof.lib
     2.9  UMTYPE=console
    2.10  #UMTYPE=windows
    2.11  UMENTRY=main