VirtualBox

source: vbox/trunk/include/iprt/process.h@ 95934

Last change on this file since 95934 was 95274, checked in by vboxsync, 2 years ago

FE/VBoxAutostart/adi: Added experimental support for running VBoxSVC session 0 (service session). Did some minor cleanup for the patch posted at comment 95. Disabled by default for now. bugref:9341

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1/** @file
2 * IPRT - Process Management.
3 */
4
5/*
6 * Copyright (C) 2006-2022 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
35RT_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 */
54typedef 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 */
95RTDECL(RTPROCESS) RTProcSelf(void);
96
97
98#ifdef IN_RING0
99/**
100 * Get the current process handle.
101 *
102 * @returns Ring-0 process handle.
103 */
104RTR0DECL(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 */
116RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority);
117
118/**
119 * Gets the current priority of this process.
120 *
121 * @returns The priority (see RTPROCPRIORITY).
122 */
123RTR3DECL(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 */
136RTR3DECL(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 pvExtraData Points to additional data as per @a fFlags:
171 * - RTPROC_FLAGS_DESIRED_SESSION_ID: Pointing to a
172 * uint32_t variable with the desired session ID.
173 * @param phProcess Where to store the process handle on successful return.
174 * The content is not changed on failure. NULL is allowed.
175 *
176 * @remarks The handles does not have to be created as inheritable, but it
177 * doesn't hurt if they are as it may avoid race conditions on some
178 * platforms.
179 *
180 * @remarks The as-user feature isn't supported/implemented on all platforms and
181 * will cause a-yet-to-be-determined-error-status on these.
182 */
183RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
184 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
185 const char *pszPassword, void *pvExtraData, PRTPROCESS phProcess);
186
187/** @name RTProcCreate and RTProcCreateEx flags
188 * @{ */
189/** Detach the child process from the parents process tree and process group,
190 * session or/and console (depends on the platform what's done applicable).
191 *
192 * The new process will not be a direct decendent of the parent and it will not
193 * be possible to wait for it, i.e. @a phProcess shall be NULL. */
194#define RTPROC_FLAGS_DETACHED RT_BIT(0)
195/** Don't show the started process.
196 * This is a Windows (and maybe OS/2) concept, do not use on other platforms. */
197#define RTPROC_FLAGS_HIDDEN RT_BIT(1)
198/** Use special code path for starting child processes from a service (daemon).
199 * This is a windows concept for dealing with the so called "Session 0"
200 * isolation which was introduced with Windows Vista. Do not use on other
201 * platforms. */
202#define RTPROC_FLAGS_SERVICE RT_BIT(2)
203/** Suppress changing the process contract id for the child process
204 * on Solaris. Without this flag the contract id is always changed, as that's
205 * the more frequently used case. */
206#define RTPROC_FLAGS_SAME_CONTRACT RT_BIT(3)
207/** Load user profile data when executing a process.
208 * This redefines the meaning of RTENV_DEFAULT to the profile environment. See
209 * also RTPROC_FLAGS_ONLY_BASIC_PROFILE */
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/** Create process using the current impersonated thread token.
225 * Caller should also specify RTPROC_FLAGS_SERVICE and RTPROC_FLAGS_PROFILE.
226 * Windows only flag, ignored everywhere else. */
227#define RTPROC_FLAGS_AS_IMPERSONATED_TOKEN RT_BIT(9)
228/** Hint that we don't expect to ever want to wait on the process. */
229#define RTPROC_FLAGS_NO_WAIT RT_BIT(10)
230/** For use with RTPROC_FLAGS_SERVICE to specify a desired session ID
231 * (Windows only, ignored elsewhere). The @a pvExtraData argument points to
232 * a uint32_t containing the session ID, UINT32_MAX means any session.
233 * Can not be set with RTPROC_FLAGS_TOKEN_SUPPLIED */
234#define RTPROC_FLAGS_DESIRED_SESSION_ID RT_BIT(11)
235/** This is a modifier to RTPROC_FLAGS_PROFILE on unix systems that makes it
236 * skip trying to dump the environment of a login shell. */
237#define RTPROC_FLAGS_ONLY_BASIC_PROFILE RT_BIT(12)
238/** Don't translate arguments to the (guessed) child process codeset.
239 * This is ignored on Windows as it is using UTF-16. */
240#define RTPROC_FLAGS_UTF8_ARGV RT_BIT_32(13)
241/** Create process using supplied token. The @a pvExtraData argument points to
242 * a HANDLE containing the token used as user credentials for process creation.
243 * Can not be set with RTPROC_FLAGS_DESIRED_SESSION_ID.
244 * Windows only flag, ignored everywhere else. */
245#define RTPROC_FLAGS_TOKEN_SUPPLIED RT_BIT(14)
246
247/** Valid flag mask. */
248#define RTPROC_FLAGS_VALID_MASK UINT32_C(0x7fff)
249/** @} */
250
251
252/**
253 * Process exit reason.
254 */
255typedef enum RTPROCEXITREASON
256{
257 /** Normal exit. iStatus contains the exit code. */
258 RTPROCEXITREASON_NORMAL = 1,
259 /** Any abnormal exit. iStatus is undefined. */
260 RTPROCEXITREASON_ABEND,
261 /** Killed by a signal. The iStatus field contains the signal number. */
262 RTPROCEXITREASON_SIGNAL
263} RTPROCEXITREASON;
264
265/**
266 * Process exit status.
267 */
268typedef struct RTPROCSTATUS
269{
270 /** The process exit status if the exit was a normal one. */
271 int iStatus;
272 /** The reason the process terminated. */
273 RTPROCEXITREASON enmReason;
274} RTPROCSTATUS;
275/** Pointer to a process exit status structure. */
276typedef RTPROCSTATUS *PRTPROCSTATUS;
277/** Pointer to a const process exit status structure. */
278typedef const RTPROCSTATUS *PCRTPROCSTATUS;
279
280
281/** Flags for RTProcWait().
282 * @{ */
283/** Block indefinitly waiting for the process to exit. */
284#define RTPROCWAIT_FLAGS_BLOCK 0
285/** Don't block, just check if the process have exited. */
286#define RTPROCWAIT_FLAGS_NOBLOCK 1
287/** @} */
288
289/**
290 * Waits for a process, resumes on interruption.
291 *
292 * @returns VINF_SUCCESS when the status code for the process was collected and
293 * put in *pProcStatus.
294 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
295 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
296 * process haven't exited yet.
297 *
298 * @param Process The process to wait for.
299 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
300 * @param pProcStatus Where to store the exit status on success.
301 * Optional.
302 */
303RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
304
305/**
306 * Waits for a process, returns on interruption.
307 *
308 * @returns VINF_SUCCESS when the status code for the process was collected and
309 * put in *pProcStatus.
310 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
311 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
312 * process haven't exited yet.
313 * @returns VERR_INTERRUPTED when the wait was interrupted by the arrival of a
314 * signal or other async event.
315 *
316 * @param Process The process to wait for.
317 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
318 * @param pProcStatus Where to store the exit status on success.
319 * Optional.
320 */
321RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
322
323/**
324 * Terminates (kills) a running process.
325 *
326 * @returns IPRT status code.
327 * @param Process The process to terminate.
328 */
329RTR3DECL(int) RTProcTerminate(RTPROCESS Process);
330
331/**
332 * Gets the processor affinity mask of the current process.
333 *
334 * @returns The affinity mask.
335 */
336RTR3DECL(uint64_t) RTProcGetAffinityMask(void);
337
338/**
339 * Gets the short process name.
340 *
341 * @returns Pointer to read-only name string.
342 * @note IPRT must've been initialized or the string will be empty.
343 */
344RTR3DECL(const char *) RTProcShortName(void);
345
346/**
347 * Gets the path to the executable image of the current process.
348 *
349 * @returns Pointer to read-only path string.
350 * @note IPRT must've been initialized or the string will be empty.
351 */
352RTR3DECL(const char *) RTProcExecutablePath(void);
353
354/**
355 * Gets a copy of the path to the executable image of the current process.
356 *
357 * @returns pszExecPath on success. NULL on buffer overflow or other errors.
358 *
359 * @param pszExecPath Where to store the path.
360 * @param cbExecPath The size of the buffer.
361 * @note IPRT must've been initialized or the string will be empty.
362 */
363RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath);
364
365/**
366 * Daemonize the current process, making it a background process.
367 *
368 * The way this work is that it will spawn a detached / backgrounded /
369 * daemonized / call-it-what-you-want process that isn't a direct child of the
370 * current process. The spawned will have the same arguments a the caller,
371 * except that the @a pszDaemonizedOpt is appended to prevent that the new
372 * process calls this API again.
373 *
374 * The new process will have the standard handles directed to/from the
375 * bitbucket.
376 *
377 * @returns IPRT status code. On success it is normal for the caller to exit
378 * the process by returning from main().
379 *
380 * @param papszArgs The argument vector of the calling process.
381 * @param pszDaemonizedOpt The daemonized option. This is appended to the
382 * end of the parameter list of the daemonized process.
383 */
384RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt);
385
386/**
387 * Daemonize the current process, making it a background process. The current
388 * process will exit if daemonizing is successful.
389 *
390 * @returns IPRT status code. On success it will only return in the child
391 * process, the parent will exit. On failure, it will return in the
392 * parent process and no child has been spawned.
393 *
394 * @param fNoChDir Pass false to change working directory to "/".
395 * @param fNoClose Pass false to redirect standard file streams to the null device.
396 * @param pszPidfile Path to a file to write the process id of the daemon
397 * process to. Daemonizing will fail if this file already
398 * exists or cannot be written. May be NULL.
399 */
400RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile);
401
402/**
403 * Check if the given process is running on the system.
404 *
405 * This check is case sensitive on most systems, except for Windows, OS/2 and
406 * Darwin.
407 *
408 * @returns true if the process is running & false otherwise.
409 * @param pszName Process name to search for. If no path is given only the
410 * filename part of the running process set will be
411 * matched. If a path is specified, the full path will be
412 * matched.
413 */
414RTR3DECL(bool) RTProcIsRunningByName(const char *pszName);
415
416/**
417 * Queries the parent process ID.
418 *
419 * @returns IPRT status code
420 * @param hProcess The process to query the parent of.
421 * @param phParent Where to return the parent process ID.
422 */
423RTR3DECL(int) RTProcQueryParent(RTPROCESS hProcess, PRTPROCESS phParent);
424
425/**
426 * Query the username of the given process.
427 *
428 * @returns IPRT status code.
429 * @retval VERR_BUFFER_OVERFLOW if the given buffer size is to small for the username.
430 * @param hProcess The process handle to query the username for.
431 * NIL_PROCESS is an alias for the current process.
432 * @param pszUser Where to store the user name on success.
433 * @param cbUser The size of the user name buffer.
434 * @param pcbUser Where to store the username length on success
435 * or the required buffer size if VERR_BUFFER_OVERFLOW
436 * is returned.
437 */
438RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser, size_t *pcbUser);
439
440/**
441 * Query the username of the given process allocating the string for the username.
442 *
443 * @returns IPRT status code.
444 * @param hProcess The process handle to query the username for.
445 * @param ppszUser Where to store the pointer to the string containing
446 * the username on success. Free with RTStrFree().
447 */
448RTR3DECL(int) RTProcQueryUsernameA(RTPROCESS hProcess, char **ppszUser);
449
450#endif /* IN_RING3 */
451
452/** @} */
453
454RT_C_DECLS_END
455
456#endif /* !IPRT_INCLUDED_process_h */
457
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette