1 | /** @file
|
---|
2 | * IPRT - Process Management.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_process_h
|
---|
27 | #define ___iprt_process_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_process RTProc - Process Management
|
---|
35 | * @ingroup grp_rt
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Process priority.
|
---|
42 | *
|
---|
43 | * The process priority is used to select how scheduling properties
|
---|
44 | * are assigned to the different thread types (see THREADTYPE).
|
---|
45 | *
|
---|
46 | * In addition to using the policy assigned to the process at startup (DEFAULT)
|
---|
47 | * it is possible to change the process priority at runtime. This allows for
|
---|
48 | * a GUI, resource manager or admin to adjust the general priority of a task
|
---|
49 | * without upsetting the fine-tuned priority of the threads within.
|
---|
50 | */
|
---|
51 | typedef enum RTPROCPRIORITY
|
---|
52 | {
|
---|
53 | /** Invalid priority. */
|
---|
54 | RTPROCPRIORITY_INVALID = 0,
|
---|
55 | /** Default priority.
|
---|
56 | * Derive the scheduling policy from the priority of the RTR3Init()
|
---|
57 | * and RTProcSetPriority() callers and the rights the process have
|
---|
58 | * to alter its own priority.
|
---|
59 | */
|
---|
60 | RTPROCPRIORITY_DEFAULT,
|
---|
61 | /** Flat priority.
|
---|
62 | * Assumes a scheduling policy which puts the process at the default priority
|
---|
63 | * and with all thread at the same priority.
|
---|
64 | */
|
---|
65 | RTPROCPRIORITY_FLAT,
|
---|
66 | /** Low priority.
|
---|
67 | * Assumes a scheduling policy which puts the process mostly below the
|
---|
68 | * default priority of the host OS.
|
---|
69 | */
|
---|
70 | RTPROCPRIORITY_LOW,
|
---|
71 | /** Normal priority.
|
---|
72 | * Assume a scheduling policy which shares the CPU resources fairly with
|
---|
73 | * other processes running with the default priority of the host OS.
|
---|
74 | */
|
---|
75 | RTPROCPRIORITY_NORMAL,
|
---|
76 | /** High priority.
|
---|
77 | * Assumes a scheduling policy which puts the task above the default
|
---|
78 | * priority of the host OS. This policy might easily cause other tasks
|
---|
79 | * in the system to starve.
|
---|
80 | */
|
---|
81 | RTPROCPRIORITY_HIGH,
|
---|
82 | /** Last priority, used for validation. */
|
---|
83 | RTPROCPRIORITY_LAST
|
---|
84 | } RTPROCPRIORITY;
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Get the current process identifier.
|
---|
89 | *
|
---|
90 | * @returns Process identifier.
|
---|
91 | */
|
---|
92 | RTDECL(RTPROCESS) RTProcSelf(void);
|
---|
93 |
|
---|
94 |
|
---|
95 | #ifdef IN_RING0
|
---|
96 | /**
|
---|
97 | * Get the current process handle.
|
---|
98 | *
|
---|
99 | * @returns Ring-0 process handle.
|
---|
100 | */
|
---|
101 | RTR0DECL(RTR0PROCESS) RTR0ProcHandleSelf(void);
|
---|
102 | #endif
|
---|
103 |
|
---|
104 |
|
---|
105 | #ifdef IN_RING3
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Attempts to alter the priority of the current process.
|
---|
109 | *
|
---|
110 | * @returns iprt status code.
|
---|
111 | * @param enmPriority The new priority.
|
---|
112 | */
|
---|
113 | RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority);
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Gets the current priority of this process.
|
---|
117 | *
|
---|
118 | * @returns The priority (see RTPROCPRIORITY).
|
---|
119 | */
|
---|
120 | RTR3DECL(RTPROCPRIORITY) RTProcGetPriority(void);
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Create a child process.
|
---|
124 | *
|
---|
125 | * @returns iprt status code.
|
---|
126 | * @param pszExec Executable image to use to create the child process.
|
---|
127 | * @param papszArgs Pointer to an array of arguments to the child. The array terminated by an entry containing NULL.
|
---|
128 | * @param Env Handle to the environment block for the child.
|
---|
129 | * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
|
---|
130 | * @param pProcess Where to store the process identifier on successful return.
|
---|
131 | * The content is not changed on failure. NULL is allowed.
|
---|
132 | */
|
---|
133 | RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess);
|
---|
134 |
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Create a child process.
|
---|
138 | *
|
---|
139 | * @returns IPRT status code.
|
---|
140 | *
|
---|
141 | * @param pszExec Executable image to use to create the child process.
|
---|
142 | * @param papszArgs Pointer to an array of arguments to the child. The
|
---|
143 | * array terminated by an entry containing NULL.
|
---|
144 | * @param hEnv Handle to the environment block for the child. Pass
|
---|
145 | * RTENV_DEFAULT to use the environment of the current
|
---|
146 | * process.
|
---|
147 | * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
|
---|
148 | * @param phStdIn The standard in handle to assign the new process. Pass
|
---|
149 | * NULL to use the same as the current process. If the
|
---|
150 | * handle is NIL, we'll close the standard input of the
|
---|
151 | * guest.
|
---|
152 | * @param phStdOut The standard out handle to assign the new process. Pass
|
---|
153 | * NULL to use the same as the current process. If the
|
---|
154 | * handle is NIL, we'll close the standard output of the
|
---|
155 | * guest.
|
---|
156 | * @param phStdErr The standard error handle to assign the new process. Pass
|
---|
157 | * NULL to use the same as the current process. If the
|
---|
158 | * handle is NIL, we'll close the standard error of the
|
---|
159 | * guest.
|
---|
160 | * @param pszAsUser User to run the process as. Pass NULL to use the same
|
---|
161 | * user as the current process.
|
---|
162 | * Windows: Use user@domain format to specify a domain.
|
---|
163 | * @param pszPassword Password to use to authenticate @a pszAsUser. Must be
|
---|
164 | * NULL wif pszAsUser is NULL. Whether this is actually
|
---|
165 | * used or not depends on the platform.
|
---|
166 | * @param phProcess Where to store the process handle on successful return.
|
---|
167 | * The content is not changed on failure. NULL is allowed.
|
---|
168 | *
|
---|
169 | * @remarks The handles does not have to be created as inheritable, but it
|
---|
170 | * doesn't hurt if they are as it may avoid race conditions on some
|
---|
171 | * platforms.
|
---|
172 | *
|
---|
173 | * @remarks The as-user feature isn't supported/implemented on all platforms and
|
---|
174 | * will cause a-yet-to-be-determined-error-status on these.
|
---|
175 | */
|
---|
176 | RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
|
---|
177 | PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
|
---|
178 | const char *pszPassword, PRTPROCESS phProcess);
|
---|
179 |
|
---|
180 | /** @name RTProcCreate and RTProcCreateEx flags
|
---|
181 | * @{ */
|
---|
182 | /** Detach the child process from the parents process tree and process group,
|
---|
183 | * session or/and console (depends on the platform what's done applicable).
|
---|
184 | *
|
---|
185 | * The new process will not be a direct decendent of the parent and it will not
|
---|
186 | * be possible to wait for it, i.e. @a phProcess shall be NULL. */
|
---|
187 | #define RTPROC_FLAGS_DETACHED RT_BIT(0)
|
---|
188 | /** Don't show the started process according to the specific
|
---|
189 | * OS guidelines. */
|
---|
190 | #define RTPROC_FLAGS_HIDDEN RT_BIT(1)
|
---|
191 | /** Use special code path for starting child processes from
|
---|
192 | * a service (daemon). On Windows this is required for services
|
---|
193 | * because of the so called "Session 0" isolation which was
|
---|
194 | * introduced with Windows Vista. */
|
---|
195 | #define RTPROC_FLAGS_SERVICE RT_BIT(2)
|
---|
196 | /** Suppress changing the process contract id for the child process
|
---|
197 | * on Solaris. Without this flag the contract id is always changed,
|
---|
198 | * as that's the more frequently used case. */
|
---|
199 | #define RTPROC_FLAGS_SAME_CONTRACT RT_BIT(3)
|
---|
200 | /** Do not load user profile data when executing a process. This bit
|
---|
201 | * at the moment only is valid on Windows. */
|
---|
202 | #define RTPROC_FLAGS_NO_PROFILE RT_BIT(4)
|
---|
203 | /** Create process w/o a console window. This bit
|
---|
204 | * at the moment only is valid on Windows. */
|
---|
205 | #define RTPROC_FLAGS_NO_WINDOW RT_BIT(5)
|
---|
206 |
|
---|
207 | /** @} */
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Process exit reason.
|
---|
212 | */
|
---|
213 | typedef enum RTPROCEXITREASON
|
---|
214 | {
|
---|
215 | /** Normal exit. iStatus contains the exit code. */
|
---|
216 | RTPROCEXITREASON_NORMAL = 1,
|
---|
217 | /** Any abnormal exit. iStatus is undefined. */
|
---|
218 | RTPROCEXITREASON_ABEND,
|
---|
219 | /** Killed by a signal. The iStatus field contains the signal number. */
|
---|
220 | RTPROCEXITREASON_SIGNAL
|
---|
221 | } RTPROCEXITREASON;
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Process exit status.
|
---|
225 | */
|
---|
226 | typedef struct RTPROCSTATUS
|
---|
227 | {
|
---|
228 | /** The process exit status if the exit was a normal one. */
|
---|
229 | int iStatus;
|
---|
230 | /** The reason the process terminated. */
|
---|
231 | RTPROCEXITREASON enmReason;
|
---|
232 | } RTPROCSTATUS;
|
---|
233 | /** Pointer to a process exit status structure. */
|
---|
234 | typedef RTPROCSTATUS *PRTPROCSTATUS;
|
---|
235 | /** Pointer to a const process exit status structure. */
|
---|
236 | typedef const RTPROCSTATUS *PCRTPROCSTATUS;
|
---|
237 |
|
---|
238 |
|
---|
239 | /** Flags for RTProcWait().
|
---|
240 | * @{ */
|
---|
241 | /** Block indefinitly waiting for the process to exit. */
|
---|
242 | #define RTPROCWAIT_FLAGS_BLOCK 0
|
---|
243 | /** Don't block, just check if the process have exited. */
|
---|
244 | #define RTPROCWAIT_FLAGS_NOBLOCK 1
|
---|
245 | /** @} */
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Waits for a process, resumes on interruption.
|
---|
249 | *
|
---|
250 | * @returns VINF_SUCCESS when the status code for the process was collected and
|
---|
251 | * put in *pProcStatus.
|
---|
252 | * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
|
---|
253 | * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
|
---|
254 | * process haven't exited yet.
|
---|
255 | *
|
---|
256 | * @param Process The process to wait for.
|
---|
257 | * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
|
---|
258 | * @param pProcStatus Where to store the exit status on success.
|
---|
259 | * Optional.
|
---|
260 | */
|
---|
261 | RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Waits for a process, returns on interruption.
|
---|
265 | *
|
---|
266 | * @returns VINF_SUCCESS when the status code for the process was collected and
|
---|
267 | * put in *pProcStatus.
|
---|
268 | * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
|
---|
269 | * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
|
---|
270 | * process haven't exited yet.
|
---|
271 | * @returns VERR_INTERRUPTED when the wait was interrupted by the arrival of a
|
---|
272 | * signal or other async event.
|
---|
273 | *
|
---|
274 | * @param Process The process to wait for.
|
---|
275 | * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
|
---|
276 | * @param pProcStatus Where to store the exit status on success.
|
---|
277 | * Optional.
|
---|
278 | */
|
---|
279 | RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Terminates (kills) a running process.
|
---|
283 | *
|
---|
284 | * @returns IPRT status code.
|
---|
285 | * @param Process The process to terminate.
|
---|
286 | */
|
---|
287 | RTR3DECL(int) RTProcTerminate(RTPROCESS Process);
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Gets the processor affinity mask of the current process.
|
---|
291 | *
|
---|
292 | * @returns The affinity mask.
|
---|
293 | */
|
---|
294 | RTR3DECL(uint64_t) RTProcGetAffinityMask(void);
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Gets the short process name.
|
---|
298 | *
|
---|
299 | * @returns Pointer to read-only name string.
|
---|
300 | */
|
---|
301 | RTR3DECL(const char *) RTProcShortName(void);
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Gets the path to the executable image of the current process.
|
---|
305 | *
|
---|
306 | * @returns pszExecPath on success. NULL on buffer overflow or other errors.
|
---|
307 | *
|
---|
308 | * @param pszExecPath Where to store the path.
|
---|
309 | * @param cbExecPath The size of the buffer.
|
---|
310 | */
|
---|
311 | RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath);
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Daemonize the current process, making it a background process.
|
---|
315 | *
|
---|
316 | * The way this work is that it will spawn a detached / backgrounded /
|
---|
317 | * daemonized / call-it-what-you-want process that isn't a direct child of the
|
---|
318 | * current process. The spawned will have the same arguments a the caller,
|
---|
319 | * except that the @a pszDaemonizedOpt is appended to prevent that the new
|
---|
320 | * process calls this API again.
|
---|
321 | *
|
---|
322 | * The new process will have the standard handles directed to/from the
|
---|
323 | * bitbucket.
|
---|
324 | *
|
---|
325 | * @returns IPRT status code. On success it is normal for the caller to exit
|
---|
326 | * the process by returning from main().
|
---|
327 | *
|
---|
328 | * @param papszArgs The argument vector of the calling process.
|
---|
329 | * @param pszDaemonized The daemonized option. This is appended to the end
|
---|
330 | * of the parameter list of the daemonized process.
|
---|
331 | */
|
---|
332 | RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt);
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Daemonize the current process, making it a background process. The current
|
---|
336 | * process will exit if daemonizing is successful.
|
---|
337 | *
|
---|
338 | * @returns IPRT status code. On success it will only return in the child
|
---|
339 | * process, the parent will exit. On failure, it will return in the
|
---|
340 | * parent process and no child has been spawned.
|
---|
341 | *
|
---|
342 | * @param fNoChDir Pass false to change working directory to "/".
|
---|
343 | * @param fNoClose Pass false to redirect standard file streams to the null device.
|
---|
344 | * @param pszPidfile Path to a file to write the process id of the daemon
|
---|
345 | * process to. Daemonizing will fail if this file already
|
---|
346 | * exists or cannot be written. May be NULL.
|
---|
347 | */
|
---|
348 | RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile);
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Check if the given process is running on the system.
|
---|
352 | *
|
---|
353 | * This check is case sensitive on most systems, except for Windows, OS/2 and
|
---|
354 | * Darwin.
|
---|
355 | *
|
---|
356 | * @returns true if the process is running & false otherwise.
|
---|
357 | * @param pszName Process name to search for. If no path is given only the
|
---|
358 | * filename part of the running process set will be
|
---|
359 | * matched. If a path is specified, the full path will be
|
---|
360 | * matched.
|
---|
361 | */
|
---|
362 | RTR3DECL(bool) RTProcIsRunningByName(const char *pszName);
|
---|
363 |
|
---|
364 | #endif /* IN_RING3 */
|
---|
365 |
|
---|
366 | /** @} */
|
---|
367 |
|
---|
368 | RT_C_DECLS_END
|
---|
369 |
|
---|
370 | #endif
|
---|
371 |
|
---|