Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-cpython/PCbuild/kill_python.c at 3.1 · sthagen/python-cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sthagen
/
python-cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
9
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Security and quality
Insights
Files
Expand file tree
3.1
Breadcrumbs
python-cpython
/
PCbuild
/
kill_python.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
178 lines (143 loc) · 5.54 KB
3.1
Breadcrumbs
python-cpython
/
PCbuild
/
kill_python.c
Copy path
Top
File metadata and controls
Code
Blame
178 lines (143 loc) · 5.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Helper program for killing lingering python[_d].exe processes before
* building, thus attempting to avoid build failures due to files being
* locked.
*/
#include
<windows.h>
#include
<wchar.h>
#include
<tlhelp32.h>
#include
<stdio.h>
#pragma
comment(lib, "psapi")
#ifdef
_DEBUG
#define
PYTHON_EXE
(L"python_d.exe")
#define
PYTHON_EXE_LEN
(12)
#define
KILL_PYTHON_EXE
(L"kill_python_d.exe")
#define
KILL_PYTHON_EXE_LEN
(17)
#else
#define
PYTHON_EXE
(L"python.exe")
#define
PYTHON_EXE_LEN
(10)
#define
KILL_PYTHON_EXE
(L"kill_python.exe")
#define
KILL_PYTHON_EXE_LEN
(15)
#endif
int
main
(
int
argc
,
char
*
*
argv
)
{
HANDLE
hp
,
hsp
,
hsm
;
/* process, snapshot processes, snapshot modules */
DWORD
dac
,
our_pid
;
size_t
len
;
wchar_t
path
[
MAX_PATH
+
1
];
MODULEENTRY32W
me
;
PROCESSENTRY32W
pe
;
me
.
dwSize
=
sizeof
(
MODULEENTRY32W
);
pe
.
dwSize
=
sizeof
(
PROCESSENTRY32W
);
memset
(
path
,
0
,
MAX_PATH
+
1
);
our_pid
=
GetCurrentProcessId
();
hsm
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPMODULE
,
our_pid
);
if
(
hsm
==
INVALID_HANDLE_VALUE
) {
printf
(
"CreateToolhelp32Snapshot[1] failed: %d\n"
,
GetLastError
());
return
1
;
}
if
(!
Module32FirstW
(
hsm
,
&
me
)) {
printf
(
"Module32FirstW[1] failed: %d\n"
,
GetLastError
());
CloseHandle
(
hsm
);
return
1
;
}
/*
* Enumerate over the modules for the current process in order to find
* kill_process[_d].exe, then take a note of the directory it lives in.
*/
do
{
if
(
_wcsnicmp
(
me
.
szModule
,
KILL_PYTHON_EXE
,
KILL_PYTHON_EXE_LEN
))
continue
;
len
=
wcsnlen_s
(
me
.
szExePath
,
MAX_PATH
)
-
KILL_PYTHON_EXE_LEN
;
wcsncpy_s
(
path
,
MAX_PATH
+
1
,
me
.
szExePath
,
len
);
break
;
}
while
(
Module32NextW
(
hsm
,
&
me
));
CloseHandle
(
hsm
);
if
(
path
==
NULL
) {
printf
(
"failed to discern directory of running process\n"
);
return
1
;
}
/*
* Take a snapshot of system processes. Enumerate over the snapshot,
* looking for python processes. When we find one, verify it lives
* in the same directory we live in. If it does, kill it. If we're
* unable to kill it, treat this as a fatal error and return 1.
*
* The rationale behind this is that we're called at the start of the
* build process on the basis that we'll take care of killing any
* running instances, such that the build won't encounter permission
* denied errors during linking. If we can't kill one of the processes,
* we can't provide this assurance, and the build shouldn't start.
*/
hsp
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPPROCESS
,
0
);
if
(
hsp
==
INVALID_HANDLE_VALUE
) {
printf
(
"CreateToolhelp32Snapshot[2] failed: %d\n"
,
GetLastError
());
return
1
;
}
if
(!
Process32FirstW
(
hsp
,
&
pe
)) {
printf
(
"Process32FirstW failed: %d\n"
,
GetLastError
());
CloseHandle
(
hsp
);
return
1
;
}
dac
=
PROCESS_QUERY_INFORMATION
|
PROCESS_VM_READ
|
PROCESS_TERMINATE
;
do
{
/*
* XXX TODO: if we really wanted to be fancy, we could check the
* modules for all processes (not just the python[_d].exe ones)
* and see if any of our DLLs are loaded (i.e. python31[_d].dll),
* as that would also inhibit our ability to rebuild the solution.
* Not worth loosing sleep over though; for now, a simple check
* for just the python executable should be sufficient.
*/
if
(
_wcsnicmp
(
pe
.
szExeFile
,
PYTHON_EXE
,
PYTHON_EXE_LEN
))
/* This isn't a python process. */
continue
;
/* It's a python process, so figure out which directory it's in... */
hsm
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPMODULE
,
pe
.
th32ProcessID
);
if
(
hsm
==
INVALID_HANDLE_VALUE
)
/*
* If our module snapshot fails (which will happen if we don't own
* the process), just ignore it and continue. (It seems different
* versions of Windows return different values for GetLastError()
* in this situation; it's easier to just ignore it and move on vs.
* stopping the build for what could be a false positive.)
*/
continue
;
if
(!
Module32FirstW
(
hsm
,
&
me
)) {
printf
(
"Module32FirstW[2] failed: %d\n"
,
GetLastError
());
CloseHandle
(
hsp
);
CloseHandle
(
hsm
);
return
1
;
}
do
{
if
(
_wcsnicmp
(
me
.
szModule
,
PYTHON_EXE
,
PYTHON_EXE_LEN
))
/* Wrong module, we're looking for python[_d].exe... */
continue
;
if
(
_wcsnicmp
(
path
,
me
.
szExePath
,
len
))
/* Process doesn't live in our directory. */
break
;
/* Python process residing in the right directory, kill it! */
hp
=
OpenProcess
(
dac
, FALSE,
pe
.
th32ProcessID
);
if
(!
hp
) {
printf
(
"OpenProcess failed: %d\n"
,
GetLastError
());
CloseHandle
(
hsp
);
CloseHandle
(
hsm
);
return
1
;
}
if
(!
TerminateProcess
(
hp
,
1
)) {
printf
(
"TerminateProcess failed: %d\n"
,
GetLastError
());
CloseHandle
(
hsp
);
CloseHandle
(
hsm
);
CloseHandle
(
hp
);
return
1
;
}
CloseHandle
(
hp
);
break
;
}
while
(
Module32NextW
(
hsm
,
&
me
));
CloseHandle
(
hsm
);
}
while
(
Process32NextW
(
hsp
,
&
pe
));
CloseHandle
(
hsp
);
return
0
;
}
/* vi: set ts=8 sw=4 sts=4 expandtab */
You can’t perform that action at this time.