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