Skip to content

Commit

Permalink
[core] list potentially blocking processes on disk access error
Browse files Browse the repository at this point in the history
* Using functionality from Process Hacker:
  https://github.com/processhacker2/processhacker2/
* Part of the #773 enhancement
* Also fix minor MinGW and WDK warnings
  • Loading branch information
pbatard committed Apr 27, 2017
1 parent 27bc4cb commit d5f6ddb
Show file tree
Hide file tree
Showing 16 changed files with 654 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/.clang/rufus.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<ClCompile Include="..\pki.c" />
<ClCompile Include="..\rufus.c" />
<ClCompile Include="..\checksum.c" />
<ClCompile Include="..\search.c" />
<ClCompile Include="..\smart.c" />
<ClCompile Include="..\stdfn.c" />
<ClCompile Include="..\stdio.c" />
Expand All @@ -225,6 +226,7 @@
<ClInclude Include="..\format.h" />
<ClInclude Include="..\hdd_vs_ufd.h" />
<ClInclude Include="..\missing.h" />
<ClInclude Include="..\search.h" />
<ClInclude Include="..\settings.h" />
<ClInclude Include="..\libcdio\cdio\cdio.h" />
<ClInclude Include="..\libcdio\cdio\iso9660.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/.clang/rufus.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ClCompile Include="..\dev.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\search.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\rufus.h">
Expand Down Expand Up @@ -143,6 +146,9 @@
<ClInclude Include="..\db.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\search.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\res\rufus.ico">
Expand Down
2 changes: 2 additions & 0 deletions src/.msvc/rufus.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<ClCompile Include="..\pki.c" />
<ClCompile Include="..\rufus.c" />
<ClCompile Include="..\checksum.c" />
<ClCompile Include="..\search.c" />
<ClCompile Include="..\smart.c" />
<ClCompile Include="..\stdfn.c" />
<ClCompile Include="..\stdio.c" />
Expand All @@ -208,6 +209,7 @@
<ClInclude Include="..\format.h" />
<ClInclude Include="..\hdd_vs_ufd.h" />
<ClInclude Include="..\missing.h" />
<ClInclude Include="..\search.h" />
<ClInclude Include="..\settings.h" />
<ClInclude Include="..\libcdio\cdio\cdio.h" />
<ClInclude Include="..\libcdio\cdio\iso9660.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/.msvc/rufus.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ClCompile Include="..\dev.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\search.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\rufus.h">
Expand Down Expand Up @@ -143,6 +146,9 @@
<ClInclude Include="..\db.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\search.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\res\rufus.ico">
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ AM_V_WINDRES = $(AM_V_WINDRES_$(V))
$(AM_V_WINDRES) $(AM_RCFLAGS) -i $< -o $@

rufus_SOURCES = badblocks.c checksum.c dev.c dos.c dos_locale.c drive.c format.c icon.c iso.c localization.c \
net.c parser.c pki.c rufus.c smart.c stdfn.c stdio.c stdlg.c syslinux.c vhd.c
net.c parser.c pki.c rufus.c search.c smart.c stdfn.c stdio.c stdlg.c syslinux.c vhd.c
rufus_CFLAGS = -I./ms-sys/inc -I./syslinux/libfat -I./syslinux/libinstaller -I./syslinux/win -I./libcdio $(AM_CFLAGS)
rufus_LDFLAGS = $(AM_LDFLAGS) -mwindows
rufus_LDADD = rufus_rc.o bled/libbled.a ms-sys/libmssys.a syslinux/libfat/libfat.a syslinux/libinstaller/libinstaller.a syslinux/win/libwin.a \
libcdio/iso9660/libiso9660.a libcdio/udf/libudf.a libcdio/driver/libdriver.a -lsetupapi -lole32 -lgdi32 -lwininet -lshlwapi -lcrypt32 -lwintrust -lcomdlg32 -luuid
libcdio/iso9660/libiso9660.a libcdio/udf/libudf.a libcdio/driver/libdriver.a -lsetupapi -lole32 -lgdi32 -lwininet -lshlwapi -lcrypt32 -lwintrust -lcomdlg32 -luuid -lpsapi
18 changes: 12 additions & 6 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ am_rufus_OBJECTS = rufus-badblocks.$(OBJEXT) rufus-checksum.$(OBJEXT) \
rufus-format.$(OBJEXT) rufus-icon.$(OBJEXT) \
rufus-iso.$(OBJEXT) rufus-localization.$(OBJEXT) \
rufus-net.$(OBJEXT) rufus-parser.$(OBJEXT) rufus-pki.$(OBJEXT) \
rufus-rufus.$(OBJEXT) rufus-smart.$(OBJEXT) \
rufus-stdfn.$(OBJEXT) rufus-stdio.$(OBJEXT) \
rufus-stdlg.$(OBJEXT) rufus-syslinux.$(OBJEXT) \
rufus-vhd.$(OBJEXT)
rufus-rufus.$(OBJEXT) rufus-search.$(OBJEXT) \
rufus-smart.$(OBJEXT) rufus-stdfn.$(OBJEXT) \
rufus-stdio.$(OBJEXT) rufus-stdlg.$(OBJEXT) \
rufus-syslinux.$(OBJEXT) rufus-vhd.$(OBJEXT)
rufus_OBJECTS = $(am_rufus_OBJECTS)
rufus_DEPENDENCIES = rufus_rc.o bled/libbled.a ms-sys/libmssys.a \
syslinux/libfat/libfat.a syslinux/libinstaller/libinstaller.a \
Expand Down Expand Up @@ -271,12 +271,12 @@ AM_V_WINDRES_1 = $(WINDRES)
AM_V_WINDRES_ = $(AM_V_WINDRES_$(AM_DEFAULT_VERBOSITY))
AM_V_WINDRES = $(AM_V_WINDRES_$(V))
rufus_SOURCES = badblocks.c checksum.c dev.c dos.c dos_locale.c drive.c format.c icon.c iso.c localization.c \
net.c parser.c pki.c rufus.c smart.c stdfn.c stdio.c stdlg.c syslinux.c vhd.c
net.c parser.c pki.c rufus.c search.c smart.c stdfn.c stdio.c stdlg.c syslinux.c vhd.c

rufus_CFLAGS = -I./ms-sys/inc -I./syslinux/libfat -I./syslinux/libinstaller -I./syslinux/win -I./libcdio $(AM_CFLAGS)
rufus_LDFLAGS = $(AM_LDFLAGS) -mwindows
rufus_LDADD = rufus_rc.o bled/libbled.a ms-sys/libmssys.a syslinux/libfat/libfat.a syslinux/libinstaller/libinstaller.a syslinux/win/libwin.a \
libcdio/iso9660/libiso9660.a libcdio/udf/libudf.a libcdio/driver/libdriver.a -lsetupapi -lole32 -lgdi32 -lwininet -lshlwapi -lcrypt32 -lwintrust -lcomdlg32 -luuid
libcdio/iso9660/libiso9660.a libcdio/udf/libudf.a libcdio/driver/libdriver.a -lsetupapi -lole32 -lgdi32 -lwininet -lshlwapi -lcrypt32 -lwintrust -lcomdlg32 -luuid -lpsapi

all: all-recursive

Expand Down Expand Up @@ -416,6 +416,12 @@ rufus-rufus.o: rufus.c
rufus-rufus.obj: rufus.c
$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(rufus_CFLAGS) $(CFLAGS) -c -o rufus-rufus.obj `if test -f 'rufus.c'; then $(CYGPATH_W) 'rufus.c'; else $(CYGPATH_W) '$(srcdir)/rufus.c'; fi`

rufus-search.o: search.c
$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(rufus_CFLAGS) $(CFLAGS) -c -o rufus-search.o `test -f 'search.c' || echo '$(srcdir)/'`search.c

rufus-search.obj: search.c
$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(rufus_CFLAGS) $(CFLAGS) -c -o rufus-search.obj `if test -f 'search.c'; then $(CYGPATH_W) 'search.c'; else $(CYGPATH_W) '$(srcdir)/search.c'; fi`

rufus-smart.o: smart.c
$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(rufus_CFLAGS) $(CFLAGS) -c -o rufus-smart.o `test -f 'smart.c' || echo '$(srcdir)/'`smart.c

Expand Down
11 changes: 7 additions & 4 deletions src/drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr
if ((safe_strlen(Path) < 5) || (Path[0] != '\\') || (Path[1] != '\\') || (Path[3] != '\\'))
goto out;

// Resolve a device path, so that users can seek for it in Process Explorer
// in case of access issues.
// Resolve a device path, so that we can look for that handle in case of access issues.
if (QueryDosDeviceA(&Path[4], DevPath, sizeof(DevPath)) == 0)
strcpy(DevPath, "???");

Expand All @@ -151,6 +150,7 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr
// If we can't seem to get a hold of the drive for some time,
// try to enable FILE_SHARE_WRITE...
uprintf("Warning: Could not obtain exclusive rights. Retrying with write sharing enabled...");
SearchProcess(DevPath, TRUE, TRUE);
bWriteShare = TRUE;
}
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
Expand All @@ -161,7 +161,7 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr
}

if (bWriteAccess) {
uprintf("Opened %s [%s] for write access\n", Path, DevPath);
uprintf("Opened %s [%s] for write access", Path, DevPath);
}

if (bLockDrive) {
Expand All @@ -177,7 +177,10 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr
Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES);
}
// If we reached this section, either we didn't manage to get a lock or the user cancelled
uprintf("Could not get exclusive access to %s [%s]: %s\n", Path, DevPath, WindowsErrorString());
uprintf("Could not get exclusive access to %s: %s", Path, WindowsErrorString());
// See if we can tell the user what processes are accessing the drive
if (!IS_ERROR(FormatStatus))
SearchProcess(DevPath, TRUE, TRUE);
safe_closehandle(hDrive);
}

Expand Down
4 changes: 4 additions & 0 deletions src/license.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const char* additional_copyrights =
"http://www.codeguru.com/forum/showthread.php?p=1951973\\line\n"
"Public Domain\\line\n"
"\\line\n"
"Handle search & process enumeration from Process Hacker by wj32 & dmex:\\line\n"
"http://processhacker.sourceforge.net/\\line\n"
"GNU General Public License (GPL) v3 or later\\line\n"
"\\line\n"
"Decompression support from BusyBox/Bled:\\line\n"
"http://www.busybox.net/\\line\n"
"https://github.com/pbatard/bled\\line\n"
Expand Down
46 changes: 46 additions & 0 deletions src/msapi_utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <share.h>
#include <fcntl.h>
#include <io.h>
#if !defined(DDKBUILD)
#include <psapi.h>
#endif

#pragma once
#if defined(_MSC_VER)
Expand Down Expand Up @@ -125,6 +128,31 @@ static __inline wchar_t* utf8_to_wchar(const char* str)
return wstr;
}

/*
* Converts an non NUL-terminated UTF-16 string of length len to UTF8 (allocate returned string)
* Returns NULL on error
*/
static __inline char* wchar_len_to_utf8(const wchar_t* wstr, int wlen)
{
int size = 0;
char* str = NULL;

// Find out the size we need to allocate for our converted string
size = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, NULL, 0, NULL, NULL);
if (size <= 1) // An empty string would be size 1
return NULL;

if ((str = (char*)calloc(size, 1)) == NULL)
return NULL;

if (WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, str, size, NULL, NULL) != size) {
sfree(str);
return NULL;
}

return str;
}

static __inline DWORD FormatMessageU(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId,
DWORD dwLanguageId, char* lpBuffer, DWORD nSize, va_list *Arguments)
{
Expand Down Expand Up @@ -554,6 +582,24 @@ static __inline DWORD GetModuleFileNameU(HMODULE hModule, char* lpFilename, DWOR
return ret;
}

#if !defined(DDKBUILD)
static __inline DWORD GetModuleFileNameExU(HANDLE hProcess, HMODULE hModule, char* lpFilename, DWORD nSize)
{
DWORD ret = 0, err = ERROR_INVALID_DATA;
// coverity[returned_null]
walloc(lpFilename, nSize);
ret = GetModuleFileNameExW(hProcess, hModule, wlpFilename, nSize);
err = GetLastError();
if ((ret != 0)
&& ((ret = wchar_to_utf8_no_alloc(wlpFilename, lpFilename, nSize)) == 0)) {
err = GetLastError();
}
wfree(lpFilename);
SetLastError(err);
return ret;
}
#endif

static __inline DWORD GetFullPathNameU(const char* lpFileName, DWORD nBufferLength, char* lpBuffer, char** lpFilePart)
{
DWORD ret = 0, err = ERROR_INVALID_DATA;
Expand Down
8 changes: 8 additions & 0 deletions src/rufus.c
Original file line number Diff line number Diff line change
Expand Up @@ -3469,3 +3469,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

return 0;
}

// The old WDK is showing its age and becoming a pain to support
#if defined(DDKBUILD)
BOOL SearchProcess(char* HandleName, BOOL bPartialMatch, BOOL bIgnoreSelf) {
uprintf("NOTE: Process search is not implemented on this platform");
return FALSE;
}
#endif
1 change: 1 addition & 0 deletions src/rufus.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ extern BOOL IsCurrentProcessElevated(void);
extern char* GetCurrentMUI(void);
extern BOOL SetFormatPromptHook(void);
extern void ClrFormatPromptHook(void);
extern BOOL SearchProcess(char* HandleName, BOOL bPartialMatch, BOOL bIgnoreSelf);

DWORD WINAPI FormatThread(void* param);
DWORD WINAPI SaveImageThread(void* param);
Expand Down
10 changes: 5 additions & 5 deletions src/rufus.rc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 2.15.1094"
CAPTION "Rufus 2.15.1095"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
Expand Down Expand Up @@ -334,8 +334,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,15,1094,0
PRODUCTVERSION 2,15,1094,0
FILEVERSION 2,15,1095,0
PRODUCTVERSION 2,15,1095,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -352,13 +352,13 @@ BEGIN
BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.15.1094"
VALUE "FileVersion", "2.15.1095"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "� 2011-2017 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.15.1094"
VALUE "ProductVersion", "2.15.1095"
END
END
BLOCK "VarFileInfo"
Expand Down
Loading

0 comments on commit d5f6ddb

Please sign in to comment.