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 |
|
---|
149 | /* pipe output of ps to temp file */
|
---|
150 | sprintf(command, "ps > %s", tmp);
|
---|
151 | system(command);
|
---|
152 |
|
---|
153 | /* open/scan temp file */
|
---|
154 | f = fopen(tmp, "r");
|
---|
155 | if (f) {
|
---|
156 | char buffer[1000], cmd[1000];
|
---|
157 | while (!feof(f)) {
|
---|
158 | int id;
|
---|
159 | fgets(buffer, 999, f);
|
---|
160 | sscanf(buffer, "%d %*s %*s %999s", &id, cmd);
|
---|
161 | if (id == pid) {
|
---|
162 | crStrncpy(name, cmd, maxLen);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | fclose(f);
|
---|
167 | }
|
---|
168 | remove(tmp);
|
---|
169 | #endif
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Return current directory string.
|
---|
175 | */
|
---|
176 | void crGetCurrentDir( char *dir, int maxLen )
|
---|
177 | {
|
---|
178 | #ifdef WINDOWS
|
---|
179 | if (!GetCurrentDirectory(maxLen, dir))
|
---|
180 | dir[0] = 0;
|
---|
181 | #else
|
---|
182 | if (!getcwd(dir, maxLen))
|
---|
183 | dir[0] = 0;
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Return current process ID number.
|
---|
190 | */
|
---|
191 | CRpid crGetPID(void)
|
---|
192 | {
|
---|
193 | #ifdef WINDOWS
|
---|
194 | //return _getpid();
|
---|
195 | return GetCurrentProcess();
|
---|
196 | #else
|
---|
197 | return getpid();
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | #if 0
|
---|
203 | /* simple test harness */
|
---|
204 | int main(int argc, char **argv)
|
---|
205 | {
|
---|
206 | char name[100];
|
---|
207 | printf("argv[0] = %s\n", argv[0]);
|
---|
208 |
|
---|
209 | crGetProcName(name, 100);
|
---|
210 | printf("crGetProcName returned %s\n", name);
|
---|
211 |
|
---|
212 | return 0;
|
---|
213 | }
|
---|
214 | #endif
|
---|