Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.9 KiB

  1. package IPC::Open2;
  2. use strict;
  3. our ($VERSION, @ISA, @EXPORT);
  4. require 5.000;
  5. require Exporter;
  6. $VERSION = 1.01;
  7. @ISA = qw(Exporter);
  8. @EXPORT = qw(open2);
  9. =head1 NAME
  10. IPC::Open2, open2 - open a process for both reading and writing
  11. =head1 SYNOPSIS
  12. use IPC::Open2;
  13. $pid = open2(\*RDRFH, \*WTRFH, 'some cmd and args');
  14. # or without using the shell
  15. $pid = open2(\*RDRFH, \*WTRFH, 'some', 'cmd', 'and', 'args');
  16. # or with handle autovivification
  17. my($rdrfh, $wtrfh);
  18. $pid = open2($rdrfh, $wtrfh, 'some cmd and args');
  19. # or without using the shell
  20. $pid = open2($rdrfh, $wtrfh, 'some', 'cmd', 'and', 'args');
  21. =head1 DESCRIPTION
  22. The open2() function runs the given $cmd and connects $rdrfh for
  23. reading and $wtrfh for writing. It's what you think should work
  24. when you try
  25. $pid = open(HANDLE, "|cmd args|");
  26. The write filehandle will have autoflush turned on.
  27. If $rdrfh is a string (that is, a bareword filehandle rather than a glob
  28. or a reference) and it begins with C<< >& >>, then the child will send output
  29. directly to that file handle. If $wtrfh is a string that begins with
  30. C<< <& >>, then $wtrfh will be closed in the parent, and the child will read
  31. from it directly. In both cases, there will be a dup(2) instead of a
  32. pipe(2) made.
  33. If either reader or writer is the null string, this will be replaced
  34. by an autogenerated filehandle. If so, you must pass a valid lvalue
  35. in the parameter slot so it can be overwritten in the caller, or
  36. an exception will be raised.
  37. open2() returns the process ID of the child process. It doesn't return on
  38. failure: it just raises an exception matching C</^open2:/>. However,
  39. C<exec> failures in the child are not detected. You'll have to
  40. trap SIGPIPE yourself.
  41. open2() does not wait for and reap the child process after it exits.
  42. Except for short programs where it's acceptable to let the operating system
  43. take care of this, you need to do this yourself. This is normally as
  44. simple as calling C<waitpid $pid, 0> when you're done with the process.
  45. Failing to do this can result in an accumulation of defunct or "zombie"
  46. processes. See L<perlfunc/waitpid> for more information.
  47. This whole affair is quite dangerous, as you may block forever. It
  48. assumes it's going to talk to something like B<bc>, both writing
  49. to it and reading from it. This is presumably safe because you
  50. "know" that commands like B<bc> will read a line at a time and
  51. output a line at a time. Programs like B<sort> that read their
  52. entire input stream first, however, are quite apt to cause deadlock.
  53. The big problem with this approach is that if you don't have control
  54. over source code being run in the child process, you can't control
  55. what it does with pipe buffering. Thus you can't just open a pipe to
  56. C<cat -v> and continually read and write a line from it.
  57. The IO::Pty and Expect modules from CPAN can help with this, as they
  58. provide a real tty (well, a pseudo-tty, actually), which gets you
  59. back to line buffering in the invoked command again.
  60. =head1 WARNING
  61. The order of arguments differs from that of open3().
  62. =head1 SEE ALSO
  63. See L<IPC::Open3> for an alternative that handles STDERR as well. This
  64. function is really just a wrapper around open3().
  65. =cut
  66. # &open2: tom christiansen, <[email protected]>
  67. #
  68. # usage: $pid = open2('rdr', 'wtr', 'some cmd and args');
  69. # or $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
  70. #
  71. # spawn the given $cmd and connect $rdr for
  72. # reading and $wtr for writing. return pid
  73. # of child, or 0 on failure.
  74. #
  75. # WARNING: this is dangerous, as you may block forever
  76. # unless you are very careful.
  77. #
  78. # $wtr is left unbuffered.
  79. #
  80. # abort program if
  81. # rdr or wtr are null
  82. # a system call fails
  83. require IPC::Open3;
  84. sub open2 {
  85. local $Carp::CarpLevel = $Carp::CarpLevel + 1;
  86. return IPC::Open3::_open3('open2', scalar caller,
  87. $_[1], $_[0], '>&STDERR', @_[2 .. $#_]);
  88. }
  89. 1