Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

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

enum  { DEFAULT_COMMAND_LINE_BUF_LEN = 1024, NO_EXEC = 1 }

Public Member Functions

 ACE_Process_Options (bool inherit_environment=true, size_t command_line_buf_len=DEFAULT_COMMAND_LINE_BUF_LEN, size_t env_buf_len=ENVIRONMENT_BUFFER, size_t 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_inheritance (void)
 Allows disabling of handle inheritance, default is TRUE.
void handle_inheritance (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.
void enable_unicode_environment (void)
void disable_unicode_environment (void)
 Disable the use of a Unicode environment.
bool use_unicode_environment (void) const
 Return the unicode environment status.
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

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_
bool handle_inheritance_
 Default true.
int set_handles_called_
 Is 1 if stdhandles was called.
size_t environment_buf_index_
size_t 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_.
size_t max_environment_args_
 Maximum number of environment variables. Configurable.
size_t max_environ_argv_index_
 Maximum index of environment_argv_ buffer.
ACE_TCHAR working_directory_ [MAXPATHLEN+1]
 The current working directory.
bool 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_
size_t 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]
bool use_unicode_environment_
 Indicate if a Unicode environment should be used.

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.

  {
    DEFAULT_COMMAND_LINE_BUF_LEN = 1024,
    // UNIX process creation flags.
#if defined (ACE_WIN32)
    NO_EXEC = 0
#else
    NO_EXEC = 1
#endif /* ACE_WIN32 */
  };

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.

  {
    MAX_COMMAND_LINE_OPTIONS = 128,
    ENVIRONMENT_BUFFER = 16 * 1024, // 16K
    MAX_ENVIRONMENT_ARGS = 512 //
  };


Constructor & Destructor Documentation

ACE_Process_Options::ACE_Process_Options ( bool  inherit_environment = true,
size_t  command_line_buf_len = DEFAULT_COMMAND_LINE_BUF_LEN,
size_t  env_buf_len = ENVIRONMENT_BUFFER,
size_t  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 796 of file Process.cpp.

  :
#if !defined (ACE_HAS_WINCE)
    inherit_environment_ (inherit_environment),
#endif /* ACE_HAS_WINCE */
    creation_flags_ (0),
    avoid_zombies_ (0),
#if !defined (ACE_HAS_WINCE)
#if defined (ACE_WIN32)
    environment_inherited_ (0),
    process_attributes_ (0),
    thread_attributes_ (0),
#else /* ACE_WIN32 */
    stdin_ (ACE_INVALID_HANDLE),
    stdout_ (ACE_INVALID_HANDLE),
    stderr_ (ACE_INVALID_HANDLE),
    ruid_ ((uid_t) -1),
    euid_ ((uid_t) -1),
    rgid_ ((uid_t) -1),
    egid_ ((uid_t) -1),
#endif /* ACE_WIN32 */
    handle_inheritance_ (true),
    set_handles_called_ (0),
    environment_buf_index_ (0),
    environment_argv_index_ (0),
    environment_buf_ (0),
    environment_buf_len_ (env_buf_len),
    max_environment_args_ (max_env_args),
    max_environ_argv_index_ (max_env_args - 1),
#endif /* !ACE_HAS_WINCE */
    command_line_argv_calculated_ (false),
    command_line_buf_ (0),
    command_line_copy_ (0),
    command_line_buf_len_ (command_line_buf_len),
    process_group_ (ACE_INVALID_PID),
    use_unicode_environment_ (false)
{
  ACE_NEW (command_line_buf_,
           ACE_TCHAR[command_line_buf_len]);
  command_line_buf_[0] = '\0';
  process_name_[0] = '\0';

#if defined (ACE_HAS_WINCE)
  ACE_UNUSED_ARG(inherit_environment);
  ACE_UNUSED_ARG(env_buf_len);
  ACE_UNUSED_ARG(max_env_args);
#endif

#if !defined (ACE_HAS_WINCE)
  working_directory_[0] = '\0';
  ACE_NEW (environment_buf_,
           ACE_TCHAR[env_buf_len]);
  ACE_NEW (environment_argv_,
           ACE_TCHAR *[max_env_args]);
  environment_buf_[0] = '\0';
  environment_argv_[0] = 0;
#if defined (ACE_WIN32)
  ACE_OS::memset ((void *) &this->startup_info_,
                  0,
                  sizeof this->startup_info_);
  this->startup_info_.cb = sizeof this->startup_info_;
#endif /* ACE_WIN32 */
#endif /* !ACE_HAS_WINCE */
}

ACE_Process_Options::~ACE_Process_Options ( void   ) 

Destructor.

Definition at line 1172 of file Process.cpp.

{
#if !defined (ACE_HAS_WINCE)
  release_handles();
  delete [] environment_buf_;
  delete [] environment_argv_;
#endif /* !ACE_HAS_WINCE */
  delete [] command_line_buf_;
  ACE::strdelete (command_line_copy_);
}


Member Function Documentation

void ACE_Process_Options::avoid_zombies ( int  avoid_zombies  ) 

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

Definition at line 167 of file Process.inl.

{
  avoid_zombies_ = avoid_zombies;
}

int ACE_Process_Options::avoid_zombies ( void   ) 

Get current value for avoid_zombies.

Definition at line 162 of file Process.inl.

{
  return avoid_zombies_;
}

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 1184 of file Process.cpp.

{
  int i = 0;

  if (argv[i])
    {
      ACE_OS::strcat (command_line_buf_, argv[i]);

      while (argv[++i])
        {
          // Check to see if the next argument will overflow the
          // command_line buffer.
          size_t const cur_len =
            ACE_OS::strlen (command_line_buf_)
              + ACE_OS::strlen (argv[i])
              + 2;

          if (cur_len > command_line_buf_len_)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 ACE_TEXT ("ACE_Process:command_line: ")
                                 ACE_TEXT ("command line is ")
                                 ACE_TEXT ("longer than %d\n"),
                                 command_line_buf_len_),
                                1);
            }

          ACE_OS::strcat (command_line_buf_, ACE_TEXT (" "));
          ACE_OS::strcat (command_line_buf_, argv[i]);
        }
    }

  command_line_argv_calculated_ = false;
  return 0; // Success.
}

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 1221 of file Process.cpp.

{
  // Store all ... args in argp.
  va_list argp;
  va_start (argp, format);

  if (command_line_buf_len_ < 1)
    return -1;

#if !defined (ACE_LACKS_VSNPRINTF) || defined (ACE_HAS_TRIO)
  // vsnprintf the format and args into command_line_buf__.
  ACE_OS::vsnprintf (command_line_buf_,
                     command_line_buf_len_,
                     format,
                     argp);
#else
  // sprintf the format and args into command_line_buf__.
  ACE_OS::vsprintf (command_line_buf_,
                    format,
                    argp);
#endif

  // Useless macro.
  va_end (argp);

  command_line_argv_calculated_ = false;
  return 0;
}

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 1299 of file Process.cpp.

{
  if (!command_line_argv_calculated_)
    {
      command_line_argv_calculated_ = true;

      // We need to free up any previous allocated memory first.
      ACE::strdelete (command_line_copy_);

      // We need to make a dynamically allocated copy here since
      // ACE_Tokenizer modifies its arguments.
      command_line_copy_ = ACE::strnew (command_line_buf_);
      // This tokenizer will replace all spaces with end-of-string
      // characters and will preserve text between "" and '' pairs.
      ACE_Tokenizer parser (command_line_copy_);
      parser.delimiter_replace (' ', '\0');
      parser.preserve_designators ('\"', '\"'); // "
      parser.preserve_designators ('\'', '\'');

      int x = 0;
      do
        command_line_argv_[x] = parser.next ();
      while (command_line_argv_[x] != 0
             // substract one for the ending zero.
             && ++x < MAX_COMMAND_LINE_OPTIONS - 1);

      command_line_argv_[x] = 0;
    }

  return command_line_argv_;
}

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.

  • max_len, if non-zero, provides a location into which the total length of the command line buffer is returned.

Definition at line 330 of file Process.inl.

{
  if (max_lenp != 0)
    *max_lenp = this->command_line_buf_len_;
  return this->command_line_buf_;
}

u_long ACE_Process_Options::creation_flags ( void   )  const

Get the creation flags.

Definition at line 120 of file Process.inl.

{
#if defined (ACE_USES_WCHAR) && defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
  return creation_flags_ | CREATE_UNICODE_ENVIRONMENT;
#else
  return creation_flags_;
#endif /* ACE_USES_WCHAR */
}

void ACE_Process_Options::creation_flags ( u_long  cf  ) 

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 130 of file Process.inl.

{
  creation_flags_ = cf;
}

void ACE_Process_Options::disable_unicode_environment ( void   ) 

Disable the use of a Unicode environment.

Definition at line 20 of file Process.inl.

{
  this->use_unicode_environment_ = false;
}

int ACE_Process_Options::dup_handles ( ACE_Handle_Set set  )  const

Get a copy of the handles the ACE_Process_Options duplicated for the spawned process. Any handles created through duplication of those passed into

  • pass_handle are returned in
  • set.
    Returns:
    0 if there were no handles to return; 1 if there were.

Definition at line 1377 of file Process.cpp.

{
  if (this->dup_handles_.num_set () == 0)
    return 0;
  set.reset ();
  set = this->dup_handles_;
  return 1;
}

void ACE_Process_Options::enable_unicode_environment ( void   ) 

Enable the use of a Unicode environment. This only makes sense for Win32 when ACE_USES_WCHAR is not defined.

Definition at line 14 of file Process.inl.

{
  this->use_unicode_environment_ = true;
}

ACE_TCHAR *const * ACE_Process_Options::env_argv ( void   ) 

argv-style array of environment settings.

Definition at line 928 of file Process.cpp.

{
  return environment_argv_;
}

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 1286 of file Process.cpp.

{
#if !defined (ACE_HAS_WINCE)
  if (environment_buf_[0] == '\0')
    return 0;
  else
    return environment_buf_;
#else
  return 0;
#endif /* !ACE_HAS_WINCE */
}

ACE_HANDLE ACE_Process_Options::get_stderr ( void   )  const

Definition at line 241 of file Process.inl.

{
  return stderr_;
}

ACE_HANDLE ACE_Process_Options::get_stdin ( void   )  const

Definition at line 229 of file Process.inl.

{
  return stdin_;
}

ACE_HANDLE ACE_Process_Options::get_stdout ( void   )  const

Definition at line 235 of file Process.inl.

{
  return stdout_;
}

uid_t ACE_Process_Options::getegid ( void   )  const

Definition at line 323 of file Process.inl.

{
  return this->egid_;
}

uid_t ACE_Process_Options::geteuid ( void   )  const

Definition at line 311 of file Process.inl.

{
  return this->euid_;
}

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 136 of file Process.inl.

{
  return process_group_;
}

uid_t ACE_Process_Options::getrgid ( void   )  const

Definition at line 317 of file Process.inl.

{
  return this->rgid_;
}

uid_t ACE_Process_Options::getruid ( void   )  const

Definition at line 305 of file Process.inl.

{
  return this->ruid_;
}

int ACE_Process_Options::handle_inheritance ( void   ) 

Allows disabling of handle inheritance, default is TRUE.

Definition at line 150 of file Process.inl.

{
  return handle_inheritance_;
}

void ACE_Process_Options::handle_inheritance ( int  hi  ) 

Definition at line 156 of file Process.inl.

bool ACE_Process_Options::inherit_environment ( void   )  const

Get the inherit_environment flag.

Definition at line 247 of file Process.inl.

void ACE_Process_Options::inherit_environment ( bool  nv  ) 

Set the inherit_environment flag.

Definition at line 253 of file Process.inl.

int ACE_Process_Options::pass_handle ( ACE_HANDLE  h  ) 

Cause the specified handle to be passed to a child process when it runs a new program image. The specified handle value will be included in the spawned process's command line as

  • +H
  • handle, if a new program is spawned (always on Win32; else if NO_EXEC is not set in creation flags). The passed handle value will be duplicated if on Win32 less capable than NT.
    Returns:
    0 if success, -1 if failure.

Definition at line 1334 of file Process.cpp.

{
# if defined (ACE_WIN32)
#  if defined (ACE_HAS_WINCE)
  ACE_NOTSUP_RETURN (-1);
#  else

  // This is oriented towards socket handles... may need some adjustment
  // for non-sockets.
  // This is all based on an MSDN article:
  // http://support.microsoft.com/support/kb/articles/Q150/5/23.asp
  // If on Win95/98, the handle needs to be duplicated for the to-be-spawned
  // process. On WinNT, they get inherited by the child process automatically.
  // If the handle is duplicated, remember the duplicate so it can be
  // closed later. Can't be closed now, or the child won't get it.
  ACE_TEXT_OSVERSIONINFO osvi;
  ZeroMemory (&osvi, sizeof (osvi));
  osvi.dwOSVersionInfoSize = sizeof (ACE_TEXT_OSVERSIONINFO);
  // If this is Win95/98 or we can't tell, duplicate the handle.
  if (!ACE_TEXT_GetVersionEx (&osvi) || osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)
    {
      HANDLE dup_handle;
      if (!DuplicateHandle (GetCurrentProcess (),
                            static_cast<HANDLE> (h),
                            GetCurrentProcess (),
                            &dup_handle,
                            0,
                            TRUE,   // Inheritable
                            DUPLICATE_SAME_ACCESS))
        return -1;
      dup_handles_.set_bit (static_cast<ACE_HANDLE> (dup_handle));
    }
#  endif /* ACE_HAS_WINCE */
#endif /* ACE_WIN32 */

  this->handles_passed_.set_bit (h);

  return 0;
}

int ACE_Process_Options::passed_handles ( ACE_Handle_Set set  )  const

Get a copy of the handles passed to the spawned process. This will be the set of handles previously passed to

  • pass_handle(). Any handles previously passed to
  • pass_handle are returned in
  • set.
    Returns:
    0 if there were no handles to return; 1 if there were.

Definition at line 1389 of file Process.cpp.

{
  if (this->handles_passed_.num_set () == 0)
    return 0;
  set.reset ();
  set = this->handles_passed_;
  return 1;
}

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 373 of file Process.inl.

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

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 379 of file Process.inl.

{
  if (process_name_[0] == '\0')
    this->process_name (this->command_line_argv ()[0]);

  return this->process_name_;
}

void ACE_Process_Options::release_handles ( void   ) 

Release the standard handles previously set with set_handles;.

Definition at line 1153 of file Process.cpp.

{
  if (set_handles_called_)
    {
#if defined (ACE_WIN32)
      ACE_OS::close (startup_info_.hStdInput);
      ACE_OS::close (startup_info_.hStdOutput);
      ACE_OS::close (startup_info_.hStdError);
#else /* ACE_WIN32 */
      ACE_OS::close (stdin_);
      ACE_OS::close (stdout_);
      ACE_OS::close (stderr_);
#endif /* ACE_WIN32 */
      set_handles_called_ = 0;
    }
}

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.

Note:
Any handle passed as ACE_INVALID_HANDLE will be changed to a duplicate of the current associated handle. For example, passing ACE_INVALID_HANDLE for std_in will cause ACE_STDIN to be duplicated and set in this object.
Returns:
0 on success, -1 on failure.

Definition at line 1099 of file Process.cpp.

{
  this->set_handles_called_ = 1;
#if defined (ACE_WIN32)

  // Tell the new process to use our std handles.
  this->startup_info_.dwFlags = STARTF_USESTDHANDLES;

  if (std_in == ACE_INVALID_HANDLE)
    std_in = ACE_STDIN;
  if (std_out == ACE_INVALID_HANDLE)
    std_out = ACE_STDOUT;
  if (std_err == ACE_INVALID_HANDLE)
    std_err = ACE_STDERR;

  if (!::DuplicateHandle (::GetCurrentProcess (),
                          std_in,
                          ::GetCurrentProcess (),
                          &this->startup_info_.hStdInput,
                          0,
                          TRUE,
                          DUPLICATE_SAME_ACCESS))
    return -1;

  if (!::DuplicateHandle (::GetCurrentProcess (),
                          std_out,
                          ::GetCurrentProcess (),
                          &this->startup_info_.hStdOutput,
                          0,
                          TRUE,
                          DUPLICATE_SAME_ACCESS))
    return -1;

  if (!::DuplicateHandle (::GetCurrentProcess (),
                          std_err,
                          ::GetCurrentProcess (),
                          &this->startup_info_.hStdError,
                          0,
                          TRUE,
                          DUPLICATE_SAME_ACCESS))
    return -1;
#else /* ACE_WIN32 */
  this->stdin_ = ACE_OS::dup (std_in);
  this->stdout_ = ACE_OS::dup (std_out);
  this->stderr_ = ACE_OS::dup (std_err);
#endif /* ACE_WIN32 */

  return 0; // Success.
}

void ACE_Process_Options::setegid ( uid_t  id  ) 

Definition at line 299 of file Process.inl.

{
  this->egid_ = id;
}

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 985 of file Process.cpp.

{
  // To address the potential buffer overflow,
  // we now allocate the buffer on heap with a variable size.
  size_t const buflen = ACE_OS::strlen (variable_name) + ACE_OS::strlen (format) + 2;
  ACE_TCHAR *newformat = 0;
  ACE_NEW_RETURN (newformat, ACE_TCHAR[buflen], -1);
  ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> safe_newformat (newformat);

  // Add in the variable name.
  ACE_OS::sprintf (safe_newformat.get (),
                   ACE_TEXT ("%s=%s"),
                   variable_name,
                   format);

  // Start varargs.
  va_list argp;
  va_start (argp, format);

  // Add the rest of the varargs.
  size_t tmp_buflen = buflen;
  if (DEFAULT_COMMAND_LINE_BUF_LEN > buflen)
    {
      tmp_buflen = DEFAULT_COMMAND_LINE_BUF_LEN;
    }
  int retval = 0;

  ACE_TCHAR *stack_buf = 0;
  ACE_NEW_RETURN (stack_buf, ACE_TCHAR[tmp_buflen], -1);
  ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> safe_stack_buf (stack_buf);

  do
    {
      retval = ACE_OS::vsnprintf (safe_stack_buf.get (), tmp_buflen, safe_newformat.get (), argp);
      if (retval > ACE_Utils::truncate_cast<int> (tmp_buflen))
        {
          tmp_buflen *= 2;
          ACE_NEW_RETURN (stack_buf, ACE_TCHAR[tmp_buflen], -1);
          safe_stack_buf.reset (stack_buf);
        }
      else
        break;
    }
  while (1);

  if (retval == -1)
    {
      // In case that vsnprintf is not supported,
      // e.g., LynxOS and VxWorks 5, we have to
      // fall back to vsprintf.
      if (errno == ENOTSUP)
        {
          // ALERT: Since we have to use vsprintf here, there is still a chance that
          // the stack_buf overflows, i.e., the length of the resulting string
          // can still possibly go beyond the allocated stack_buf.
          retval = ACE_OS::vsprintf (safe_stack_buf.get (), safe_newformat.get (), argp);
          if (retval == -1)
            // vsprintf is failed.
            return -1;
        }
      else
        // vsnprintf is failed.
        return -1;
    }

  // End varargs.
  va_end (argp);

  // Append the string to our environment buffer.
  if (this->setenv_i (safe_stack_buf.get (),
                      ACE_OS::strlen (safe_stack_buf.get ())) == -1)
    return -1;

#if defined (ACE_WIN32)
  if (inherit_environment_)
    this->inherit_environment ();
#endif /* ACE_WIN32 */

  return 0;
}

int ACE_Process_Options::setenv ( ACE_TCHAR envp[]  ) 

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

Definition at line 936 of file Process.cpp.

{
  int i = 0;
  while (envp[i])
    {
      if (this->setenv_i (envp[i],
                          ACE_OS::strlen (envp[i])) == -1)
        return -1;
      i++;
    }

#if defined (ACE_WIN32)
  if (inherit_environment_)
    this->inherit_environment ();
#endif /* ACE_WIN32 */

  return 0;
}

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 956 of file Process.cpp.

{
  ACE_TCHAR stack_buf[DEFAULT_COMMAND_LINE_BUF_LEN];

  // Start varargs.
  va_list argp;
  va_start (argp, format);

  // Add the rest of the varargs.
  ACE_OS::vsprintf (stack_buf,
                    format,
                    argp);
  // End varargs.
  va_end (argp);

  // Append the string to are environment buffer.
  if (this->setenv_i (stack_buf,
                      ACE_OS::strlen (stack_buf)) == -1)
    return -1;

#if defined (ACE_WIN32)
  if (inherit_environment_)
    this->inherit_environment ();
#endif /* ACE_WIN32 */

  return 0;
}

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 1068 of file Process.cpp.

{
  // Add one for the null char.
  ++len;

  // If environment larger than allocated buffer return. Also check to
  // make sure we have enough room.
  if (environment_argv_index_ == max_environ_argv_index_
      || (len + environment_buf_index_) >= environment_buf_len_)
    return -1;

  // Copy the new environment string.
  ACE_OS::memcpy (environment_buf_ + environment_buf_index_,
                  assignment,
                  len * sizeof (ACE_TCHAR));

  // Update the argv array.
  environment_argv_[environment_argv_index_++] =
    environment_buf_ + environment_buf_index_;
  environment_argv_[environment_argv_index_] = 0;

  // Update our index.
  environment_buf_index_ += len;

  // Make sure the buffer is null-terminated.
  environment_buf_[environment_buf_index_] = '\0';
  return 0;
}

void ACE_Process_Options::seteuid ( uid_t  id  ) 

Definition at line 287 of file Process.inl.

{
  this->euid_ = id;
}

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 142 of file Process.inl.

{
  pid_t old = process_group_;
  process_group_ = pgrp;
  return old;
}

int ACE_Process_Options::setreugid ( const ACE_TCHAR user  ) 

Definition at line 259 of file Process.inl.

{
#if !defined (ACE_LACKS_PWD_FUNCTIONS)
  struct passwd *ent = ACE_OS::getpwnam (ACE_TEXT_ALWAYS_CHAR (user));

  if (ent != 0)
    {
      this->euid_ = ent->pw_uid;
      this->ruid_ = ent->pw_uid;
      this->egid_ = ent->pw_gid;
      this->rgid_ = ent->pw_gid;
      return 0;
    }
  else
    return -1;
#else
  ACE_UNUSED_ARG (user);
  ACE_NOTSUP_RETURN (-1);
#endif /* ACE_LACKS_PWD_FUNCTIONS */
}

void ACE_Process_Options::setrgid ( uid_t  id  ) 

Definition at line 293 of file Process.inl.

{
  this->rgid_ = id;
}

void ACE_Process_Options::setruid ( uid_t  id  ) 

Definition at line 281 of file Process.inl.

{
  this->ruid_ = id;
}

bool ACE_Process_Options::use_unicode_environment ( void   )  const

Return the unicode environment status.

Definition at line 26 of file Process.inl.

{
  return this->use_unicode_environment_;
}

ACE_TCHAR * ACE_Process_Options::working_directory ( void   ) 

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

Definition at line 338 of file Process.inl.

{
#if !defined (ACE_HAS_WINCE)
  if (working_directory_[0] == '\0')
    return 0;
  else
    return working_directory_;
#else
  return 0;
#endif /* !ACE_HAS_WINCE */
}

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 351 of file Process.inl.

{
#if !defined(ACE_HAS_WINCE)
  ACE_OS::strcpy (working_directory_, ACE_TEXT_CHAR_TO_TCHAR (wd));
#else
  ACE_UNUSED_ARG (wd);
#endif /* !ACE_HAS_WINCE */
}


Member Data Documentation

Avoid zombies for spawned processes.

Definition at line 348 of file Process.h.

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

Argv-style command-line arguments.

Definition at line 432 of file Process.h.

Ensures command_line_argv is only calculated once.

Definition at line 419 of file Process.h.

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

Definition at line 422 of file Process.h.

Max length of command_line_buf_.

Definition at line 429 of file Process.h.

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

Definition at line 426 of file Process.h.

Default 0.

Definition at line 345 of file Process.h.

Results of duplicating handles passed in pass_handle ().

Definition at line 441 of file Process.h.

uid_t ACE_Process_Options::egid_ [protected]

Definition at line 382 of file Process.h.

Pointers into environment_buf_.

Definition at line 406 of file Process.h.

Pointer to environment_argv_.

Definition at line 397 of file Process.h.

Pointer to buffer of the environment settings.

Definition at line 400 of file Process.h.

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

Definition at line 394 of file Process.h.

Size of the environment buffer. Configurable.

Definition at line 403 of file Process.h.

uid_t ACE_Process_Options::euid_ [protected]

Definition at line 380 of file Process.h.

Default true.

Definition at line 386 of file Process.h.

Set of handles that were passed in pass_handle ().

Definition at line 438 of file Process.h.

Whether the child process inherits the current process environment.

Definition at line 341 of file Process.h.

Maximum index of environment_argv_ buffer.

Definition at line 412 of file Process.h.

Maximum number of environment variables. Configurable.

Definition at line 409 of file Process.h.

Process-group on Unix; unused on Win32.

Definition at line 435 of file Process.h.

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 445 of file Process.h.

uid_t ACE_Process_Options::rgid_ [protected]

Definition at line 381 of file Process.h.

uid_t ACE_Process_Options::ruid_ [protected]

Definition at line 379 of file Process.h.

Is 1 if stdhandles was called.

Definition at line 390 of file Process.h.

ACE_HANDLE ACE_Process_Options::stderr_ [protected]

Definition at line 375 of file Process.h.

ACE_HANDLE ACE_Process_Options::stdin_ [protected]

Definition at line 373 of file Process.h.

ACE_HANDLE ACE_Process_Options::stdout_ [protected]

Definition at line 374 of file Process.h.

Indicate if a Unicode environment should be used.

Definition at line 448 of file Process.h.

The current working directory.

Definition at line 415 of file Process.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines