Revision d6491981 lib/utils/process.py

b/lib/utils/process.py
141 141

  
142 142

  
143 143
def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False,
144
           interactive=False, timeout=None, noclose_fds=None):
144
           interactive=False, timeout=None, noclose_fds=None,
145
           _postfork_fn=None):
145 146
  """Execute a (shell) command.
146 147

  
147 148
  The command should not read from its standard input, as it will be
......
169 170
  @type noclose_fds: list
170 171
  @param noclose_fds: list of additional (fd >=3) file descriptors to leave
171 172
                      open for the child process
173
  @param _postfork_fn: Callback run after fork but before timeout (unittest)
172 174
  @rtype: L{RunResult}
173 175
  @return: RunResult instance
174 176
  @raise errors.ProgrammerError: if we call this when forks are disabled
......
200 202
    if output is None:
201 203
      out, err, status, timeout_action = _RunCmdPipe(cmd, cmd_env, shell, cwd,
202 204
                                                     interactive, timeout,
203
                                                     noclose_fds)
205
                                                     noclose_fds,
206
                                                     _postfork_fn=_postfork_fn)
204 207
    else:
208
      assert _postfork_fn is None, \
209
          "_postfork_fn not supported if output provided"
205 210
      timeout_action = _TIMEOUT_NONE
206 211
      status = _RunCmdFile(cmd, cmd_env, shell, output, cwd, noclose_fds)
207 212
      out = err = ""
......
468 473

  
469 474

  
470 475
def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds,
471
                _linger_timeout=constants.CHILD_LINGER_TIMEOUT):
476
                _linger_timeout=constants.CHILD_LINGER_TIMEOUT,
477
                _postfork_fn=None):
472 478
  """Run a command and return its output.
473 479

  
474 480
  @type  cmd: string or list
......
486 492
  @type noclose_fds: list
487 493
  @param noclose_fds: list of additional (fd >=3) file descriptors to leave
488 494
                      open for the child process
495
  @param _postfork_fn: Function run after fork but before timeout (unittest)
489 496
  @rtype: tuple
490 497
  @return: (out, err, status)
491 498

  
......
514 521
                           cwd=cwd,
515 522
                           preexec_fn=preexec_fn)
516 523

  
524
  if _postfork_fn:
525
    _postfork_fn(child.pid)
526

  
517 527
  out = StringIO()
518 528
  err = StringIO()
519 529

  

Also available in: Unified diff