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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
//=============================================================================
/**
* @file process.cpp
*
* This example tests the <ACE_Process>. For more info, check the
* README file in this directory.
*
*
* @author Tim Harrison <harrison@cs.wustl.edu>.
*/
//=============================================================================
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_fcntl.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Get_Opt.h"
#include "ace/Process.h"
#include "ace/Log_Msg.h"
#include "ace/Time_Value.h"
#include "ace/SString.h"
#include "ace/Truncate.h"
#include "ace/Tokenizer_T.h"
#if defined (ACE_WIN32)
#define EXEC_NAME ACE_TEXT ("MORE.COM")
const ACE_TCHAR *DATE_PATH = ACE_TEXT ("date.exe");
const ACE_TCHAR *LS_PATH = ACE_TEXT ("ls.exe");
const ACE_TCHAR *SLEEP_PATH = ACE_TEXT ("sleep.exe");
#else
#define EXEC_NAME ACE_TEXT ("less")
const ACE_TCHAR *DATE_PATH = ACE_TEXT ("date");
const ACE_TCHAR *LS_PATH = ACE_TEXT ("ls");
const ACE_TCHAR *SLEEP_PATH = ACE_TEXT ("sleep");
#endif /* ACE_WIN32 */
static const ACE_TCHAR *executable = EXEC_NAME;
static ACE_TCHAR *print_file = 0;
static ACE_TCHAR *environment_string = 0;
static int get_env = 0;
static int run_date = 0;
static int run_ls = 0;
static int run_all = 0;
static int run_setenv = 0;
static int run_tokenizer = 0;
static int run_wait = 0;
// Parse the command-line arguments and set options.
static int
parse_args (int argc, ACE_TCHAR **argv)
{
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("dlx:p:e:gastuw"));
int c;
while ((c = get_opt ()) != -1)
{
switch (c)
{
case 't':
run_tokenizer = 1;
break;
case 's':
run_setenv = 1;
break;
case 'a':
run_all = 1;
break;
case 'd':
run_date = 1;
break;
case 'l':
run_ls = 1;
break;
case 'x':
executable = get_opt.opt_arg ();
break;
case 'p':
print_file = get_opt.opt_arg ();
break;
case 'e':
environment_string = get_opt.opt_arg ();
break;
case 'g':
get_env = 1;
break;
case 'w':
run_wait = 1;
break;
case 'u':
default:
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Usage:\n")
ACE_TEXT ("-d print date\n")
ACE_TEXT ("-l run ls\n")
ACE_TEXT ("-x <executable=more.com>\n")
ACE_TEXT ("-p print <file_name>\n")
ACE_TEXT ("-e <env variable message>\n")
ACE_TEXT ("-s setenv ACE_PROCESS_ENV and spawn -g\n")
ACE_TEXT ("-g get_env ACE_PROCESS_ENV\n")
ACE_TEXT ("-t test tokenizer\n")
ACE_TEXT ("-w test wait functions\n")
ACE_TEXT ("-a run all (d,l,e \"running\")\n")),
-1);
}
}
return 0;
}
// This shows how to set handles.
static void
test_more (void)
{
ACE_HANDLE infile = ACE_OS::open (print_file, O_RDONLY);
if (infile == ACE_INVALID_HANDLE)
{
ACE_ERROR ((LM_DEBUG, ACE_TEXT ("%p\n"), print_file));
return;
}
ACE_Process new_process;
ACE_Process_Options options;
options.command_line (executable);
options.set_handles (infile);
if (new_process.spawn (options) == -1)
{
int const error_number = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p errno = %d.\n"),
ACE_TEXT ("test_more"),
error_number));
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Process exit with status %d\n"),
status));
ACE_OS::close (infile);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("More succeeded.\n")));
}
// This is a simple usage of ACE_Process.
static void
test_date (void)
{
ACE_Process_Options options;
options.command_line (DATE_PATH);
// Try to create a new process running date.
ACE_Process new_process;
if (new_process.spawn (options) == -1)
{
int const error_number = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p errno = %d.\n"),
ACE_TEXT ("test_date"),
error_number));
return;
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Process exit with status %d\n"),
status));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("date succeeded.\n")));
}
static void
test_ls (void)
{
ACE_Process_Options options;
#if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
options.command_line (ACE_TEXT ("%s -al"), LS_PATH);
#else
options.command_line (ACE_TEXT ("%ls -al"), LS_PATH);
#endif
ACE_Process new_process;
if (new_process.spawn (options) == -1)
{
int error_number = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p errno = %d.\n"),
ACE_TEXT ("test_ls"),
error_number));
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Process exit with status %d\n"),
status));
}
static void
test_wait (void)
{
ACE_Process_Options options;
#if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
options.command_line (ACE_TEXT ("%s 10"), SLEEP_PATH);
#else
options.command_line (ACE_TEXT ("%ls 10"), SLEEP_PATH);
#endif
ACE_Process process1;
if (process1.spawn (options) == -1)
{
int const error_number = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p errno = %d.\n"),
ACE_TEXT ("test_ls"),
error_number));
}
int result;
ACE_exitcode status;
//FUZZ: disable check_for_lack_ACE_OS
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] New process sleeping 10; try wait(2)\n")));
//FUZZ: enable check_for_lack_ACE_OS
result = process1.wait (ACE_Time_Value (2), &status);
//FUZZ: disable check_for_lack_ACE_OS
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] wait(2) returns %d(%d)...now try regular wait\n"),
result,
status));
//FUZZ: enable check_for_lack_ACE_OS
result = process1.wait (&status);
//FUZZ: disable check_for_lack_ACE_OS
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] wait() returns %d(%d)\n"),
result,
status));
//FUZZ: enable check_for_lack_ACE_OS
ACE_Process process2;
if (process2.spawn (options) == -1)
{
int const error_number = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p errno = %d.\n"),
ACE_TEXT ("test_ls"),
error_number));
}
//FUZZ: disable check_for_lack_ACE_OS
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] New process sleeping 10; try wait(12)\n"),
status));
//FUZZ: enable check_for_lack_ACE_OS
result = process2.wait (ACE_Time_Value (12), &status);
//FUZZ: disable check_for_lack_ACE_OS
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] wait(12) returns %d(%d)...now try regular wait\n"),
result,
status));
//FUZZ: enable check_for_lack_ACE_OS
result = process2.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%T] wait returns %d(%d)\n"),
result,
status));
}
#if defined (ACE_WIN32)
// This is just to test the direct usage of CreateProcess. I use this
// occasionally as a sanity check when ACE_Process breaks.
static void
win32_test_ls (void)
{
PROCESS_INFORMATION process_info;
ACE_TEXT_STARTUPINFO startup_info;
ACE_OS::memset ((void *) &startup_info,
0,
sizeof startup_info);
ACE_OS::memset ((void *) &process_info,
0,
sizeof process_info);
startup_info.cb = sizeof startup_info;
startup_info.dwFlags = STARTF_USESTDHANDLES;
ACE_HANDLE std_out = ACE_STDOUT;
if (!::DuplicateHandle (::GetCurrentProcess (),
std_out,
::GetCurrentProcess (),
&startup_info.hStdOutput,
0,
TRUE,
DUPLICATE_SAME_ACCESS))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p duplicate failed.\n"),
ACE_TEXT ("test_ls")));
return;
}
ACE_TCHAR cmd_line[8];
ACE_OS::strncpy (cmd_line, ACE_TEXT ("-a"), sizeof (cmd_line));
BOOL fork_result =
ACE_TEXT_CreateProcess (ACE_TEXT ("c:\\Utils\\bin\\ls.exe"),
cmd_line,
0, // No process attributes.
0, // No thread attributes.
TRUE, // Allow handle inheritance.
0, // CREATE_NEW_CONSOLE, // Create a new console window.
0,
0, // Current directory to start in.
&startup_info,
&process_info);
::CloseHandle (startup_info.hStdOutput);
if (fork_result == 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p CreateProcess failed.\n"),
ACE_TEXT ("test_ls")));
else
{
::WaitForSingleObject (process_info.hProcess,
INFINITE);
ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ls succeeded.\n")));
}
}
// This code spawns a new process. The new process inherits our
// existing environment, plus one more. This has to be done by hand
// since CreateProcess does not allow us to inherit AND add
// environment variables.
static void
win32_spawn_environment_process (void)
{
PROCESS_INFORMATION process_info;
ACE_TEXT_STARTUPINFO startup_info;
ACE_OS::memset ((void *) &startup_info,
0,
sizeof startup_info);
ACE_OS::memset ((void *) &process_info,
0,
sizeof process_info);
startup_info.cb = sizeof (startup_info);
startup_info.dwFlags = STARTF_USESTDHANDLES;
ACE_HANDLE std_in = ACE_STDIN;
ACE_HANDLE std_out = ACE_STDOUT;
ACE_HANDLE std_err = ACE_STDERR;
if (!::DuplicateHandle (::GetCurrentProcess(),
std_out,
::GetCurrentProcess(),
&startup_info.hStdOutput,
0,
TRUE,
DUPLICATE_SAME_ACCESS))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p duplicate failed.\n"),
ACE_TEXT ("spawn_environment_process")));
return;
}
if (!::DuplicateHandle (::GetCurrentProcess(),
std_err,
::GetCurrentProcess(),
&startup_info.hStdError,
0,
TRUE,
DUPLICATE_SAME_ACCESS))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p duplicate failed.\n"),
ACE_TEXT ("spawn_environment_process")));
return;
}
if (!::DuplicateHandle (::GetCurrentProcess(),
std_in,
::GetCurrentProcess(),
&startup_info.hStdInput,
0,
TRUE,
DUPLICATE_SAME_ACCESS))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p duplicate failed.\n"),
ACE_TEXT ("spawn_environment_process")));
return;
}
// Normally, this would be just GetEnvironmentStrings, but it
// doesn't follow the same rules as the rest of the Win32 API
ACE_TCHAR *existing_environment = ACE_OS::getenvstrings ();
ACE_TCHAR environment[10240];
ACE_OS::sprintf (environment,
ACE_TEXT("ACE_PROCESS_TEST=%s"),
environment_string);
int size = 0;
while (existing_environment[size] != '\0')
size +=
ACE_Utils::truncate_cast<int> (
ACE_OS::strlen (existing_environment + size) + 1);
ACE_OS::memcpy (environment + (ACE_OS::strlen (environment) + 1),
existing_environment,
size);
ACE_TEXT_FreeEnvironmentStrings (existing_environment);
ACE_TCHAR cmd_line[16];
ACE_OS::strncpy (cmd_line, ACE_TEXT ("process -g"), sizeof (cmd_line));
BOOL fork_result =
ACE_TEXT_CreateProcess (ACE_TEXT ("d:\\harrison\\ACE_wrappers\\examples\\OS\\Process\\process.exe"),
cmd_line,
0, // No process attributes.
0, // No thread attributes.
TRUE, // Allow handle inheritance.
0, // CREATE_NEW_CONSOLE, // Create a new console window.
environment, // Environment.
//"d:\\harrison\\ACE_wrappers\\examples\\OS\\Process\\",
0,
&startup_info,
&process_info);
::CloseHandle (startup_info.hStdOutput);
::CloseHandle (startup_info.hStdError);
if (fork_result == 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("spawn_environment_process")));
else
{
::WaitForSingleObject (process_info.hProcess,
INFINITE);
ACE_DEBUG ((LM_ERROR,
ACE_TEXT ("spawn_environment_process succeeded.\n")));
}
}
#endif
static void
test_setenv (const ACE_TCHAR *argv0)
{
ACE_Process_Options options;
// options.setenv ("ACE_PROCESS_TEST", "here's a really large number: %u", 0 - 1);
options.setenv (ACE_TEXT ("ACE_PROCESS_TEST= here's a large number %u"),
0 - 1);
options.setenv (ACE_TEXT ("ACE_PROCESS_TEST2"), ACE_TEXT ("ophilli"));
#if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
options.command_line ("%s -g", argv0);
#else
options.command_line ("%ls -g", argv0);
#endif
ACE_Process process;
if (process.spawn (options) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("test_setenv")));
return;
}
ACE_exitcode status;
process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Process exit with status %d\n"),
status));
}
// Tests the ACE_Tokenizer.
static void
tokenize (ACE_TCHAR *buffer)
{
// This tokenizer will replace all spaces with end-of-string
// characters and will preserve text between "" and '' pairs.
ACE_Tokenizer parser (buffer);
parser.delimiter_replace (' ', '\0');
parser.preserve_designators ('\"', '\"'); // " This quote is for emacs
parser.preserve_designators ('\'', '\'');
for (const ACE_TCHAR *temp; ;)
{
temp = parser.next ();
if (temp == 0)
break;
ACE_DEBUG ((LM_DEBUG, temp));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
}
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
if (ACE_LOG_MSG->open (argv[0]) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("cannot open logger!!!\n")));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("starting...\n")));
if (::parse_args (argc, argv) == -1)
return -1;
if (run_all)
{
#if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
const ACE_TCHAR *cmdline = ACE_TEXT ("%s -d -l -s -w");
#else
const ACE_TCHAR *cmdline = ACE_TEXT ("%ls -d -l -s -w");
#endif
ACE_Process_Options options;
options.command_line (cmdline, argv[0]);
ACE_Process process;
if (process.spawn (options) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("main")),
-1);
ACE_exitcode status;
process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Process exit with status %d\n"),
status));
}
if (run_date)
::test_date ();
if (run_setenv)
::test_setenv (argv[0]);
if (get_env)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("checking ACE_PROCESS_TEST\n")));
char *value = ACE_OS::getenv ("ACE_PROCESS_TEST");
char *value2 = ACE_OS::getenv ("ACE_PROCESS_TEST2");
ACE_DEBUG ((LM_DEBUG,
"ACE_PROCESS_TEST = %C.\n"
"ACE_PROCESS_TEST2 = %C.\n",
value == 0 ? "no value" : value,
value2 == 0 ? "no value" : value2));
}
if (run_ls)
::test_ls ();
if (run_wait)
::test_wait ();
#if defined (ACE_WIN32)
ACE_UNUSED_ARG (&win32_test_ls);
if (environment_string != 0)
win32_spawn_environment_process ();
#endif /* ACE_WIN32 */
if (print_file != 0)
test_more ();
ACE_TCHAR buf1[30];
ACE_TCHAR buf2[30];
ACE_OS::strcpy(buf1, ACE_TEXT (" -f hi honey -g \"I\'m home\""));
ACE_OS::strcpy(buf2, ACE_TEXT ("\"token 1\"\'token 2\'\"token 3\" "));
if (run_tokenizer)
{
tokenize (buf1);
tokenize (buf2);
}
return 0;
}
|