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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Process_Test.cpp
//
// = DESCRIPTION
// Tests ACE_Process file handle inheritance for UNIX-like systems
//
// = AUTHOR
// Christian Fromme <kaner@strace.org>
//
// ============================================================================
#include "test_config.h"
#include "ace/Process.h"
#include "ace/Get_Opt.h"
#include "ace/ACE.h"
#include "ace/OS_NS_sys_stat.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Dirent.h"
#include "ace/SString.h"
ACE_RCSID(tests, Process_Test, "Process_Test.cpp,v 4.11 1999/09/02 04:36:30 schmidt Exp")
// This will only work on UNIX-like with /proc filesys.
static const ACE_TCHAR *proc_self_fd = ACE_TEXT ("/proc/self/fd/");
int
check_temp_file (const ACE_TString &tmpfilename)
{
ACE_DIRENT *dir = 0;
ACE_Dirent entr;
ACE_stat stat;
char filename[MAXPATHLEN + 1];
ACE_OS::memset (&stat, 0, sizeof (stat));
ACE_OS::memset (&entr, 0, sizeof (entr));
// Loop through /proc/self/fs/
if (entr.open (proc_self_fd) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Could not open dir %s\n"),
proc_self_fd),
-1);
while ((dir = entr.read ()))
{
ACE_TString fullp = proc_self_fd;
fullp += dir->d_name;
if ((ACE_OS::lstat (fullp.c_str (), &stat)) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Stat failed for %s\n"),
fullp.c_str ()),
-1);
if (S_ISLNK (stat.st_mode))
{
ssize_t size = 0;
if ((size= ACE_OS::readlink (fullp.c_str (),
filename,
MAXPATHLEN + 1)) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Readlink failed for %s\n"),
fullp.c_str ()),
-1);
filename[size] = '\0';
if (tmpfilename == ACE_TString (filename))
return 1;
}
}
return 0;
}
void
run_parent (bool inherit_files)
{
ACE_TCHAR t[] = ACE_TEXT ("ace_testXXXXXX");
// Create tempfile. This will be tested for inheritance.
ACE_TCHAR tempfile[MAXPATHLEN + 1];
if (ACE::get_temp_dir (tempfile, MAXPATHLEN - sizeof (t)) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could not get temp dir\n")));
ACE_OS::strcat (tempfile, t);
int result = ACE_OS::mkstemp (tempfile);
if (result == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could not get temp filename\n")));
// Build child options
ACE_Process_Options options;
options.command_line (ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("Process_Test")
ACE_PLATFORM_EXE_SUFFIX
ACE_TEXT (" -c -h %d -f %s"),
(int)inherit_files,
tempfile);
options.handle_inheritance (inherit_files); /* ! */
// Spawn child
ACE_Process child;
result = child.spawn (options);
if (result != -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Parent could NOT spawn child process\n")));
else
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Parent spawned child process with pid = %d.\n"),
child.getpid ()));
ACE_exitcode child_status;
result = child.wait (&child_status);
if (result != -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could NOT wait on child process\n")));
else if (child_status == 0)
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Child %d finished ok\n"),
child.getpid ()));
else
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Child %d finished with status %d\n"),
child.getpid (), child_status));
}
int
run_main (int argc, ACE_TCHAR *argv[])
{
#if defined (ACE_LACKS_FORK) || defined (ACE_LACKS_READLINK)
ACE_UNUSED_ARG (argc);
ACE_UNUSED_ARG (argv);
ACE_START_TEST (ACE_TEXT ("Process_Test"));
ACE_ERROR ((LM_INFO,
ACE_TEXT ("This test is not supported on this platform\n")));
ACE_END_TEST;
#else
int c = 0;
int handle_inherit = 0; /* Disable inheritance by default */
bool ischild = false;
ACE_TString temp_file_name;
ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("ch:f:"));
while ((c = getopt ()) != -1)
switch ((char) c)
{
case 'c':
ischild = true;
break;
case 'h':
handle_inherit = ACE_OS::atoi (getopt.opt_arg ());
break;
case 'f':
temp_file_name = getopt.opt_arg ();
break;
default:
// Don't forgive bad options
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Bad option\n")),
-1);
break;
}
if (ischild)
{
ACE_TCHAR lognm[MAXPATHLEN];
int mypid (ACE_OS::getpid ());
ACE_OS::sprintf(lognm, ACE_TEXT ("Process_Test-child-%d"), mypid);
ACE_START_TEST (lognm);
int result = check_temp_file (temp_file_name);
// Check descriptor inheritance
if (result == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Could not retrieve open files\n")),
-1);
else if (result == handle_inherit)
result = 0;
else
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Handle inheritance test failed\n")));
ACE_END_LOG;
return result;
}
else
{
ACE_START_TEST (ACE_TEXT ("Process_Test"));
// Test handle inheritance set to true
run_parent (true);
// ... and set to false
run_parent (false);
ACE_END_TEST;
}
#endif /* ! ACE_LACKS_FORK */
return 0;
}
|