Skip to content

Commit d3eca5b

Browse files
committed
Boot - don't store pid commands on macOS
When executing within a hardened runtime on macOS it appears that calling `ps` either directly or via `Sys::ProcTable.ps` is denied causing the Ruby process to be immediately terminated. We called `ps` in order to get the command name of the pid we were registering to store it alongside and to check with when attempting to clear and kill running processes. We no longer do this on macOS and therefore avoid the Ruby process from being terminated prematurely. Eventually we need to look into re-designing the process creation and management system from a suite of Ruby scripts to a reliable running process that we connect to via a TCP connection which when disconnected will cause all registered pids to be terminated.
1 parent 1ef230f commit d3eca5b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

app/server/ruby/bin/task-clear.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
pids = ARGV
4949
end
5050

51-
log_process_info "\n\nClearing pids: #{pids.inspect}\n"
51+
log_process_info "\n\task-clear.rb\n\n"
52+
log_process_info "\nClearing pids: #{pids.inspect}\n"
5253

5354
if pids.empty?
5455
log_process_info "No pids to clear :-)\n"
@@ -76,17 +77,26 @@
7677
FileUtils.rm pid_path
7778
end
7879

80+
command_line = ""
81+
7982
begin
80-
info = Sys::ProcTable.ps(pid: pid)
81-
raise unless info
83+
if os == :osx
84+
# We can't use ProcTable.ps or `ps` from within a hardened runtime
85+
# on macOS So don't attempt to extract command line for pid
86+
else
87+
info = Sys::ProcTable.ps(pid: pid)
88+
raise unless info
89+
command_line = info.cmdline.strip
90+
end
91+
8292
rescue
8393
log_process_info " -- unable to get ProcTable info for: #{pid}"
8494
log_process_info " -- process: #{pid} not running"
8595
next
8696
end
8797

8898
# Don't kill process unless the command line arguments match
89-
next unless info.cmdline.strip == orig_cmdline.strip
99+
next unless command_line.strip == orig_cmdline.strip
90100

91101
if os == :windows
92102
# We're on Windows, so go straight for the jugular

app/server/ruby/bin/task-register.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,34 @@
3333

3434
f = nil
3535

36+
os = case RUBY_PLATFORM
37+
when /.*arm.*-linux.*/
38+
:raspberry
39+
when /.*linux.*/
40+
:linux
41+
when /.*darwin.*/
42+
:osx
43+
when /.*mingw.*/
44+
:windows
45+
else
46+
:unknown
47+
end
48+
49+
command_line = ""
50+
3651
begin
37-
if s = Sys::ProcTable.ps(pid: pid)
52+
if os == :osx
53+
# We can't use ProcTable.ps or `ps` from within a hardened runtime
54+
# on macOS So don't attempt to extract command line for pid
55+
else
56+
info = Sys::ProcTable.ps(pid: pid)
57+
command_line = info.cmdline.strip
58+
end
59+
60+
if command_line
3861
f = File.open(pid_path, 'w')
39-
f.puts s.cmdline
40-
log_process_info "Started [#{pid}] [-] #{s.cmdline} [-] #{pid_path}"
62+
f.puts command_line
63+
log_process_info "Started [#{pid}] [-] #{command_line} [-] #{pid_path}"
4164
end
4265
rescue Exception => e
4366
log_process_info "ERROR: Unable to write information for PID #{pid} to path #{pid_path}!"

0 commit comments

Comments
 (0)