Fix rehash function

Use SendMessageTimeout instead of SendMessage which was causing the
system to hang in some scenarios
Use ctypes to access the dll directly instead of using pywin32 as this
seems to actually work
This commit is contained in:
twangboy 2018-01-29 12:57:10 -07:00
parent 9d14ad9ccf
commit 967b83940c
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB

View file

@ -12,12 +12,12 @@ from __future__ import absolute_import
import logging
import re
import os
import ctypes
from salt.ext.six.moves import map
# Third party libs
try:
from win32con import HWND_BROADCAST, WM_SETTINGCHANGE
from win32api import SendMessage
from win32con import HWND_BROADCAST, WM_SETTINGCHANGE, SMTO_ABORTIFHUNG
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
@ -47,7 +47,15 @@ def _normalize_dir(string):
def rehash():
'''
Send a WM_SETTINGCHANGE Broadcast to Windows to refresh the Environment variables
Send a WM_SETTINGCHANGE Broadcast to Windows to refresh the Environment
variables for new processes.
.. note::
This will only affect new processes that aren't launched by services. To
apply changes to the path to services, the host must be restarted. The
``salt-minion``, if running as a service, will not see changes to the
environment until the system is restarted. See
`MSDN Documentation <https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services>`
CLI Example:
@ -55,7 +63,12 @@ def rehash():
salt '*' win_path.rehash
'''
return bool(SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment'))
broadcast_message = ctypes.create_unicode_buffer('Environment')
user32 = ctypes.windll('user32', use_last_error=True)
result = user32.SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
broadcast_message, SMTO_ABORTIFHUNG,
5000, 0)
return result == 1
def get_path():