[1] | 1 | /* $Id: process.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Process, Common.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 |
|
---|
[57358] | 39 | /*********************************************************************************************************************************
|
---|
| 40 | * Header Files *
|
---|
| 41 | *********************************************************************************************************************************/
|
---|
[1] | 42 | #include <iprt/process.h>
|
---|
| 43 | #include <iprt/assert.h>
|
---|
[76452] | 44 | #include <iprt/errcore.h>
|
---|
[11836] | 45 | #include <iprt/string.h>
|
---|
[1] | 46 | #include "internal/process.h"
|
---|
| 47 | #include "internal/thread.h"
|
---|
| 48 |
|
---|
[3672] | 49 | #ifdef RT_OS_WINDOWS
|
---|
[95805] | 50 | # include <iprt/win/windows.h>
|
---|
[1] | 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 | */
|
---|
[385] | 61 | RTDECL(RTPROCESS) RTProcSelf(void)
|
---|
[1] | 62 | {
|
---|
| 63 | RTPROCESS Self = g_ProcessSelf;
|
---|
| 64 | if (Self != NIL_RTPROCESS)
|
---|
| 65 | return Self;
|
---|
| 66 |
|
---|
| 67 | /* lazy init. */
|
---|
| 68 | #ifdef _MSC_VER
|
---|
[95805] | 69 | Self = GetCurrentProcessId(); /* since NT 3.1, not 3.51+ as listed on geoffchappell.com */
|
---|
[1] | 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 |
|
---|
[11836] | 103 |
|
---|
[33806] | 104 | RTR3DECL(char *) RTProcGetExecutablePath(char *pszExecPath, size_t cbExecPath)
|
---|
[11836] | 105 | {
|
---|
[26549] | 106 | if (RT_UNLIKELY(g_szrtProcExePath[0] == '\0'))
|
---|
| 107 | return NULL;
|
---|
[11836] | 108 |
|
---|
| 109 | /*
|
---|
| 110 | * Calc the length and check if there is space before copying.
|
---|
| 111 | */
|
---|
| 112 | size_t cch = g_cchrtProcExePath;
|
---|
[33806] | 113 | if (cch < cbExecPath)
|
---|
[11836] | 114 | {
|
---|
[33806] | 115 | memcpy(pszExecPath, g_szrtProcExePath, cch);
|
---|
| 116 | pszExecPath[cch] = '\0';
|
---|
| 117 | return pszExecPath;
|
---|
[11836] | 118 | }
|
---|
| 119 |
|
---|
[33806] | 120 | AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cbExecPath, cch));
|
---|
[11836] | 121 | return NULL;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[33806] | 124 |
|
---|
[83661] | 125 | RTR3DECL(const char *) RTProcExecutablePath(void)
|
---|
| 126 | {
|
---|
| 127 | return g_szrtProcExePath;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
[33806] | 131 | RTR3DECL(const char *) RTProcShortName(void)
|
---|
| 132 | {
|
---|
| 133 | return &g_szrtProcExePath[g_offrtProcName];
|
---|
| 134 | }
|
---|
| 135 |
|
---|