VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/process.c@ 62425

最後變更 在這個檔案從62425是 58583,由 vboxsync 提交於 9 年 前

bugref:8087: Additions/x11: support non-root X server: do not fork and exec in a shared library constructor, as this plays very badly with LD_PRELOAD. Blind fix for Darwin.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.5 KB
 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_error.h"
8#include "cr_process.h"
9#include "cr_string.h"
10#include "cr_mem.h"
11#include <stdio.h>
12#include <stdlib.h>
13#include <sys/types.h>
14#include <signal.h>
15#ifndef WINDOWS
16#include <unistd.h>
17# ifdef VBOX
18# include <string.h>
19# endif
20#else
21#pragma warning ( disable : 4127 )
22#define snprintf _snprintf
23#endif
24
25/**
26 * Sleep/pause for the given number of seconds.
27 */
28void crSleep( unsigned int seconds )
29{
30#ifdef WINDOWS
31 Sleep(seconds*1000); /* milliseconds */
32#else
33 sleep(seconds);
34#endif
35}
36
37/**
38 * Sleep/pause for the given number of milliseconds.
39 */
40void crMsleep( unsigned int msec )
41{
42#ifdef WINDOWS
43 Sleep(msec);
44#else
45 usleep(msec*1000); /* usecs */
46#endif
47}
48
49
50/*
51 * Spawn (i.e. fork/exec) a new process.
52 */
53CRpid crSpawn( const char *command, const char *argv[] )
54{
55#ifdef WINDOWS
56 char newargv[1000];
57 int i;
58 STARTUPINFO si;
59 PROCESS_INFORMATION pi;
60
61 (void) command;
62
63 ZeroMemory( &si, sizeof(si) );
64 si.cb = sizeof(si);
65 ZeroMemory( &pi, sizeof(pi) );
66
67 crStrncpy(newargv, argv[0], 1000 );
68 for (i = 1; argv[i]; i++) {
69 crStrcat(newargv, " ");
70 crStrcat(newargv, argv[i]);
71 }
72
73 if ( !CreateProcess(NULL, newargv, NULL, NULL, FALSE, 0, NULL,
74 NULL, &si, &pi) )
75 {
76 crWarning("crSpawn failed, %d", GetLastError());
77 return 0;
78 }
79 return pi.hProcess;
80#else
81 pid_t pid;
82 if ((pid = fork()) == 0)
83 {
84 /* I'm the child */
85 int err = execvp(command, (char * const *) argv);
86 crWarning("crSpawn failed (return code: %d)", err);
87 return 0;
88 }
89 return (unsigned long) pid;
90#endif
91}
92
93
94/*
95 * Kill the named process.
96 */
97void crKill( CRpid pid )
98{
99#ifdef WINDOWS
100 TerminateProcess( pid, 0 );
101#else
102 kill((pid_t) pid, SIGKILL);
103#endif
104}
105
106
107/*
108 * Return the name of the running process.
109 * name[0] will be zero if anything goes wrong.
110 */
111void crGetProcName( char *name, int maxLen )
112{
113#ifdef WINDOWS
114 char command[1000];
115 int c = 0;
116
117 *name = 0;
118
119 if (!GetModuleFileName( NULL, command, maxLen ))
120 return;
121
122 while (1) {
123 /* crude mechanism to blank out the backslashes
124 * in the Windows filename and recover the actual
125 * program name to return */
126 if (crStrstr(command, "\\")) {
127 crStrncpy(name, command+c+1, maxLen);
128 command[c] = 32;
129 c++;
130 }
131 else
132 break;
133 }
134#else
135#ifdef VBOX
136 const char *pszExecName, *pszProgName;
137# ifdef SunOS
138 pszExecName = getexecname();
139# elif defined(DARWIN)
140 pszExecName = getprogname();
141# else
142 extern const char *__progname;
143 pszExecName = __progname;
144# endif
145 if (!pszExecName)
146 pszExecName = "<unknown>";
147 pszProgName = strrchr(pszExecName, '/');
148 if (pszProgName && *(pszProgName + 1))
149 pszProgName++;
150 else
151 pszProgName = pszExecName;
152 strncpy(name, pszProgName, maxLen);
153 name[maxLen - 1] = '\0';
154# else
155 /* Unix:
156 * Call getpid() to get our process ID.
157 * Then use system() to write the output of 'ps' to a temp file.
158 * Read/scan the temp file to map the process ID to process name.
159 * I'd love to find a better solution! (BrianP)
160 */
161 FILE *f;
162 pid_t pid = getpid();
163 char *tmp, command[1000];
164
165 /* init to NULL in case of early return */
166 *name = 0;
167
168 /* get a temporary file name */
169 tmp = tmpnam(NULL);
170 if (!tmp)
171 return;
172 /* pipe output of ps to temp file */
173#ifndef SunOS
174 sprintf(command, "ps > %s", tmp);
175#else
176 sprintf(command, "ps -e -o 'pid tty time comm'> %s", tmp);
177#endif
178 system(command);
179
180 /* open/scan temp file */
181 f = fopen(tmp, "r");
182 if (f) {
183 char buffer[1000], cmd[1000], *psz, *pname;
184 while (!feof(f)) {
185 int id;
186 fgets(buffer, 999, f);
187 sscanf(buffer, "%d %*s %*s %999s", &id, cmd);
188 if (id == pid) {
189 for (pname=psz=&cmd[0]; *psz!=0; psz++)
190 {
191 switch (*psz)
192 {
193 case '/':
194 pname = psz+1;
195 break;
196 }
197 }
198 crStrncpy(name, pname, maxLen);
199 break;
200 }
201 }
202 fclose(f);
203 }
204 remove(tmp);
205# endif
206#endif
207}
208
209
210/*
211 * Return current directory string.
212 */
213void crGetCurrentDir( char *dir, int maxLen )
214{
215#ifdef WINDOWS
216 if (!GetCurrentDirectory(maxLen, dir))
217 dir[0] = 0;
218#else
219 if (!getcwd(dir, maxLen))
220 dir[0] = 0;
221#endif
222}
223
224
225/**
226 * Return current process ID number.
227 */
228CRpid crGetPID(void)
229{
230#ifdef WINDOWS
231 //return _getpid();
232 return GetCurrentProcess();
233#else
234 return getpid();
235#endif
236}
237
238
239#if 0
240/* simple test harness */
241int main(int argc, char **argv)
242{
243 char name[100];
244 printf("argv[0] = %s\n", argv[0]);
245
246 crGetProcName(name, 100);
247 printf("crGetProcName returned %s\n", name);
248
249 return 0;
250}
251#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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