- Update to r53061
[reactos.git] / dll / win32 / kernel32 / client / job.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/process/job.c
6 * PURPOSE: Job functions
7 * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
8 * UPDATE HISTORY:
9 * Created 9/23/2004
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <k32.h>
15
16 #define NDEBUG
17 #include <debug.h>
18
19 /* FUNCTIONS ****************************************************************/
20
21 /*
22 * @implemented
23 */
24 HANDLE
25 WINAPI
26 CreateJobObjectA(IN LPSECURITY_ATTRIBUTES lpJobAttributes,
27 IN LPCSTR lpName)
28 {
29 /* Call the W(ide) function */
30 ConvertWin32AnsiObjectApiToUnicodeApi(JobObject, lpName, lpJobAttributes);
31 }
32
33 /*
34 * @implemented
35 */
36 HANDLE
37 WINAPI
38 CreateJobObjectW(IN LPSECURITY_ATTRIBUTES lpJobAttributes,
39 IN LPCWSTR lpName)
40 {
41 /* Create the NT object */
42 CreateNtObjectFromWin32Api(JobObject, JobObject, JOB, lpJobAttributes, lpName);
43 }
44
45 /*
46 * @implemented
47 */
48 HANDLE
49 WINAPI
50 OpenJobObjectW(DWORD dwDesiredAccess,
51 BOOL bInheritHandle,
52 LPCWSTR lpName)
53 {
54 /* Open the NT object */
55 OpenNtObjectFromWin32Api(JobObject, dwDesiredAccess, bInheritHandle, lpName);
56 }
57
58
59 /*
60 * @implemented
61 */
62 HANDLE
63 WINAPI
64 OpenJobObjectA(DWORD dwDesiredAccess,
65 BOOL bInheritHandle,
66 LPCSTR lpName)
67 {
68 /* Call the W(ide) function */
69 ConvertOpenWin32AnsiObjectApiToUnicodeApi(JobObject, dwDesiredAccess, bInheritHandle, lpName);
70 }
71
72
73 /*
74 * @implemented
75 */
76 BOOL
77 WINAPI
78 IsProcessInJob(HANDLE ProcessHandle,
79 HANDLE JobHandle,
80 PBOOL Result)
81 {
82 NTSTATUS Status;
83
84 Status = NtIsProcessInJob(ProcessHandle, JobHandle);
85 if (NT_SUCCESS(Status))
86 {
87 *Result = (Status == STATUS_PROCESS_IN_JOB);
88 return TRUE;
89 }
90
91 BaseSetLastNTError(Status);
92 return FALSE;
93 }
94
95
96 /*
97 * @implemented
98 */
99 BOOL
100 WINAPI
101 AssignProcessToJobObject(HANDLE hJob,
102 HANDLE hProcess)
103 {
104 NTSTATUS Status;
105
106 Status = NtAssignProcessToJobObject(hJob, hProcess);
107 if (!NT_SUCCESS(Status))
108 {
109 BaseSetLastNTError(Status);
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116
117 /*
118 * @implemented
119 */
120 BOOL
121 WINAPI
122 QueryInformationJobObject(HANDLE hJob,
123 JOBOBJECTINFOCLASS JobObjectInformationClass,
124 LPVOID lpJobObjectInformation,
125 DWORD cbJobObjectInformationLength,
126 LPDWORD lpReturnLength)
127 {
128 NTSTATUS Status;
129
130 Status = NtQueryInformationJobObject(hJob,
131 JobObjectInformationClass,
132 lpJobObjectInformation,
133 cbJobObjectInformationLength,
134 lpReturnLength);
135 if (NT_SUCCESS(Status))
136 {
137 PJOBOBJECT_BASIC_LIMIT_INFORMATION BasicInfo;
138
139 switch (JobObjectInformationClass)
140 {
141 case JobObjectBasicLimitInformation:
142 BasicInfo = (PJOBOBJECT_BASIC_LIMIT_INFORMATION)lpJobObjectInformation;
143 break;
144
145 case JobObjectExtendedLimitInformation:
146 BasicInfo = &((PJOBOBJECT_EXTENDED_LIMIT_INFORMATION)lpJobObjectInformation)->BasicLimitInformation;
147 break;
148
149 default:
150 BasicInfo = NULL;
151 break;
152 }
153
154 if (BasicInfo != NULL)
155 {
156 /* we need to convert the process priority classes in the
157 JOBOBJECT_BASIC_LIMIT_INFORMATION structure the same way as
158 GetPriorityClass() converts it! */
159 switch (BasicInfo->PriorityClass)
160 {
161 case PROCESS_PRIORITY_CLASS_IDLE:
162 BasicInfo->PriorityClass = IDLE_PRIORITY_CLASS;
163 break;
164
165 case PROCESS_PRIORITY_CLASS_BELOW_NORMAL:
166 BasicInfo->PriorityClass = BELOW_NORMAL_PRIORITY_CLASS;
167 break;
168
169 case PROCESS_PRIORITY_CLASS_NORMAL:
170 BasicInfo->PriorityClass = NORMAL_PRIORITY_CLASS;
171 break;
172
173 case PROCESS_PRIORITY_CLASS_ABOVE_NORMAL:
174 BasicInfo->PriorityClass = ABOVE_NORMAL_PRIORITY_CLASS;
175 break;
176
177 case PROCESS_PRIORITY_CLASS_HIGH:
178 BasicInfo->PriorityClass = HIGH_PRIORITY_CLASS;
179 break;
180
181 case PROCESS_PRIORITY_CLASS_REALTIME:
182 BasicInfo->PriorityClass = REALTIME_PRIORITY_CLASS;
183 break;
184
185 default:
186 BasicInfo->PriorityClass = NORMAL_PRIORITY_CLASS;
187 break;
188 }
189 }
190
191 return TRUE;
192 }
193
194 BaseSetLastNTError(Status);
195 return FALSE;
196 }
197
198
199 /*
200 * @implemented
201 */
202 BOOL
203 WINAPI
204 SetInformationJobObject(HANDLE hJob,
205 JOBOBJECTINFOCLASS JobObjectInformationClass,
206 LPVOID lpJobObjectInformation,
207 DWORD cbJobObjectInformationLength)
208 {
209 JOBOBJECT_EXTENDED_LIMIT_INFORMATION ExtendedLimitInfo;
210 PJOBOBJECT_BASIC_LIMIT_INFORMATION BasicInfo;
211 PVOID ObjectInfo;
212 NTSTATUS Status;
213
214 switch (JobObjectInformationClass)
215 {
216 case JobObjectBasicLimitInformation:
217 if (cbJobObjectInformationLength != sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION))
218 {
219 SetLastError(ERROR_BAD_LENGTH);
220 return FALSE;
221 }
222
223 ObjectInfo = &ExtendedLimitInfo.BasicLimitInformation;
224 BasicInfo = (PJOBOBJECT_BASIC_LIMIT_INFORMATION)ObjectInfo;
225 RtlCopyMemory(ObjectInfo, lpJobObjectInformation, cbJobObjectInformationLength);
226 break;
227
228 case JobObjectExtendedLimitInformation:
229 if (cbJobObjectInformationLength != sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION))
230 {
231 SetLastError(ERROR_BAD_LENGTH);
232 return FALSE;
233 }
234
235 ObjectInfo = &ExtendedLimitInfo;
236 BasicInfo = &ExtendedLimitInfo.BasicLimitInformation;
237 RtlCopyMemory(ObjectInfo, lpJobObjectInformation, cbJobObjectInformationLength);
238 break;
239
240 default:
241 ObjectInfo = lpJobObjectInformation;
242 BasicInfo = NULL;
243 break;
244 }
245
246 if (BasicInfo != NULL)
247 {
248 /* we need to convert the process priority classes in the
249 JOBOBJECT_BASIC_LIMIT_INFORMATION structure the same way as
250 SetPriorityClass() converts it! */
251 switch(BasicInfo->PriorityClass)
252 {
253 case IDLE_PRIORITY_CLASS:
254 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_IDLE;
255 break;
256
257 case BELOW_NORMAL_PRIORITY_CLASS:
258 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_BELOW_NORMAL;
259 break;
260
261 case NORMAL_PRIORITY_CLASS:
262 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_NORMAL;
263 break;
264
265 case ABOVE_NORMAL_PRIORITY_CLASS:
266 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_ABOVE_NORMAL;
267 break;
268
269 case HIGH_PRIORITY_CLASS:
270 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_HIGH;
271 break;
272
273 case REALTIME_PRIORITY_CLASS:
274 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_REALTIME;
275 break;
276
277 default:
278 BasicInfo->PriorityClass = PROCESS_PRIORITY_CLASS_NORMAL;
279 break;
280 }
281 }
282
283 Status = NtSetInformationJobObject(hJob,
284 JobObjectInformationClass,
285 ObjectInfo,
286 cbJobObjectInformationLength);
287 if (!NT_SUCCESS(Status))
288 {
289 BaseSetLastNTError(Status);
290 return FALSE;
291 }
292
293 return TRUE;
294 }
295
296
297 /*
298 * @implemented
299 */
300 BOOL
301 WINAPI
302 TerminateJobObject(HANDLE hJob,
303 UINT uExitCode)
304 {
305 NTSTATUS Status;
306
307 Status = NtTerminateJobObject(hJob, uExitCode);
308 if (!NT_SUCCESS(Status))
309 {
310 BaseSetLastNTError(Status);
311 return FALSE;
312 }
313
314 return TRUE;
315 }
316
317 /*
318 * @unimplemented
319 */
320 BOOL
321 WINAPI
322 CreateJobSet (
323 ULONG NumJob,
324 PJOB_SET_ARRAY UserJobSet,
325 ULONG Flags)
326 {
327 STUB;
328 return 0;
329 }
330
331 /* EOF */