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 | #else
|
---|
18 | #pragma warning ( disable : 4127 )
|
---|
19 | #define snprintf _snprintf
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Sleep/pause for the given number of seconds.
|
---|
24 | */
|
---|
25 | void crSleep( unsigned int seconds )
|
---|
26 | {
|
---|
27 | #ifdef WINDOWS
|
---|
28 | Sleep(seconds*1000); /* milliseconds */
|
---|
29 | #else
|
---|
30 | sleep(seconds);
|
---|
31 | #endif
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Sleep/pause for the given number of milliseconds.
|
---|
36 | */
|
---|
37 | void crMsleep( unsigned int msec )
|
---|
38 | {
|
---|
39 | #ifdef WINDOWS
|
---|
40 | Sleep(msec);
|
---|
41 | #else
|
---|
42 | usleep(msec*1000); /* usecs */
|
---|
43 | #endif
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Spawn (i.e. fork/exec) a new process.
|
---|
49 | */
|
---|
50 | CRpid crSpawn( const char *command, const char *argv[] )
|
---|
51 | {
|
---|
52 | #ifdef WINDOWS
|
---|
53 | char newargv[1000];
|
---|
54 | int i;
|
---|
55 | STARTUPINFO si;
|
---|
56 | PROCESS_INFORMATION pi;
|
---|
57 |
|
---|
58 | (void) command;
|
---|
59 |
|
---|
60 | ZeroMemory( &si, sizeof(si) );
|
---|
61 | si.cb = sizeof(si);
|
---|
62 | ZeroMemory( &pi, sizeof(pi) );
|
---|
63 |
|
---|
64 | crStrncpy(newargv, argv[0], 1000 );
|
---|
65 | for (i = 1; argv[i]; i++) {
|
---|
66 | crStrcat(newargv, " ");
|
---|
67 | crStrcat(newargv, argv[i]);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if ( !CreateProcess(NULL, newargv, NULL, NULL, FALSE, 0, NULL,
|
---|
71 | NULL, &si, &pi) )
|
---|
72 | {
|
---|
73 | crWarning("crSpawn failed, %d", GetLastError());
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 | return pi.hProcess;
|
---|
77 | #else
|
---|
78 | pid_t pid;
|
---|
79 | if ((pid = fork()) == 0)
|
---|
80 | {
|
---|
81 | /* I'm the child */
|
---|
82 | int err = execvp(command, (char * const *) argv);
|
---|
83 | crWarning("crSpawn failed (return code: %d)", err);
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 | return (unsigned long) pid;
|
---|
87 | #endif
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Kill the named process.
|
---|
93 | */
|
---|
94 | void crKill( CRpid pid )
|
---|
95 | {
|
---|
96 | #ifdef WINDOWS
|
---|
97 | TerminateProcess( pid, 0 );
|
---|
98 | #else
|
---|
99 | kill((pid_t) pid, SIGKILL);
|
---|
100 | #endif
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Return the name of the running process.
|
---|
106 | * name[0] will be zero if anything goes wrong.
|
---|
107 | */
|
---|
108 | void crGetProcName( char *name, int maxLen )
|
---|
109 | {
|
---|
110 | #ifdef WINDOWS
|
---|
111 | char command[1000];
|
---|
112 | int c = 0;
|
---|
113 |
|
---|
114 | *name = 0;
|
---|
115 |
|
---|
116 | if (!GetModuleFileName( NULL, command, maxLen ))
|
---|
117 | return;
|
---|
118 |
|
---|
119 | while (1) {
|
---|
120 | /* crude mechanism to blank out the backslashes
|
---|
121 | * in the Windows filename and recover the actual
|
---|
122 | * program name to return */
|
---|
123 | if (crStrstr(command, "\\")) {
|
---|
124 | crStrncpy(name, command+c+1, maxLen);
|
---|
125 | command[c] = 32;
|
---|
126 | c++;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | #else
|
---|
132 | /* Unix:
|
---|
133 | * Call getpid() to get our process ID.
|
---|
134 | * Then use system() to write the output of 'ps' to a temp file.
|
---|
135 | * Read/scan the temp file to map the process ID to process name.
|
---|
136 | * I'd love to find a better solution! (BrianP)
|
---|
137 | */
|
---|
138 | FILE *f;
|
---|
139 | pid_t pid = getpid();
|
---|
140 | char *tmp, command[1000];
|
---|
141 |
|
---|
142 | /* init to NULL in case of early return */
|
---|
143 | *name = 0;
|
---|
144 |
|
---|
145 | /* get a temporary file name */
|
---|
146 | tmp = tmpnam(NULL);
|
---|
147 | if (!tmp)
|
---|
148 | return;
|
---|
149 | /* pipe output of ps to temp file */
|
---|
150 | #ifndef SunOS
|
---|
151 | # ifdef VBOX
|
---|
152 | snprintf(command, sizeof(command), "ps > %s", tmp);
|
---|
153 | # else
|
---|
154 | sprintf(command, "ps > %s", tmp);
|
---|
155 | # endif
|
---|
156 | #else
|
---|
157 | # ifdef VBOX
|
---|
158 | snprintf(command, sizeof(command), "ps -e -o 'pid tty time comm'> %s", tmp);
|
---|
159 | # else
|
---|
160 | sprintf(command, "ps -e -o 'pid tty time comm'> %s", tmp);
|
---|
161 | # endif
|
---|
162 | #endif
|
---|
163 | system(command);
|
---|
164 |
|
---|
165 | /* open/scan temp file */
|
---|
166 | f = fopen(tmp, "r");
|
---|
167 | if (f) {
|
---|
168 | char buffer[1000], cmd[1000], *psz, *pname;
|
---|
169 | while (!feof(f)) {
|
---|
170 | int id;
|
---|
171 | fgets(buffer, 999, f);
|
---|
172 | sscanf(buffer, "%d %*s %*s %999s", &id, cmd);
|
---|
173 | if (id == pid) {
|
---|
174 | for (pname=psz=&cmd[0]; *psz!=0; psz++)
|
---|
175 | {
|
---|
176 | switch (*psz)
|
---|
177 | {
|
---|
178 | case '/':
|
---|
179 | pname = psz+1;
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | crStrncpy(name, pname, maxLen);
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | fclose(f);
|
---|
188 | }
|
---|
189 | remove(tmp);
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Return current directory string.
|
---|
196 | */
|
---|
197 | void crGetCurrentDir( char *dir, int maxLen )
|
---|
198 | {
|
---|
199 | #ifdef WINDOWS
|
---|
200 | if (!GetCurrentDirectory(maxLen, dir))
|
---|
201 | dir[0] = 0;
|
---|
202 | #else
|
---|
203 | if (!getcwd(dir, maxLen))
|
---|
204 | dir[0] = 0;
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Return current process ID number.
|
---|
211 | */
|
---|
212 | CRpid crGetPID(void)
|
---|
213 | {
|
---|
214 | #ifdef WINDOWS
|
---|
215 | //return _getpid();
|
---|
216 | return GetCurrentProcess();
|
---|
217 | #else
|
---|
218 | return getpid();
|
---|
219 | #endif
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | #if 0
|
---|
224 | /* simple test harness */
|
---|
225 | int main(int argc, char **argv)
|
---|
226 | {
|
---|
227 | char name[100];
|
---|
228 | printf("argv[0] = %s\n", argv[0]);
|
---|
229 |
|
---|
230 | crGetProcName(name, 100);
|
---|
231 | printf("crGetProcName returned %s\n", name);
|
---|
232 |
|
---|
233 | return 0;
|
---|
234 | }
|
---|
235 | #endif
|
---|