Skip to content

Commit 57a5c2f

Browse files
committed
support non-blocking attach
The prior attach command was blocked not showing a command prompt. This behavior used to lead a user to misunderstand that attach command does not work. So, a new attacher.exe is introduced to execute a forward routine in a separate process. As a side benefit, attach command is able to report an assigned port. A new attach command has '-t' option, which displays a port number only. It would be helpful to write an external script or program to utilize usbip.exe. related issues: #202, #186
1 parent e2196f7 commit 57a5c2f

File tree

5 files changed

+368
-14
lines changed

5 files changed

+368
-14
lines changed

usbip_win.sln

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libdrv", "driver\lib\libdrv
1717
EndProject
1818
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "usbip_vhci_ude", "driver\vhci_ude\usbip_vhci_ude.vcxproj", "{27D5F152-C29D-4C5C-A65C-78ABEE57BAEA}"
1919
EndProject
20+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "attacher", "userspace\src\attacher\attacher.vcxproj", "{C059278A-86F7-4263-8B71-9BF15949A994}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|x64 = Debug|x64
@@ -97,6 +99,14 @@ Global
9799
{27D5F152-C29D-4C5C-A65C-78ABEE57BAEA}.Release|x86.ActiveCfg = Release|Win32
98100
{27D5F152-C29D-4C5C-A65C-78ABEE57BAEA}.Release|x86.Build.0 = Release|Win32
99101
{27D5F152-C29D-4C5C-A65C-78ABEE57BAEA}.Release|x86.Deploy.0 = Release|Win32
102+
{C059278A-86F7-4263-8B71-9BF15949A994}.Debug|x64.ActiveCfg = Debug|x64
103+
{C059278A-86F7-4263-8B71-9BF15949A994}.Debug|x64.Build.0 = Debug|x64
104+
{C059278A-86F7-4263-8B71-9BF15949A994}.Debug|x86.ActiveCfg = Debug|Win32
105+
{C059278A-86F7-4263-8B71-9BF15949A994}.Debug|x86.Build.0 = Debug|Win32
106+
{C059278A-86F7-4263-8B71-9BF15949A994}.Release|x64.ActiveCfg = Release|x64
107+
{C059278A-86F7-4263-8B71-9BF15949A994}.Release|x64.Build.0 = Release|x64
108+
{C059278A-86F7-4263-8B71-9BF15949A994}.Release|x86.ActiveCfg = Release|Win32
109+
{C059278A-86F7-4263-8B71-9BF15949A994}.Release|x86.Build.0 = Release|Win32
100110
EndGlobalSection
101111
GlobalSection(SolutionProperties) = preSolution
102112
HideSolutionNode = FALSE

userspace/src/attacher/attacher.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
#include <windows.h>
3+
4+
#include "usbip_forward.h"
5+
6+
static HANDLE
7+
read_handle_value(HANDLE hStdin)
8+
{
9+
HANDLE handle;
10+
LPBYTE buf = (LPBYTE)&handle;
11+
DWORD buflen = sizeof(HANDLE);
12+
13+
while (buflen > 0) {
14+
DWORD nread;
15+
16+
if (!ReadFile(hStdin, buf + sizeof(HANDLE) - buflen, buflen, &nread, NULL)) {
17+
return INVALID_HANDLE_VALUE;
18+
}
19+
if (nread == 0)
20+
return INVALID_HANDLE_VALUE;
21+
buflen -= nread;
22+
}
23+
return handle;
24+
}
25+
26+
static BOOL
27+
setup_forwarder(void)
28+
{
29+
HANDLE hdev, sockfd;
30+
HANDLE hStdin, hStdout;
31+
32+
hStdin = GetStdHandle(STD_INPUT_HANDLE);
33+
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
34+
35+
hdev = read_handle_value(hStdin);
36+
sockfd = read_handle_value(hStdin);
37+
38+
usbip_forward(hdev, sockfd, FALSE);
39+
40+
CloseHandle(hStdin);
41+
CloseHandle(hStdout);
42+
43+
return TRUE;
44+
}
45+
46+
int APIENTRY
47+
wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
48+
_In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
49+
{
50+
UNREFERENCED_PARAMETER(hPrevInstance);
51+
UNREFERENCED_PARAMETER(lpCmdLine);
52+
53+
if (!setup_forwarder())
54+
return 1;
55+
56+
return 0;
57+
}

userspace/src/attacher/attacher.rc

1.5 KB
Binary file not shown.
+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<ProjectGuid>{C059278A-86F7-4263-8B71-9BF15949A994}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>forwarder</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
<ProjectName>attacher</ProjectName>
28+
</PropertyGroup>
29+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
30+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
31+
<ConfigurationType>Application</ConfigurationType>
32+
<UseDebugLibraries>true</UseDebugLibraries>
33+
<PlatformToolset>v142</PlatformToolset>
34+
<CharacterSet>NotSet</CharacterSet>
35+
<SpectreMitigation>false</SpectreMitigation>
36+
</PropertyGroup>
37+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
38+
<ConfigurationType>Application</ConfigurationType>
39+
<UseDebugLibraries>false</UseDebugLibraries>
40+
<PlatformToolset>v142</PlatformToolset>
41+
<WholeProgramOptimization>true</WholeProgramOptimization>
42+
<CharacterSet>NotSet</CharacterSet>
43+
<SpectreMitigation>false</SpectreMitigation>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
46+
<ConfigurationType>Application</ConfigurationType>
47+
<UseDebugLibraries>true</UseDebugLibraries>
48+
<PlatformToolset>v142</PlatformToolset>
49+
<CharacterSet>NotSet</CharacterSet>
50+
<SpectreMitigation>false</SpectreMitigation>
51+
</PropertyGroup>
52+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
53+
<ConfigurationType>Application</ConfigurationType>
54+
<UseDebugLibraries>false</UseDebugLibraries>
55+
<PlatformToolset>v142</PlatformToolset>
56+
<WholeProgramOptimization>true</WholeProgramOptimization>
57+
<CharacterSet>NotSet</CharacterSet>
58+
<SpectreMitigation>false</SpectreMitigation>
59+
</PropertyGroup>
60+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
61+
<ImportGroup Label="ExtensionSettings">
62+
</ImportGroup>
63+
<ImportGroup Label="Shared">
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
69+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70+
</ImportGroup>
71+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
72+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
73+
</ImportGroup>
74+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
75+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
76+
</ImportGroup>
77+
<PropertyGroup Label="UserMacros" />
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
79+
<LinkIncremental>true</LinkIncremental>
80+
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\</OutDir>
81+
<IntDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</IntDir>
82+
</PropertyGroup>
83+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
84+
<LinkIncremental>true</LinkIncremental>
85+
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\</OutDir>
86+
<IntDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</IntDir>
87+
</PropertyGroup>
88+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
89+
<LinkIncremental>false</LinkIncremental>
90+
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\</OutDir>
91+
<IntDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</IntDir>
92+
</PropertyGroup>
93+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
94+
<LinkIncremental>false</LinkIncremental>
95+
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\</OutDir>
96+
<IntDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</IntDir>
97+
</PropertyGroup>
98+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
99+
<ClCompile>
100+
<PrecompiledHeader>
101+
</PrecompiledHeader>
102+
<WarningLevel>Level3</WarningLevel>
103+
<Optimization>Disabled</Optimization>
104+
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105+
<AdditionalIncludeDirectories>..\..\..\include; ..\..\lib; windows</AdditionalIncludeDirectories>
106+
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
107+
</ClCompile>
108+
<Link>
109+
<SubSystem>Windows</SubSystem>
110+
<GenerateDebugInformation>true</GenerateDebugInformation>
111+
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
112+
</Link>
113+
</ItemDefinitionGroup>
114+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
115+
<ClCompile>
116+
<PrecompiledHeader>
117+
</PrecompiledHeader>
118+
<WarningLevel>Level3</WarningLevel>
119+
<Optimization>Disabled</Optimization>
120+
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121+
<AdditionalIncludeDirectories>..\..\..\include; ..\..\lib; windows</AdditionalIncludeDirectories>
122+
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
123+
</ClCompile>
124+
<Link>
125+
<SubSystem>Windows</SubSystem>
126+
<GenerateDebugInformation>true</GenerateDebugInformation>
127+
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
128+
</Link>
129+
</ItemDefinitionGroup>
130+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
131+
<ClCompile>
132+
<PrecompiledHeader>
133+
</PrecompiledHeader>
134+
<WarningLevel>Level3</WarningLevel>
135+
<Optimization>MaxSpeed</Optimization>
136+
<FunctionLevelLinking>true</FunctionLevelLinking>
137+
<IntrinsicFunctions>true</IntrinsicFunctions>
138+
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
139+
<AdditionalIncludeDirectories>..\..\..\include; ..\..\lib; windows</AdditionalIncludeDirectories>
140+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
141+
</ClCompile>
142+
<Link>
143+
<SubSystem>Windows</SubSystem>
144+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
145+
<OptimizeReferences>true</OptimizeReferences>
146+
<GenerateDebugInformation>true</GenerateDebugInformation>
147+
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
148+
</Link>
149+
</ItemDefinitionGroup>
150+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
151+
<ClCompile>
152+
<PrecompiledHeader>
153+
</PrecompiledHeader>
154+
<WarningLevel>Level3</WarningLevel>
155+
<Optimization>MaxSpeed</Optimization>
156+
<FunctionLevelLinking>true</FunctionLevelLinking>
157+
<IntrinsicFunctions>true</IntrinsicFunctions>
158+
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
159+
<AdditionalIncludeDirectories>..\..\..\include; ..\..\lib; windows</AdditionalIncludeDirectories>
160+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
161+
</ClCompile>
162+
<Link>
163+
<SubSystem>Windows</SubSystem>
164+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
165+
<OptimizeReferences>true</OptimizeReferences>
166+
<GenerateDebugInformation>true</GenerateDebugInformation>
167+
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
168+
</Link>
169+
</ItemDefinitionGroup>
170+
<ItemGroup>
171+
<ClInclude Include="Resource.h" />
172+
</ItemGroup>
173+
<ItemGroup>
174+
<ClCompile Include="attacher.c" />
175+
</ItemGroup>
176+
<ItemGroup>
177+
<ResourceCompile Include="attacher.rc" />
178+
</ItemGroup>
179+
<ItemGroup>
180+
<ProjectReference Include="..\..\lib\usbip_common.vcxproj">
181+
<Project>{2c173853-88c0-4334-85bf-0b46cfd5a007}</Project>
182+
</ProjectReference>
183+
</ItemGroup>
184+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
185+
<ImportGroup Label="ExtensionTargets">
186+
</ImportGroup>
187+
</Project>

0 commit comments

Comments
 (0)