VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/process.cpp@ 18989

Last change on this file since 18989 was 18989, checked in by vboxsync, 16 years ago

iprt/process: update the cached process ID on fork

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/* $Id: process.cpp 18989 2009-04-17 13:01:30Z vboxsync $ */
2/** @file
3 * IPRT - Process, Common.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <iprt/process.h>
37#include <iprt/assert.h>
38#include <iprt/err.h>
39#include <iprt/string.h>
40#include "internal/process.h"
41#include "internal/thread.h"
42
43#ifdef RT_OS_WINDOWS
44# include <process.h>
45#else
46# include <unistd.h>
47#endif
48
49#if !defined (RT_OS_OS2) && !defined(RT_OS_WINDOWS)
50# include <pthread.h>
51#endif
52
53#if !defined (RT_OS_OS2) && !defined(RT_OS_WINDOWS)
54/** Update the cached pid on fork on posix systems */
55static void rtProcPThreadAtFork(void)
56{
57 g_ProcessSelf = getpid();
58}
59#endif
60
61/**
62 * Get the identifier for the current process.
63 *
64 * @returns Process identifier.
65 */
66RTDECL(RTPROCESS) RTProcSelf(void)
67{
68 RTPROCESS Self = g_ProcessSelf;
69 if (Self != NIL_RTPROCESS)
70 return Self;
71
72 /* lazy init. */
73#if !defined (RT_OS_OS2) && !defined(RT_OS_WINDOWS)
74 /* Update the cached pid on fork on posix systems */
75 if (pthread_atfork(NULL, NULL, rtProcPThreadAtFork) != 0)
76 return 0;
77#endif
78#ifdef _MSC_VER
79 Self = _getpid(); /* crappy ansi compiler */
80#else
81 Self = getpid();
82#endif
83 g_ProcessSelf = Self;
84 return Self;
85}
86
87
88/**
89 * Attempts to alter the priority of the current process.
90 *
91 * @returns iprt status code.
92 * @param enmPriority The new priority.
93 */
94RTR3DECL(int) RTProcSetPriority(RTPROCPRIORITY enmPriority)
95{
96 if (enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST)
97 return rtThreadDoSetProcPriority(enmPriority);
98 AssertMsgFailed(("enmPriority=%d\n", enmPriority));
99 return VERR_INVALID_PARAMETER;
100}
101
102
103/**
104 * Gets the current priority of this process.
105 *
106 * @returns The priority (see RTPROCPRIORITY).
107 */
108RTR3DECL(RTPROCPRIORITY) RTProcGetPriority(void)
109{
110 return g_enmProcessPriority;
111}
112
113
114RTR3DECL(char *) RTProcGetExecutableName(char *pszExecName, size_t cchExecName)
115{
116 AssertReturn(g_szrtProcExePath[0] != '\0', NULL);
117
118 /*
119 * Calc the length and check if there is space before copying.
120 */
121 size_t cch = g_cchrtProcExePath;
122 if (cch <= cchExecName)
123 {
124 memcpy(pszExecName, g_szrtProcExePath, cch);
125 pszExecName[cch] = '\0';
126 return pszExecName;
127 }
128
129 AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchExecName, cch));
130 return NULL;
131}
132
Note: See TracBrowser for help on using the repository browser.

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