VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/process-posix.cpp@ 46697

Last change on this file since 46697 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: process-posix.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT - Process, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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
27
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#define LOG_GROUP RTLOGGROUP_PROCESS
33#include <unistd.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39#include <signal.h>
40#include <pwd.h>
41
42#include <iprt/process.h>
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/env.h>
47#include <iprt/err.h>
48#include <iprt/file.h>
49#include <iprt/pipe.h>
50#include <iprt/socket.h>
51#include <iprt/string.h>
52#include <iprt/mem.h>
53#include "internal/process.h"
54
55
56RTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
57{
58 int rc;
59 do rc = RTProcWaitNoResume(Process, fFlags, pProcStatus);
60 while (rc == VERR_INTERRUPTED);
61 return rc;
62}
63
64
65RTR3DECL(int) RTProcWaitNoResume(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
66{
67 /*
68 * Validate input.
69 */
70 if (Process <= 0)
71 {
72 AssertMsgFailed(("Invalid Process=%d\n", Process));
73 return VERR_INVALID_PARAMETER;
74 }
75 if (fFlags & ~(RTPROCWAIT_FLAGS_NOBLOCK | RTPROCWAIT_FLAGS_BLOCK))
76 {
77 AssertMsgFailed(("Invalid flags %#x\n", fFlags));
78 return VERR_INVALID_PARAMETER;
79 }
80
81 /*
82 * Perform the wait.
83 */
84 int iStatus = 0;
85 int rc = waitpid(Process, &iStatus, fFlags & RTPROCWAIT_FLAGS_NOBLOCK ? WNOHANG : 0);
86 if (rc > 0)
87 {
88 /*
89 * Fill in the status structure.
90 */
91 if (pProcStatus)
92 {
93 if (WIFEXITED(iStatus))
94 {
95 pProcStatus->enmReason = RTPROCEXITREASON_NORMAL;
96 pProcStatus->iStatus = WEXITSTATUS(iStatus);
97 }
98 else if (WIFSIGNALED(iStatus))
99 {
100 pProcStatus->enmReason = RTPROCEXITREASON_SIGNAL;
101 pProcStatus->iStatus = WTERMSIG(iStatus);
102 }
103 else
104 {
105 Assert(!WIFSTOPPED(iStatus));
106 pProcStatus->enmReason = RTPROCEXITREASON_ABEND;
107 pProcStatus->iStatus = iStatus;
108 }
109 }
110 return VINF_SUCCESS;
111 }
112
113 /*
114 * Child running?
115 */
116 if (!rc)
117 {
118 Assert(fFlags & RTPROCWAIT_FLAGS_NOBLOCK);
119 return VERR_PROCESS_RUNNING;
120 }
121
122 /*
123 * Figure out which error to return.
124 */
125 int iErr = errno;
126 if (iErr == ECHILD)
127 return VERR_PROCESS_NOT_FOUND;
128 return RTErrConvertFromErrno(iErr);
129}
130
131
132RTR3DECL(int) RTProcTerminate(RTPROCESS Process)
133{
134 if (Process == NIL_RTPROCESS)
135 return VINF_SUCCESS;
136
137 if (!kill(Process, SIGKILL))
138 return VINF_SUCCESS;
139 return RTErrConvertFromErrno(errno);
140}
141
142
143RTR3DECL(uint64_t) RTProcGetAffinityMask(void)
144{
145 /// @todo
146 return 1;
147}
148
149
150RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser,
151 size_t *pcbUser)
152{
153 AssertReturn( (pszUser && cbUser > 0)
154 || (!pszUser && !cbUser), VERR_INVALID_PARAMETER);
155
156 if (hProcess != RTProcSelf())
157 return VERR_NOT_SUPPORTED;
158
159 int32_t cbPwdMax = sysconf(_SC_GETPW_R_SIZE_MAX);
160 if (cbPwdMax == -1)
161 return RTErrConvertFromErrno(errno);
162
163 char *pbBuf = (char *)RTMemAllocZ(cbPwdMax);
164 if (!pbBuf)
165 return VERR_NO_MEMORY;
166
167 struct passwd Pwd, *pPwd;
168 int rc = getpwuid_r(geteuid(), &Pwd, pbBuf, cbPwdMax, &pPwd);
169 if (!rc)
170 {
171 size_t cbPwdUser = strlen(pPwd->pw_name) + 1;
172
173 if (pcbUser)
174 *pcbUser = cbPwdUser;
175
176 if (cbPwdUser > cbUser)
177 rc = VERR_BUFFER_OVERFLOW;
178 else
179 {
180 memcpy(pszUser, pPwd->pw_name, cbPwdUser);
181 rc = VINF_SUCCESS;
182 }
183 }
184 else
185 rc = RTErrConvertFromErrno(rc);
186
187 RTMemFree(pbBuf);
188 return rc;
189}
190
191
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