1 | /* $Id: process.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Process, Common.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Header Files *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #include <iprt/process.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include "internal/process.h"
|
---|
47 | #include "internal/thread.h"
|
---|
48 |
|
---|
49 | #ifdef RT_OS_WINDOWS
|
---|
50 | # include <iprt/win/windows.h>
|
---|
51 | #else
|
---|
52 | # include <unistd.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Get the identifier for the current process.
|
---|
58 | *
|
---|
59 | * @returns Process identifier.
|
---|
60 | */
|
---|
61 | RTDECL(RTPROCESS) RTProcSelf(void)
|
---|
62 | {
|
---|
63 | RTPROCESS Self = g_ProcessSelf;
|
---|
64 | if (Self != NIL_RTPROCESS)
|
---|
65 | return Self;
|
---|
66 |
|
---|
67 | /* lazy init. */
|
---|
68 | #ifdef _MSC_VER
|
---|
69 | Self = GetCurrentProcessId(); /* since NT 3.1, not 3.51+ as listed on geoffchappell.com */
|
---|
70 | #else
|
---|
71 | Self = getpid();
|
---|
72 | #endif
|
---|
73 | g_ProcessSelf = Self;
|
---|
74 | return Self;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Attempts to alter the priority of the current process.
|
---|
80 | *
|
---|
81 | * @returns iprt status code.
|
---|
82 | * @param enmPriority The new priority.
|
---|
83 | */
|
---|
84 | RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority)
|
---|
85 | {
|
---|
86 | if (enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST)
|
---|
87 | return rtThreadDoSetProcPriority(enmPriority);
|
---|
88 | AssertMsgFailed(("enmPriority=%d\n", enmPriority));
|
---|
89 | return VERR_INVALID_PARAMETER;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Gets the current priority of this process.
|
---|
95 | *
|
---|
96 | * @returns The priority (see RTPROCPRIORITY).
|
---|
97 | */
|
---|
98 | RTR3DECL(RTPROCPRIORITY) RTProcGetPriority(void)
|
---|
99 | {
|
---|
100 | return g_enmProcessPriority;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath)
|
---|
105 | {
|
---|
106 | if (RT_UNLIKELY(g_szrtProcExePath[0] == '\0'))
|
---|
107 | return NULL;
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Calc the length and check if there is space before copying.
|
---|
111 | */
|
---|
112 | size_t cch = g_cchrtProcExePath;
|
---|
113 | if (cch < cbExecPath)
|
---|
114 | {
|
---|
115 | memcpy(pszExecPath, g_szrtProcExePath, cch);
|
---|
116 | pszExecPath[cch] = '\0';
|
---|
117 | return pszExecPath;
|
---|
118 | }
|
---|
119 |
|
---|
120 | AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cbExecPath, cch));
|
---|
121 | return NULL;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTR3DECL(const char *) RTProcExecutablePath(void)
|
---|
126 | {
|
---|
127 | return g_szrtProcExePath;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTR3DECL(const char *) RTProcShortName(void)
|
---|
132 | {
|
---|
133 | return &g_szrtProcExePath[g_offrtProcName];
|
---|
134 | }
|
---|
135 |
|
---|