ACE_Process_Options Class Reference

Process Options. More...

#include <Process.h>

Collaboration diagram for ACE_Process_Options:

Collaboration graph
[legend]
List of all members.

Public Types

 DEFAULT_COMMAND_LINE_BUF_LEN = 1024
 NO_EXEC = 1
enum  { DEFAULT_COMMAND_LINE_BUF_LEN = 1024, NO_EXEC = 1 }

Public Member Functions

 ACE_Process_Options (bool inherit_environment=true, int command_line_buf_len=DEFAULT_COMMAND_LINE_BUF_LEN, int env_buf_len=ENVIRONMENT_BUFFER, int max_env_args=MAX_ENVIRONMENT_ARGS)
 ~ACE_Process_Options (void)
 Destructor.
int set_handles (ACE_HANDLE std_in, ACE_HANDLE std_out=ACE_INVALID_HANDLE, ACE_HANDLE std_err=ACE_INVALID_HANDLE)
void release_handles (void)
 Release the standard handles previously set with set_handles;.
int setenv (const ACE_TCHAR *format,...)
int setenv (const ACE_TCHAR *variable_name, const ACE_TCHAR *format,...)
int setenv (ACE_TCHAR *envp[])
 Same as above with argv format. envp must be null terminated.
void working_directory (const char *wd)
int command_line (const ACE_TCHAR *format,...)
int command_line (const ACE_TCHAR *const argv[])
 Same as above in argv format. argv must be null terminated.
void process_name (const ACE_TCHAR *name)
const ACE_TCHARprocess_name (void)
u_long creation_flags (void) const
 Get the creation flags.
void creation_flags (u_long)
ACE_TCHARworking_directory (void)
 Current working directory. Returns "" if nothing has been set.
ACE_TCHARcommand_line_buf (int *max_len=0)
ACE_TCHAR *const * command_line_argv (void)
ACE_TCHARenv_buf (void)
pid_t getgroup (void) const
pid_t setgroup (pid_t pgrp)
int handle_inheritence (void)
 Allows disabling of handle inheritence, default is TRUE.
void handle_inheritence (int)
int pass_handle (ACE_HANDLE)
int dup_handles (ACE_Handle_Set &set) const
int passed_handles (ACE_Handle_Set &set) const
void avoid_zombies (int)
 Set value for avoid_zombies (has no real effect except on *nix).
int avoid_zombies (void)
 Get current value for avoid_zombies.
ACE_TCHAR *const * env_argv (void)
 argv-style array of environment settings.
ACE_HANDLE get_stdin (void) const
ACE_HANDLE get_stdout (void) const
ACE_HANDLE get_stderr (void) const
int setreugid (const ACE_TCHAR *user)
void setruid (uid_t id)
void seteuid (uid_t id)
void setrgid (uid_t id)
void setegid (uid_t id)
uid_t getruid (void) const
uid_t geteuid (void) const
uid_t getrgid (void) const
uid_t getegid (void) const
bool inherit_environment (void) const
void inherit_environment (bool nv)

Protected Types

 MAX_COMMAND_LINE_OPTIONS = 128
 ENVIRONMENT_BUFFER = 16 * 1024
 MAX_ENVIRONMENT_ARGS = 512
enum  { MAX_COMMAND_LINE_OPTIONS = 128, ENVIRONMENT_BUFFER = 16 * 1024, MAX_ENVIRONMENT_ARGS = 512 }

Protected Member Functions

int setenv_i (ACE_TCHAR *assignment, size_t len)

Protected Attributes

bool inherit_environment_
u_long creation_flags_
 Default 0.
int avoid_zombies_
 Avoid zombies for spawned processes.
ACE_HANDLE stdin_
ACE_HANDLE stdout_
ACE_HANDLE stderr_
uid_t ruid_
uid_t euid_
uid_t rgid_
uid_t egid_
int set_handles_called_
 Is 1 if stdhandles was called.
size_t environment_buf_index_
int environment_argv_index_
 Pointer to environment_argv_.
ACE_TCHARenvironment_buf_
 Pointer to buffer of the environment settings.
size_t environment_buf_len_
 Size of the environment buffer. Configurable.
ACE_TCHAR ** environment_argv_
 Pointers into environment_buf_.
int max_environment_args_
 Maximum number of environment variables. Configurable.
int max_environ_argv_index_
 Maximum index of environment_argv_ buffer.
ACE_TCHAR working_directory_ [MAXPATHLEN+1]
 The current working directory.
int command_line_argv_calculated_
 Ensures command_line_argv is only calculated once.
ACE_TCHARcommand_line_buf_
 Pointer to buffer of command-line arguments. E.g., "-f foo -b bar".
ACE_TCHARcommand_line_copy_
int command_line_buf_len_
 Max length of command_line_buf_.
ACE_TCHARcommand_line_argv_ [MAX_COMMAND_LINE_OPTIONS]
 Argv-style command-line arguments.
pid_t process_group_
 Process-group on Unix; unused on Win32.
ACE_Handle_Set handles_passed_
 Set of handles that were passed in pass_handle ().
ACE_Handle_Set dup_handles_
 Results of duplicating handles passed in pass_handle ().
ACE_TCHAR process_name_ [MAXPATHLEN+1]

Detailed Description

Process Options.

This class controls the options passed to <CreateProcess> (or <fork> and <exec>). Notice that on Windows CE, creating a process merely means instantiating a new process. You can't set the handles (since there's no stdin, stdout and stderr,) specify process/thread options, set environment,... So, basically, this class only set the command line and nothing else. Notice that on UNIX platforms, if the <setenv> is used, the <spawn> is using the <execve> system call. It means that the <command_line> should include a full path to the program file (<execve> does not search the PATH). If <setenv> is not used then, the <spawn> is using the <execvp> which searches for the program file in the PATH variable.

Definition at line 52 of file Process.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
DEFAULT_COMMAND_LINE_BUF_LEN 
NO_EXEC 

Definition at line 55 of file Process.h.

00056   {
00057     DEFAULT_COMMAND_LINE_BUF_LEN = 1024,
00058     // UNIX process creation flags.
00059 #if defined (ACE_WIN32)
00060     NO_EXEC = 0
00061 #else
00062     NO_EXEC = 1
00063 #endif /* ACE_WIN32 */
00064   };

anonymous enum [protected]

Todo:
These sizes should be taken from the appropriate POSIX/system header files and/or defined dynamically.
Enumerator:
MAX_COMMAND_LINE_OPTIONS 
ENVIRONMENT_BUFFER 
MAX_ENVIRONMENT_ARGS 

Definition at line 71 of file Process.h.

00072   {
00073     MAX_COMMAND_LINE_OPTIONS = 128,
00074     ENVIRONMENT_BUFFER = 16 * 1024, // 16K
00075     MAX_ENVIRONMENT_ARGS = 512 //
00076   };


Constructor & Destructor Documentation

ACE_Process_Options::ACE_Process_Options ( bool  inherit_environment = true,
int  command_line_buf_len = DEFAULT_COMMAND_LINE_BUF_LEN,
int  env_buf_len = ENVIRONMENT_BUFFER,
int  max_env_args = MAX_ENVIRONMENT_ARGS 
)

If inherit_environment == true, the new process will inherit the environment of the current process. command_line_buf_len is the max strlen for command-line arguments.

Definition at line 715 of file Process.cpp.

References ACE_NEW, command_line_buf_, environment_argv_, environment_buf_, ACE_OS::memset(), process_name_, and working_directory_.

00719   :
00720 #if !defined (ACE_HAS_WINCE)
00721     inherit_environment_ (inherit_environment),
00722 #endif /* ACE_HAS_WINCE */
00723     creation_flags_ (0),
00724     avoid_zombies_ (0),
00725 #if !defined (ACE_HAS_WINCE)
00726 #if defined (ACE_WIN32)
00727     environment_inherited_ (0),
00728     handle_inheritence_ (TRUE),
00729     process_attributes_ (0),
00730     thread_attributes_ (0),
00731 #else /* ACE_WIN32 */
00732     stdin_ (ACE_INVALID_HANDLE),
00733     stdout_ (ACE_INVALID_HANDLE),
00734     stderr_ (ACE_INVALID_HANDLE),
00735     ruid_ ((uid_t) -1),
00736     euid_ ((uid_t) -1),
00737     rgid_ ((uid_t) -1),
00738     egid_ ((uid_t) -1),
00739 #endif /* ACE_WIN32 */
00740     set_handles_called_ (0),
00741     environment_buf_index_ (0),
00742     environment_argv_index_ (0),
00743     environment_buf_ (0),
00744     environment_buf_len_ (env_buf_len),
00745     max_environment_args_ (max_env_args),
00746     max_environ_argv_index_ (max_env_args - 1),
00747 #endif /* !ACE_HAS_WINCE */
00748     command_line_argv_calculated_ (0),
00749     command_line_buf_ (0),
00750     command_line_copy_ (0),
00751     command_line_buf_len_ (command_line_buf_len),
00752     process_group_ (ACE_INVALID_PID)
00753 {
00754   ACE_NEW (command_line_buf_,
00755            ACE_TCHAR[command_line_buf_len]);
00756   command_line_buf_[0] = '\0';
00757 
00758 #if !defined (ACE_HAS_WINCE)
00759   working_directory_[0] = '\0';
00760   ACE_NEW (environment_buf_,
00761            ACE_TCHAR[env_buf_len]);
00762   ACE_NEW (environment_argv_,
00763            ACE_TCHAR *[max_env_args]);
00764   environment_buf_[0] = '\0';
00765   environment_argv_[0] = 0;
00766   process_name_[0] = '\0';
00767 #if defined (ACE_WIN32)
00768   ACE_OS::memset ((void *) &this->startup_info_,
00769                   0,
00770                   sizeof this->startup_info_);
00771   this->startup_info_.cb = sizeof this->startup_info_;
00772 #endif /* ACE_WIN32 */
00773 #endif /* !ACE_HAS_WINCE */
00774 }

ACE_Process_Options::~ACE_Process_Options ( void   ) 

Destructor.

Definition at line 1055 of file Process.cpp.

References command_line_buf_, command_line_copy_, environment_argv_, environment_buf_, release_handles(), and ACE::strdelete().

01056 {
01057 #if !defined (ACE_HAS_WINCE)
01058   release_handles();
01059   delete [] environment_buf_;
01060   delete [] environment_argv_;
01061 #endif /* !ACE_HAS_WINCE */
01062   delete [] command_line_buf_;
01063   ACE::strdelete (command_line_copy_);
01064 }


Member Function Documentation

ACE_INLINE int ACE_Process_Options::avoid_zombies ( void   ) 

Get current value for avoid_zombies.

Definition at line 153 of file Process.inl.

References avoid_zombies_.

00154 {
00155   return avoid_zombies_;
00156 }

ACE_INLINE void ACE_Process_Options::avoid_zombies ( int   ) 

Set value for avoid_zombies (has no real effect except on *nix).

Definition at line 158 of file Process.inl.

References avoid_zombies_.

Referenced by ACE_Process::spawn().

00159 {
00160   avoid_zombies_ = avoid_zombies;
00161 }

int ACE_Process_Options::command_line ( const ACE_TCHAR *const   argv[]  ) 

Same as above in argv format. argv must be null terminated.

Definition at line 1067 of file Process.cpp.

References ACE_TEXT, command_line_argv_calculated_, command_line_buf_, and ACE_OS::strcat().

01068 {
01069   // @@ Factor out the code between this
01070   int i = 0;
01071 
01072   if (argv[i])
01073     {
01074       ACE_OS::strcat (command_line_buf_, argv[i]);
01075       while (argv[++i])
01076         {
01077           ACE_OS::strcat (command_line_buf_,
01078                           ACE_TEXT (" "));
01079           ACE_OS::strcat (command_line_buf_,
01080                           argv[i]);
01081         }
01082     }
01083 
01084   command_line_argv_calculated_ = 0;
01085   return 0; // Success.
01086 }

int ACE_Process_Options::command_line ( const ACE_TCHAR format,
  ... 
)

Set the command-line arguments. format can use any printf formats. The first token in format should be the path to the application. This can either be a full path, relative path, or just an executable name. If an executable name is used, we rely on the platform's support for searching paths. Since we need a path to run a process, this method *must* be called! Returns 0 on success, -1 on failure.

Definition at line 1089 of file Process.cpp.

References command_line_argv_calculated_, command_line_buf_, command_line_buf_len_, ACE_OS::vsnprintf(), and ACE_OS::vsprintf().

01090 {
01091   // Store all ... args in argp.
01092   va_list argp;
01093   va_start (argp, format);
01094 
01095   if (command_line_buf_len_ < 1)
01096     return -1;
01097 
01098 #if !defined (ACE_LACKS_VSNPRINTF) || defined (ACE_HAS_TRIO)
01099   // vsnprintf the format and args into command_line_buf__.
01100   ACE_OS::vsnprintf (command_line_buf_,
01101                      command_line_buf_len_,
01102                      format,
01103                      argp);
01104 #else
01105   // sprintf the format and args into command_line_buf__.
01106   ACE_OS::vsprintf (command_line_buf_,
01107                     format,
01108                     argp);
01109 #endif
01110 
01111   // Useless macro.
01112   va_end (argp);
01113 
01114   command_line_argv_calculated_ = 0;
01115   return 0;
01116 }

ACE_TCHAR *const * ACE_Process_Options::command_line_argv ( void   ) 

argv-style command-line options. Parses and modifies the string created from <command_line_>. All spaces not in quotes ("" or '') are replaced with null () bytes. An argv array is built and returned with each entry pointing to the start of null-terminated string. Returns { 0 } if nothing has been set.

Definition at line 1167 of file Process.cpp.

References command_line_argv_, command_line_argv_calculated_, command_line_buf_, command_line_copy_, ACE_Tokenizer::delimiter_replace(), MAX_COMMAND_LINE_OPTIONS, ACE_Tokenizer::next(), ACE_Tokenizer::preserve_designators(), ACE::strdelete(), and ACE::strnew().

Referenced by ACE_Process::spawn().

01168 {
01169   if (command_line_argv_calculated_ == 0)
01170     {
01171       command_line_argv_calculated_ = 1;
01172 
01173       // We need to free up any previous allocated memory first.
01174       ACE::strdelete (command_line_copy_);
01175 
01176       // We need to make a dynamically allocated copy here since
01177       // ACE_Tokenizer modifies its arguments.
01178       command_line_copy_ = ACE::strnew (command_line_buf_);
01179       // This tokenizer will replace all spaces with end-of-string
01180       // characters and will preserve text between "" and '' pairs.
01181       ACE_Tokenizer parser (command_line_copy_);
01182       parser.delimiter_replace (' ', '\0');
01183       parser.preserve_designators ('\"', '\"'); // "
01184       parser.preserve_designators ('\'', '\'');
01185 
01186       int x = 0;
01187       do
01188         command_line_argv_[x] = parser.next ();
01189       while (command_line_argv_[x] != 0
01190              // substract one for the ending zero.
01191              && ++x < MAX_COMMAND_LINE_OPTIONS - 1);
01192 
01193       command_line_argv_[x] = 0;
01194     }
01195 
01196   return command_line_argv_;
01197 }

ACE_INLINE ACE_TCHAR * ACE_Process_Options::command_line_buf ( int *  max_len = 0  ) 

Buffer of command-line options. Returns a pointer to a buffer that contains the list of command line options. Prior to a call to command_line_argv(), this is a single string of space separated arguments independent of which form of command_line() was used to create it. After a call to command_line_argv(), this is a list of strings each terminated by ''. [Note: spawn() will call command_line_argv().] The total length of all these strings is the same as the single string in the prior case and can be obtained by providing max_len.

Definition at line 321 of file Process.inl.

References command_line_buf_, and command_line_buf_len_.

Referenced by ACE_Process::spawn().

00322 {
00323   if (max_lenp != 0)
00324     *max_lenp = this->command_line_buf_len_;
00325   return this->command_line_buf_;
00326 }

ACE_INLINE void ACE_Process_Options::creation_flags ( u_long   ) 

Set the creation flags to affect how a new process is spawned. The only ACE-defined flag is NO_EXEC which prevents the new process from executing a new program image; this is a simple POSIX fork(). The NO_EXEC option has no affect on Windows; on other platforms where a POSIX fork is not possible, specifying NO_EXEC will cause ACE_Process::spawn() to fail.

On Windows, the value of creation_flags is passed to the CreateProcess system call as the value of the dwCreationFlags parameter.

Definition at line 112 of file Process.inl.

References creation_flags_.

00113 {
00114   creation_flags_ = cf;
00115 }

ACE_INLINE u_long ACE_Process_Options::creation_flags ( void   )  const

Get the creation flags.

Definition at line 102 of file Process.inl.

References creation_flags_.

Referenced by ACE_Process::spawn().

00103 {
00104 #if defined (ACE_USES_WCHAR) && defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
00105   return creation_flags_ | CREATE_UNICODE_ENVIRONMENT;
00106 #else
00107   return creation_flags_;
00108 #endif /* ACE_USES_WCHAR */
00109 }

int ACE_Process_Options::dup_handles ( ACE_Handle_Set set  )  const

Any handles created through duplication of those passed into

Definition at line 1245 of file Process.cpp.

References dup_handles_, and ACE_Handle_Set::reset().

Referenced by ACE_Process::spawn().

01246 {
01247   if (this->dup_handles_.num_set () == 0)
01248     return 0;
01249   set.reset ();
01250   set = this->dup_handles_;
01251   return 1;
01252 }

ACE_TCHAR *const * ACE_Process_Options::env_argv ( void   ) 

argv-style array of environment settings.

Definition at line 814 of file Process.cpp.

References environment_argv_.

Referenced by ACE_Process::spawn().

00815 {
00816   return environment_argv_;
00817 }

ACE_TCHAR * ACE_Process_Options::env_buf ( void   ) 

Null-terminated buffer of null terminated strings. Each string is an environment assignment "VARIABLE=value". This buffer should end with two null characters.

Definition at line 1154 of file Process.cpp.

References environment_buf_.

Referenced by ACE_Process::spawn().

01155 {
01156 #if !defined (ACE_HAS_WINCE)
01157   if (environment_buf_[0] == '\0')
01158     return 0;
01159   else
01160     return environment_buf_;
01161 #else
01162   return 0;
01163 #endif /* !ACE_HAS_WINCE */
01164 }

ACE_INLINE ACE_HANDLE ACE_Process_Options::get_stderr ( void   )  const

Definition at line 232 of file Process.inl.

References stderr_.

Referenced by ACE_Process::spawn().

00233 {
00234   return stderr_;
00235 }

ACE_INLINE ACE_HANDLE ACE_Process_Options::get_stdin ( void   )  const

Definition at line 220 of file Process.inl.

References stdin_.

Referenced by ACE_Process::spawn().

00221 {
00222   return stdin_;
00223 }

ACE_INLINE ACE_HANDLE ACE_Process_Options::get_stdout ( void   )  const

Definition at line 226 of file Process.inl.

References stdout_.

Referenced by ACE_Process::spawn().

00227 {
00228   return stdout_;
00229 }

ACE_INLINE uid_t ACE_Process_Options::getegid ( void   )  const

Definition at line 314 of file Process.inl.

References egid_.

Referenced by ACE_Process::spawn().

00315 {
00316   return this->egid_;
00317 }

ACE_INLINE uid_t ACE_Process_Options::geteuid ( void   )  const

Definition at line 302 of file Process.inl.

References euid_.

Referenced by ACE_Process::spawn().

00303 {
00304   return this->euid_;
00305 }

ACE_INLINE pid_t ACE_Process_Options::getgroup ( void   )  const

Get the process group. On UNIX, these methods are used by the ACE_Process_Manager to manage groups of processes.

Definition at line 118 of file Process.inl.

References process_group_.

Referenced by ACE_Process::spawn().

00119 {
00120   return process_group_;
00121 }

ACE_INLINE uid_t ACE_Process_Options::getrgid ( void   )  const

Definition at line 308 of file Process.inl.

References rgid_.

Referenced by ACE_Process::spawn().

00309 {
00310   return this->rgid_;
00311 }

ACE_INLINE uid_t ACE_Process_Options::getruid ( void   )  const

Definition at line 296 of file Process.inl.

References ruid_.

Referenced by ACE_Process::spawn().

00297 {
00298   return this->ruid_;
00299 }

ACE_INLINE void ACE_Process_Options::handle_inheritence ( int   ) 

Definition at line 142 of file Process.inl.

References ACE_NOTSUP.

00143 {
00144 #if defined (ACE_WIN32) &&  !defined (ACE_HAS_WINCE)
00145   handle_inheritence_ = hi;
00146 #else
00147   ACE_UNUSED_ARG (hi);
00148   ACE_NOTSUP;
00149 #endif /* !ACE_HAS_WINCE */
00150 }

ACE_INLINE int ACE_Process_Options::handle_inheritence ( void   ) 

Allows disabling of handle inheritence, default is TRUE.

Definition at line 132 of file Process.inl.

References ACE_NOTSUP_RETURN.

Referenced by ACE_Process::spawn().

00133 {
00134 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
00135   return handle_inheritence_;
00136 #else
00137   ACE_NOTSUP_RETURN (0);  // This is a benign error.
00138 #endif /* ACE_WIN32 && ! ACE_HAS_WINCE */
00139 }

ACE_INLINE void ACE_Process_Options::inherit_environment ( bool  nv  ) 

Set the inherit_environment flag.

Definition at line 244 of file Process.inl.

References inherit_environment_.

00245 {
00246   inherit_environment_ = nv;
00247 }

ACE_INLINE bool ACE_Process_Options::inherit_environment ( void   )  const

Get the inherit_environment flag.

Definition at line 238 of file Process.inl.

References inherit_environment_.

Referenced by setenv(), and ACE_Process::spawn().

00239 {
00240   return inherit_environment_;
00241 }

int ACE_Process_Options::pass_handle ( ACE_HANDLE   ) 

The specified handle value will be included in the spawned process's command line as

Definition at line 1202 of file Process.cpp.

References ACE_NOTSUP_RETURN, dup_handles_, handles_passed_, and ACE_Handle_Set::set_bit().

01203 {
01204 # if defined (ACE_WIN32)
01205 #  if defined (ACE_HAS_WINCE)
01206   ACE_NOTSUP_RETURN (-1);
01207 #  else
01208 
01209   // This is oriented towards socket handles... may need some adjustment
01210   // for non-sockets.
01211   // This is all based on an MSDN article:
01212   // http://support.microsoft.com/support/kb/articles/Q150/5/23.asp
01213   // If on Win95/98, the handle needs to be duplicated for the to-be-spawned
01214   // process. On WinNT, they get inherited by the child process automatically.
01215   // If the handle is duplicated, remember the duplicate so it can be
01216   // closed later. Can't be closed now, or the child won't get it.
01217   ACE_TEXT_OSVERSIONINFO osvi;
01218   ZeroMemory (&osvi, sizeof (osvi));
01219   osvi.dwOSVersionInfoSize = sizeof (ACE_TEXT_OSVERSIONINFO);
01220   // If this is Win95/98 or we can't tell, duplicate the handle.
01221   if (!ACE_TEXT_GetVersionEx (&osvi) || osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)
01222     {
01223       HANDLE dup_handle;
01224       if (!DuplicateHandle (GetCurrentProcess (),
01225                             static_cast<HANDLE> (h),
01226                             GetCurrentProcess (),
01227                             &dup_handle,
01228                             0,
01229                             TRUE,   // Inheritable
01230                             DUPLICATE_SAME_ACCESS))
01231         return -1;
01232       dup_handles_.set_bit (static_cast<ACE_HANDLE> (dup_handle));
01233     }
01234 #  endif /* ACE_HAS_WINCE */
01235 #endif /* ACE_WIN32 */
01236 
01237   this->handles_passed_.set_bit (h);
01238 
01239   return 0;
01240 }

int ACE_Process_Options::passed_handles ( ACE_Handle_Set set  )  const

Any handles previously passed to

Definition at line 1257 of file Process.cpp.

References handles_passed_, and ACE_Handle_Set::reset().

Referenced by ACE_Process::spawn().

01258 {
01259   if (this->handles_passed_.num_set () == 0)
01260     return 0;
01261   set.reset ();
01262   set = this->handles_passed_;
01263   return 1;
01264 }

ACE_INLINE const ACE_TCHAR * ACE_Process_Options::process_name ( void   ) 

Return the process_name. If the <process_name(name)> set method is not called, this method will return argv[0].

Definition at line 370 of file Process.inl.

References process_name_.

00371 {
00372   if (process_name_[0] == '\0')
00373     this->process_name (this->command_line_argv ()[0]);
00374 
00375   return this->process_name_;
00376 }

ACE_INLINE void ACE_Process_Options::process_name ( const ACE_TCHAR name  ) 

Specify the full path or relative path, or just the executable name for the process. If this is set, then name will be used to create the process instead of argv[0] set in the command line. This is here so that you can supply something other than executable name as argv[0].

Definition at line 364 of file Process.inl.

References ACE_OS::strcpy().

Referenced by ACE_Process::spawn().

00365 {
00366   ACE_OS::strcpy (this->process_name_, p);
00367 }

void ACE_Process_Options::release_handles ( void   ) 

Release the standard handles previously set with set_handles;.

Definition at line 1036 of file Process.cpp.

References ACE_OS::close(), set_handles_called_, stderr_, stdin_, and stdout_.

Referenced by ~ACE_Process_Options().

01037 {
01038   if (set_handles_called_)
01039     {
01040 #if defined (ACE_WIN32)
01041       ACE_OS::close (startup_info_.hStdInput);
01042       ACE_OS::close (startup_info_.hStdOutput);
01043       ACE_OS::close (startup_info_.hStdError);
01044 #else /* ACE_WIN32 */
01045       ACE_OS::close (stdin_);
01046       ACE_OS::close (stdout_);
01047       ACE_OS::close (stderr_);
01048 #endif /* ACE_WIN32 */
01049       set_handles_called_ = 0;
01050     }
01051 }

int ACE_Process_Options::set_handles ( ACE_HANDLE  std_in,
ACE_HANDLE  std_out = ACE_INVALID_HANDLE,
ACE_HANDLE  std_err = ACE_INVALID_HANDLE 
)

Set the standard handles of the new process to the respective handles. If you want to affect a subset of the handles, make sure to set the others to ACE_INVALID_HANDLE. Returns 0 on success, -1 on failure.

Definition at line 982 of file Process.cpp.

References ACE_STDERR, ACE_STDIN, ACE_STDOUT, ACE_OS::dup(), set_handles_called_, stderr_, stdin_, and stdout_.

00985 {
00986   this->set_handles_called_ = 1;
00987 #if defined (ACE_WIN32)
00988 
00989   // Tell the new process to use our std handles.
00990   this->startup_info_.dwFlags = STARTF_USESTDHANDLES;
00991 
00992   if (std_in == ACE_INVALID_HANDLE)
00993     std_in = ACE_STDIN;
00994   if (std_out == ACE_INVALID_HANDLE)
00995     std_out = ACE_STDOUT;
00996   if (std_err == ACE_INVALID_HANDLE)
00997     std_err = ACE_STDERR;
00998 
00999   if (!::DuplicateHandle (::GetCurrentProcess (),
01000                           std_in,
01001                           ::GetCurrentProcess (),
01002                           &this->startup_info_.hStdInput,
01003                           0,
01004                           TRUE,
01005                           DUPLICATE_SAME_ACCESS))
01006     return -1;
01007 
01008   if (!::DuplicateHandle (::GetCurrentProcess (),
01009                           std_out,
01010                           ::GetCurrentProcess (),
01011                           &this->startup_info_.hStdOutput,
01012                           0,
01013                           TRUE,
01014                           DUPLICATE_SAME_ACCESS))
01015     return -1;
01016 
01017   if (!::DuplicateHandle (::GetCurrentProcess (),
01018                           std_err,
01019                           ::GetCurrentProcess (),
01020                           &this->startup_info_.hStdError,
01021                           0,
01022                           TRUE,
01023                           DUPLICATE_SAME_ACCESS))
01024     return -1;
01025 #else /* ACE_WIN32 */
01026   this->stdin_ = ACE_OS::dup (std_in);
01027   this->stdout_ = ACE_OS::dup (std_out);
01028   this->stderr_ = ACE_OS::dup (std_err);
01029 #endif /* ACE_WIN32 */
01030 
01031   return 0; // Success.
01032 }

ACE_INLINE void ACE_Process_Options::setegid ( uid_t  id  ) 

Definition at line 290 of file Process.inl.

References egid_.

00291 {
00292   this->egid_ = id;
00293 }

int ACE_Process_Options::setenv ( ACE_TCHAR envp[]  ) 

Same as above with argv format. envp must be null terminated.

Definition at line 822 of file Process.cpp.

References inherit_environment(), inherit_environment_, and ACE_OS::strlen().

00823 {
00824   int i = 0;
00825   while (envp[i])
00826     {
00827       if (this->setenv_i (envp[i],
00828                           ACE_OS::strlen (envp[i])) == -1)
00829         return -1;
00830       i++;
00831     }
00832 
00833 #if defined (ACE_WIN32)
00834   if (inherit_environment_)
00835     this->inherit_environment ();
00836 #endif /* ACE_WIN32 */
00837 
00838   return 0;
00839 }

int ACE_Process_Options::setenv ( const ACE_TCHAR variable_name,
const ACE_TCHAR format,
  ... 
)

Set a single environment variable, variable_name. Since different platforms separate each environment variable differently, you must call this method once for each variable. <format> can be any printf format string. So options->setenv ("FOO","one + two = %s", "three") will result in "FOO=one + two = three".

Definition at line 871 of file Process.cpp.

References ACE_NEW_RETURN, ACE_TEXT, DEFAULT_COMMAND_LINE_BUF_LEN, ENOTSUP, ACE_Auto_Basic_Array_Ptr< X >::get(), inherit_environment(), inherit_environment_, ACE_Auto_Basic_Array_Ptr< X >::reset(), ACE_OS::sprintf(), ACE_OS::strlen(), ACE_OS::vsnprintf(), and ACE_OS::vsprintf().

00873 {
00874   // To address the potential buffer overflow,
00875   // we now allocate the buffer on heap with a variable size.
00876   size_t const buflen = ACE_OS::strlen (variable_name) + ACE_OS::strlen (format) + 2;
00877   ACE_TCHAR *newformat = 0;
00878   ACE_NEW_RETURN (newformat, ACE_TCHAR[buflen], -1);
00879   ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> safe_newformat (newformat);
00880 
00881   // Add in the variable name.
00882   ACE_OS::sprintf (safe_newformat.get (),
00883                    ACE_TEXT ("%s=%s"),
00884                    variable_name,
00885                    format);
00886 
00887   // Start varargs.
00888   va_list argp;
00889   va_start (argp, format);
00890 
00891   // Add the rest of the varargs.
00892   size_t tmp_buflen = DEFAULT_COMMAND_LINE_BUF_LEN > buflen
00893                       ? static_cast<size_t> (DEFAULT_COMMAND_LINE_BUF_LEN) : buflen;
00894   int retval = 0;
00895 
00896   ACE_TCHAR *stack_buf = 0;
00897   ACE_NEW_RETURN (stack_buf, ACE_TCHAR[tmp_buflen], -1);
00898   ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> safe_stack_buf (stack_buf);
00899 
00900   do
00901     {
00902       retval = ACE_OS::vsnprintf (safe_stack_buf.get (), tmp_buflen, safe_newformat.get (), argp);
00903       if (retval > ACE_Utils::truncate_cast<int> (tmp_buflen))
00904         {
00905           tmp_buflen *= 2;
00906           ACE_NEW_RETURN (stack_buf, ACE_TCHAR[tmp_buflen], -1);
00907           safe_stack_buf.reset (stack_buf);
00908         }
00909       else
00910         break;
00911     }
00912   while (1);
00913 
00914   if (retval == -1)
00915     {
00916       // In case that vsnprintf is not supported,
00917       // e.g., LynxOS and VxWorks 5, we have to
00918       // fall back to vsprintf.
00919       if (errno == ENOTSUP)
00920         {
00921           // ALERT: Since we have to use vsprintf here, there is still a chance that
00922           // the stack_buf overflows, i.e., the length of the resulting string
00923           // can still possibly go beyond the allocated stack_buf.
00924           retval = ACE_OS::vsprintf (safe_stack_buf.get (), safe_newformat.get (), argp);
00925           if (retval == -1)
00926             // vsprintf is failed.
00927             return -1;
00928         }
00929       else
00930         // vsnprintf is failed.
00931         return -1;
00932     }
00933 
00934   // End varargs.
00935   va_end (argp);
00936 
00937   // Append the string to our environment buffer.
00938   if (this->setenv_i (safe_stack_buf.get (),
00939                       ACE_OS::strlen (safe_stack_buf.get ())) == -1)
00940     return -1;
00941 
00942 #if defined (ACE_WIN32)
00943   if (inherit_environment_)
00944     this->inherit_environment ();
00945 #endif /* ACE_WIN32 */
00946 
00947   return 0;
00948 }

int ACE_Process_Options::setenv ( const ACE_TCHAR format,
  ... 
)

Parameters:
format must be of the form "VARIABLE=VALUE". There can not be any spaces between VARIABLE and the equal sign.

Definition at line 842 of file Process.cpp.

References DEFAULT_COMMAND_LINE_BUF_LEN, inherit_environment(), inherit_environment_, ACE_OS::strlen(), and ACE_OS::vsprintf().

00843 {
00844   ACE_TCHAR stack_buf[DEFAULT_COMMAND_LINE_BUF_LEN];
00845 
00846   // Start varargs.
00847   va_list argp;
00848   va_start (argp, format);
00849 
00850   // Add the rest of the varargs.
00851   ACE_OS::vsprintf (stack_buf,
00852                     format,
00853                     argp);
00854   // End varargs.
00855   va_end (argp);
00856 
00857   // Append the string to are environment buffer.
00858   if (this->setenv_i (stack_buf,
00859                       ACE_OS::strlen (stack_buf)) == -1)
00860     return -1;
00861 
00862 #if defined (ACE_WIN32)
00863   if (inherit_environment_)
00864     this->inherit_environment ();
00865 #endif /* ACE_WIN32 */
00866 
00867   return 0;
00868 }

int ACE_Process_Options::setenv_i ( ACE_TCHAR assignment,
size_t  len 
) [protected]

Add assignment to environment_buf_ and adjust environment_argv_. len is the strlen of assignment.

Definition at line 951 of file Process.cpp.

References environment_argv_, environment_argv_index_, environment_buf_, environment_buf_index_, environment_buf_len_, max_environ_argv_index_, and ACE_OS::memcpy().

00953 {
00954   // Add one for the null char.
00955   ++len;
00956 
00957   // If environment larger than allocated buffer return. Also check to
00958   // make sure we have enough room.
00959   if (environment_argv_index_ == max_environ_argv_index_
00960       || (len + environment_buf_index_) >= environment_buf_len_)
00961     return -1;
00962 
00963   // Copy the new environment string.
00964   ACE_OS::memcpy (environment_buf_ + environment_buf_index_,
00965                   assignment,
00966                   len * sizeof (ACE_TCHAR));
00967 
00968   // Update the argv array.
00969   environment_argv_[environment_argv_index_++] =
00970     environment_buf_ + environment_buf_index_;
00971   environment_argv_[environment_argv_index_] = 0;
00972 
00973   // Update our index.
00974   environment_buf_index_ += len;
00975 
00976   // Make sure the buffer is null-terminated.
00977   environment_buf_[environment_buf_index_] = '\0';
00978   return 0;
00979 }

ACE_INLINE void ACE_Process_Options::seteuid ( uid_t  id  ) 

Definition at line 278 of file Process.inl.

References euid_.

00279 {
00280   this->euid_ = id;
00281 }

ACE_INLINE pid_t ACE_Process_Options::setgroup ( pid_t  pgrp  ) 

Set the process group. On UNIX, these methods are used by the ACE_Process_Manager to manage groups of processes.

Definition at line 124 of file Process.inl.

References process_group_.

00125 {
00126   pid_t old = process_group_;
00127   process_group_ = pgrp;
00128   return old;
00129 }

ACE_INLINE int ACE_Process_Options::setreugid ( const ACE_TCHAR user  ) 

Definition at line 250 of file Process.inl.

References ACE_NOTSUP_RETURN, ACE_TEXT_ALWAYS_CHAR, egid_, euid_, ACE_OS::getpwnam(), rgid_, and ruid_.

00251 {
00252 #if !defined (ACE_LACKS_PWD_FUNCTIONS)
00253   struct passwd *ent = ACE_OS::getpwnam (ACE_TEXT_ALWAYS_CHAR (user));
00254 
00255   if (ent != 0)
00256     {
00257       this->euid_ = ent->pw_uid;
00258       this->ruid_ = ent->pw_uid;
00259       this->egid_ = ent->pw_gid;
00260       this->rgid_ = ent->pw_gid;
00261       return 0;
00262     }
00263   else
00264     return -1;
00265 #else
00266   ACE_UNUSED_ARG (user);
00267   ACE_NOTSUP_RETURN (-1);
00268 #endif /* ACE_LACKS_PWD_FUNCTIONS */
00269 }

ACE_INLINE void ACE_Process_Options::setrgid ( uid_t  id  ) 

Definition at line 284 of file Process.inl.

References rgid_.

00285 {
00286   this->rgid_ = id;
00287 }

ACE_INLINE void ACE_Process_Options::setruid ( uid_t  id  ) 

Definition at line 272 of file Process.inl.

References ruid_.

00273 {
00274   this->ruid_ = id;
00275 }

ACE_INLINE ACE_TCHAR * ACE_Process_Options::working_directory ( void   ) 

Current working directory. Returns "" if nothing has been set.

Definition at line 329 of file Process.inl.

References working_directory_.

00330 {
00331 #if !defined (ACE_HAS_WINCE)
00332   if (working_directory_[0] == '\0')
00333     return 0;
00334   else
00335     return working_directory_;
00336 #else
00337   return 0;
00338 #endif /* !ACE_HAS_WINCE */
00339 }

ACE_INLINE void ACE_Process_Options::working_directory ( const char *  wd  ) 

Set the working directory for the process. strlen of wd must be <= MAXPATHLEN.

Definition at line 342 of file Process.inl.

References ACE_TEXT_CHAR_TO_TCHAR, ACE_OS::strcpy(), and working_directory_.

Referenced by ACE_Process::spawn().

00343 {
00344 #if !defined(ACE_HAS_WINCE)
00345   ACE_OS::strcpy (working_directory_, ACE_TEXT_CHAR_TO_TCHAR (wd));
00346 #else
00347   ACE_UNUSED_ARG (wd);
00348 #endif /* !ACE_HAS_WINCE */
00349 }


Member Data Documentation

int ACE_Process_Options::avoid_zombies_ [protected]

Avoid zombies for spawned processes.

Definition at line 332 of file Process.h.

Referenced by avoid_zombies().

ACE_TCHAR* ACE_Process_Options::command_line_argv_[MAX_COMMAND_LINE_OPTIONS] [protected]

Argv-style command-line arguments.

Definition at line 416 of file Process.h.

Referenced by command_line_argv().

int ACE_Process_Options::command_line_argv_calculated_ [protected]

Ensures command_line_argv is only calculated once.

Definition at line 403 of file Process.h.

Referenced by command_line(), and command_line_argv().

ACE_TCHAR* ACE_Process_Options::command_line_buf_ [protected]

Pointer to buffer of command-line arguments. E.g., "-f foo -b bar".

Definition at line 406 of file Process.h.

Referenced by ACE_Process_Options(), command_line(), command_line_argv(), command_line_buf(), and ~ACE_Process_Options().

int ACE_Process_Options::command_line_buf_len_ [protected]

Max length of command_line_buf_.

Definition at line 413 of file Process.h.

Referenced by command_line(), and command_line_buf().

ACE_TCHAR* ACE_Process_Options::command_line_copy_ [protected]

Pointer to copy of command-line arguments, which is needed when converting a command-line string into a command-line argv.

Definition at line 410 of file Process.h.

Referenced by command_line_argv(), and ~ACE_Process_Options().

u_long ACE_Process_Options::creation_flags_ [protected]

Default 0.

Definition at line 329 of file Process.h.

Referenced by creation_flags().

ACE_Handle_Set ACE_Process_Options::dup_handles_ [protected]

Results of duplicating handles passed in pass_handle ().

Definition at line 425 of file Process.h.

Referenced by dup_handles(), and pass_handle().

uid_t ACE_Process_Options::egid_ [protected]

Definition at line 369 of file Process.h.

Referenced by getegid(), setegid(), and setreugid().

ACE_TCHAR** ACE_Process_Options::environment_argv_ [protected]

Pointers into environment_buf_.

Definition at line 390 of file Process.h.

Referenced by ACE_Process_Options(), env_argv(), setenv_i(), and ~ACE_Process_Options().

int ACE_Process_Options::environment_argv_index_ [protected]

Pointer to environment_argv_.

Definition at line 381 of file Process.h.

Referenced by setenv_i().

ACE_TCHAR* ACE_Process_Options::environment_buf_ [protected]

Pointer to buffer of the environment settings.

Definition at line 384 of file Process.h.

Referenced by ACE_Process_Options(), env_buf(), setenv_i(), and ~ACE_Process_Options().

size_t ACE_Process_Options::environment_buf_index_ [protected]

Pointer into environment_buf_. This should point to the next free spot.

Definition at line 378 of file Process.h.

Referenced by setenv_i().

size_t ACE_Process_Options::environment_buf_len_ [protected]

Size of the environment buffer. Configurable.

Definition at line 387 of file Process.h.

Referenced by setenv_i().

uid_t ACE_Process_Options::euid_ [protected]

Definition at line 367 of file Process.h.

Referenced by geteuid(), seteuid(), and setreugid().

ACE_Handle_Set ACE_Process_Options::handles_passed_ [protected]

Set of handles that were passed in pass_handle ().

Definition at line 422 of file Process.h.

Referenced by pass_handle(), and passed_handles().

bool ACE_Process_Options::inherit_environment_ [protected]

Whether the child process inherits the current process environment.

Definition at line 325 of file Process.h.

Referenced by inherit_environment(), and setenv().

int ACE_Process_Options::max_environ_argv_index_ [protected]

Maximum index of environment_argv_ buffer.

Definition at line 396 of file Process.h.

Referenced by setenv_i().

int ACE_Process_Options::max_environment_args_ [protected]

Maximum number of environment variables. Configurable.

Definition at line 393 of file Process.h.

pid_t ACE_Process_Options::process_group_ [protected]

Process-group on Unix; unused on Win32.

Definition at line 419 of file Process.h.

Referenced by getgroup(), and setgroup().

ACE_TCHAR ACE_Process_Options::process_name_[MAXPATHLEN+1] [protected]

Pathname for the process. Relative path or absolute path or just the program name.

Definition at line 429 of file Process.h.

Referenced by ACE_Process_Options(), and process_name().

uid_t ACE_Process_Options::rgid_ [protected]

Definition at line 368 of file Process.h.

Referenced by getrgid(), setreugid(), and setrgid().

uid_t ACE_Process_Options::ruid_ [protected]

Definition at line 366 of file Process.h.

Referenced by getruid(), setreugid(), and setruid().

int ACE_Process_Options::set_handles_called_ [protected]

Is 1 if stdhandles was called.

Definition at line 374 of file Process.h.

Referenced by release_handles(), and set_handles().

ACE_HANDLE ACE_Process_Options::stderr_ [protected]

Definition at line 362 of file Process.h.

Referenced by get_stderr(), release_handles(), and set_handles().

ACE_HANDLE ACE_Process_Options::stdin_ [protected]

Definition at line 360 of file Process.h.

Referenced by get_stdin(), release_handles(), and set_handles().

ACE_HANDLE ACE_Process_Options::stdout_ [protected]

Definition at line 361 of file Process.h.

Referenced by get_stdout(), release_handles(), and set_handles().

ACE_TCHAR ACE_Process_Options::working_directory_[MAXPATHLEN+1] [protected]

The current working directory.

Definition at line 399 of file Process.h.

Referenced by ACE_Process_Options(), and working_directory().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:27 2010 for ACE by  doxygen 1.4.7