summaryrefslogtreecommitdiffstats
path: root/Modules/_multiprocessing/win32_functions.c
blob: 379a49515e37e02d5e14957d9454165f2a9c8283 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Win32 functions used by multiprocessing package
 *
 * win32_functions.c
 *
 * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
 */

#include "multiprocessing.h"


#define WIN32_FUNCTION(func) \
    {#func, (PyCFunction)win32_ ## func, METH_VARARGS | METH_STATIC, ""}

#define WIN32_CONSTANT(fmt, con) \
    PyDict_SetItemString(Win32Type.tp_dict, #con, Py_BuildValue(fmt, con))


static PyObject *
win32_CloseHandle(PyObject *self, PyObject *args)
{
	HANDLE hObject;
	BOOL success;

	if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	success = CloseHandle(hObject); 
	Py_END_ALLOW_THREADS

	if (!success)
		return PyErr_SetFromWindowsErr(0);

	Py_RETURN_NONE;
}

static PyObject *
win32_ConnectNamedPipe(PyObject *self, PyObject *args)
{
	HANDLE hNamedPipe;
	LPOVERLAPPED lpOverlapped;
	BOOL success;

	if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER, 
			      &hNamedPipe, &lpOverlapped))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	success = ConnectNamedPipe(hNamedPipe, lpOverlapped);
	Py_END_ALLOW_THREADS

	if (!success)
		return PyErr_SetFromWindowsErr(0);

	Py_RETURN_NONE;
}

static PyObject *
win32_CreateFile(PyObject *self, PyObject *args)
{
	LPCTSTR lpFileName;
	DWORD dwDesiredAccess;
	DWORD dwShareMode;
	LPSECURITY_ATTRIBUTES lpSecurityAttributes;
	DWORD dwCreationDisposition;
	DWORD dwFlagsAndAttributes;
	HANDLE hTemplateFile;
	HANDLE handle;

	if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER 
			      F_DWORD F_DWORD F_HANDLE,
			      &lpFileName, &dwDesiredAccess, &dwShareMode, 
			      &lpSecurityAttributes, &dwCreationDisposition, 
			      &dwFlagsAndAttributes, &hTemplateFile))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	handle = CreateFile(lpFileName, dwDesiredAccess, 
			    dwShareMode, lpSecurityAttributes, 
			    dwCreationDisposition, 
			    dwFlagsAndAttributes, hTemplateFile);
	Py_END_ALLOW_THREADS

	if (handle == INVALID_HANDLE_VALUE)
		return PyErr_SetFromWindowsErr(0);

	return Py_BuildValue(F_HANDLE, handle);
}

static PyObject *
win32_CreateNamedPipe(PyObject *self, PyObject *args)
{
	LPCTSTR lpName;
	DWORD dwOpenMode;
	DWORD dwPipeMode;
	DWORD nMaxInstances;
	DWORD nOutBufferSize;
	DWORD nInBufferSize;
	DWORD nDefaultTimeOut;
	LPSECURITY_ATTRIBUTES lpSecurityAttributes;
	HANDLE handle;

	if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD 
			      F_DWORD F_DWORD F_DWORD F_POINTER,
			      &lpName, &dwOpenMode, &dwPipeMode, 
			      &nMaxInstances, &nOutBufferSize, 
			      &nInBufferSize, &nDefaultTimeOut,
			      &lpSecurityAttributes))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, 
				 nMaxInstances, nOutBufferSize, 
				 nInBufferSize, nDefaultTimeOut,
				 lpSecurityAttributes);
	Py_END_ALLOW_THREADS

	if (handle == INVALID_HANDLE_VALUE)
		return PyErr_SetFromWindowsErr(0);

	return Py_BuildValue(F_HANDLE, handle);
}

static PyObject *
win32_ExitProcess(PyObject *self, PyObject *args)
{
	UINT uExitCode;

	if (!PyArg_ParseTuple(args, "I", &uExitCode))
		return NULL;

	ExitProcess(uExitCode);

	return NULL;
}

static PyObject *
win32_GetLastError(PyObject *self, PyObject *args)
{
	return Py_BuildValue(F_DWORD, GetLastError());
}

static PyObject *
win32_OpenProcess(PyObject *self, PyObject *args)
{
	DWORD dwDesiredAccess;
	BOOL bInheritHandle;
	DWORD dwProcessId;
	HANDLE handle;

	if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD, 
			      &dwDesiredAccess, &bInheritHandle, &dwProcessId))
		return NULL;

	handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);    
	if (handle == NULL)
		return PyErr_SetFromWindowsErr(0);

	return Py_BuildValue(F_HANDLE, handle);
}

static PyObject *
win32_SetNamedPipeHandleState(PyObject *self, PyObject *args)
{
	HANDLE hNamedPipe;
	PyObject *oArgs[3];
	DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
	int i;

	if (!PyArg_ParseTuple(args, F_HANDLE "OOO", 
			      &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
		return NULL;

	PyErr_Clear();

	for (i = 0 ; i < 3 ; i++) {
		if (oArgs[i] != Py_None) {
			dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
			if (PyErr_Occurred())
				return NULL;
			pArgs[i] = &dwArgs[i];
		}
	}

	if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
		return PyErr_SetFromWindowsErr(0);

	Py_RETURN_NONE;
}

static PyObject *
win32_WaitNamedPipe(PyObject *self, PyObject *args)
{
	LPCTSTR lpNamedPipeName;
	DWORD nTimeOut;
	BOOL success;

	if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
	Py_END_ALLOW_THREADS

	if (!success)
		return PyErr_SetFromWindowsErr(0);

	Py_RETURN_NONE;
}

static PyMethodDef win32_methods[] = {
	WIN32_FUNCTION(CloseHandle),
	WIN32_FUNCTION(GetLastError),
	WIN32_FUNCTION(OpenProcess),
	WIN32_FUNCTION(ExitProcess),
	WIN32_FUNCTION(ConnectNamedPipe),
	WIN32_FUNCTION(CreateFile),
	WIN32_FUNCTION(CreateNamedPipe),
	WIN32_FUNCTION(SetNamedPipeHandleState),
	WIN32_FUNCTION(WaitNamedPipe),
	{NULL}
};


PyTypeObject Win32Type = {
	PyVarObject_HEAD_INIT(NULL, 0)
};


PyObject *
create_win32_namespace(void)
{
	Win32Type.tp_name = "_multiprocessing.win32";
	Win32Type.tp_methods = win32_methods;
	if (PyType_Ready(&Win32Type) < 0)
		return NULL;
	Py_INCREF(&Win32Type);

	WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
	WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
	WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
	WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
	WIN32_CONSTANT(F_DWORD, GENERIC_READ);
	WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
	WIN32_CONSTANT(F_DWORD, INFINITE);
	WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
	WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);
	WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
	WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
	WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
	WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
	WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
	WIN32_CONSTANT(F_DWORD, PIPE_WAIT);
	WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);

	WIN32_CONSTANT("i", NULL);

	return (PyObject*)&Win32Type;
}