win-pvdrivers

changeset 548:47fae39bc373

Fixed an off-by-one crash. Tidied up error reporting a bit
author James Harper <james.harper@bendigoit.com.au>
date Sat Mar 28 10:04:45 2009 +1100 (2009-03-28)
parents ac614e49832c
children 967b567eaf6e
files shutdownmon/shutdownmon.c
line diff
     1.1 --- a/shutdownmon/shutdownmon.c	Sun Mar 08 14:37:45 2009 +1100
     1.2 +++ b/shutdownmon/shutdownmon.c	Sat Mar 28 10:04:45 2009 +1100
     1.3 @@ -53,19 +53,6 @@ struct xsd_sockmsg
     1.4  SERVICE_STATUS service_status; 
     1.5  SERVICE_STATUS_HANDLE hStatus; 
     1.6  
     1.7 -#define LOGFILE "C:\\xsm.log"
     1.8 -
     1.9 -int write_log(char* str)
    1.10 -{
    1.11 -   FILE* log;
    1.12 -   log = fopen(LOGFILE, "a+");
    1.13 -   if (log == NULL)
    1.14 -      return -1;
    1.15 -   fprintf(log, "%s\n", str);
    1.16 -   fclose(log);
    1.17 -   return 0;
    1.18 -}
    1.19 -
    1.20  static void
    1.21  install_service()
    1.22  {
    1.23 @@ -188,15 +175,24 @@ do_shutdown(BOOL bRebootAfterShutdown)
    1.24    TOKEN_PRIVILEGES *tp;
    1.25    HANDLE token_handle;
    1.26  
    1.27 +  printf("proc_handle = %p\n", proc_handle);
    1.28 +
    1.29    if (!OpenProcessToken(proc_handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token_handle))
    1.30 +  {
    1.31 +    printf("OpenProcessToken failed\n");
    1.32      return;
    1.33 +  }
    1.34 +  printf("token_handle = %p\n", token_handle);
    1.35 +
    1.36    tp = malloc(sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES));
    1.37    tp->PrivilegeCount = 1;
    1.38 -  if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tp->Privileges[0].Luid))
    1.39 +  if (!LookupPrivilegeValueA(NULL, SE_SHUTDOWN_NAME, &tp->Privileges[0].Luid))
    1.40    {
    1.41 +    printf("LookupPrivilegeValue failed\n");
    1.42      CloseHandle(token_handle);
    1.43      return;
    1.44    }
    1.45 +
    1.46    tp->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    1.47    if (!AdjustTokenPrivileges(token_handle, FALSE, tp, 0, NULL, NULL))
    1.48    {
    1.49 @@ -206,8 +202,10 @@ do_shutdown(BOOL bRebootAfterShutdown)
    1.50  
    1.51    if (!InitiateSystemShutdownEx(NULL, NULL, 0, TRUE, bRebootAfterShutdown, SHTDN_REASON_FLAG_PLANNED | SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
    1.52    {
    1.53 +    printf("InitiateSystemShutdownEx failed\n");
    1.54      // Log a message to the system log here about a failed shutdown
    1.55    }
    1.56 +  printf("InitiateSystemShutdownEx succeeded\n");
    1.57  
    1.58    CloseHandle(token_handle);
    1.59  }
    1.60 @@ -224,13 +222,13 @@ get_xen_interface_path()
    1.61    handle = SetupDiGetClassDevs(&GUID_XENBUS_IFACE, 0, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    1.62    if (handle == INVALID_HANDLE_VALUE)
    1.63    {
    1.64 -    write_log("SetupDiGetClassDevs failed\n"); 
    1.65 +    printf("SetupDiGetClassDevs failed\n"); 
    1.66      return NULL;
    1.67    }
    1.68    sdid.cbSize = sizeof(sdid);
    1.69    if (!SetupDiEnumDeviceInterfaces(handle, NULL, &GUID_XENBUS_IFACE, 0, &sdid))
    1.70    {
    1.71 -    write_log("SetupDiEnumDeviceInterfaces failed\n");
    1.72 +    printf("SetupDiEnumDeviceInterfaces failed\n");
    1.73      return NULL;
    1.74    }
    1.75    SetupDiGetDeviceInterfaceDetail(handle, &sdid, NULL, 0, &buf_len, NULL);
    1.76 @@ -238,7 +236,7 @@ get_xen_interface_path()
    1.77    sdidd->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    1.78    if (!SetupDiGetDeviceInterfaceDetail(handle, &sdid, sdidd, buf_len, NULL, NULL))
    1.79    {
    1.80 -    write_log("SetupDiGetDeviceInterfaceDetail failed\n"); 
    1.81 +    printf("SetupDiGetDeviceInterfaceDetail failed\n"); 
    1.82      return NULL;
    1.83    }
    1.84    
    1.85 @@ -321,6 +319,7 @@ printf("read start\n");
    1.86    msg->tx_id = 0;
    1.87    msg->len = (ULONG)(strlen(path) + 1);
    1.88    strcpy(buf + sizeof(*msg), path);
    1.89 +
    1.90    if (!WriteFile(handle, buf, sizeof(*msg) + msg->len, &bytes_written, NULL))
    1.91    {
    1.92      printf("write failed\n");
    1.93 @@ -336,7 +335,7 @@ printf("read start\n");
    1.94    printf("msg->len = %d\n", msg->len);
    1.95    buf[sizeof(*msg) + msg->len] = 0;
    1.96    printf("msg text = %s\n", buf + sizeof(*msg));
    1.97 -  ret = malloc(strlen(buf + sizeof(*msg)));
    1.98 +  ret = malloc(strlen(buf + sizeof(*msg)) + 1);
    1.99    strcpy(ret, buf + sizeof(*msg));
   1.100    return ret;
   1.101  }
   1.102 @@ -363,6 +362,7 @@ do_monitoring()
   1.103      if (service_status.dwCurrentState != SERVICE_RUNNING)
   1.104        return;
   1.105      buf = xb_read(handle, "control/shutdown");
   1.106 +
   1.107      //printf("msg = '%s'\n", msg);
   1.108      if (strcmp("poweroff", buf) == 0 || strcmp("halt", buf) == 0)
   1.109      {
   1.110 @@ -405,7 +405,7 @@ void service_main(int argc, char *argv[]
   1.111    UNREFERENCED_PARAMETER (argc);
   1.112    UNREFERENCED_PARAMETER (argv);
   1.113  
   1.114 -  write_log("Entering service_main\n"); 
   1.115 +  printf("Entering service_main\n"); 
   1.116  
   1.117    service_status.dwServiceType = SERVICE_WIN32; 
   1.118    service_status.dwCurrentState =  SERVICE_START_PENDING; 
   1.119 @@ -418,7 +418,7 @@ void service_main(int argc, char *argv[]
   1.120    hStatus = RegisterServiceCtrlHandler(SERVICE_ID, (LPHANDLER_FUNCTION)control_handler); 
   1.121    if (hStatus == (SERVICE_STATUS_HANDLE)0) 
   1.122    { 
   1.123 -    write_log("RegisterServiceCtrlHandler failed\n"); 
   1.124 +    printf("RegisterServiceCtrlHandler failed\n"); 
   1.125      return; 
   1.126    }  
   1.127  
   1.128 @@ -427,7 +427,7 @@ void service_main(int argc, char *argv[]
   1.129  
   1.130    do_monitoring();
   1.131  
   1.132 -  write_log("All done\n"); 
   1.133 +  printf("All done\n"); 
   1.134  
   1.135    return; 
   1.136  }