Skip to content

Commit

Permalink
Significant changes to APLProcess to support Ssh - as yet untested, t…
Browse files Browse the repository at this point in the history
…his commit for backup purposes
  • Loading branch information
mkromberg committed Sep 29, 2016
1 parent b1b4661 commit 655dfc6
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 46 deletions.
153 changes: 113 additions & 40 deletions APLProcess.dyalog
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
:Field Public Exe''
:Field Public Proc⎕NS ''
:Field Public onExit''
:Field Public RunTime0 Boolean or name of runtime executable
:Field Public RunTime0 Boolean or name of runtime executable
:Field Public IsWin0
:Field Public IsSsh0

:Field Public RIDE_INIT'' RIDE parameters if remote debugging is to be allowed

endswith{w, a, w(-(a)w)a}
tonum{⊃⊃(//)⎕VFI }
eis{2>|:, } enclose if simple

make
:Access Public Instance
:Implements Constructor
pathSourcePath;source
Determine the source path of the class

:Trap 6
source'(⊃⊃⎕CLASS ⎕THIS).SALT_Data.SourceFile' ⍎ works around a bug
:Else
source(⊃⊃⎕CLASS ⎕THIS).SALT_Data.SourceFile
:If 0=source{((¨)⊃⊃⎕CLASS ⎕THIS),''}5177
source⎕WSID
:Else source4source
:EndIf
:EndTrap
path{(-/()'\/')}source

make1 args;rt;cmd;ws
Expand All @@ -30,8 +42,9 @@
{[3]} if present, a Boolean indicating whether to use the runtime version, OR a character vector of the executable name to run
args{2>|:, }args
args3args,(args)'' '' 0
(ws cmd rt)args
Start(ws cmd rt)
(ws cmd rt)args
PATHSourcePath
Start(ws cmd rt)

Run
Expand All @@ -42,7 +55,7 @@
Start(ws args rt);psi;pid
(Ws Args)ws args
:If 0RIDE_INIT
argsargs,' RIDE_INIT=',RIDE_INIT
argsargs,' RIDE_SPAWNED=1 RIDE_INIT=',RIDE_INIT
:EndIf

:If ~0 210|⎕DR rt if rt is character, it's the executable name
Expand All @@ -52,15 +65,19 @@
rt0
:EndIf
ws,←rt/' salt' ⍝ if runtime, load the salt workspace first, which will subsequently load the target workspace
:If IsWin
:If IsWinIsWindows
⎕USING'System,System.dll'
psi⎕NEW Diagnostics.ProcessStartInfo,Exe(ws,' ',args)
psi.WindowStyleDiagnostics.ProcessWindowStyle.Minimized
ProcDiagnostics.Process.Start psi
:Else Unix
:Else Unix
:If IsSsh326=⎕DR Exe
ProcSshProc Exe
:Else
pid_SH'{ ',args,' ',Exe,' +s ',ws,' -c APLppid=',(GetCurrentProcessId),' </dev/null >/dev/null 2>&1 & } ; echo $!'
Proc.Idpid
Proc.HasExitedHasExited
:EndIf
Proc.StartTime⎕NEW Time ⎕TS
:EndIf
Expand All @@ -84,7 +101,7 @@
{}Kill Proc

rIsWin
rIsWindows
:Access Public Shared
r'Win'3⎕IO#.⎕WG'APLVersion'
Expand All @@ -93,6 +110,9 @@
:Access Public Shared
:If IsWin
r't'⎕NA'U4 kernel32|GetCurrentProcessId'
:ElseIf IsSsh
∘∘∘
rProc.Pid
:Else
rtonum_SH'echo $PPID'
:EndIf
Expand All @@ -111,7 +131,9 @@
r2 ⎕NQ'.' 'GetEnvironment' 'DYALOG'
rr,(~(¯1r)'\/')/'/' Add separator if necessary
rr,(Diagnostics.Process.GetCurrentProcess.ProcessName),'.exe'
:EndIf
:EndIf
:ElseIf IsSsh
∘∘∘ Not supported
:Else
t_PS'-o args -p ',GetCurrentProcessId AWS
:If '"'''t if command begins with ' or "
Expand Down Expand Up @@ -155,6 +177,8 @@
r(kids[;1]m/p.Id)kids
:EndIf
:EndIf
:ElseIf IsSsh
∘∘∘
:Else
mask(⍴⍴kids)0
:For i :In mask
Expand Down Expand Up @@ -200,7 +224,9 @@
:EndFor
:EndFor
:EndIf
:EndIf
:EndIf
:ElseIf IsSsh
∘∘∘
:Else Linux
unfortunately, Ubuntu (and perhaps others) report the PPID of tasks started via ⎕SH as 1
so, the best we can do at this point is identify processes that we tagged with ppid=
Expand Down Expand Up @@ -229,7 +255,9 @@
⎕DL delay
delay+delay
:Until (delay>10)Proc.HasExited
:Else
:ElseIf IsSsh
∘∘∘
:Else Local UNIX
{}UNIXIssueKill 3 Proc.Id issue strong interrupt
{}⎕DL 2 wait a couple seconds for it to react
:If ~Proc.HasExited~UNIXIsRunning Proc.Id
Expand All @@ -255,6 +283,8 @@
:If IsWin
Proc.Kill
⎕DL 0.2
:ElseIf IsSsh
∘∘∘
:Else
{}UNIXIssueKill 3 Proc.Id issue strong interrupt AWS
{}⎕DL 2 wait a couple seconds for it to react
Expand All @@ -276,7 +306,9 @@
rHasExited
:Access public instance
:If IsWin
r{0:: Proc.HasExited}1
r{0:: Proc.HasExited}1
:ElseIf IsSsh
∘∘∘
:Else
r~UNIXIsRunning Proc.Id AWS
:EndIf
Expand Down Expand Up @@ -307,6 +339,8 @@
r0
:EndTrap
:EndIf
:ElseIf IsSsh
∘∘∘
:Else
rUNIXIsRunning pid
:EndIf
Expand All @@ -326,7 +360,9 @@
proc.Kill
{}⎕DL 0.5
r~##.APLProcess.IsRunning pid
:Else
:ElseIf IsSsh
∘∘∘
:ElseIf
{}UNIXIssueKill 3 pid issue strong interrupt
:EndIf
Expand All @@ -340,12 +376,22 @@

{r}UNIXIssueKill(signal pid)
signal pid¨signal pid
r⎕SH'kill -',signal,' ',pid,' >/dev/null 2>&1 ; echo $?'
cmd'kill -',signal,' ',pid,' >/dev/null 2>&1 ; echo $?'
:If IsSsh
∘∘∘
:Else
r⎕SH cmd
:EndIf

rUNIXGetShortCmd pid
Retrieve sort form of cmd used to start process <pid>
r1⎕SH'ps -o cmd -p ',(pid),' 2>/dev/null ; exit 0'
rUNIXGetShortCmd pid;cmd
Retrieve sort form of cmd used to start process <pid>
cmd'ps -o cmd -p ',(pid),' 2>/dev/null ; exit 0'
:If IsSsh
∘∘∘
:Else
r1⎕SH cmd
:EndIf

r_PS cmd;ps
Expand All @@ -362,22 +408,23 @@
r{0::'' ⎕SH }cmd

:class Time
:field public Year
:field public Month
:field public Day
:field public Hour
:field public Minute
:field public Second
:field public Millisecond
:Class Time
:Field Public Year
:Field Public Month
:Field Public Day
:Field Public Hour
:Field Public Minute
:Field Public Second
:Field Public Millisecond

make ts
:Implements constructor
:Access public
:Implements Constructor
:Access Public
(Year Month Day Hour Minute Second Millisecond)7ts
⎕DF(¯2'00',Day),'-',((12 3'JanFebMarAprMayJunJulAugSepOctNovDec')[Month;]),'-',(100|Year),' ',1,/{':',¯2'00',}¨Hour Minute Second
:endclass

:EndClass

rProcessUsingPort port;t
return the process ID of the process (if any) using a port
Expand All @@ -403,18 +450,44 @@
'GCN'⎕NA'I4 Kernel32|GetComputerNameEx* U4 >0T =U4'
r2GCN 7 255 255
:Return
ComputerNameNetBIOS = 0
ComputerNameDnsHostname = 1
ComputerNameDnsDomain = 2
ComputerNameDnsFullyQualified = 3
ComputerNamePhysicalNetBIOS = 4
ComputerNamePhysicalDnsHostname = 5
ComputerNamePhysicalDnsDomain = 6
ComputerNamePhysicalDnsFullyQualified = 7 <<<
ComputerNameMax = 8
:Else
ComputerNameNetBIOS = 0
ComputerNameDnsHostname = 1
ComputerNameDnsDomain = 2
ComputerNameDnsFullyQualified = 3
ComputerNamePhysicalNetBIOS = 4
ComputerNamePhysicalDnsHostname = 5
ComputerNamePhysicalDnsDomain = 6
ComputerNamePhysicalDnsFullyQualified = 7 <<<
ComputerNameMax = 8
:ElseIf IsSsh
∘∘∘ Not supported
:ElseIf
r_SH'hostname'
:EndIf

ProcSshProc(host user keyfile cmd);conn;z;kf;allpids;guid;listpids;pids;⎕USING;pid;tid
⎕USING'Renci.SshNet,',PATH,'/Renci.SshNet.dll'
kf⎕NEW PrivateKeyFile (,keyfile)
conn⎕NEW SshClient (host 22 user (,kf))

:Trap 0
conn.Connect This is defined to be a void()
:Case 90 ('Error creating ssh client instance: ',⎕EXCEPTION.Message) ⎕SIGNAL 11
:Else 'Unexpected error creating ssh client instance' ⎕SIGNAL 11
:EndTrap

listpids{0~2(⎕UCS 10)⎕VFI (conn.RunCommand 'ps -u ',user,' | grep dyalog | grep -v grep | awk ''{print $2}''').Result}
guid'dyalog-ssh-',(⎕TS)~' '
pidslistpids
tidconn.RunCommand&cmd ,' -c ''',guid,''''
:If 1=pid(listpids )~pids pidpid
:Else ∘∘∘ :EndIf failed to start
Proc⎕NS ''
Proc.SshConnconn
Proc.tidtid
Proc.Pid


:EndClass
Loading

0 comments on commit 655dfc6

Please sign in to comment.