ReactOS  0.4.11-dev-433-g473ca91
job.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS system libraries
4  * FILE: dll/win32/kernel32/client/job.c
5  * PURPOSE: Job functions
6  * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
7  * UPDATE HISTORY:
8  * Created 9/23/2004
9  */
10 
11 /* INCLUDES *******************************************************************/
12 
13 #include <k32.h>
14 #include <winspool.h>
15 
16 #define NDEBUG
17 #include <debug.h>
18 
19 /* FUNCTIONS ******************************************************************/
20 
21 /*
22  * @implemented
23  */
24 HANDLE
25 WINAPI
28 {
29  /* Call the W(ide) function */
30  ConvertWin32AnsiObjectApiToUnicodeApi(JobObject, lpName, lpJobAttributes);
31 }
32 
33 /*
34  * @implemented
35  */
36 HANDLE
37 WINAPI
40 {
41  /* Create the NT object */
42  CreateNtObjectFromWin32Api(JobObject, JobObject, JOB_OBJECT, lpJobAttributes, lpName);
43 }
44 
45 /*
46  * @implemented
47  */
48 HANDLE
49 WINAPI
50 OpenJobObjectW(IN DWORD dwDesiredAccess,
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(IN DWORD dwDesiredAccess,
67 {
68  /* Call the W(ide) function */
69  ConvertOpenWin32AnsiObjectApiToUnicodeApi(JobObject, dwDesiredAccess, bInheritHandle, lpName);
70 }
71 
72 /*
73  * @implemented
74  */
75 BOOL
76 WINAPI
78  IN HANDLE JobHandle,
80 {
82 
83  Status = NtIsProcessInJob(ProcessHandle, JobHandle);
84  if (NT_SUCCESS(Status))
85  {
86  *Result = (Status == STATUS_PROCESS_IN_JOB);
87  return TRUE;
88  }
89 
90  BaseSetLastNTError(Status);
91  return FALSE;
92 }
93 
94 /*
95  * @implemented
96  */
97 BOOL
98 WINAPI
101 {
103 
104  Status = NtAssignProcessToJobObject(hJob, hProcess);
105  if (!NT_SUCCESS(Status))
106  {
107  BaseSetLastNTError(Status);
108  return FALSE;
109  }
110 
111  return TRUE;
112 }
113 
114 /*
115  * @implemented
116  */
117 BOOL
118 WINAPI
120  IN JOBOBJECTINFOCLASS JobObjectInformationClass,
121  IN LPVOID lpJobObjectInformation,
122  IN DWORD cbJobObjectInformationLength,
123  OUT LPDWORD lpReturnLength)
124 {
126  PVOID JobInfo;
128  ULONG ExpectedSize;
129 
130  if (JobObjectInformationClass == JobObjectBasicLimitInformation)
131  {
132  ExpectedSize = sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION);
133  JobInfo = &LocalInfo;
134  }
135  else if (JobObjectInformationClass == JobObjectExtendedLimitInformation)
136  {
137  ExpectedSize = sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION);
138  JobInfo = &LocalInfo;
139  }
140  else
141  {
142  ExpectedSize = cbJobObjectInformationLength;
143  JobInfo = lpJobObjectInformation;
144  }
145 
146  if (cbJobObjectInformationLength != ExpectedSize)
147  {
149  return FALSE;
150  }
151 
152  Status = NtQueryInformationJobObject(hJob,
153  JobObjectInformationClass,
154  JobInfo,
155  ExpectedSize,
156  lpReturnLength);
157  if (NT_SUCCESS(Status))
158  {
159  if (JobInfo != &LocalInfo) return TRUE;
160 
161  switch (LocalInfo.BasicLimitInformation.PriorityClass)
162  {
166  break;
167 
171  break;
172 
176  break;
177 
181  break;
182 
186  break;
187 
191  break;
192 
193  default:
196  break;
197  }
198 
199  RtlCopyMemory(lpJobObjectInformation, &LocalInfo, ExpectedSize);
200  return TRUE;
201  }
202 
203  BaseSetLastNTError(Status);
204  return FALSE;
205 }
206 
207 /*
208  * @implemented
209  */
210 BOOL
211 WINAPI
213  IN JOBOBJECTINFOCLASS JobObjectInformationClass,
214  IN LPVOID lpJobObjectInformation,
215  IN DWORD cbJobObjectInformationLength)
216 {
218  PVOID JobInfo;
220  ULONG ExpectedSize;
221  PVOID State = NULL;
223 
224  if (JobObjectInformationClass == JobObjectBasicLimitInformation)
225  {
226  ExpectedSize = sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION);
227  JobInfo = &LocalInfo;
228  }
229  else if (JobObjectInformationClass == JobObjectExtendedLimitInformation)
230  {
231  ExpectedSize = sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION);
232  JobInfo = &LocalInfo;
233  }
234  else
235  {
236  ExpectedSize = cbJobObjectInformationLength;
237  JobInfo = lpJobObjectInformation;
238  }
239 
240  if (cbJobObjectInformationLength != ExpectedSize)
241  {
243  return FALSE;
244  }
245 
246  if (JobInfo == &LocalInfo)
247  {
248  RtlCopyMemory(&LocalInfo, lpJobObjectInformation, ExpectedSize);
249 
250  if (LocalInfo.BasicLimitInformation.LimitFlags &
252  {
253  switch (LocalInfo.BasicLimitInformation.PriorityClass)
254  {
255  case IDLE_PRIORITY_CLASS:
258  break;
259 
263  break;
264 
268  break;
269 
273  break;
274 
275  case HIGH_PRIORITY_CLASS:
278  break;
279 
283  break;
284 
285  default:
288  break;
289  }
290  }
291 
292  if (LocalInfo.BasicLimitInformation.LimitFlags &
294  {
295  Status = RtlAcquirePrivilege(&Privilege, 1, 0, &State);
296  }
297  }
298 
299  Status = NtSetInformationJobObject(hJob,
300  JobObjectInformationClass,
301  JobInfo,
302  ExpectedSize);
303  if (NT_SUCCESS(Status))
304  {
305  if (State != NULL) RtlReleasePrivilege(State);
306  return TRUE;
307  }
308 
309  BaseSetLastNTError(Status);
310  return FALSE;
311 }
312 
313 /*
314  * @implemented
315  */
316 BOOL
317 WINAPI
319  IN UINT uExitCode)
320 {
322 
323  Status = NtTerminateJobObject(hJob, uExitCode);
324  if (!NT_SUCCESS(Status))
325  {
326  BaseSetLastNTError(Status);
327  return FALSE;
328  }
329 
330  return TRUE;
331 }
332 
333 /*
334  * @implemented
335  */
336 BOOL
337 WINAPI
339  IN PJOB_SET_ARRAY UserJobSet,
340  IN ULONG Flags)
341 {
343 
344  Status = NtCreateJobSet(NumJob, UserJobSet, Flags);
345  if (!NT_SUCCESS(Status))
346  {
347  BaseSetLastNTError(Status);
348  return FALSE;
349  }
350 
351  return TRUE;
352 }
353 
354 /* EOF */
DWORD *typedef PVOID
Definition: winlogon.h:61
enum _JOBOBJECTINFOCLASS JOBOBJECTINFOCLASS
struct _JOBOBJECT_EXTENDED_LIMIT_INFORMATION JOBOBJECT_EXTENDED_LIMIT_INFORMATION
#define IN
Definition: typedefs.h:38
#define REALTIME_PRIORITY_CLASS
Definition: winbase.h:184
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
BOOL WINAPI AssignProcessToJobObject(IN HANDLE hJob, IN HANDLE hProcess)
Definition: job.c:99
BOOL WINAPI TerminateJobObject(IN HANDLE hJob, IN UINT uExitCode)
Definition: job.c:318
_In_ LPCSTR lpName
Definition: winbase.h:2729
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSTATUS NTAPI NtSetInformationJobObject(HANDLE JobHandle, JOBOBJECTINFOCLASS JobInformationClass, PVOID JobInformation, ULONG JobInformationLength)
Definition: job.c:752
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define NORMAL_PRIORITY_CLASS
Definition: winbase.h:181
HANDLE WINAPI CreateJobObjectA(IN LPSECURITY_ATTRIBUTES lpJobAttributes, IN LPCSTR lpName)
Definition: job.c:26
NTSTATUS NTAPI NtCreateJobSet(IN ULONG NumJob, IN PJOB_SET_ARRAY UserJobSet, IN ULONG Flags)
Definition: job.c:243
DWORD DWORD
Definition: winlogon.h:84
#define OpenNtObjectFromWin32Api(ntobj, acc, inh, name)
Definition: base_x.h:148
#define ABOVE_NORMAL_PRIORITY_CLASS
Definition: winbase.h:191
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define FALSE
Definition: types.h:117
static BOOL bInheritHandle
Definition: pipe.c:82
#define ConvertWin32AnsiObjectApiToUnicodeApi(obj, name,...)
Definition: base_x.h:54
BOOL WINAPI QueryInformationJobObject(IN HANDLE hJob, IN JOBOBJECTINFOCLASS JobObjectInformationClass, IN LPVOID lpJobObjectInformation, IN DWORD cbJobObjectInformationLength, OUT LPDWORD lpReturnLength)
Definition: job.c:119
JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation
Definition: pstypes.h:1533
#define JOB_OBJECT_LIMIT_WORKINGSET
Definition: pstypes.h:204
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI VOID NTAPI RtlReleasePrivilege(_In_ PVOID ReturnedState)
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define PROCESS_PRIORITY_CLASS_NORMAL
Definition: pstypes.h:108
const char * LPCSTR
Definition: xmlstorage.h:183
BOOL * PBOOL
Definition: windef.h:161
#define PROCESS_PRIORITY_CLASS_ABOVE_NORMAL
Definition: pstypes.h:112
#define IDLE_PRIORITY_CLASS
Definition: winbase.h:182
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG NTSTATUS
Definition: precomp.h:26
#define PROCESS_PRIORITY_CLASS_REALTIME
Definition: pstypes.h:110
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
BOOL Privilege(LPTSTR pszPrivilege, BOOL bEnable)
Definition: user_lib.cpp:531
#define PROCESS_PRIORITY_CLASS_HIGH
Definition: pstypes.h:109
#define CreateNtObjectFromWin32Api(obj, ntobj, capsobj, sec, name,...)
Definition: base_x.h:140
BOOL WINAPI SetInformationJobObject(IN HANDLE hJob, IN JOBOBJECTINFOCLASS JobObjectInformationClass, IN LPVOID lpJobObjectInformation, IN DWORD cbJobObjectInformationLength)
Definition: job.c:212
#define SE_INC_BASE_PRIORITY_PRIVILEGE
Definition: security.c:668
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name)
Definition: base_x.h:86
#define WINAPI
Definition: msvc.h:20
NTSTATUS NTAPI NtAssignProcessToJobObject(HANDLE JobHandle, HANDLE ProcessHandle)
Definition: job.c:157
Status
Definition: gdiplustypes.h:24
DWORD *typedef HANDLE
Definition: winlogon.h:61
unsigned int UINT
Definition: ndis.h:50
struct _JOBOBJECT_BASIC_LIMIT_INFORMATION JOBOBJECT_BASIC_LIMIT_INFORMATION
NTSYSAPI NTSTATUS NTAPI RtlAcquirePrivilege(_In_ PULONG Privilege, _In_ ULONG NumPriv, _In_ ULONG Flags, _Out_ PVOID *ReturnedState)
BOOL WINAPI IsProcessInJob(IN HANDLE ProcessHandle, IN HANDLE JobHandle, OUT PBOOL Result)
Definition: job.c:77
#define STATUS_PROCESS_IN_JOB
Definition: ntstatus.h:108
#define OUT
Definition: typedefs.h:39
uint32_t * LPDWORD
Definition: typedefs.h:57
#define BELOW_NORMAL_PRIORITY_CLASS
Definition: winbase.h:190
unsigned int ULONG
Definition: retypes.h:1
HANDLE WINAPI OpenJobObjectW(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
Definition: job.c:50
BOOL WINAPI CreateJobSet(IN ULONG NumJob, IN PJOB_SET_ARRAY UserJobSet, IN ULONG Flags)
Definition: job.c:338
#define PROCESS_PRIORITY_CLASS_BELOW_NORMAL
Definition: pstypes.h:111
NTSTATUS NTAPI NtIsProcessInJob(IN HANDLE ProcessHandle, IN HANDLE JobHandle OPTIONAL)
Definition: job.c:361
#define PROCESS_PRIORITY_CLASS_IDLE
Definition: pstypes.h:107
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
NTSTATUS NTAPI NtQueryInformationJobObject(HANDLE JobHandle, JOBOBJECTINFOCLASS JobInformationClass, PVOID JobInformation, ULONG JobInformationLength, PULONG ReturnLength)
Definition: job.c:485
NTSTATUS NTAPI NtTerminateJobObject(HANDLE JobHandle, NTSTATUS ExitStatus)
Definition: job.c:850
HANDLE WINAPI CreateJobObjectW(IN LPSECURITY_ATTRIBUTES lpJobAttributes, IN LPCWSTR lpName)
Definition: job.c:38
#define JOB_OBJECT_LIMIT_PRIORITY_CLASS
Definition: pstypes.h:209
HANDLE WINAPI OpenJobObjectA(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
Definition: job.c:64