diff options
author | sersut <serdar@opscode.com> | 2013-10-17 15:28:23 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2013-10-17 15:28:23 -0700 |
commit | 180e9c1ec6cc324c598b3575a6ba2a209df44a24 (patch) | |
tree | 09bfce4f8c4e8f29a7c6c740f7ed86cec11d5737 /lib/chef/win32 | |
parent | c995e37339f37be82d7e5b1b7866bef64c1bd05a (diff) | |
parent | 05ba3e301794bd09173c5c3f13a62c2530d8b403 (diff) | |
download | chef-180e9c1ec6cc324c598b3575a6ba2a209df44a24.tar.gz |
Merge branch 'master' into 11-stable
Diffstat (limited to 'lib/chef/win32')
-rw-r--r-- | lib/chef/win32/api/file.rb | 2 | ||||
-rw-r--r-- | lib/chef/win32/api/synchronization.rb | 89 | ||||
-rw-r--r-- | lib/chef/win32/handle.rb | 2 | ||||
-rw-r--r-- | lib/chef/win32/mutex.rb | 94 | ||||
-rw-r--r-- | lib/chef/win32/security/ace.rb | 2 | ||||
-rw-r--r-- | lib/chef/win32/security/sid.rb | 2 | ||||
-rw-r--r-- | lib/chef/win32/version.rb | 8 |
7 files changed, 192 insertions, 7 deletions
diff --git a/lib/chef/win32/api/file.rb b/lib/chef/win32/api/file.rb index afa746efce..7a8dafd8b5 100644 --- a/lib/chef/win32/api/file.rb +++ b/lib/chef/win32/api/file.rb @@ -474,7 +474,7 @@ BOOL WINAPI DeviceIoControl( # Workaround for CHEF-4419: # Make sure paths starting with "/" has a drive letter # assigned from the current working diretory. - # Note: In chef 11.8 and beyond this issue will be fixed with a + # Note: With CHEF-4427 this issue will be fixed with a # broader fix to map all the paths starting with "/" to # SYSTEM_DRIVE on windows. path = ::File.expand_path(path) if path.start_with? "/" diff --git a/lib/chef/win32/api/synchronization.rb b/lib/chef/win32/api/synchronization.rb new file mode 100644 index 0000000000..9c148d7e2b --- /dev/null +++ b/lib/chef/win32/api/synchronization.rb @@ -0,0 +1,89 @@ +# +# Author:: Serdar Sutay (<serdar@opscode.com>) +# Copyright:: Copyright 2011 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/win32/api' + +class Chef + module ReservedNames::Win32 + module API + module Synchronization + extend Chef::ReservedNames::Win32::API + + ffi_lib 'kernel32' + + # Constant synchronization functions use to indicate wait + # forever. + INFINITE = 0xFFFFFFFF + + # Return codes + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx + WAIT_FAILED = 0xFFFFFFFF + WAIT_TIMEOUT = 0x00000102 + WAIT_OBJECT_0 = 0x00000000 + WAIT_ABANDONED = 0x00000080 + + # Security and access rights for synchronization objects + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms686670(v=vs.85).aspx + DELETE = 0x00010000 + READ_CONTROL = 0x00020000 + SYNCHRONIZE = 0x00100000 + WRITE_DAC = 0x00040000 + WRITE_OWNER = 0x00080000 + + # Mutex specific rights + MUTEX_ALL_ACCESS = 0x001F0001 + MUTEX_MODIFY_STATE = 0x00000001 + +=begin +HANDLE WINAPI CreateMutex( + _In_opt_ LPSECURITY_ATTRIBUTES lpMutexAttributes, + _In_ BOOL bInitialOwner, + _In_opt_ LPCTSTR lpName +); +=end + safe_attach_function :CreateMutexW, [ :LPSECURITY_ATTRIBUTES, :BOOL, :LPCTSTR ], :HANDLE + safe_attach_function :CreateMutexA, [ :LPSECURITY_ATTRIBUTES, :BOOL, :LPCTSTR ], :HANDLE + +=begin +DWORD WINAPI WaitForSingleObject( + _In_ HANDLE hHandle, + _In_ DWORD dwMilliseconds +); +=end + safe_attach_function :WaitForSingleObject, [ :HANDLE, :DWORD ], :DWORD + +=begin +BOOL WINAPI ReleaseMutex( + _In_ HANDLE hMutex +); +=end + safe_attach_function :ReleaseMutex, [ :HANDLE ], :BOOL + +=begin +HANDLE WINAPI OpenMutex( + _In_ DWORD dwDesiredAccess, + _In_ BOOL bInheritHandle, + _In_ LPCTSTR lpName +); +=end + safe_attach_function :OpenMutexW, [ :DWORD, :BOOL, :LPCTSTR ], :HANDLE + safe_attach_function :OpenMutexA, [ :DWORD, :BOOL, :LPCTSTR ], :HANDLE + end + end + end +end diff --git a/lib/chef/win32/handle.rb b/lib/chef/win32/handle.rb index 3e92703db9..21a8fdf339 100644 --- a/lib/chef/win32/handle.rb +++ b/lib/chef/win32/handle.rb @@ -29,7 +29,7 @@ class Chef # See http://msdn.microsoft.com/en-us/library/windows/desktop/ms683179(v=vs.85).aspx # The handle value returned by the GetCurrentProcess function is the pseudo handle (HANDLE)-1 (which is 0xFFFFFFFF) CURRENT_PROCESS_HANDLE = 4294967295 - + def initialize(handle) @handle = handle ObjectSpace.define_finalizer(self, Handle.close_handle_finalizer(handle)) diff --git a/lib/chef/win32/mutex.rb b/lib/chef/win32/mutex.rb new file mode 100644 index 0000000000..b0a9ba210e --- /dev/null +++ b/lib/chef/win32/mutex.rb @@ -0,0 +1,94 @@ +# +# Author:: Serdar Sutay (<serdar@opscode.com>) +# Copyright:: Copyright 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/win32/api/synchronization' + +class Chef + module ReservedNames::Win32 + class Mutex + include Chef::ReservedNames::Win32::API::Synchronization + extend Chef::ReservedNames::Win32::API::Synchronization + + def initialize(name) + @name = name + # First check if there exists a mutex in the system with the + # given name. + + # In the initial creation of the mutex initial_owner is set to + # false so that mutex will not be acquired until someone calls + # acquire. + # In order to call "*W" windows apis, strings needs to be + # encoded as wide strings. + @handle = CreateMutexW(nil, false, name.to_wstring) + + # Fail early if we can't get a handle to the named mutex + if @handle == 0 + Chef::Log.error("Failed to create system mutex with name'#{name}'") + Chef::ReservedNames::Win32::Error.raise! + end + end + + attr_reader :handle + attr_reader :name + + ##################################################### + # Attempts to grab the mutex. + # Returns true if the mutex is grabbed or if it's already + # owned; false otherwise. + def test + WaitForSingleObject(handle, 0) == WAIT_OBJECT_0 + end + + ##################################################### + # Attempts to grab the mutex and waits until it is acquired. + def wait + wait_result = WaitForSingleObject(handle, INFINITE) + case wait_result + when WAIT_ABANDONED + # Previous owner of the mutex died before it can release the + # mutex. Log a warning and continue. + Chef::Log.debug "Existing owner of the mutex exited prematurely." + when WAIT_OBJECT_0 + # Mutex is successfully acquired. + else + Chef::Log.error("Failed to acquire system mutex '#{name}'. Return code: #{wait_result}") + Chef::ReservedNames::Win32::Error.raise! + end + end + + ##################################################### + # Releaes the mutex + def release + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms685066(v=vs.85).aspx + # Note that release method needs to be called more than once + # if mutex is acquired more than once. + unless ReleaseMutex(handle) + # Don't fail things in here if we can't release the mutex. + # Because it will be automatically released when the owner + # of the process goes away and this class is only being used + # to synchronize chef-clients runs on a node. + Chef::Log.error("Can not release mutex '#{name}'. This might cause issues \ +if the mutex is attempted to be acquired by other threads.") + Chef::ReservedNames::Win32::Error.raise! + end + end + end + end +end + + diff --git a/lib/chef/win32/security/ace.rb b/lib/chef/win32/security/ace.rb index efd44b1c85..3aeae35532 100644 --- a/lib/chef/win32/security/ace.rb +++ b/lib/chef/win32/security/ace.rb @@ -122,4 +122,4 @@ class Chef end end end -end
\ No newline at end of file +end diff --git a/lib/chef/win32/security/sid.rb b/lib/chef/win32/security/sid.rb index 7ca21eee79..e1b20224bb 100644 --- a/lib/chef/win32/security/sid.rb +++ b/lib/chef/win32/security/sid.rb @@ -196,4 +196,4 @@ class Chef end end end -end
\ No newline at end of file +end diff --git a/lib/chef/win32/version.rb b/lib/chef/win32/version.rb index c8c923a6f2..62f817503e 100644 --- a/lib/chef/win32/version.rb +++ b/lib/chef/win32/version.rb @@ -30,14 +30,16 @@ class Chef # http://msdn.microsoft.com/en-us/library/ms724358(v=vs.85).aspx private - + def self.get_system_metrics(n_index) Win32API.new('user32', 'GetSystemMetrics', 'I', 'I').call(n_index) end public - + WIN_VERSIONS = { + "Windows 8.1" => {:major => 6, :minor => 3, :callable => lambda{ @product_type == VER_NT_WORKSTATION }}, + "Windows Server 2012 R2" => {:major => 6, :minor => 3, :callable => lambda{ @product_type != VER_NT_WORKSTATION }}, "Windows 8" => {:major => 6, :minor => 2, :callable => lambda{ @product_type == VER_NT_WORKSTATION }}, "Windows Server 2012" => {:major => 6, :minor => 2, :callable => lambda{ @product_type != VER_NT_WORKSTATION }}, "Windows 7" => {:major => 6, :minor => 1, :callable => lambda{ @product_type == VER_NT_WORKSTATION }}, @@ -68,7 +70,7 @@ class Chef # The get_product_info API is not supported on Win2k3, # use an alternative to identify datacenter skus @sku = get_datacenter_product_info_windows_server_2003(ver_info) - end + end end marketing_names = Array.new |