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 | */
|
---|
28 | void 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 | */
|
---|
40 | void 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 | */
|
---|
53 | CRpid 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 | */
|
---|
97 | void 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 | */
|
---|
111 | void 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 | # else
|
---|
140 | extern const char *__progname;
|
---|
141 | pszExecName = __progname;
|
---|
142 | # endif
|
---|
143 | if (!pszExecName)
|
---|
144 | pszExecName = "<unknown>";
|
---|
145 | pszProgName = strrchr(pszExecName, '/');
|
---|
146 | if (pszProgName && *(pszProgName + 1))
|
---|
147 | pszProgName++;
|
---|
148 | else
|
---|
149 | pszProgName = pszExecName;
|
---|
150 | strncpy(name, pszProgName, maxLen);
|
---|
151 | name[maxLen - 1] = '\0';
|
---|
152 | # else
|
---|
153 | /* Unix:
|
---|
154 | * Call getpid() to get our process ID.
|
---|
155 | * Then use system() to write the output of 'ps' to a temp file.
|
---|
156 | * Read/scan the temp file to map the process ID to process name.
|
---|
157 | * I'd love to find a better solution! (BrianP)
|
---|
158 | */
|
---|
159 | FILE *f;
|
---|
160 | pid_t pid = getpid();
|
---|
161 | char *tmp, command[1000];
|
---|
162 |
|
---|
163 | /* init to NULL in case of early return */
|
---|
164 | *name = 0;
|
---|
165 |
|
---|
166 | /* get a temporary file name */
|
---|
167 | tmp = tmpnam(NULL);
|
---|
168 | if (!tmp)
|
---|
169 | return;
|
---|
170 | /* pipe output of ps to temp file */
|
---|
171 | #ifndef SunOS
|
---|
172 | sprintf(command, "ps > %s", tmp);
|
---|
173 | #else
|
---|
174 | sprintf(command, "ps -e -o 'pid tty time comm'> %s", tmp);
|
---|
175 | #endif
|
---|
176 | system(command);
|
---|
177 |
|
---|
178 | /* open/scan temp file */
|
---|
179 | f = fopen(tmp, "r");
|
---|
180 | if (f) {
|
---|
181 | char buffer[1000], cmd[1000], *psz, *pname;
|
---|
182 | while (!feof(f)) {
|
---|
183 | int id;
|
---|
184 | fgets(buffer, 999, f);
|
---|
185 | sscanf(buffer, "%d %*s %*s %999s", &id, cmd);
|
---|
186 | if (id == pid) {
|
---|
187 | for (pname=psz=&cmd[0]; *psz!=0; psz++)
|
---|
188 | {
|
---|
189 | switch (*psz)
|
---|
190 | {
|
---|
191 | case '/':
|
---|
192 | pname = psz+1;
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | crStrncpy(name, pname, maxLen);
|
---|
197 | break;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | fclose(f);
|
---|
201 | }
|
---|
202 | remove(tmp);
|
---|
203 | # endif
|
---|
204 | #endif
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Return current directory string.
|
---|
210 | */
|
---|
211 | void crGetCurrentDir( char *dir, int maxLen )
|
---|
212 | {
|
---|
213 | #ifdef WINDOWS
|
---|
214 | if (!GetCurrentDirectory(maxLen, dir))
|
---|
215 | dir[0] = 0;
|
---|
216 | #else
|
---|
217 | if (!getcwd(dir, maxLen))
|
---|
218 | dir[0] = 0;
|
---|
219 | #endif
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Return current process ID number.
|
---|
225 | */
|
---|
226 | CRpid crGetPID(void)
|
---|
227 | {
|
---|
228 | #ifdef WINDOWS
|
---|
229 | //return _getpid();
|
---|
230 | return GetCurrentProcess();
|
---|
231 | #else
|
---|
232 | return getpid();
|
---|
233 | #endif
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | #if 0
|
---|
238 | /* simple test harness */
|
---|
239 | int main(int argc, char **argv)
|
---|
240 | {
|
---|
241 | char name[100];
|
---|
242 | printf("argv[0] = %s\n", argv[0]);
|
---|
243 |
|
---|
244 | crGetProcName(name, 100);
|
---|
245 | printf("crGetProcName returned %s\n", name);
|
---|
246 |
|
---|
247 | return 0;
|
---|
248 | }
|
---|
249 | #endif
|
---|