Class: RightScale::Platform::Controller

Inherits:
PlatformHelperBase show all
Defined in:
lib/right_agent/platform.rb,
lib/right_agent/platform/windows/platform.rb,
lib/right_agent/platform/unix/linux/platform.rb,
lib/right_agent/platform/unix/darwin/platform.rb,
lib/right_agent/platform/windows/mingw/platform.rb,
lib/right_agent/platform/windows/mswin/platform.rb

Overview

Filesystem

Defined Under Namespace

Classes: API

Constant Summary collapse

TOKEN_ADJUST_PRIVILEGES =
0x0020
TOKEN_QUERY =
0x0008
SE_PRIVILEGE_ENABLED =
0x00000002

Constants inherited from PlatformHelperBase

PlatformHelperBase::API_FALSE, PlatformHelperBase::API_NULL, PlatformHelperBase::API_TRUE, PlatformHelperBase::SIZEOF_DWORD, PlatformHelperBase::SIZEOF_QWORD, PlatformHelperBase::WIDE

Instance Method Summary collapse

Methods inherited from PlatformHelperBase

#copy_to_string_buffer, #with_unicode_buffer

Instance Method Details

#initiate_system_shutdown(reboot_after_shutdown) ⇒ Object



1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/right_agent/platform/windows/platform.rb', line 1431

def initiate_system_shutdown(reboot_after_shutdown)
  # APIs
  process = ::RightScale::Platform.process
  windows_common = ::RightScale::Platform.windows_common
  windows_security = ::RightScale::Platform.windows_security

  # get current process token.
  token_handle = 0.chr * 4
  unless process.OpenProcessToken(
            process_handle = process.GetCurrentProcess(),
            desired_access = TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY,
            token_handle)
    raise ::RightScale::Platform::Win32Error,
          'Failed to open process token'
  end
  token_handle = token_handle.unpack('V')[0]

  begin
    # lookup shutdown privilege ID.
    luid = 0.chr * 8
    unless windows_security.LookupPrivilegeValue(
              system_name = nil,
              priviledge_name = 'SeShutdownPrivilege' + 0.chr,
              luid)
      raise ::RightScale::Platform::Win32Error,
            'Failed to lookup shutdown privilege'
    end
    luid = luid.unpack('VV')

    # adjust token privilege to enable shutdown.
    token_privileges       = 0.chr * 16                        # TOKEN_PRIVILEGES tokenPrivileges;
    token_privileges[0,4]  = [1].pack('V')                     # tokenPrivileges.PrivilegeCount = 1;
    token_privileges[4,8]  = luid.pack('VV')                   # tokenPrivileges.Privileges[0].Luid = luid;
    token_privileges[12,4] = [SE_PRIVILEGE_ENABLED].pack('V')  # tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    unless windows_security.AdjustTokenPrivileges(
              token_handle, disable_all_privileges = false,
              token_privileges, buffer_length = 0,
              previous_state = nil, return_length = nil)
      raise ::RightScale::Platform::Win32Error,
            'Failed to adjust token privileges'
    end
    unless InitiateSystemShutdown(machine_name = nil,
                                  message = nil,
                                  timeout_secs = 1,
                                  force_apps_closed = true,
                                  reboot_after_shutdown)
      raise ::RightScale::Platform::Win32Error,
            'Failed to initiate system shutdown'
    end
  ensure
    windows_common.CloseHandle(token_handle)
  end
  true
end

#InitiateSystemShutdown(machine_name, message, timeout, force_apps_closed, reboot_after_shutdown) ⇒ Object

Overrides base Controller#InitiateSystemShutdown



1498
1499
1500
# File 'lib/right_agent/platform/windows/platform.rb', line 1498

def InitiateSystemShutdown(machine_name, message, timeout, force_apps_closed, reboot_after_shutdown)
  must_be_overridden
end

#rebootObject

Overrides base Controller#reboot



573
574
575
# File 'lib/right_agent/platform.rb', line 573

def reboot
  must_be_overridden
end

#shutdownObject

Overrides base Controller#shutdown



580
581
582
# File 'lib/right_agent/platform.rb', line 580

def shutdown
  must_be_overridden
end