VirtualBox

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

Last change on this file since 80536 was 80481, checked in by vboxsync, 5 years ago

IPRT/process-win.cpp: Adding RTPROC_FLAGS_AS_IMPERSONATED_TOKEN and RTPROC_FLAGS_NO_WAIT for windows. bugref:9341 bugref:8078

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.6 KB
Line 
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
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 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 */
180RTR3DECL(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/** 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/** Valid flag mask. */
231#define RTPROC_FLAGS_VALID_MASK UINT32_C(0x7ff)
232/** @} */
233
234
235/**
236 * Process exit reason.
237 */
238typedef enum RTPROCEXITREASON
239{
240 /** Normal exit. iStatus contains the exit code. */
241 RTPROCEXITREASON_NORMAL = 1,
242 /** Any abnormal exit. iStatus is undefined. */
243 RTPROCEXITREASON_ABEND,
244 /** Killed by a signal. The iStatus field contains the signal number. */
245 RTPROCEXITREASON_SIGNAL
246} RTPROCEXITREASON;
247
248/**
249 * Process exit status.
250 */
251typedef struct RTPROCSTATUS
252{
253 /** The process exit status if the exit was a normal one. */
254 int iStatus;
255 /** The reason the process terminated. */
256 RTPROCEXITREASON enmReason;
257} RTPROCSTATUS;
258/** Pointer to a process exit status structure. */
259typedef RTPROCSTATUS *PRTPROCSTATUS;
260/** Pointer to a const process exit status structure. */
261typedef const RTPROCSTATUS *PCRTPROCSTATUS;
262
263
264/** Flags for RTProcWait().
265 * @{ */
266/** Block indefinitly waiting for the process to exit. */
267#define RTPROCWAIT_FLAGS_BLOCK 0
268/** Don't block, just check if the process have exited. */
269#define RTPROCWAIT_FLAGS_NOBLOCK 1
270/** @} */
271
272/**
273 * Waits for a process, resumes on interruption.
274 *
275 * @returns VINF_SUCCESS when the status code for the process was collected and
276 * put in *pProcStatus.
277 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
278 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
279 * process haven't exited yet.
280 *
281 * @param Process The process to wait for.
282 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
283 * @param pProcStatus Where to store the exit status on success.
284 * Optional.
285 */
286RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
287
288/**
289 * Waits for a process, returns on interruption.
290 *
291 * @returns VINF_SUCCESS when the status code for the process was collected and
292 * put in *pProcStatus.
293 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
294 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
295 * process haven't exited yet.
296 * @returns VERR_INTERRUPTED when the wait was interrupted by the arrival of a
297 * signal or other async event.
298 *
299 * @param Process The process to wait for.
300 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
301 * @param pProcStatus Where to store the exit status on success.
302 * Optional.
303 */
304RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
305
306/**
307 * Terminates (kills) a running process.
308 *
309 * @returns IPRT status code.
310 * @param Process The process to terminate.
311 */
312RTR3DECL(int) RTProcTerminate(RTPROCESS Process);
313
314/**
315 * Gets the processor affinity mask of the current process.
316 *
317 * @returns The affinity mask.
318 */
319RTR3DECL(uint64_t) RTProcGetAffinityMask(void);
320
321/**
322 * Gets the short process name.
323 *
324 * @returns Pointer to read-only name string.
325 */
326RTR3DECL(const char *) RTProcShortName(void);
327
328/**
329 * Gets the path to the executable image of the current process.
330 *
331 * @returns pszExecPath on success. NULL on buffer overflow or other errors.
332 *
333 * @param pszExecPath Where to store the path.
334 * @param cbExecPath The size of the buffer.
335 */
336RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath);
337
338/**
339 * Daemonize the current process, making it a background process.
340 *
341 * The way this work is that it will spawn a detached / backgrounded /
342 * daemonized / call-it-what-you-want process that isn't a direct child of the
343 * current process. The spawned will have the same arguments a the caller,
344 * except that the @a pszDaemonizedOpt is appended to prevent that the new
345 * process calls this API again.
346 *
347 * The new process will have the standard handles directed to/from the
348 * bitbucket.
349 *
350 * @returns IPRT status code. On success it is normal for the caller to exit
351 * the process by returning from main().
352 *
353 * @param papszArgs The argument vector of the calling process.
354 * @param pszDaemonizedOpt The daemonized option. This is appended to the
355 * end of the parameter list of the daemonized process.
356 */
357RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt);
358
359/**
360 * Daemonize the current process, making it a background process. The current
361 * process will exit if daemonizing is successful.
362 *
363 * @returns IPRT status code. On success it will only return in the child
364 * process, the parent will exit. On failure, it will return in the
365 * parent process and no child has been spawned.
366 *
367 * @param fNoChDir Pass false to change working directory to "/".
368 * @param fNoClose Pass false to redirect standard file streams to the null device.
369 * @param pszPidfile Path to a file to write the process id of the daemon
370 * process to. Daemonizing will fail if this file already
371 * exists or cannot be written. May be NULL.
372 */
373RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile);
374
375/**
376 * Check if the given process is running on the system.
377 *
378 * This check is case sensitive on most systems, except for Windows, OS/2 and
379 * Darwin.
380 *
381 * @returns true if the process is running & false otherwise.
382 * @param pszName Process name to search for. If no path is given only the
383 * filename part of the running process set will be
384 * matched. If a path is specified, the full path will be
385 * matched.
386 */
387RTR3DECL(bool) RTProcIsRunningByName(const char *pszName);
388
389/**
390 * Queries the parent process ID.
391 *
392 * @returns IPRT status code
393 * @param hProcess The process to query the parent of.
394 * @param phParent Where to return the parent process ID.
395 */
396RTR3DECL(int) RTProcQueryParent(RTPROCESS hProcess, PRTPROCESS phParent);
397
398/**
399 * Query the username of the given process.
400 *
401 * @returns IPRT status code.
402 * @retval VERR_BUFFER_OVERFLOW if the given buffer size is to small for the username.
403 * @param hProcess The process handle to query the username for.
404 * NIL_PROCESS is an alias for the current process.
405 * @param pszUser Where to store the user name on success.
406 * @param cbUser The size of the user name buffer.
407 * @param pcbUser Where to store the username length on success
408 * or the required buffer size if VERR_BUFFER_OVERFLOW
409 * is returned.
410 */
411RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser, size_t *pcbUser);
412
413/**
414 * Query the username of the given process allocating the string for the username.
415 *
416 * @returns IPRT status code.
417 * @param hProcess The process handle to query the username for.
418 * @param ppszUser Where to store the pointer to the string containing
419 * the username on success. Free with RTStrFree().
420 */
421RTR3DECL(int) RTProcQueryUsernameA(RTPROCESS hProcess, char **ppszUser);
422
423#endif /* IN_RING3 */
424
425/** @} */
426
427RT_C_DECLS_END
428
429#endif /* !IPRT_INCLUDED_process_h */
430
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