Skip to content

Commit

Permalink
LibCompat: rewrite OSProcess to use LibUnix's #spawn:...
Browse files Browse the repository at this point in the history
...rather than Mariano's `OSSubprocess` package. The problem is that
`OSSubProcess` reaping logic assumes that all processes are spawned by
it so it may (does) reap processes spawned by LibUnix.

This commit makes `OSProcess` compatibility class to use `LibUnix`
to avoid this problem and throw an error is OSSubProcess is loaded.

A proper fix would be to change `OSSubprocess` to only reap processes
it has spawned (see [1])

[1] pharo-contributions/OSSubprocess#76
  • Loading branch information
janvrany committed Aug 22, 2023
1 parent eb419e3 commit 76a8f9f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/LibCompat/OSProcess.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Class {
#name : #OSProcess,
#superclass : #Object,
#instVars : [
'process'
'command',
'pid',
'status'
],
#category : #LibCompat
}
Expand All @@ -23,31 +25,41 @@ OSProcess class >> command: aString [

{ #category : #accesing }
OSProcess >> command: aString [
process shellCommand: aString.
command := aString

]

{ #category : #initialization }
OSProcess >> initialize [
process := OSSUnixSubprocess new.
(Smalltalk globals includesKey: #OSSUnixSubprocess) ifTrue:[
self error: 'OSSubProcess package loaded. OSSubProcess is incompatbile with LibCompat''s OSProcess implementation.'
].
]

{ #category : #testing }
OSProcess >> isComplete [
^process isComplete
^status notNil
]

{ #category : #testing }
OSProcess >> isRunning [
^process isRunning
^status isNil.
]

{ #category : #running }
OSProcess >> startProcess [
process run
pid := Smalltalk os spawn: {'/bin/sh' . '-c' . command} stdin: Stdio stdin stdout: Stdio stdout stderr: Stdio stderr exit: [ :sts | status := sts. ].
]

{ #category : #'terminating child' }
OSProcess >> terminate [
process terminate
pid isNil ifTrue:[
self error: 'Process not (yet) running'.
^ self.
].
status notNil ifTrue:[
self error: 'Process already terminated'.
].
LibC kill: pid _: 15"SIGTERM"

]
6 changes: 6 additions & 0 deletions src/LibUnix/LibC.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ LibC class >> ioctl: fd _: request _: param [
^ self ffiCall:#( int ioctl(int fd, long request, void* param) ) module: LibC
]

{ #category : #'*LibUnix' }
LibC class >> kill: pid _: sig [
^ self ffiCall:#(int kill(int pid, int sig)) module: LibC

]

{ #category : #'*LibUnix' }
LibC class >> open: pathname _: flags [
^ self ffiCall: #(int open(char* pathname, int flags)) module: LibC
Expand Down
14 changes: 14 additions & 0 deletions src/LibUnix/UnixProcessStatus.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ UnixProcessStatus >> pid: pidValue wstatus: wstatusValue [
wstatus := wstatusValue
]

{ #category : #printing }
UnixProcessStatus >> printOn: aStream [
super printOn: aStream.
aStream nextPutAll: '(pid '.
pid printOn: aStream.
aStream nextPutAll: ', '.
aStream nextPutAll: self status.
aStream nextPutAll: ' '.
self code printOn: aStream.
aStream nextPutAll: ')'.


]

{ #category : #accessing }
UnixProcessStatus >> status [
"return status as a Symbol;
Expand Down

0 comments on commit 76a8f9f

Please sign in to comment.