1 | /** @file
|
---|
2 | * IPRT - Process Management.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2019 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_INCLUDED_process_h
|
---|
27 | #define IPRT_INCLUDED_process_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/cdefs.h>
|
---|
33 | #include <iprt/types.h>
|
---|
34 |
|
---|
35 | RT_C_DECLS_BEGIN
|
---|
36 |
|
---|
37 | /** @defgroup grp_rt_process RTProc - Process Management
|
---|
38 | * @ingroup grp_rt
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Process priority.
|
---|
45 | *
|
---|
46 | * The process priority is used to select how scheduling properties
|
---|
47 | * are assigned to the different thread types (see THREADTYPE).
|
---|
48 | *
|
---|
49 | * In addition to using the policy assigned to the process at startup (DEFAULT)
|
---|
50 | * it is possible to change the process priority at runtime. This allows for
|
---|
51 | * a GUI, resource manager or admin to adjust the general priority of a task
|
---|
52 | * without upsetting the fine-tuned priority of the threads within.
|
---|
53 | */
|
---|
54 | typedef enum RTPROCPRIORITY
|
---|
55 | {
|
---|
56 | /** Invalid priority. */
|
---|
57 | RTPROCPRIORITY_INVALID = 0,
|
---|
58 | /** Default priority.
|
---|
59 | * Derive the scheduling policy from the priority of the RTR3Init()
|
---|
60 | * and RTProcSetPriority() callers and the rights the process have
|
---|
61 | * to alter its own priority.
|
---|
62 | */
|
---|
63 | RTPROCPRIORITY_DEFAULT,
|
---|
64 | /** Flat priority.
|
---|
65 | * Assumes a scheduling policy which puts the process at the default priority
|
---|
66 | * and with all thread at the same priority.
|
---|
67 | */
|
---|
68 | RTPROCPRIORITY_FLAT,
|
---|
69 | /** Low priority.
|
---|
70 | * Assumes a scheduling policy which puts the process mostly below the
|
---|
71 | * default priority of the host OS.
|
---|
72 | */
|
---|
73 | RTPROCPRIORITY_LOW,
|
---|
74 | /** Normal priority.
|
---|
75 | * Assume a scheduling policy which shares the CPU resources fairly with
|
---|
76 | * other processes running with the default priority of the host OS.
|
---|
77 | */
|
---|
78 | RTPROCPRIORITY_NORMAL,
|
---|
79 | /** High priority.
|
---|
80 | * Assumes a scheduling policy which puts the task above the default
|
---|
81 | * priority of the host OS. This policy might easily cause other tasks
|
---|
82 | * in the system to starve.
|
---|
83 | */
|
---|
84 | RTPROCPRIORITY_HIGH,
|
---|
85 | /** Last priority, used for validation. */
|
---|
86 | RTPROCPRIORITY_LAST
|
---|
87 | } RTPROCPRIORITY;
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get the current process identifier.
|
---|
92 | *
|
---|
93 | * @returns Process identifier.
|
---|
94 | */
|
---|
95 | RTDECL(RTPROCESS) RTProcSelf(void);
|
---|
96 |
|
---|
97 |
|
---|
98 | #ifdef IN_RING0
|
---|
99 | /**
|
---|
100 | * Get the current process handle.
|
---|
101 | *
|
---|
102 | * @returns Ring-0 process handle.
|
---|
103 | */
|
---|
104 | RTR0DECL(RTR0PROCESS) RTR0ProcHandleSelf(void);
|
---|
105 | #endif
|
---|
106 |
|
---|
107 |
|
---|
108 | #ifdef IN_RING3
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Attempts to alter the priority of the current process.
|
---|
112 | *
|
---|
113 | * @returns iprt status code.
|
---|
114 | * @param enmPriority The new priority.
|
---|
115 | */
|
---|
116 | RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority);
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Gets the current priority of this process.
|
---|
120 | *
|
---|
121 | * @returns The priority (see RTPROCPRIORITY).
|
---|
122 | */
|
---|
123 | RTR3DECL(RTPROCPRIORITY) RTProcGetPriority(void);
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Create a child process.
|
---|
127 | *
|
---|
128 | * @returns iprt status code.
|
---|
129 | * @param pszExec Executable image to use to create the child process.
|
---|
130 | * @param papszArgs Pointer to an array of arguments to the child. The array terminated by an entry containing NULL.
|
---|
131 | * @param Env Handle to the environment block for the child.
|
---|
132 | * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
|
---|
133 | * @param pProcess Where to store the process identifier on successful return.
|
---|
134 | * The content is not changed on failure. NULL is allowed.
|
---|
135 | */
|
---|
136 | RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess);
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Create a child process.
|
---|
141 | *
|
---|
142 | * @returns IPRT status code.
|
---|
143 | *
|
---|
144 | * @param pszExec Executable image to use to create the child process.
|
---|
145 | * @param papszArgs Pointer to an array of arguments to the child. The
|
---|
146 | * array terminated by an entry containing NULL.
|
---|
147 | * @param hEnv Handle to the environment block for the child. Pass
|
---|
148 | * RTENV_DEFAULT to use the environment of the current
|
---|
149 | * process.
|
---|
150 | * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
|
---|
151 | * @param phStdIn The standard in handle to assign the new process. Pass
|
---|
152 | * NULL to use the same as the current process. If the
|
---|
153 | * handle is NIL, we'll close the standard input of the
|
---|
154 | * guest.
|
---|
155 | * @param phStdOut The standard out handle to assign the new process. Pass
|
---|
156 | * NULL to use the same as the current process. If the
|
---|
157 | * handle is NIL, we'll close the standard output of the
|
---|
158 | * guest.
|
---|
159 | * @param phStdErr The standard error handle to assign the new process. Pass
|
---|
160 | * NULL to use the same as the current process. If the
|
---|
161 | * handle is NIL, we'll close the standard error of the
|
---|
162 | * guest.
|
---|
163 | * @param pszAsUser User to run the process as. Pass NULL to use the same
|
---|
164 | * user as the current process.
|
---|
165 | * Windows: Use user\@domain (UPN, User Principal Name)
|
---|
166 | * format to specify a domain.
|
---|
167 | * @param pszPassword Password to use to authenticate @a pszAsUser. Must be
|
---|
168 | * NULL wif pszAsUser is NULL. Whether this is actually
|
---|
169 | * used or not depends on the platform.
|
---|
170 | * @param phProcess Where to store the process handle on successful return.
|
---|
171 | * The content is not changed on failure. NULL is allowed.
|
---|
172 | *
|
---|
173 | * @remarks The handles does not have to be created as inheritable, but it
|
---|
174 | * doesn't hurt if they are as it may avoid race conditions on some
|
---|
175 | * platforms.
|
---|
176 | *
|
---|
177 | * @remarks The as-user feature isn't supported/implemented on all platforms and
|
---|
178 | * will cause a-yet-to-be-determined-error-status on these.
|
---|
179 | */
|
---|
180 | RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
|
---|
181 | PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
|
---|
182 | const char *pszPassword, PRTPROCESS phProcess);
|
---|
183 |
|
---|
184 | /** @name RTProcCreate and RTProcCreateEx flags
|
---|
185 | * @{ */
|
---|
186 | /** Detach the child process from the parents process tree and process group,
|
---|
187 | * session or/and console (depends on the platform what's done applicable).
|
---|
188 | *
|
---|
189 | * The new process will not be a direct decendent of the parent and it will not
|
---|
190 | * be possible to wait for it, i.e. @a phProcess shall be NULL. */
|
---|
191 | #define RTPROC_FLAGS_DETACHED RT_BIT(0)
|
---|
192 | /** Don't show the started process.
|
---|
193 | * This is a Windows (and maybe OS/2) concept, do not use on other platforms. */
|
---|
194 | #define RTPROC_FLAGS_HIDDEN RT_BIT(1)
|
---|
195 | /** Use special code path for starting child processes from a service (daemon).
|
---|
196 | * This is a windows concept for dealing with the so called "Session 0"
|
---|
197 | * isolation which was introduced with Windows Vista. Do not use on other
|
---|
198 | * platforms. */
|
---|
199 | #define RTPROC_FLAGS_SERVICE RT_BIT(2)
|
---|
200 | /** Suppress changing the process contract id for the child process
|
---|
201 | * on Solaris. Without this flag the contract id is always changed, as that's
|
---|
202 | * the more frequently used case. */
|
---|
203 | #define RTPROC_FLAGS_SAME_CONTRACT RT_BIT(3)
|
---|
204 | /** Load user profile data when executing a process.
|
---|
205 | * This redefines the meaning of RTENV_DEFAULT to the profile environment.
|
---|
206 | * @remarks On non-windows platforms, the resulting environment maybe very
|
---|
207 | * different from what you see in your shell. Among other reasons,
|
---|
208 | * we cannot run shell profile scripts which typically sets up the
|
---|
209 | * environment. */
|
---|
210 | #define RTPROC_FLAGS_PROFILE RT_BIT(4)
|
---|
211 | /** Create process without a console window.
|
---|
212 | * This is a Windows (and OS/2) concept, do not use on other platforms. */
|
---|
213 | #define RTPROC_FLAGS_NO_WINDOW RT_BIT(5)
|
---|
214 | /** Search the PATH for the executable. */
|
---|
215 | #define RTPROC_FLAGS_SEARCH_PATH RT_BIT(6)
|
---|
216 | /** Don't quote and escape arguments on Windows and similar platforms where a
|
---|
217 | * command line is passed to the child process instead of an argument vector,
|
---|
218 | * just join up argv with a space between each. Ignored on platforms
|
---|
219 | * passing argument the vector. */
|
---|
220 | #define RTPROC_FLAGS_UNQUOTED_ARGS RT_BIT(7)
|
---|
221 | /** Consider hEnv an environment change record to be applied to RTENV_DEFAULT.
|
---|
222 | * If hEnv is RTENV_DEFAULT, the flag has no effect. */
|
---|
223 | #define RTPROC_FLAGS_ENV_CHANGE_RECORD RT_BIT(8)
|
---|
224 | /** Valid flag mask. */
|
---|
225 | #define RTPROC_FLAGS_VALID_MASK UINT32_C(0x1ff)
|
---|
226 | /** @} */
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Process exit reason.
|
---|
231 | */
|
---|
232 | typedef enum RTPROCEXITREASON
|
---|
233 | {
|
---|
234 | /** Normal exit. iStatus contains the exit code. */
|
---|
235 | RTPROCEXITREASON_NORMAL = 1,
|
---|
236 | /** Any abnormal exit. iStatus is undefined. */
|
---|
237 | RTPROCEXITREASON_ABEND,
|
---|
238 | /** Killed by a signal. The iStatus field contains the signal number. */
|
---|
239 | RTPROCEXITREASON_SIGNAL
|
---|
240 | } RTPROCEXITREASON;
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Process exit status.
|
---|
244 | */
|
---|
245 | typedef struct RTPROCSTATUS
|
---|
246 | {
|
---|
247 | /** The process exit status if the exit was a normal one. */
|
---|
248 | int iStatus;
|
---|
249 | /** The reason the process terminated. */
|
---|
250 | RTPROCEXITREASON enmReason;
|
---|
251 | } RTPROCSTATUS;
|
---|
252 | /** Pointer to a process exit status structure. */
|
---|
253 | typedef RTPROCSTATUS *PRTPROCSTATUS;
|
---|
254 | /** Pointer to a const process exit status structure. */
|
---|
255 | typedef const RTPROCSTATUS *PCRTPROCSTATUS;
|
---|
256 |
|
---|
257 |
|
---|
258 | /** Flags for RTProcWait().
|
---|
259 | * @{ */
|
---|
260 | /** Block indefinitly waiting for the process to exit. */
|
---|
261 | #define RTPROCWAIT_FLAGS_BLOCK 0
|
---|
262 | /** Don't block, just check if the process have exited. */
|
---|
263 | #define RTPROCWAIT_FLAGS_NOBLOCK 1
|
---|
264 | /** @} */
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Waits for a process, resumes on interruption.
|
---|
268 | *
|
---|
269 | * @returns VINF_SUCCESS when the status code for the process was collected and
|
---|
270 | * put in *pProcStatus.
|
---|
271 | * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
|
---|
272 | * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
|
---|
273 | * process haven't exited yet.
|
---|
274 | *
|
---|
275 | * @param Process The process to wait for.
|
---|
276 | * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
|
---|
277 | * @param pProcStatus Where to store the exit status on success.
|
---|
278 | * Optional.
|
---|
279 | */
|
---|
280 | RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Waits for a process, returns on interruption.
|
---|
284 | *
|
---|
285 | * @returns VINF_SUCCESS when the status code for the process was collected and
|
---|
286 | * put in *pProcStatus.
|
---|
287 | * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
|
---|
288 | * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
|
---|
289 | * process haven't exited yet.
|
---|
290 | * @returns VERR_INTERRUPTED when the wait was interrupted by the arrival of a
|
---|
291 | * signal or other async event.
|
---|
292 | *
|
---|
293 | * @param Process The process to wait for.
|
---|
294 | * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
|
---|
295 | * @param pProcStatus Where to store the exit status on success.
|
---|
296 | * Optional.
|
---|
297 | */
|
---|
298 | RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Terminates (kills) a running process.
|
---|
302 | *
|
---|
303 | * @returns IPRT status code.
|
---|
304 | * @param Process The process to terminate.
|
---|
305 | */
|
---|
306 | RTR3DECL(int) RTProcTerminate(RTPROCESS Process);
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Gets the processor affinity mask of the current process.
|
---|
310 | *
|
---|
311 | * @returns The affinity mask.
|
---|
312 | */
|
---|
313 | RTR3DECL(uint64_t) RTProcGetAffinityMask(void);
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Gets the short process name.
|
---|
317 | *
|
---|
318 | * @returns Pointer to read-only name string.
|
---|
319 | */
|
---|
320 | RTR3DECL(const char *) RTProcShortName(void);
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Gets the path to the executable image of the current process.
|
---|
324 | *
|
---|
325 | * @returns pszExecPath on success. NULL on buffer overflow or other errors.
|
---|
326 | *
|
---|
327 | * @param pszExecPath Where to store the path.
|
---|
328 | * @param cbExecPath The size of the buffer.
|
---|
329 | */
|
---|
330 | RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath);
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Daemonize the current process, making it a background process.
|
---|
334 | *
|
---|
335 | * The way this work is that it will spawn a detached / backgrounded /
|
---|
336 | * daemonized / call-it-what-you-want process that isn't a direct child of the
|
---|
337 | * current process. The spawned will have the same arguments a the caller,
|
---|
338 | * except that the @a pszDaemonizedOpt is appended to prevent that the new
|
---|
339 | * process calls this API again.
|
---|
340 | *
|
---|
341 | * The new process will have the standard handles directed to/from the
|
---|
342 | * bitbucket.
|
---|
343 | *
|
---|
344 | * @returns IPRT status code. On success it is normal for the caller to exit
|
---|
345 | * the process by returning from main().
|
---|
346 | *
|
---|
347 | * @param papszArgs The argument vector of the calling process.
|
---|
348 | * @param pszDaemonizedOpt The daemonized option. This is appended to the
|
---|
349 | * end of the parameter list of the daemonized process.
|
---|
350 | */
|
---|
351 | RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt);
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Daemonize the current process, making it a background process. The current
|
---|
355 | * process will exit if daemonizing is successful.
|
---|
356 | *
|
---|
357 | * @returns IPRT status code. On success it will only return in the child
|
---|
358 | * process, the parent will exit. On failure, it will return in the
|
---|
359 | * parent process and no child has been spawned.
|
---|
360 | *
|
---|
361 | * @param fNoChDir Pass false to change working directory to "/".
|
---|
362 | * @param fNoClose Pass false to redirect standard file streams to the null device.
|
---|
363 | * @param pszPidfile Path to a file to write the process id of the daemon
|
---|
364 | * process to. Daemonizing will fail if this file already
|
---|
365 | * exists or cannot be written. May be NULL.
|
---|
366 | */
|
---|
367 | RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile);
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Check if the given process is running on the system.
|
---|
371 | *
|
---|
372 | * This check is case sensitive on most systems, except for Windows, OS/2 and
|
---|
373 | * Darwin.
|
---|
374 | *
|
---|
375 | * @returns true if the process is running & false otherwise.
|
---|
376 | * @param pszName Process name to search for. If no path is given only the
|
---|
377 | * filename part of the running process set will be
|
---|
378 | * matched. If a path is specified, the full path will be
|
---|
379 | * matched.
|
---|
380 | */
|
---|
381 | RTR3DECL(bool) RTProcIsRunningByName(const char *pszName);
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Queries the parent process ID.
|
---|
385 | *
|
---|
386 | * @returns IPRT status code
|
---|
387 | * @param hProcess The process to query the parent of.
|
---|
388 | * @param phParent Where to return the parent process ID.
|
---|
389 | */
|
---|
390 | RTR3DECL(int) RTProcQueryParent(RTPROCESS hProcess, PRTPROCESS phParent);
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Query the username of the given process.
|
---|
394 | *
|
---|
395 | * @returns IPRT status code.
|
---|
396 | * @retval VERR_BUFFER_OVERFLOW if the given buffer size is to small for the username.
|
---|
397 | * @param hProcess The process handle to query the username for.
|
---|
398 | * NIL_PROCESS is an alias for the current process.
|
---|
399 | * @param pszUser Where to store the user name on success.
|
---|
400 | * @param cbUser The size of the user name buffer.
|
---|
401 | * @param pcbUser Where to store the username length on success
|
---|
402 | * or the required buffer size if VERR_BUFFER_OVERFLOW
|
---|
403 | * is returned.
|
---|
404 | */
|
---|
405 | RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser, size_t *pcbUser);
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Query the username of the given process allocating the string for the username.
|
---|
409 | *
|
---|
410 | * @returns IPRT status code.
|
---|
411 | * @param hProcess The process handle to query the username for.
|
---|
412 | * @param ppszUser Where to store the pointer to the string containing
|
---|
413 | * the username on success. Free with RTStrFree().
|
---|
414 | */
|
---|
415 | RTR3DECL(int) RTProcQueryUsernameA(RTPROCESS hProcess, char **ppszUser);
|
---|
416 |
|
---|
417 | #endif /* IN_RING3 */
|
---|
418 |
|
---|
419 | /** @} */
|
---|
420 |
|
---|
421 | RT_C_DECLS_END
|
---|
422 |
|
---|
423 | #endif /* !IPRT_INCLUDED_process_h */
|
---|
424 |
|
---|