1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_fcntl.h"
3 #include "ace/OS_NS_pwd.h"
4 #include "ace/os_include/os_pwd.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/OS_NS_pwd.h"
9 #include "ace/Process.h"
10 #include "ace/Log_Msg.h"
12 // Listing 1 code/ch10
13 class Manager
: public ACE_Process
16 Manager (const ACE_TCHAR
* program_name
)
18 ACE_TRACE ("Manager::Manager");
19 ACE_OS::strcpy (programName_
, program_name
);
24 ACE_TRACE ("Manager::doWork");
26 // Spawn the new process; prepare() hook is called first.
27 ACE_Process_Options options
;
28 pid_t pid
= this->spawn (options
);
29 if (pid
== ACE_INVALID_PID
)
30 ACE_ERROR_RETURN((LM_ERROR
, ACE_TEXT ("%p\n"),
31 ACE_TEXT ("spawn")), -1);
33 // Wait forever for my child to exit.
34 if (this->wait () == -1)
35 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
36 ACE_TEXT ("wait")), -1);
38 // Dump whatever happened.
45 // Listing 3 code/ch10
48 ACE_TRACE ("Manager::dumpRun");
50 if (ACE_OS::lseek (this->outputfd_
, 0, SEEK_SET
) == -1)
51 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
52 ACE_TEXT ("lseek")), -1);
57 // Read the contents of the error stream written
58 // by the child and print it out.
59 while ((length
= ACE_OS::read (this->outputfd_
,
60 buf
, sizeof(buf
)-1)) > 0)
63 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%C\n"), buf
));
66 ACE_OS::close (this->outputfd_
);
71 // Listing 2 code/ch10
72 // prepare() is inherited from ACE_Process.
73 int prepare (ACE_Process_Options
&options
)
75 ACE_TRACE ("Manager::prepare");
77 options
.command_line (ACE_TEXT ("%s 1"), this->programName_
);
78 if (this->setStdHandles (options
) == -1 ||
79 this->setEnvVariable (options
) == -1)
81 #if !defined (ACE_WIN32) && !defined (ACE_LACKS_PWD_FUNCTIONS)
82 return this->setUserID (options
);
88 int setStdHandles (ACE_Process_Options
&options
)
90 ACE_TRACE ("Manager::setStdHandles");
92 ACE_OS::unlink ("output.dat");
94 ACE_OS::open ("output.dat", O_RDWR
| O_CREAT
);
95 return options
.set_handles
96 (ACE_STDIN
, ACE_STDOUT
, this->outputfd_
);
99 int setEnvVariable (ACE_Process_Options
&options
)
101 ACE_TRACE ("Manager::setEnvVariables");
102 return options
.setenv
103 (ACE_TEXT ("PRIVATE_VAR=/that/seems/to/be/it"));
107 #if !defined (ACE_LACKS_PWD_FUNCTIONS)
108 // Listing 10 code/ch10
109 int setUserID (ACE_Process_Options
&options
)
111 ACE_TRACE ("Manager::setUserID");
112 passwd
* pw
= ACE_OS::getpwnam ("nobody");
115 options
.seteuid (pw
->pw_uid
);
119 #endif /* !ACE_LACKS_PWD_FUNCTIONS */
122 ACE_HANDLE outputfd_
;
123 ACE_TCHAR programName_
[256];
126 // Listing 4 code/ch10
132 ACE_TRACE ("Slave::Slave");
137 ACE_TRACE ("Slave::doWork");
140 ACE_TEXT ("(%P) started at %T, parent is %d\n"),
141 ACE_OS::getppid ()));
144 ACE_TEXT ("(%P) the private environment is %s\n"),
145 ACE_OS::getenv ("PRIVATE_VAR")));
148 ACE_OS::sprintf (str
, ACE_TEXT ("(%d) Enter your command\n"),
149 static_cast<int>(ACE_OS::getpid ()));
150 ACE_OS::write (ACE_STDOUT
, str
, ACE_OS::strlen (str
));
151 this->readLine (str
);
152 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%P) Executed: %C\n"),
160 ACE_TRACE ("Slave::showWho");
161 #if !defined (ACE_LACKS_PWD_FUNCTIONS)
162 passwd
*pw
= ::getpwuid (ACE_OS::geteuid ());
164 ACE_TEXT ("(%P) Running this process as:%s\n"),
169 ACE_TCHAR
* readLine (ACE_TCHAR
* str
)
171 ACE_TRACE ("Slave::readLine");
176 ssize_t retval
= ACE_OS::read (ACE_STDIN
, &str
[i
], 1);
192 // Listing 0 code/ch10
193 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
195 if (argc
> 1) // Slave mode