VirtualBox

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

Last change on this file since 98009 was 97646, checked in by vboxsync, 2 years ago

IPRT: Added RTProcSignalName and a generic implementation of it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 KB
Line 
1/** @file
2 * IPRT - Process Management.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_process_h
37#define IPRT_INCLUDED_process_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43#include <iprt/types.h>
44
45RT_C_DECLS_BEGIN
46
47/** @defgroup grp_rt_process RTProc - Process Management
48 * @ingroup grp_rt
49 * @{
50 */
51
52
53/**
54 * Process priority.
55 *
56 * The process priority is used to select how scheduling properties
57 * are assigned to the different thread types (see THREADTYPE).
58 *
59 * In addition to using the policy assigned to the process at startup (DEFAULT)
60 * it is possible to change the process priority at runtime. This allows for
61 * a GUI, resource manager or admin to adjust the general priority of a task
62 * without upsetting the fine-tuned priority of the threads within.
63 */
64typedef enum RTPROCPRIORITY
65{
66 /** Invalid priority. */
67 RTPROCPRIORITY_INVALID = 0,
68 /** Default priority.
69 * Derive the scheduling policy from the priority of the RTR3Init()
70 * and RTProcSetPriority() callers and the rights the process have
71 * to alter its own priority.
72 */
73 RTPROCPRIORITY_DEFAULT,
74 /** Flat priority.
75 * Assumes a scheduling policy which puts the process at the default priority
76 * and with all thread at the same priority.
77 */
78 RTPROCPRIORITY_FLAT,
79 /** Low priority.
80 * Assumes a scheduling policy which puts the process mostly below the
81 * default priority of the host OS.
82 */
83 RTPROCPRIORITY_LOW,
84 /** Normal priority.
85 * Assume a scheduling policy which shares the CPU resources fairly with
86 * other processes running with the default priority of the host OS.
87 */
88 RTPROCPRIORITY_NORMAL,
89 /** High priority.
90 * Assumes a scheduling policy which puts the task above the default
91 * priority of the host OS. This policy might easily cause other tasks
92 * in the system to starve.
93 */
94 RTPROCPRIORITY_HIGH,
95 /** Last priority, used for validation. */
96 RTPROCPRIORITY_LAST
97} RTPROCPRIORITY;
98
99
100/**
101 * Get the current process identifier.
102 *
103 * @returns Process identifier.
104 */
105RTDECL(RTPROCESS) RTProcSelf(void);
106
107
108#ifdef IN_RING0
109/**
110 * Get the current process handle.
111 *
112 * @returns Ring-0 process handle.
113 */
114RTR0DECL(RTR0PROCESS) RTR0ProcHandleSelf(void);
115#endif
116
117
118/**
119 * Translate a signal number to a short name on the form SIGXXXX.
120 *
121 * If the signal is not known, it will be formatted as a number into one of
122 * several static buffers. This means that there could be concurrency issues if
123 * this suddenly happens on multiple threads, though that is unlikely.
124 *
125 * @returns Readonly string naming the signal.
126 * @param iSignal The signal to name.
127 */
128RTDECL(const char *) RTProcSignalName(int iSignal);
129
130#ifdef IN_RING3
131
132/**
133 * Attempts to alter the priority of the current process.
134 *
135 * @returns iprt status code.
136 * @param enmPriority The new priority.
137 */
138RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority);
139
140/**
141 * Gets the current priority of this process.
142 *
143 * @returns The priority (see RTPROCPRIORITY).
144 */
145RTR3DECL(RTPROCPRIORITY) RTProcGetPriority(void);
146
147/**
148 * Create a child process.
149 *
150 * @returns iprt status code.
151 * @param pszExec Executable image to use to create the child process.
152 * @param papszArgs Pointer to an array of arguments to the child. The array terminated by an entry containing NULL.
153 * @param Env Handle to the environment block for the child.
154 * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
155 * @param pProcess Where to store the process identifier on successful return.
156 * The content is not changed on failure. NULL is allowed.
157 */
158RTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, RTENV Env, unsigned fFlags, PRTPROCESS pProcess);
159
160
161/**
162 * Create a child process.
163 *
164 * @returns IPRT status code.
165 *
166 * @param pszExec Executable image to use to create the child process.
167 * @param papszArgs Pointer to an array of arguments to the child. The
168 * array terminated by an entry containing NULL.
169 * @param hEnv Handle to the environment block for the child. Pass
170 * RTENV_DEFAULT to use the environment of the current
171 * process.
172 * @param fFlags Flags, one of the RTPROC_FLAGS_* defines.
173 * @param phStdIn The standard in handle to assign the new process. Pass
174 * NULL to use the same as the current process. If the
175 * handle is NIL, we'll close the standard input of the
176 * guest.
177 * @param phStdOut The standard out handle to assign the new process. Pass
178 * NULL to use the same as the current process. If the
179 * handle is NIL, we'll close the standard output of the
180 * guest.
181 * @param phStdErr The standard error handle to assign the new process. Pass
182 * NULL to use the same as the current process. If the
183 * handle is NIL, we'll close the standard error of the
184 * guest.
185 * @param pszAsUser User to run the process as. Pass NULL to use the same
186 * user as the current process.
187 * Windows: Use user\@domain (UPN, User Principal Name)
188 * format to specify a domain.
189 * @param pszPassword Password to use to authenticate @a pszAsUser. Must be
190 * NULL wif pszAsUser is NULL. Whether this is actually
191 * used or not depends on the platform.
192 * @param pvExtraData Points to additional data as per @a fFlags:
193 * - RTPROC_FLAGS_DESIRED_SESSION_ID: Pointing to a
194 * uint32_t variable with the desired session ID.
195 * @param phProcess Where to store the process handle on successful return.
196 * The content is not changed on failure. NULL is allowed.
197 *
198 * @remarks The handles does not have to be created as inheritable, but it
199 * doesn't hurt if they are as it may avoid race conditions on some
200 * platforms.
201 *
202 * @remarks The as-user feature isn't supported/implemented on all platforms and
203 * will cause a-yet-to-be-determined-error-status on these.
204 */
205RTR3DECL(int) RTProcCreateEx(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
206 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
207 const char *pszPassword, void *pvExtraData, PRTPROCESS phProcess);
208
209/** @name RTProcCreate and RTProcCreateEx flags
210 * @{ */
211/** Detach the child process from the parents process tree and process group,
212 * session or/and console (depends on the platform what's done applicable).
213 *
214 * The new process will not be a direct decendent of the parent and it will not
215 * be possible to wait for it, i.e. @a phProcess shall be NULL. */
216#define RTPROC_FLAGS_DETACHED RT_BIT(0)
217/** Don't show the started process.
218 * This is a Windows (and maybe OS/2) concept, do not use on other platforms. */
219#define RTPROC_FLAGS_HIDDEN RT_BIT(1)
220/** Use special code path for starting child processes from a service (daemon).
221 * This is a windows concept for dealing with the so called "Session 0"
222 * isolation which was introduced with Windows Vista. Do not use on other
223 * platforms. */
224#define RTPROC_FLAGS_SERVICE RT_BIT(2)
225/** Suppress changing the process contract id for the child process
226 * on Solaris. Without this flag the contract id is always changed, as that's
227 * the more frequently used case. */
228#define RTPROC_FLAGS_SAME_CONTRACT RT_BIT(3)
229/** Load user profile data when executing a process.
230 * This redefines the meaning of RTENV_DEFAULT to the profile environment. See
231 * also RTPROC_FLAGS_ONLY_BASIC_PROFILE */
232#define RTPROC_FLAGS_PROFILE RT_BIT(4)
233/** Create process without a console window.
234 * This is a Windows (and OS/2) concept, do not use on other platforms. */
235#define RTPROC_FLAGS_NO_WINDOW RT_BIT(5)
236/** Search the PATH for the executable. */
237#define RTPROC_FLAGS_SEARCH_PATH RT_BIT(6)
238/** Don't quote and escape arguments on Windows and similar platforms where a
239 * command line is passed to the child process instead of an argument vector,
240 * just join up argv with a space between each. Ignored on platforms
241 * passing argument the vector. */
242#define RTPROC_FLAGS_UNQUOTED_ARGS RT_BIT(7)
243/** Consider hEnv an environment change record to be applied to RTENV_DEFAULT.
244 * If hEnv is RTENV_DEFAULT, the flag has no effect. */
245#define RTPROC_FLAGS_ENV_CHANGE_RECORD RT_BIT(8)
246/** Create process using the current impersonated thread token.
247 * Caller should also specify RTPROC_FLAGS_SERVICE and RTPROC_FLAGS_PROFILE.
248 * Windows only flag, ignored everywhere else. */
249#define RTPROC_FLAGS_AS_IMPERSONATED_TOKEN RT_BIT(9)
250/** Hint that we don't expect to ever want to wait on the process. */
251#define RTPROC_FLAGS_NO_WAIT RT_BIT(10)
252/** For use with RTPROC_FLAGS_SERVICE to specify a desired session ID
253 * (Windows only, ignored elsewhere). The @a pvExtraData argument points to
254 * a uint32_t containing the session ID, UINT32_MAX means any session.
255 * Can not be set with RTPROC_FLAGS_TOKEN_SUPPLIED */
256#define RTPROC_FLAGS_DESIRED_SESSION_ID RT_BIT(11)
257/** This is a modifier to RTPROC_FLAGS_PROFILE on unix systems that makes it
258 * skip trying to dump the environment of a login shell. */
259#define RTPROC_FLAGS_ONLY_BASIC_PROFILE RT_BIT(12)
260/** Don't translate arguments to the (guessed) child process codeset.
261 * This is ignored on Windows as it is using UTF-16. */
262#define RTPROC_FLAGS_UTF8_ARGV RT_BIT_32(13)
263/** Create process using supplied token. The @a pvExtraData argument points to
264 * a HANDLE containing the token used as user credentials for process creation.
265 * Can not be set with RTPROC_FLAGS_DESIRED_SESSION_ID.
266 * Windows only flag, ignored everywhere else. */
267#define RTPROC_FLAGS_TOKEN_SUPPLIED RT_BIT(14)
268
269/** Valid flag mask. */
270#define RTPROC_FLAGS_VALID_MASK UINT32_C(0x7fff)
271/** @} */
272
273
274/**
275 * Process exit reason.
276 */
277typedef enum RTPROCEXITREASON
278{
279 /** Normal exit. iStatus contains the exit code. */
280 RTPROCEXITREASON_NORMAL = 1,
281 /** Any abnormal exit. iStatus is undefined. */
282 RTPROCEXITREASON_ABEND,
283 /** Killed by a signal. The iStatus field contains the signal number. */
284 RTPROCEXITREASON_SIGNAL
285} RTPROCEXITREASON;
286
287/**
288 * Process exit status.
289 */
290typedef struct RTPROCSTATUS
291{
292 /** The process exit status if the exit was a normal one. */
293 int iStatus;
294 /** The reason the process terminated. */
295 RTPROCEXITREASON enmReason;
296} RTPROCSTATUS;
297/** Pointer to a process exit status structure. */
298typedef RTPROCSTATUS *PRTPROCSTATUS;
299/** Pointer to a const process exit status structure. */
300typedef const RTPROCSTATUS *PCRTPROCSTATUS;
301
302
303/** Flags for RTProcWait().
304 * @{ */
305/** Block indefinitly waiting for the process to exit. */
306#define RTPROCWAIT_FLAGS_BLOCK 0
307/** Don't block, just check if the process have exited. */
308#define RTPROCWAIT_FLAGS_NOBLOCK 1
309/** @} */
310
311/**
312 * Waits for a process, resumes on interruption.
313 *
314 * @returns VINF_SUCCESS when the status code for the process was collected and
315 * put in *pProcStatus.
316 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
317 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
318 * process haven't exited yet.
319 *
320 * @param Process The process to wait for.
321 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
322 * @param pProcStatus Where to store the exit status on success.
323 * Optional.
324 */
325RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
326
327/**
328 * Waits for a process, returns on interruption.
329 *
330 * @returns VINF_SUCCESS when the status code for the process was collected and
331 * put in *pProcStatus.
332 * @returns VERR_PROCESS_NOT_FOUND if the specified process wasn't found.
333 * @returns VERR_PROCESS_RUNNING when the RTPROCWAIT_FLAGS_NOBLOCK and the
334 * process haven't exited yet.
335 * @returns VERR_INTERRUPTED when the wait was interrupted by the arrival of a
336 * signal or other async event.
337 *
338 * @param Process The process to wait for.
339 * @param fFlags The wait flags, any of the RTPROCWAIT_FLAGS_ \#defines.
340 * @param pProcStatus Where to store the exit status on success.
341 * Optional.
342 */
343RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus);
344
345/**
346 * Terminates (kills) a running process.
347 *
348 * @returns IPRT status code.
349 * @param Process The process to terminate.
350 */
351RTR3DECL(int) RTProcTerminate(RTPROCESS Process);
352
353/**
354 * Gets the processor affinity mask of the current process.
355 *
356 * @returns The affinity mask.
357 */
358RTR3DECL(uint64_t) RTProcGetAffinityMask(void);
359
360/**
361 * Gets the short process name.
362 *
363 * @returns Pointer to read-only name string.
364 * @note IPRT must've been initialized or the string will be empty.
365 */
366RTR3DECL(const char *) RTProcShortName(void);
367
368/**
369 * Gets the path to the executable image of the current process.
370 *
371 * @returns Pointer to read-only path string.
372 * @note IPRT must've been initialized or the string will be empty.
373 */
374RTR3DECL(const char *) RTProcExecutablePath(void);
375
376/**
377 * Gets a copy of the path to the executable image of the current process.
378 *
379 * @returns pszExecPath on success. NULL on buffer overflow or other errors.
380 *
381 * @param pszExecPath Where to store the path.
382 * @param cbExecPath The size of the buffer.
383 * @note IPRT must've been initialized or the string will be empty.
384 */
385RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath);
386
387/**
388 * Daemonize the current process, making it a background process.
389 *
390 * The way this work is that it will spawn a detached / backgrounded /
391 * daemonized / call-it-what-you-want process that isn't a direct child of the
392 * current process. The spawned will have the same arguments a the caller,
393 * except that the @a pszDaemonizedOpt is appended to prevent that the new
394 * process calls this API again.
395 *
396 * The new process will have the standard handles directed to/from the
397 * bitbucket.
398 *
399 * @returns IPRT status code. On success it is normal for the caller to exit
400 * the process by returning from main().
401 *
402 * @param papszArgs The argument vector of the calling process.
403 * @param pszDaemonizedOpt The daemonized option. This is appended to the
404 * end of the parameter list of the daemonized process.
405 */
406RTR3DECL(int) RTProcDaemonize(const char * const *papszArgs, const char *pszDaemonizedOpt);
407
408/**
409 * Daemonize the current process, making it a background process. The current
410 * process will exit if daemonizing is successful.
411 *
412 * @returns IPRT status code. On success it will only return in the child
413 * process, the parent will exit. On failure, it will return in the
414 * parent process and no child has been spawned.
415 *
416 * @param fNoChDir Pass false to change working directory to "/".
417 * @param fNoClose Pass false to redirect standard file streams to the null device.
418 * @param pszPidfile Path to a file to write the process id of the daemon
419 * process to. Daemonizing will fail if this file already
420 * exists or cannot be written. May be NULL.
421 */
422RTR3DECL(int) RTProcDaemonizeUsingFork(bool fNoChDir, bool fNoClose, const char *pszPidfile);
423
424/**
425 * Check if the given process is running on the system.
426 *
427 * This check is case sensitive on most systems, except for Windows, OS/2 and
428 * Darwin.
429 *
430 * @returns true if the process is running & false otherwise.
431 * @param pszName Process name to search for. If no path is given only the
432 * filename part of the running process set will be
433 * matched. If a path is specified, the full path will be
434 * matched.
435 */
436RTR3DECL(bool) RTProcIsRunningByName(const char *pszName);
437
438/**
439 * Queries the parent process ID.
440 *
441 * @returns IPRT status code
442 * @param hProcess The process to query the parent of.
443 * @param phParent Where to return the parent process ID.
444 */
445RTR3DECL(int) RTProcQueryParent(RTPROCESS hProcess, PRTPROCESS phParent);
446
447/**
448 * Query the username of the given process.
449 *
450 * @returns IPRT status code.
451 * @retval VERR_BUFFER_OVERFLOW if the given buffer size is to small for the username.
452 * @param hProcess The process handle to query the username for.
453 * NIL_PROCESS is an alias for the current process.
454 * @param pszUser Where to store the user name on success.
455 * @param cbUser The size of the user name buffer.
456 * @param pcbUser Where to store the username length on success
457 * or the required buffer size if VERR_BUFFER_OVERFLOW
458 * is returned.
459 */
460RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser, size_t *pcbUser);
461
462/**
463 * Query the username of the given process allocating the string for the username.
464 *
465 * @returns IPRT status code.
466 * @param hProcess The process handle to query the username for.
467 * @param ppszUser Where to store the pointer to the string containing
468 * the username on success. Free with RTStrFree().
469 */
470RTR3DECL(int) RTProcQueryUsernameA(RTPROCESS hProcess, char **ppszUser);
471
472#endif /* IN_RING3 */
473
474/** @} */
475
476RT_C_DECLS_END
477
478#endif /* !IPRT_INCLUDED_process_h */
479
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