bluepill 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +105 -8
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -231
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +134 -56
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -94
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -92
  20. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  21. data/lib/bluepill/group.rb +187 -78
  22. data/lib/bluepill/logger.rb +139 -48
  23. data/lib/bluepill/pid_file.rb +23 -0
  24. data/lib/bluepill/process/attributes.rb +379 -0
  25. data/lib/bluepill/process/children.rb +70 -0
  26. data/lib/bluepill/process/pid.rb +154 -0
  27. data/lib/bluepill/process/state_machine.rb +196 -0
  28. data/lib/bluepill/process/transition.rb +45 -0
  29. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  30. data/lib/bluepill/process/user_commands.rb +71 -0
  31. data/lib/bluepill/process/watches.rb +141 -0
  32. data/lib/bluepill/process.rb +56 -653
  33. data/lib/bluepill/process_commands/execution.rb +89 -0
  34. data/lib/bluepill/process_commands/hooks.rb +186 -0
  35. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  36. data/lib/bluepill/process_commands.rb +104 -172
  37. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  38. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  39. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  40. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  41. data/lib/bluepill/process_conditions/http.rb +52 -24
  42. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  43. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  44. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  45. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  46. data/lib/bluepill/process_conditions.rb +3 -2
  47. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  48. data/lib/bluepill/process_journal/lock.rb +106 -0
  49. data/lib/bluepill/process_journal.rb +126 -233
  50. data/lib/bluepill/process_statistics.rb +2 -2
  51. data/lib/bluepill/socket.rb +60 -20
  52. data/lib/bluepill/system/command_setup.rb +98 -0
  53. data/lib/bluepill/system/commands.rb +219 -248
  54. data/lib/bluepill/system/daemon.rb +161 -0
  55. data/lib/bluepill/system/ps.rb +85 -0
  56. data/lib/bluepill/system/ps_table.rb +163 -0
  57. data/lib/bluepill/system.rb +45 -126
  58. data/lib/bluepill/trigger.rb +52 -26
  59. data/lib/bluepill/triggers/flapping.rb +7 -15
  60. data/lib/bluepill/units.rb +126 -0
  61. data/lib/bluepill/util/inflections.rb +34 -0
  62. data/lib/bluepill/util/levenshtein.rb +53 -0
  63. data/lib/bluepill/util/rotational_array.rb +1 -1
  64. data/lib/bluepill/version.rb +13 -12
  65. data/lib/bluepill.rb +33 -36
  66. data/sig/bluepill/application.rbs +257 -0
  67. data/sig/bluepill/checks.rbs +203 -0
  68. data/sig/bluepill/dsl.rbs +126 -0
  69. data/sig/bluepill/logger.rbs +104 -0
  70. data/sig/bluepill/process.rbs +542 -0
  71. data/sig/bluepill/system.rbs +316 -0
  72. data/sig/bluepill.rbs +47 -869
  73. metadata +39 -45
@@ -0,0 +1,161 @@
1
+ require "shellwords"
2
+ require "bluepill/daemonize"
3
+ require "bluepill/system/command_setup"
4
+
5
+ module Bluepill
6
+ module System
7
+ # Run commands in the background, as daemons
8
+ #
9
+ # System extends this module, so these are System's module functions.
10
+ #
11
+ # @api private
12
+ module Daemon
13
+ include CommandSetup
14
+
15
+ # Start a command as a daemon, and write its pid file
16
+ #
17
+ # It forks a child that drops privileges, sets up the environment and
18
+ # standard streams, and daemonizes the command. The child tells the
19
+ # parent the daemon's pid over a pipe.
20
+ #
21
+ # @param [String] cmd the command, split into words without a shell
22
+ # @param [Hash{Symbol => Object}] options :uid, :gid,
23
+ # :supplementary_groups, :working_dir, :environment, :pid_file,
24
+ # :logger, :stdin, :stdout and :stderr
25
+ #
26
+ # @return [Integer, nil] the daemon's pid, or nil if it didn't start
27
+ #
28
+ # @api private
29
+ def daemonize(cmd, options = {})
30
+ rd, wr = IO.pipe
31
+
32
+ child = Daemonize.safefork
33
+ if child
34
+ # we don't want to create zombies, so detach ourselves from the child exit status
35
+ ::Process.detach(child)
36
+
37
+ read_daemon_pid(rd, wr)
38
+ else
39
+ # child
40
+ rd.close
41
+
42
+ drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
43
+ start_daemon(cmd, options, wr)
44
+ end
45
+ end
46
+
47
+ # Read the daemon's pid from the child that started it, in the parent
48
+ #
49
+ # @param [IO] reader the end of the pipe that the child writes the pid to
50
+ # @param [IO] writer the other end, which only the child uses
51
+ #
52
+ # @return [Integer, nil] the daemon's pid, or nil if it didn't start
53
+ #
54
+ # @api private
55
+ def read_daemon_pid(reader, writer)
56
+ # parent
57
+ writer.close
58
+
59
+ daemon_id = reader.read.to_i
60
+ reader.close
61
+
62
+ daemon_id if daemon_id > 0
63
+ end
64
+
65
+ # Daemonize the command, write its pid file, and tell the parent its pid
66
+ #
67
+ # It runs in the child, once it has dropped privileges.
68
+ #
69
+ # @param [String] cmd the command
70
+ # @param [Hash{Symbol => Object}] options as for daemonize
71
+ # @param [IO] pid_pipe where to write the daemon's pid for the parent
72
+ #
73
+ # @return [void] it never returns, since it exits the child
74
+ #
75
+ # @api private
76
+ def start_daemon(cmd, options, pid_pipe)
77
+ # Processes that bluepill daemonizes always have a pid file and a logger
78
+ pid_file = options[:pid_file] #: String
79
+ logger = options[:logger] #: Logger
80
+
81
+ # if we cannot write the pid file as the provided user, err out
82
+ exit unless can_write_pid_file(pid_file, logger)
83
+
84
+ daemon_id = Daemonize.call_as_daemon(daemon_command(cmd, options), cmd)
85
+
86
+ # call_as_daemon returns 0 when the daemon fails to report its pid
87
+ File.write(pid_file, daemon_id.to_s) if daemon_id > 0
88
+
89
+ pid_pipe.write daemon_id
90
+ pid_pipe.close
91
+
92
+ ::Process.exit!(true)
93
+ end
94
+
95
+ # Return what the daemon runs: the command, in its environment
96
+ #
97
+ # @param [String] cmd the command
98
+ # @param [Hash{Symbol => Object}] options as for daemonize
99
+ #
100
+ # @return [Proc] a block that execs the command
101
+ #
102
+ # @api private
103
+ def daemon_command(cmd, options)
104
+ lambda do
105
+ setup_command_environment(options)
106
+ redirect_io(options[:stdin], options[:stdout], options[:stderr])
107
+
108
+ Kernel.exec(*Shellwords.shellwords(cmd))
109
+ exit
110
+ end
111
+ end
112
+
113
+ # Return whether the pid file can be written, logging why if it can't
114
+ #
115
+ # @param [String] pid_file the pid file's path
116
+ # @param [Logger] logger where to log the error
117
+ #
118
+ # @return [Boolean] true if the file could be created and removed
119
+ #
120
+ # @api private
121
+ def can_write_pid_file(pid_file, logger)
122
+ FileUtils.touch(pid_file)
123
+ File.unlink(pid_file)
124
+ true
125
+ rescue => e
126
+ logger.warning(format("%s - %s", e.class, e))
127
+ # A rescued exception was raised, so it has a backtrace
128
+ backtrace = e.backtrace #: Array[String]
129
+ backtrace.each { |l| logger.warning l }
130
+ false
131
+ end
132
+
133
+ # Point standard input, output and error at files
134
+ #
135
+ # Output and error share one file when they name the same one. It
136
+ # reopens STDIN, STDOUT and STDERR, which are always file descriptors
137
+ # 0, 1 and 2, the ones the command inherits, even if $stdout and the
138
+ # others have been pointed somewhere else.
139
+ #
140
+ # @param [String, nil] io_in the file for standard input
141
+ # @param [String, nil] io_out the file for standard output, appended to
142
+ # @param [String, nil] io_err the file for standard error, appended to
143
+ #
144
+ # @return [void]
145
+ #
146
+ # @api private
147
+ def redirect_io(io_in, io_out, io_err)
148
+ STDIN.reopen(io_in) if io_in
149
+
150
+ if !io_out.nil? && io_out.eql?(io_err)
151
+ STDOUT.reopen(io_out, APPEND_MODE)
152
+ STDERR.reopen(STDOUT)
153
+
154
+ else
155
+ STDOUT.reopen(io_out, APPEND_MODE) if io_out
156
+ STDERR.reopen(io_err, APPEND_MODE) if io_err
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,85 @@
1
+ require "bluepill/system/ps_table"
2
+
3
+ module Bluepill
4
+ module System
5
+ # Run ps once a tick, and read its table of processes
6
+ #
7
+ # System extends this module, so these are System's module functions.
8
+ # The table is memoized on System, so every watch of every process
9
+ # reads the same one instead of forking a ps of its own.
10
+ #
11
+ # @api private
12
+ module Ps
13
+ # Return what this tick has memoized
14
+ #
15
+ # @return [Hash{Symbol => PsTable, String}] the ps table, or ps's
16
+ # error, by name
17
+ #
18
+ # @api private
19
+ def store
20
+ @store ||= {}
21
+ end
22
+
23
+ # Forget what the last tick memoized, so ps runs again
24
+ #
25
+ # @return [void]
26
+ #
27
+ # @api private
28
+ def reset_data
29
+ @children_table = nil
30
+ store.clear
31
+ end
32
+
33
+ # Return this tick's ps table, running ps the first time it's asked for
34
+ #
35
+ # @return [PsTable] each process's ps fields
36
+ #
37
+ # @raise [PsError] if ps could not be run this tick
38
+ #
39
+ # @api private
40
+ def ps_axu
41
+ ps = PS_MUTEX.synchronize { store[:ps_axu] ||= read_ps_table }
42
+ raise PsError, ps if ps.instance_of?(String)
43
+
44
+ ps
45
+ end
46
+
47
+ # Run ps, and parse what it prints into a table
48
+ #
49
+ # @return [PsTable, String] each process's ps fields, or why ps could
50
+ # not be run
51
+ #
52
+ # @api private
53
+ def read_ps_table
54
+ # POSIX options, so this also works with System V ps on Solaris and SmartOS.
55
+ # Use -A because -e shows the environment on FreeBSD and OpenBSD.
56
+ PsTable.parse(`ps -Ao pid,ppid,pcpu,rss,etime,args`)
57
+ rescue SystemCallError => e
58
+ # Forking ps can fail, for example with ENOMEM or EAGAIN under memory pressure.
59
+ # Cache the message so ps is not run again until the next tick.
60
+ "Could not run ps: #{e.class}: #{e}"
61
+ end
62
+
63
+ # Return the pids of a process's descendants, from this tick's ps table
64
+ #
65
+ # @param [Integer, nil] parent_pid the process's pid
66
+ #
67
+ # @return [Array<Integer>] its children's pids, followed by theirs, or
68
+ # none if ps could not be run
69
+ #
70
+ # @api private
71
+ def get_children(parent_pid)
72
+ # A process that is still starting has no pid yet, and nil.to_i is 0.
73
+ # The ps header row parses as pid 0 with ppid 0, so it would be its own
74
+ # child and the recursion would never end.
75
+ return NO_PIDS unless parent_pid.to_i > 0
76
+
77
+ # Each process asks every tick, so keep the table rather than take
78
+ # PS_MUTEX again for each one
79
+ (@children_table ||= ps_axu).descendants(parent_pid)
80
+ rescue PsError
81
+ NO_PIDS
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,163 @@
1
+ module Bluepill
2
+ module System
3
+ # A tick's table of processes, as ps listed them
4
+ #
5
+ # Each row is a process's ps fields, Strings in the order of the column
6
+ # constants, and the rows are by pid. System.ps_axu makes one each tick,
7
+ # and every watch of every process reads it.
8
+ #
9
+ # @api private
10
+ class PsTable
11
+ # The column of a process's pid
12
+ #
13
+ # @api private
14
+ PID = 0
15
+
16
+ # The column of a process's parent's pid
17
+ #
18
+ # @api private
19
+ PPID = 1
20
+
21
+ # The column of the percent of a CPU a process uses
22
+ #
23
+ # @api private
24
+ PCPU = 2
25
+
26
+ # The column of a process's resident memory, in kilobytes
27
+ #
28
+ # @api private
29
+ RSS = 3
30
+
31
+ # The column of how long a process has been running, [[dd-]hh:]mm:ss
32
+ #
33
+ # @api private
34
+ ETIME = 4
35
+
36
+ # The column of a process's command and its arguments
37
+ #
38
+ # @api private
39
+ COMMAND = 5
40
+
41
+ # Parse what ps printed into a table
42
+ #
43
+ # @param [String] output ps's output, a line per process
44
+ #
45
+ # @return [PsTable] the table of each process's ps fields
46
+ #
47
+ # @api private
48
+ def self.parse(output)
49
+ lines = output.split("\n")
50
+
51
+ rows = {} #: Hash[Integer, Array[String]]
52
+ lines.each do |line|
53
+ # Split off the fields before the command, and keep the command
54
+ # whole, as ps printed it
55
+ row = line.split(" ", COMMAND + 1)
56
+ row[COMMAND] ||= ""
57
+ rows[row.fetch(PID).to_i] = row
58
+ end
59
+ new(rows)
60
+ end
61
+
62
+ # Return each process's ps fields, by pid
63
+ #
64
+ # @return [Hash{Integer => Array<String>}] the rows
65
+ #
66
+ # @api private
67
+ attr_reader :rows
68
+
69
+ # Make a table of rows
70
+ #
71
+ # @param [Hash{Integer => Array<String>}] rows each process's ps
72
+ # fields, by pid
73
+ #
74
+ # @return [undefined]
75
+ #
76
+ # @api private
77
+ def initialize(rows)
78
+ @rows = rows
79
+ end
80
+
81
+ # Return a process's ps fields
82
+ #
83
+ # @param [Integer, nil] pid the process's pid
84
+ #
85
+ # @return [Array<String>, nil] its fields, or nil if ps doesn't list it
86
+ #
87
+ # @api private
88
+ def row(pid)
89
+ @rows[pid]
90
+ end
91
+
92
+ # Return the pids of a process's children
93
+ #
94
+ # @param [Integer, nil] pid the process's pid
95
+ #
96
+ # @return [Array<Integer>] its children's pids, in ps's order, or
97
+ # NO_PIDS if it has none
98
+ #
99
+ # @api private
100
+ def children(pid)
101
+ children_by_parent.fetch(pid, NO_PIDS)
102
+ end
103
+
104
+ # Return the pids of a process's descendants
105
+ #
106
+ # The pid must be above 0. The ps header row parses as pid 0 with ppid
107
+ # 0, so it's its own child, and pid 0's descendants never end.
108
+ #
109
+ # @param [Integer] pid the process's pid
110
+ #
111
+ # @return [Array<Integer>] its children's pids, followed by theirs
112
+ #
113
+ # @api private
114
+ def descendants(pid)
115
+ children = children(pid)
116
+ # Most processes have no children, and they all share one empty array
117
+ return children if children.empty?
118
+
119
+ found = children.dup
120
+ children.each { |child| found.concat(descendants(child)) }
121
+ found
122
+ end
123
+
124
+ # Add up a column of some processes' rows
125
+ #
126
+ # @param [Array<Integer>] pids the processes' pids, any that ps doesn't
127
+ # list skipped
128
+ # @param [Integer] column the column to add up, such as RSS
129
+ # @param [Float] total what to add them to
130
+ #
131
+ # @return [Float] the total
132
+ #
133
+ # @api private
134
+ def sum(pids, column, total = 0.0)
135
+ pids.each do |pid|
136
+ row = @rows[pid]
137
+ total += row.fetch(column).to_f if row
138
+ end
139
+ total
140
+ end
141
+
142
+ private
143
+
144
+ # Return the pids in the table by their parent's pid
145
+ #
146
+ # It's built the first time it's asked for, so finding a process's
147
+ # descendants takes as long as there are descendants, not a pass over
148
+ # the whole table for each one.
149
+ #
150
+ # @return [Hash{Integer => Array<Integer>}] each parent's children, in
151
+ # ps's order
152
+ #
153
+ # @api private
154
+ def children_by_parent
155
+ @children_by_parent ||= begin
156
+ index = {} #: Hash[Integer, Array[Integer]]
157
+ @rows.each_pair { |pid, row| (index[row.fetch(PPID).to_i] ||= []) << pid }
158
+ index
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -1,34 +1,31 @@
1
+ require "bluepill/system/ps"
2
+ require "bluepill/system/commands"
3
+
1
4
  module Bluepill
2
5
  # Read what the system says about its processes, and run commands
3
6
  #
4
7
  # This represents the system that bluepill is running on. It memoizes
5
8
  # the results of running ps each tick, so that every watch of every
6
- # process doesn't fork a ps of its own.
9
+ # process doesn't fork a ps of its own. Running ps and reading its table
10
+ # is in System::Ps, and running commands is in System::Commands and
11
+ # System::Daemon. System extends them all.
7
12
  #
8
13
  # @api private
9
14
  module System
15
+ extend Ps
16
+ extend Commands
17
+ extend Daemon
18
+
10
19
  # The mode that a daemon's stdout and stderr files are opened in
11
20
  #
12
21
  # @api private
13
- APPEND_MODE = 'a'.freeze
22
+ APPEND_MODE = "a".freeze
14
23
 
15
24
  # Raised when ps could not be run, so there is no data for this tick
16
25
  #
17
26
  # @api private
18
27
  class PsError < StandardError; end
19
28
 
20
- # The position of each field in ps output
21
- #
22
- # @api private
23
- IDX_MAP = {
24
- pid: 0,
25
- ppid: 1,
26
- pcpu: 2,
27
- rss: 3,
28
- etime: 4,
29
- command: 5,
30
- }.freeze #: { pid: Integer, ppid: Integer, pcpu: Integer, rss: Integer, etime: Integer, command: Integer }
31
-
32
29
  # Return whether a process is running
33
30
  #
34
31
  # @param [Integer] pid the process's pid
@@ -42,7 +39,7 @@ module Bluepill
42
39
  true
43
40
  rescue Errno::EPERM # no permission, but it is definitely alive
44
41
  true
45
- rescue StandardError
42
+ rescue
46
43
  false # no such process (ESRCH), or no pid
47
44
  end
48
45
 
@@ -59,7 +56,7 @@ module Bluepill
59
56
  # @api private
60
57
  def self.started_at(pid)
61
58
  # ps says why on stderr when there's no such process
62
- elapsed = IO.popen(['ps', '-o', 'etime=', '-p', Integer(pid).to_s], err: File::NULL, &:read).rstrip
59
+ elapsed = IO.popen(["ps", "-o", "etime=", "-p", Integer(pid).to_s], err: File::NULL, &:read).rstrip
63
60
  Integer(Time.now) - parse_elapsed_time(elapsed) unless elapsed.empty?
64
61
  rescue SystemCallError
65
62
  nil
@@ -89,16 +86,7 @@ module Bluepill
89
86
  #
90
87
  # @api private
91
88
  def self.cpu_usage(pid, include_children)
92
- ps = ps_axu
93
- return unless ps.key?(pid)
94
-
95
- cpu_used = ps.fetch(pid).fetch(IDX_MAP.fetch(:pcpu)).to_f
96
- if include_children
97
- get_children(pid).each do |child_pid|
98
- cpu_used += ps.fetch(child_pid).fetch(IDX_MAP.fetch(:pcpu)).to_f if ps.key?(child_pid)
99
- end
100
- end
101
- cpu_used
89
+ usage(pid, include_children, PsTable::PCPU)
102
90
  end
103
91
 
104
92
  # Return a process's resident memory, from this tick's ps table
@@ -113,16 +101,28 @@ module Bluepill
113
101
  #
114
102
  # @api private
115
103
  def self.memory_usage(pid, include_children)
104
+ usage(pid, include_children, PsTable::RSS)
105
+ end
106
+
107
+ # Return a column of a process's row, from this tick's ps table, as a Float
108
+ #
109
+ # @param [Integer, nil] pid the process's pid
110
+ # @param [Boolean] include_children whether to add its descendants'
111
+ # @param [Integer] column the column, PsTable::PCPU or PsTable::RSS
112
+ #
113
+ # @return [Float, nil] the process's value, and its descendants' if
114
+ # asked for, or nil if ps doesn't list it
115
+ #
116
+ # @raise [PsError] if ps could not be run this tick
117
+ #
118
+ # @api private
119
+ def self.usage(pid, include_children, column)
116
120
  ps = ps_axu
117
- return unless ps.key?(pid)
121
+ row = ps.row(pid)
122
+ return unless row
118
123
 
119
- mem_used = ps.fetch(pid).fetch(IDX_MAP.fetch(:rss)).to_f
120
- if include_children
121
- get_children(pid).each do |child_pid|
122
- mem_used += ps.fetch(child_pid).fetch(IDX_MAP.fetch(:rss)).to_f if ps.key?(child_pid)
123
- end
124
- end
125
- mem_used
124
+ used = row.fetch(column).to_f
125
+ include_children ? ps.sum(get_children(pid), column, used) : used
126
126
  end
127
127
 
128
128
  # Return how long a process has been running, from this tick's ps table
@@ -136,10 +136,8 @@ module Bluepill
136
136
  #
137
137
  # @api private
138
138
  def self.running_time(pid)
139
- ps = ps_axu
140
- return unless ps.key?(pid)
141
-
142
- parse_elapsed_time(ps.fetch(pid).fetch(IDX_MAP.fetch(:etime)))
139
+ row = ps_axu.row(pid)
140
+ parse_elapsed_time(row.fetch(PsTable::ETIME)) if row
143
141
  end
144
142
 
145
143
  # Return a process's command line, from this tick's ps table
@@ -153,34 +151,8 @@ module Bluepill
153
151
  #
154
152
  # @api private
155
153
  def self.command(pid)
156
- ps = ps_axu
157
- return unless ps.key?(pid)
158
-
159
- ps.fetch(pid).fetch(IDX_MAP.fetch(:command))
160
- end
161
-
162
- # Return the pids of a process's descendants, from this tick's ps table
163
- #
164
- # @param [Integer, nil] parent_pid the process's pid
165
- #
166
- # @return [Array<Integer>] its children's pids, followed by theirs, or
167
- # none if ps could not be run
168
- #
169
- # @api private
170
- def self.get_children(parent_pid)
171
- # A process that is still starting has no pid yet, and nil.to_i is 0.
172
- # The ps header row parses as pid 0 with ppid 0, so it would be its own
173
- # child and the recursion would never end.
174
- return [] unless parent_pid.to_i > 0
175
-
176
- child_pids = [] #: Array[Integer]
177
- ps_axu.each_pair do |_pid, chunks|
178
- child_pids << Integer(chunks.fetch(IDX_MAP.fetch(:pid))) if chunks.fetch(IDX_MAP.fetch(:ppid)).to_i.eql?(parent_pid)
179
- end
180
- grand_children = child_pids.collect { |pid| get_children(pid) }.flatten
181
- child_pids.concat grand_children
182
- rescue PsError
183
- []
154
+ row = ps_axu.row(pid)
155
+ row.fetch(PsTable::COMMAND) if row
184
156
  end
185
157
 
186
158
  # Delete a file if it exists
@@ -199,70 +171,21 @@ module Bluepill
199
171
  begin
200
172
  File.unlink(filename) if filename && File.exist?(filename)
201
173
  rescue IOError, Errno::ENOENT
174
+ # Already gone, or another process removed it in between
202
175
  rescue Errno::EACCES
203
176
  retry if (tries += 1) < 3
204
177
  $stderr.puts("Warning: permission denied trying to delete #{filename}")
205
178
  end
206
179
  end
207
180
 
208
- # Return what this tick has memoized
209
- #
210
- # @return [Hash{Symbol => Hash{Integer => Array<String>}, String}] the
211
- # ps table, or ps's error, by name
212
- #
213
- # @api private
214
- def self.store
215
- @store ||= {}
216
- end
217
-
218
- # Forget what the last tick memoized, so ps runs again
219
- #
220
- # @return [void]
221
- #
222
- # @api private
223
- def self.reset_data
224
- store.clear
225
- end
226
-
227
181
  # Watches run in their own threads, so only let one of them run ps per tick
228
182
  #
229
183
  # @api private
230
184
  PS_MUTEX = Mutex.new
231
185
 
232
- # Return this tick's ps table, running ps the first time it's asked for
233
- #
234
- # @return [Hash{Integer => Array<String>}] each process's ps fields, by
235
- # pid, in the order of IDX_MAP
236
- #
237
- # @raise [PsError] if ps could not be run this tick
238
- #
239
- # @api private
240
- def self.ps_axu
241
- ps = PS_MUTEX.synchronize do
242
- store[:ps_axu] ||= begin
243
- # POSIX options, so this also works with System V ps on Solaris and SmartOS.
244
- # Use -A because -e shows the environment on FreeBSD and OpenBSD.
245
- lines = `ps -Ao pid,ppid,pcpu,rss,etime,args`.split("\n")
246
-
247
- table = {} #: ps_table
248
- lines.each do |line|
249
- chunks = line.split
250
- pid = chunks.fetch(IDX_MAP.fetch(:pid)).to_i
251
- command = chunks.slice!(IDX_MAP.fetch(:command), chunks.length) #: Array[String]
252
- chunks[IDX_MAP.fetch(:command)] = command.join ' '
253
- table[pid] = chunks
254
- end
255
- table
256
- rescue SystemCallError => e
257
- # Forking ps can fail, for example with ENOMEM or EAGAIN under memory pressure.
258
- # Cache the message so ps is not run again until the next tick.
259
- "Could not run ps: #{e.class}: #{e}"
260
- end
261
- end
262
- raise PsError, ps if ps.instance_of?(String)
263
-
264
- ps
265
- end
186
+ # No pids, which get_children returns for a process with no children
187
+ no_pids = [] #: Array[Integer]
188
+ NO_PIDS = no_pids.freeze
266
189
 
267
190
  # Convert ps's elapsed time to seconds
268
191
  #
@@ -275,16 +198,12 @@ module Bluepill
275
198
  # [[dd-]hh:]mm:ss
276
199
  match = /(?:(?:(\d+)-)?(\d\d):)?(\d\d):(\d\d)/.match(str)
277
200
  if match
278
- days = match[1].to_i
279
- hours = match[2].to_i
280
- mins = match[3].to_i
281
- secs = match[4].to_i
282
- ((days * 24 + hours) * 60 + mins) * 60 + secs
201
+ hours = (match[1].to_i * 24) + match[2].to_i
202
+ mins = (hours * 60) + match[3].to_i
203
+ (mins * 60) + match[4].to_i
283
204
  else
284
205
  0
285
206
  end
286
207
  end
287
208
  end
288
209
  end
289
-
290
- require 'bluepill/system/commands'