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