-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwithlockfile.cpp
230 lines (200 loc) · 7.71 KB
/
withlockfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* withlockfile - a program for synchronising command execution
* Copyright 2014-2017 froglogic GmbH
*
* This file is part of withlockfile.
*
* withlockfile is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* withlockfile is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with withlockfile. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stdexcept>
#include <windows.h>
#include <shlwapi.h>
class Win32Error : public std::runtime_error
{
public:
Win32Error( const char *what, DWORD errorCode_ )
: std::runtime_error( what )
, errorCode( errorCode_ )
{
}
const DWORD errorCode;
};
static std::string enforceExeExtension( const std::string &s )
{
std::string exe = s;
const std::string::size_type len = exe.size();
if ( len < 4 ||
_stricmp( exe.substr( len - 4 ).c_str(), ".exe" ) != 0 ) {
exe += ".exe";
}
return exe;
}
static std::string quoteArgument( std::string arg )
{
const std::string::size_type space = arg.find_first_of( " \t" );
if ( space != std::string::npos ) {
std::string s = "\"";
s += arg;
s += '"';
return s;
}
return arg;
}
int main( int argc, char **argv )
{
try {
if ( argc < 3 ) {
std::cerr << "usage: withlockfile <lockfile> <command> [args..]\n";
return 1;
}
HANDLE f = ::CreateFileA( argv[1],
GENERIC_READ, /* required by LockFileEx */
FILE_SHARE_READ, /* allow concurrent opening */
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_READONLY,
NULL );
if ( f == INVALID_HANDLE_VALUE ) {
throw Win32Error( "CreateFileA", ::GetLastError() );
}
OVERLAPPED ol;
::ZeroMemory( &ol, sizeof( ol ) );
bool lockAcquired = false;
for ( int i = 0; i < 300; ++i ) {
if ( ::LockFileEx( f,
LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
0,
1,
0,
&ol ) ) {
lockAcquired = true;
break;
}
const DWORD lastError = ::GetLastError();
if ( lastError == ERROR_LOCK_VIOLATION ) {
::Sleep( 1000 );
continue;
} else {
throw Win32Error( "LockFileEx", lastError );
}
}
if ( !lockAcquired ) {
throw Win32Error( "LockFileEx", ERROR_LOCK_VIOLATION );
}
std::string executable = enforceExeExtension( argv[2] );
{
// According to a comment on the PathSearchAndQualify function
// at http://msdn.microsoft.com/en-us/library/bb773751(VS.85).aspx
// the buffer must be at least MAX_PATH in size.
char buf[MAX_PATH] = { 0 };
if ( !::PathSearchAndQualifyA( executable.c_str(),
buf, sizeof( buf ) ) ) {
throw Win32Error( "PathSearchAndQualifyA", ::GetLastError() );
}
executable = buf;
}
std::string commandLine = quoteArgument( executable );
{
for ( int i = 3; i < argc; ++i ) {
commandLine += ' ';
commandLine += quoteArgument( argv[i] );
}
}
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { sizeof( si ) };
si.hStdError = ::GetStdHandle( STD_ERROR_HANDLE );
si.hStdOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
si.hStdInput = ::GetStdHandle( STD_INPUT_HANDLE );
if ( ::CreateProcessA( executable.c_str(),
const_cast<char *>( commandLine.c_str() ),
NULL,
NULL,
TRUE,
CREATE_SUSPENDED,
NULL,
NULL,
&si,
&pi ) == FALSE ) {
throw Win32Error( "CreateProcessA", ::GetLastError() );
}
HANDLE jobObject = ::CreateJobObject( NULL, NULL );
if ( !jobObject ) {
throw Win32Error( "CreateJobObject", ::GetLastError() );
}
{
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobObjectInfo = { 0 };
jobObjectInfo.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if ( !::SetInformationJobObject( jobObject,
JobObjectExtendedLimitInformation,
&jobObjectInfo,
sizeof( jobObjectInfo ) ) ) {
::CloseHandle( jobObject );
throw Win32Error( "SetInformationJobObject", ::GetLastError() );
}
}
/* Don't bother reporting access denied with AssignProcessToJobObject
* because it's quite common for this to happen on Windows 7 and
* earlier if withlockfile is already part of a job object.
*/
if ( !::AssignProcessToJobObject( jobObject, pi.hProcess ) &&
::GetLastError() != ERROR_ACCESS_DENIED ) {
throw Win32Error( "AssignProcessToJobObject", ::GetLastError() );
}
if ( ::ResumeThread( pi.hThread ) == -1 ) {
throw Win32Error( "ResumeThread", ::GetLastError() );
}
if ( ::WaitForSingleObject( pi.hProcess, INFINITE ) == WAIT_FAILED ) {
throw Win32Error( "WaitForSingleObject", ::GetLastError() );
}
DWORD exitCode;
if ( ::GetExitCodeProcess( pi.hProcess, &exitCode ) == FALSE ) {
throw Win32Error( "GetExitCodeProcess", ::GetLastError() );
}
if ( ::UnlockFileEx( f, 0, 1, 0, &ol ) == FALSE ) {
throw Win32Error( "UnlockFileEx", ::GetLastError() );
}
if ( ::CloseHandle( f ) == FALSE ) {
throw Win32Error( "CloseHandle", ::GetLastError() );
}
return 0;
} catch ( const Win32Error &e ) {
/* The MSDN documentation for FormatMessage says that the buf cannot
* be larger than 64K bytes.
*/
char buf[65536 / sizeof(wchar_t)] = { L'0' };
const DWORD result = ::FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
e.errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf,
sizeof( buf ),
NULL );
/* It seems that many error messages end in a newline, but I don't want
* them, so strip them.
*/
const size_t len = strlen( buf );
if ( len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n' ) {
buf[len - 2] = '\0';
}
std::cerr << "error: " << e.what() << " failed: "
<< buf << " (code " << e.errorCode << ")"
<< std::endl;
return e.errorCode;
} catch ( const std::exception &e ) {
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
}