PipeStream.st
author Claus Gittinger <cg@exept.de>
Sat, 12 Sep 1998 17:35:08 +0200
changeset 3834 93ec07316a98
parent 3833 3ae90f8da01a
child 3956 51f1a9a4d63f
permissions -rw-r--r--
oops - must nil pid when terminating the process

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

NonPositionableExternalStream subclass:#PipeStream
	instanceVariableNames:'commandString pid exitStatus exitSema exitAction'
	classVariableNames:'BrokenPipeSignal'
	poolDictionaries:''
	category:'Streams-External'
!

!PipeStream primitiveDefinitions!
%{

#if defined(NT) || defined(WIN32) || defined(MSDOS)
# undef UNIX_LIKE
# define MSDOS_LIKE
#endif

#if defined(__openVMS__)
# undef __new
#endif

#include <stdio.h>
#define _STDIO_H_INCLUDED_

#include <errno.h>
#define _ERRNO_H_INCLUDED_

#ifndef transputer
# include <sys/types.h>
# include <sys/stat.h>
#endif

/*
 * on some systems errno is a macro ... check for it here
 */
#ifndef errno
 extern errno;
#endif

#ifdef LINUX
# define BUGGY_STDIO_LIB
#endif

%}
! !

!PipeStream primitiveFunctions!
%{

/*
 * no longer needed - popen is useless ...
 */
#undef NEED_POPEN_WITH_VFORK


/*
 * some systems (i.e. ultrix) use fork;
 * were better off with a popen based on vfork ...
 */
#ifdef NEED_POPEN_WITH_VFORK

static int popen_pid = 0;

FILE *
popen(command, type)
/* const */ char *command;
/* const */ char *type;
{
    int pipes[2];
    int itype = (strcmp(type, "w") == 0 ? 1 : 0);

    if (pipe(pipes) == -1)
	return NULL;

    switch (popen_pid = vfork()) {
    case -1:
	(void)close(pipes[0]);
	(void)close(pipes[1]);
	return NULL;

    case 0:
	if (itype) {
	    dup2(pipes[0], fileno(stdin));
	    close(pipes[1]);
	} else {
	    dup2(pipes[1], fileno(stdout));
	    close(pipes[0]);
	}
	execl("/bin/sh", "/bin/sh", "-c", command, 0);
	fprintf(stderr, "XRN Error: failed the execlp\n");
	_exit(-1);
	/* NOTREACHED */

    default:
	    if (itype) {
		close(pipes[0]);
		return fdopen(pipes[1], "w");
	    } else {
		close(pipes[1]);
		return fdopen(pipes[0], "r");
	    }
    }
}

int
pclose(str)
FILE *str;
{
    int pd = 0;
    int status;
    int err;

    err = fclose(str);

    do {
	if ((pd = wait(&status)) == -1)
	{
		err = EOF;
		break;
	}
    } while (pd !=  popen_pid);

    if (err == EOF)
	return  -1;

    if (status)
	status >>= 8;   /* exit status in high byte */

    return status;
}

#endif /* NEED_POPEN_WITH_VFORK */

%}
! !

!PipeStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Pipestreams allow reading or writing from/to a unix command.
    For example, to get a stream reading the output of an 'ls -l'
    command, a PipeStream can be created with:

	PipeStream readingFrom:'ls -l'

    the characters of the commands output can be read using the
    standard stream messages as next, nextLine etc.

    If a writing pipeStream is written to, after the command has finished,
    UNIX will generate an error-signal (SIGPIPE), which will raise the BrokenPipeSignal. 
    Thus, to handle this condition correctly, the following code is suggested:

	|p|
	p := PipeStream writingTo:'echo hello'.
	PipeStream brokenPipeSignal handle:[:ex |
	    'broken pipe' printNewline.
	    p shutDown.
	    ex return
	] do:[
	    p nextPutLine:'oops'.
	   'after write' printNewline.
	    p close.
	   'after close' printNewline
	]

    Notice, that iff the Stream is buffered, the Signal may occur some time after
    the write - or even at close time; to avoid a recursive signal in the exception
    handler, a #shutDown is useful there; if you use close in the handler, this would
    try to send any buffered output to the pipe, leading to another brokenPipe exception.

    Buffered pipes do not work with Linux - the stdio library seems to be
    buggy (trying to restart the read ...)

    Currently, no filtering pipeStreams (i.e. both reading AND writing) are provided.
    However, if you look at how things are setup, this can be implemented using the
    low level primitives #mapePipe and #executeCommand from the OS class protocol.

    [author:]
	Claus Gittinger

    [see also:]
	ExternalStream FileStream Socket
	OperatingSystem
"
! !

!PipeStream class methodsFor:'initialization'!

initialize
    "setup the signal"

    BrokenPipeSignal isNil ifTrue:[
	BrokenPipeSignal := WriteErrorSignal newSignalMayProceed:true.
	BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
	BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
    ]
! !

!PipeStream class methodsFor:'instance creation'!

readingFrom:commandString
    "create and return a new pipeStream which can read from the unix command
     given by command."

    ^ (self basicNew) readingFrom:commandString

    "unix:
	PipeStream readingFrom:'ls -l'.
    "

    "
	p := PipeStream readingFrom:'ls -l'.
	Transcript showCR:p nextLine.
	p close
    "

    "
	|s|
	s := PipeStream readingFrom:'sh -c sleep\ 600'.
	(Delay forSeconds:2) wait.
	s shutDown
    "

    "vms:
	PipeStream readingFrom:'dir'.
    "

    "
	|p|
	p := PipeStream readingFrom:'dir'.
	Transcript showCR:p nextLine.
	p close
    "

    "msdos:
	PipeStream readingFrom:'dir'.
    "
    "
	|p|
	p := PipeStream readingFrom:'dir'.
	Transcript showCR:p nextLine.
	p close
    "

    "Modified: 24.4.1996 / 09:09:25 / stefan"
!

readingFrom:commandString errorDisposition:handleError inDirectory:aDirectory
    "similar to #readingFrom, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code.
     handleError may be one of #discard #inline #stderr (default), which causes
     stderr to me discarded (/dev/null), merged into the PipeStream or written
     to smalltalks stderr."

    |cmd tmpComFile pipe|

    OperatingSystem isVMSlike ifTrue:[
	tmpComFile := OperatingSystem createCOMFileForVMSCommand:commandString in:aDirectory.
	cmd := '@' , tmpComFile osName.
	pipe := self readingFrom:cmd.
	pipe notNil ifTrue:[
	    pipe exitAction:[tmpComFile delete].
	] ifFalse:[
	    tmpComFile delete
	].
	^ pipe
    ].
    OperatingSystem isMSDOSlike ifTrue:[
	^ (self basicNew) readingFrom:commandString inDirectory:aDirectory.
    ].


    "/ unix - prepend a 'cd' to the command
    cmd := 'cd ' , aDirectory asFilename name, '; ' , commandString.
    handleError == #discard ifTrue:[
	cmd := cmd, ' 2>/dev/null'.
    ] ifFalse:[
	handleError == #inline ifTrue:[
	    cmd := cmd, ' 2>&1'.
        ]
    ].
    ^ self readingFrom:cmd

    "Created: 24.9.1997 / 09:32:35 / stefan"
    "Modified: 20.1.1998 / 17:19:33 / md"
!

readingFrom:commandString inDirectory:aDirectory
    "similar to #readingFrom, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code."

     ^ self 
	readingFrom:commandString
	errorDisposition:#stderr 
	inDirectory:aDirectory

    "Modified: 24.9.1997 / 09:33:48 / stefan"
!

writingTo:commandString
    "create and return a new pipeStream which can write to the unix command
     given by command."

    ^ (self basicNew) writingTo:commandString

    "unix:
	 PipeStream writingTo:'sort'
    "
!

writingTo:commandString inDirectory:aDirectory
    "similar to #writingTo, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code."

    |cmd tmpComFile pipe|

    (OperatingSystem isVMSlike) ifTrue:[
	tmpComFile := OperatingSystem createCOMFileForVMSCommand:commandString in:aDirectory.
	cmd := '@' , tmpComFile osName.
	pipe := self writingTo:cmd.
	pipe notNil ifTrue:[
	    pipe exitAction:[tmpComFile delete].
	] ifFalse:[
	    tmpComFile delete
	].
	^ pipe
    ].

    "/ unix - prepend a 'cd' to the command
    cmd := 'cd ' , aDirectory asFilename name, '; ' , commandString.
    ^ self writingTo:cmd
! !

!PipeStream class methodsFor:'Signal constants'!

brokenPipeSignal
    "return the signal used to handle SIGPIPE unix-signals.
     Since SIGPIPE is asynchronous, we can't decide which smalltalk process
     should handle BrokenPipeSignal. So the system doesn't raise 
     BrokenPipeSignal for SIGPIPE any longer."

    ^ BrokenPipeSignal

    "Modified: 24.9.1997 / 09:43:23 / stefan"
! !

!PipeStream methodsFor:'accessing'!

commandString
    "return the command string"

    ^ commandString
!

exitStatus
    "return exitStatus"

    ^ exitStatus

    "Created: 28.12.1995 / 14:54:41 / stefan"
!

pid
    "return pid"

    ^ pid

    "Created: 28.12.1995 / 14:54:30 / stefan"
! !

!PipeStream methodsFor:'instance release'!

closeFile
    "low level close
     This waits for the command to finish. 
     Use shutDown for a fast (nonBlocking) close."

    |tpid|

    filePointer notNil ifTrue:[
        (tpid := pid) notNil ifTrue:[
            OperatingSystem isMSDOSlike ifTrue:[
                OperatingSystem terminateProcess:tpid.
                OperatingSystem terminateProcessGroup:tpid.
                pid := nil.
            ]
        ].
        super closeFile.

        filePointer := nil.
        pid notNil ifTrue:[
            [
                pid notNil ifTrue:[
                    exitSema wait.
                ]
            ] valueUninterruptably
        ].
    ].

    "Modified: / 12.9.1998 / 16:51:04 / cg"
!

closeFileDescriptor
    "alternative very low level close 
     This closes the underlying OS-fileDescriptor 
     - and will NOT write any buffered data to the stream.
     You have been warned."

    |action|

%{  
#if !defined(transputer)
    OBJ fp;
    FILE *f;

    if ((fp = __INST(filePointer)) != nil) {
	__INST(filePointer) = nil;
	f = __FILEVal(fp);
#ifdef WIN32
	close(fileno(f));
#else
	__BEGIN_INTERRUPTABLE__
	close(fileno(f));
	__END_INTERRUPTABLE__
#endif
    }
#endif /* not transputer  */
%}.
    exitAction notNil ifTrue:[
	action := exitAction.
	exitAction := nil.
	action value.
    ]
!

disposed
    "redefined to avoid blocking in close."

    self shutDown
!

shutDown
    "close the Stream, ignoring any broken-pipe errors.
     Terminate the command"

    BrokenPipeSignal catch:[
        |tpid|

        Lobby unregister:self.

        "/ terminate first under windows.
        (tpid := pid) notNil ifTrue:[      "copy pid to avoid race"
            OperatingSystem isMSDOSlike ifTrue:[
                OperatingSystem terminateProcess:tpid.
                OperatingSystem terminateProcessGroup:tpid.
                pid := nil.
            ].
        ].

        self closeFileDescriptor.

        "/ terminate last under unix.
        (tpid := pid) notNil ifTrue:[      "copy pid to avoid race"
            "/
            "/ Terminate both the process and group, just in case the
            "/ operating system does not support process groups.
            "/
            OperatingSystem terminateProcess:tpid.
            OperatingSystem terminateProcessGroup:tpid.
            pid := nil.
        ].

    ]

    "Modified: / 23.5.1996 / 09:15:41 / stefan"
    "Modified: / 12.9.1998 / 16:50:18 / cg"
! !

!PipeStream methodsFor:'private'!

exitAction:aBlock
    "define a block to be evaluated when the pipe is closed.
     This is only used with VMS, to remove any temporary COM file.
     (see readingFrom:inDirectory:)"

    exitAction := aBlock
!

openPipeFor:aCommandString withMode:mode
    "open a pipe to the unix command in commandString;
     mode may be 'r' or 'w'"

    ^ self 
	openPipeFor:aCommandString withMode:mode inDirectory:nil
!

openPipeFor:aCommandString withMode:mode inDirectory:aDirectrory
    "open a pipe to the unix command in commandString; 
     mode may be 'r' or 'w'"

    |blocked pipeFdArray execFdArray execFd myFd shellAndArgs
     shellPath shellArgs closeFdArray mbx mbxName|

    filePointer notNil ifTrue:[
	"the pipe was already open ...
	 this should (can) not happen."
	^ self errorOpen
    ].
    lastErrorNumber := nil.
    exitStatus := nil.
    exitSema := Semaphore new name:'pipe exitSema'.

    OperatingSystem isVMSlike ifTrue:[
	mbx := OperatingSystem createMailBox.
	mbx isNil ifTrue:[
	    lastErrorNumber := OperatingSystem currentErrorNumber.
	    ^ self openError
	].
	mbxName := OperatingSystem mailBoxNameOf:mbx.
	"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR.
	shellPath := ''.
	shellArgs := aCommandString.

	mode = 'r' ifTrue:[
	    execFdArray := Array with:0 with:mbx with:2.
	] ifFalse:[
	    execFdArray := Array with:mbx with:1 with:2.
	].
	closeFdArray := nil.
    ] ifFalse:[
	pipeFdArray := OperatingSystem makePipe.
	pipeFdArray isNil ifTrue:[
	    lastErrorNumber := OperatingSystem currentErrorNumber.
	    ^ self openError
	].

	shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommandString.
	shellPath := shellAndArgs at:1.
	shellArgs := shellAndArgs at:2.

	mode = 'r' ifTrue:[
	    execFd := pipeFdArray at:2.
	    execFdArray := Array with:0 with:execFd with:2.
	    myFd := pipeFdArray at:1.
	] ifFalse:[
	    execFd := pipeFdArray at:1.
	    execFdArray := Array with:execFd with:1 with:2.
	    myFd := pipeFdArray at:2.
	].
	closeFdArray := Array with:myFd.
    ].


    "/ must block here, to avoid races due to early finishing
    "/ subprocesses ...

    blocked := OperatingSystem blockInterrupts.

    pid := Processor 
	       monitor:[
		  OperatingSystem 
		      exec:shellPath
		      withArguments:shellArgs
		      fileDescriptors:execFdArray
		      closeDescriptors:closeFdArray
		      fork:true
		      newPgrp:true
		      inDirectory:aDirectrory.
	       ]
	       action:[:status |
		  status stillAlive ifFalse:[
		      exitStatus := status.
		      OperatingSystem closePid:pid.
		      pid := nil.
		      exitSema signal.
		  ].
	       ].

    OperatingSystem isVMSlike ifFalse:[
	OperatingSystem closeFd:execFd.
    ].

    pid notNil ifTrue:[
	OperatingSystem isMSDOSlike ifTrue:[
	    self setFileDescriptor:myFd mode:mode.
	    "/ self setFileHandle:myFd mode:mode
	] ifFalse:[
	    OperatingSystem isVMSlike ifTrue:[
		"/
		"/ reopen the mailbox as a file ...
		"/
		mbxName := OperatingSystem mailBoxNameOf:mbx.
		mbxName notNil ifTrue:[
		    super open:mbxName withMode:mode
		].
	    ] ifFalse:[
		self setFileDescriptor:myFd mode:mode.
	    ]
	]
    ] ifFalse:[
	lastErrorNumber := OperatingSystem currentErrorNumber.
	OperatingSystem isVMSlike ifTrue:[
	    OperatingSystem destroyMailBox:mbx
	] ifFalse:[
	    OperatingSystem closeFd:myFd.
	].
    ].

    blocked ifFalse:[
	OperatingSystem unblockInterrupts
    ].

    lastErrorNumber notNil ifTrue:[
	"
	 the pipe open failed for some reason ...
	 ... this may be either due to an invalid command string,
	 or due to the system running out of memory (when forking
	 the unix process)
	"
	^ self openError
    ].

    commandString := aCommandString.
%{
    /* LINUX stdio is corrupt here ... */
#if defined(BUGGY_STDIO_LIB) || defined(WIN32)
    __INST(buffered) = false;
#else
    __INST(buffered) = true;
#endif
%}.
    position := 1.
    hitEOF := false.
    binary := false.
    Lobby register:self.

    "Modified: 23.4.1996 / 17:05:59 / stefan"
    "Modified: 28.1.1998 / 14:47:34 / md"
!

readingFrom:command
    "setup the receiver to read from command"

    mode := #readonly. didWrite := false.
    ^ self openPipeFor:command withMode:'r'
!

readingFrom:command inDirectory:aDirecrory
    "setup the receiver to read from command"

    mode := #readonly. didWrite := false.
    ^ self openPipeFor:command withMode:'r' inDirectory:aDirecrory
!

writingTo:command
    "setup the receiver to write to command"

    mode := #writeonly. didWrite := true.
    ^ self openPipeFor:command withMode:'w'
! !

!PipeStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.74 1998-09-12 15:35:08 cg Exp $'
! !
PipeStream initialize!