bluepill 0.2.9 → 0.3.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -2
  3. data/README.md +12 -7
  4. data/bin/bluepill +8 -2
  5. data/bluepill.gemspec +1 -1
  6. data/lib/bluepill/application/lifecycle.rb +44 -34
  7. data/lib/bluepill/application/listener.rb +1 -1
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +33 -29
  10. data/lib/bluepill/base_directory.rb +18 -20
  11. data/lib/bluepill/check_registry.rb +104 -0
  12. data/lib/bluepill/clock.rb +1 -3
  13. data/lib/bluepill/condition_watch.rb +45 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +32 -37
  16. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  17. data/lib/bluepill/dsl/attributes.rb +135 -0
  18. data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
  20. data/lib/bluepill/dsl/process_factory.rb +83 -84
  21. data/lib/bluepill/dsl/process_proxy.rb +61 -168
  22. data/lib/bluepill/dsl.rb +16 -15
  23. data/lib/bluepill/errors.rb +190 -0
  24. data/lib/bluepill/group.rb +14 -37
  25. data/lib/bluepill/logger.rb +55 -97
  26. data/lib/bluepill/process/attributes.rb +66 -32
  27. data/lib/bluepill/process/children.rb +4 -6
  28. data/lib/bluepill/process/pid.rb +41 -35
  29. data/lib/bluepill/process/state_machine.rb +15 -59
  30. data/lib/bluepill/process/stop_signals.rb +120 -0
  31. data/lib/bluepill/process/transition.rb +1 -3
  32. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  33. data/lib/bluepill/process/triggers.rb +90 -0
  34. data/lib/bluepill/process/user_commands.rb +1 -1
  35. data/lib/bluepill/process/watches.rb +9 -23
  36. data/lib/bluepill/process.rb +55 -52
  37. data/lib/bluepill/process_commands/execution.rb +14 -22
  38. data/lib/bluepill/process_commands/hooks.rb +1 -31
  39. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  40. data/lib/bluepill/process_commands.rb +10 -8
  41. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  42. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  43. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  44. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  45. data/lib/bluepill/process_conditions/http.rb +28 -67
  46. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  47. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  48. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  49. data/lib/bluepill/process_conditions.rb +5 -14
  50. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  51. data/lib/bluepill/process_journal/config.rb +73 -0
  52. data/lib/bluepill/process_journal/entries.rb +128 -0
  53. data/lib/bluepill/process_journal/journal.rb +69 -0
  54. data/lib/bluepill/process_journal/kind.rb +60 -0
  55. data/lib/bluepill/process_journal/lock.rb +9 -4
  56. data/lib/bluepill/process_journal.rb +13 -324
  57. data/lib/bluepill/process_statistics.rb +3 -7
  58. data/lib/bluepill/socket.rb +19 -17
  59. data/lib/bluepill/system/command_options.rb +135 -0
  60. data/lib/bluepill/system/command_result.rb +73 -0
  61. data/lib/bluepill/system/command_setup.rb +3 -4
  62. data/lib/bluepill/system/commands.rb +21 -18
  63. data/lib/bluepill/system/daemon.rb +19 -14
  64. data/lib/bluepill/system/files.rb +35 -0
  65. data/lib/bluepill/system/ps.rb +20 -17
  66. data/lib/bluepill/system/ps_table.rb +7 -9
  67. data/lib/bluepill/system/usage.rb +155 -0
  68. data/lib/bluepill/system.rb +7 -192
  69. data/lib/bluepill/trigger.rb +18 -69
  70. data/lib/bluepill/triggers/flapping.rb +12 -8
  71. data/lib/bluepill/units.rb +7 -21
  72. data/lib/bluepill/version.rb +12 -22
  73. data/lib/bluepill.rb +1 -1
  74. data/sig/bluepill/application.rbs +11 -12
  75. data/sig/bluepill/attributes.rbs +48 -0
  76. data/sig/bluepill/checks.rbs +40 -40
  77. data/sig/bluepill/dsl.rbs +97 -60
  78. data/sig/bluepill/errors.rbs +48 -0
  79. data/sig/bluepill/logger.rbs +15 -31
  80. data/sig/bluepill/process.rbs +39 -305
  81. data/sig/bluepill/process_commands.rbs +235 -0
  82. data/sig/bluepill/stop_signals.rbs +21 -0
  83. data/sig/bluepill/system.rbs +154 -99
  84. data/sig/bluepill/version.rbs +17 -0
  85. data/sig/bluepill.rbs +1 -17
  86. data/sig/manifest.yaml +1 -0
  87. metadata +21 -8
  88. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  89. data/lib/bluepill/deprecation/dsl.rb +0 -141
  90. data/lib/bluepill/deprecation/options.rb +0 -140
  91. data/lib/bluepill/deprecation.rb +0 -92
  92. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  93. data/sig/bluepill/deprecation.rbs +0 -87
@@ -27,14 +27,12 @@ module Bluepill
27
27
  # @return [Object] what the block returns
28
28
  #
29
29
  # @api private
30
- def self.client(base_dir, name, &block)
31
- UNIXSocket.open(socket_path(base_dir, name), &block)
32
- end
30
+ def self.client(base_dir, name, &block) = UNIXSocket.open(socket_path(base_dir, name), &block)
33
31
 
34
32
  # Send a command to an application's daemon, and return its answer
35
33
  #
36
34
  # It tries MAX_ATTEMPTS times when the daemon hangs up or takes longer
37
- # than TIMEOUT, and aborts after the last.
35
+ # than TIMEOUT.
38
36
  #
39
37
  # @param [String] base_dir the directory with the daemons' sockets
40
38
  # @param [String] name the application's name
@@ -42,6 +40,8 @@ module Bluepill
42
40
  #
43
41
  # @return [Object] what the daemon answered, unmarshaled
44
42
  #
43
+ # @raise [ConnectionError] if the last attempt fails too
44
+ #
45
45
  # @api private
46
46
  def self.client_command(base_dir, name, command)
47
47
  res = nil
@@ -49,7 +49,7 @@ module Bluepill
49
49
  res = send_command(base_dir, name, command)
50
50
  break
51
51
  rescue EOFError, Timeout::Error
52
- retry_or_abort(current_attempt)
52
+ retry_or_give_up(current_attempt)
53
53
  end
54
54
  res
55
55
  end
@@ -75,28 +75,32 @@ module Bluepill
75
75
  end
76
76
  end
77
77
 
78
- # Say that a client command tries again, or abort after the last attempt
78
+ # Say that a client command tries again, or give up after the last try
79
79
  #
80
80
  # @param [Integer] current_attempt the attempt that failed, from 0
81
81
  #
82
82
  # @return [void]
83
83
  #
84
+ # @raise [ConnectionError] after the last attempt
85
+ #
84
86
  # @api private
85
- def self.retry_or_abort(current_attempt)
86
- abort("Socket Timeout: Server may not be responding") if current_attempt.eql?(MAX_ATTEMPTS - 1)
87
+ def self.retry_or_give_up(current_attempt)
88
+ raise ConnectionError, "Socket Timeout: Server may not be responding" if current_attempt.eql?(MAX_ATTEMPTS - 1)
89
+
87
90
  puts "Retry #{current_attempt + 1} of #{MAX_ATTEMPTS}"
88
91
  end
89
92
 
90
93
  # Open the socket that an application's daemon listens on
91
94
  #
92
- # It replaces a socket file that no daemon answers on, and exits with
93
- # status 7 if a daemon already does.
95
+ # It replaces a socket file that no daemon answers on.
94
96
  #
95
97
  # @param [String] base_dir the directory with the daemons' sockets
96
98
  # @param [String] name the application's name
97
99
  #
98
100
  # @return [UNIXServer]
99
101
  #
102
+ # @raise [AlreadyRunningError] if a daemon already answers on it
103
+ #
100
104
  # @api private
101
105
  def self.server(base_dir, name)
102
106
  # @type var path: String
@@ -108,13 +112,14 @@ module Bluepill
108
112
 
109
113
  # Open a daemon's socket in place of a socket file that already exists
110
114
  #
111
- # It deletes the file and opens the socket if no daemon answers on it,
112
- # and exits with status 7 if a daemon does.
115
+ # It deletes the file and opens the socket if no daemon answers on it.
113
116
  #
114
117
  # @param [String] path the socket file
115
118
  #
116
119
  # @return [UNIXServer]
117
120
  #
121
+ # @raise [AlreadyRunningError] if a daemon answers on it
122
+ #
118
123
  # @api private
119
124
  def self.replace_stale_socket(path)
120
125
  # if sock file has been created, test to see if there is a server
@@ -123,8 +128,7 @@ module Bluepill
123
128
  File.delete(path)
124
129
  UNIXServer.open(path)
125
130
  else
126
- $stderr.puts("Server is already running!")
127
- exit(7)
131
+ raise AlreadyRunningError, "Server is already running!"
128
132
  end
129
133
 
130
134
  # Return the path of an application's socket
@@ -135,8 +139,6 @@ module Bluepill
135
139
  # @return [String]
136
140
  #
137
141
  # @api private
138
- def self.socket_path(base_dir, name)
139
- BaseDirectory.new(base_dir).socket_file(name)
140
- end
142
+ def self.socket_path(base_dir, name) = BaseDirectory.new(base_dir).socket_file(name)
141
143
  end
142
144
  end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module System
5
+ # How a command runs: as which user and groups, in which directory,
6
+ # with which environment and standard streams, and for how long
7
+ #
8
+ # Every option is nil unless it's given. Process#command_options makes
9
+ # one from the process's attributes.
10
+ #
11
+ # @api private
12
+ class CommandOptions
13
+ # Return the user the command runs as
14
+ #
15
+ # @return [String, nil] the user's name
16
+ #
17
+ # @api private
18
+ attr_reader :uid
19
+
20
+ # Return the group the command runs as
21
+ #
22
+ # @return [String, nil] the group's name
23
+ #
24
+ # @api private
25
+ attr_reader :gid
26
+
27
+ # Return the groups the command joins, besides the user's own
28
+ #
29
+ # @return [Array<String>, nil] the groups' names
30
+ #
31
+ # @api private
32
+ attr_reader :supplementary_groups
33
+
34
+ # Return the directory the command runs in
35
+ #
36
+ # @return [String, nil]
37
+ #
38
+ # @api private
39
+ attr_reader :working_dir
40
+
41
+ # Return the environment variables the command gets
42
+ #
43
+ # @return [Hash{#to_s => #to_s}] the variables, empty when there are
44
+ # none
45
+ #
46
+ # @api private
47
+ attr_reader :environment
48
+
49
+ # Return the file a daemon's standard input reads
50
+ #
51
+ # @return [String, nil]
52
+ #
53
+ # @api private
54
+ attr_reader :stdin
55
+
56
+ # Return the file a daemon's standard output is appended to
57
+ #
58
+ # @return [String, nil]
59
+ #
60
+ # @api private
61
+ attr_reader :stdout
62
+
63
+ # Return the file a daemon's standard error is appended to
64
+ #
65
+ # @return [String, nil]
66
+ #
67
+ # @api private
68
+ attr_reader :stderr
69
+
70
+ # Return how long a command that runs until it finishes may run
71
+ #
72
+ # @return [Numeric, ActiveSupport::Duration, nil] the seconds, or nil
73
+ # to wait until it finishes
74
+ #
75
+ # @api private
76
+ attr_reader :timeout
77
+
78
+ # Describe how a command runs
79
+ #
80
+ # @param [String, nil] uid the user's name
81
+ # @param [String, nil] gid the group's name
82
+ # @param [Array<String>, nil] supplementary_groups more groups' names
83
+ # @param [String, nil] working_dir the directory to run in
84
+ # @param [Hash{#to_s => #to_s}, nil] environment the environment
85
+ # variables, or nil for none
86
+ # @param [String, nil] stdin a daemon's standard input
87
+ # @param [String, nil] stdout a daemon's standard output
88
+ # @param [String, nil] stderr a daemon's standard error
89
+ # @param [Numeric, ActiveSupport::Duration, nil] timeout the seconds a
90
+ # command that runs until it finishes may take
91
+ #
92
+ # @return [void]
93
+ #
94
+ # @api private
95
+ def initialize(uid: nil, gid: nil, supplementary_groups: nil, working_dir: nil, environment: nil, stdin: nil, stdout: nil, stderr: nil, timeout: nil)
96
+ @uid = uid
97
+ @gid = gid
98
+ @supplementary_groups = supplementary_groups
99
+ @working_dir = working_dir
100
+ @environment = environment || {}
101
+ @stdin = stdin
102
+ @stdout = stdout
103
+ @stderr = stderr
104
+ @timeout = timeout
105
+ end
106
+
107
+ # Return the options for a hook command
108
+ #
109
+ # They're these, with more environment variables and a timeout.
110
+ #
111
+ # @param [Hash{String => String}] env more environment variables
112
+ # @param [Numeric, ActiveSupport::Duration] timeout the seconds the hook
113
+ # may take
114
+ #
115
+ # @return [CommandOptions]
116
+ #
117
+ # @api private
118
+ def for_hook(env, timeout)
119
+ self.class.new(**to_h, environment: environment.merge(env), timeout: timeout)
120
+ end
121
+
122
+ # Return the options as a Hash
123
+ #
124
+ # @return [Hash{Symbol => Object}] each option by name, nil when unset
125
+ #
126
+ # @api private
127
+ def to_h
128
+ {
129
+ uid: uid, gid: gid, supplementary_groups: supplementary_groups, working_dir: working_dir,
130
+ environment: environment, stdin: stdin, stdout: stdout, stderr: stderr, timeout: timeout
131
+ }
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module System
5
+ # What a command that ran until it finished printed, and how it exited
6
+ #
7
+ # @api private
8
+ class CommandResult
9
+ # Make a result from the Hash that the command's child reports
10
+ #
11
+ # @param [Hash{Symbol => Object}] result its :exit_code, :stdout and
12
+ # :stderr
13
+ #
14
+ # @return [CommandResult]
15
+ #
16
+ # @api private
17
+ def self.from_h(result)
18
+ new(exit_code: result.fetch(:exit_code), stdout: result.fetch(:stdout), stderr: result.fetch(:stderr))
19
+ end
20
+
21
+ # Return the command's exit status
22
+ #
23
+ # @return [Integer, nil] the status, or nil if a signal ended it
24
+ #
25
+ # @api private
26
+ attr_reader :exit_code
27
+
28
+ # Return what the command printed to its standard output
29
+ #
30
+ # @return [String]
31
+ #
32
+ # @api private
33
+ attr_reader :stdout
34
+
35
+ # Return what the command printed to its standard error
36
+ #
37
+ # @return [String]
38
+ #
39
+ # @api private
40
+ attr_reader :stderr
41
+
42
+ # Describe a command's result
43
+ #
44
+ # @param [Integer, nil] exit_code the exit status, or nil if a signal
45
+ # ended the command
46
+ # @param [String] stdout what it printed to its standard output
47
+ # @param [String] stderr what it printed to its standard error
48
+ #
49
+ # @return [void]
50
+ #
51
+ # @api private
52
+ def initialize(exit_code:, stdout: "", stderr: "")
53
+ @exit_code = exit_code
54
+ @stdout = stdout
55
+ @stderr = stderr
56
+ end
57
+
58
+ # Return whether the command exited with status 0
59
+ #
60
+ # @return [Boolean]
61
+ #
62
+ # @api private
63
+ def success? = exit_code&.zero? || false
64
+
65
+ # Return the result as a Hash, as the command's child reports it
66
+ #
67
+ # @return [Hash{Symbol => Object}] the :exit_code, :stdout and :stderr
68
+ #
69
+ # @api private
70
+ def to_h = {exit_code: exit_code, stdout: stdout, stderr: stderr}
71
+ end
72
+ end
73
+ end
@@ -14,17 +14,16 @@ module Bluepill
14
14
  module CommandSetup
15
15
  # Change to the command's working directory and set its environment
16
16
  #
17
- # @param [Hash{Symbol => Object}] options :working_dir and :environment
17
+ # @param [CommandOptions] options its working_dir and environment
18
18
  #
19
19
  # @return [void]
20
20
  #
21
21
  # @api private
22
22
  def setup_command_environment(options)
23
23
  # Setting end PWD env emulates bash behavior when dealing with symlinks
24
- working_dir = options[:working_dir]
24
+ working_dir = options.working_dir
25
25
  Dir.chdir(ENV["PWD"] = working_dir.to_s) if working_dir
26
- environment = options[:environment]
27
- environment&.each { |key, value| ENV[key.to_s] = value.to_s }
26
+ options.environment.each { |key, value| ENV[key.to_s] = value.to_s }
28
27
  end
29
28
 
30
29
  # Switch to another user and groups, when bluepill runs as root
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "io/wait"
3
4
  require "shellwords"
4
5
  require "bluepill/daemonize"
6
+ require "bluepill/system/command_options"
7
+ require "bluepill/system/command_result"
5
8
  require "bluepill/system/command_setup"
6
9
  require "bluepill/system/daemon"
7
10
 
@@ -18,22 +21,20 @@ module Bluepill
18
21
 
19
22
  # Run a command until it finishes, and return what it printed
20
23
  #
21
- # With a :timeout, the command runs in a process group of its own, and
24
+ # With a timeout, the command runs in a process group of its own, and
22
25
  # if it hasn't finished in time, that whole group is killed, so a hung
23
26
  # command and anything it started don't keep running.
24
27
  #
25
28
  # @param [String] cmd the command, split into words without a shell
26
- # @param [Hash{Symbol => Object}] options :uid, :gid,
27
- # :supplementary_groups, :working_dir, :environment and :timeout (in
28
- # seconds)
29
+ # @param [CommandOptions] options its uid, gid, supplementary_groups,
30
+ # working_dir, environment and timeout
29
31
  #
30
- # @return [Hash{Symbol => Object}] its :stdout and :stderr Strings, and
31
- # its :exit_code (nil if a signal ended it)
32
+ # @return [CommandResult] what it printed, and its exit code
32
33
  #
33
- # @raise [Timeout::Error] if it ran longer than the :timeout
34
+ # @raise [Timeout::Error] if it ran longer than the timeout
34
35
  #
35
36
  # @api private
36
- def execute_blocking(cmd, options = {})
37
+ def execute_blocking(cmd, options = CommandOptions.new)
37
38
  rd, wr = IO.pipe
38
39
 
39
40
  child = Daemonize.safefork
@@ -44,18 +45,21 @@ module Bluepill
44
45
  waiter = ::Process.detach(child)
45
46
 
46
47
  wr.close
47
- cmd_status = read_blocking_status(cmd, options[:timeout], child, rd)
48
+ cmd_status = read_blocking_status(cmd, options.timeout, child, rd)
48
49
  waiter.join
49
50
 
50
- cmd_status.empty? ? {exit_code: 0, stdout: "", stderr: ""} : Marshal.load(cmd_status)
51
+ return CommandResult.new(exit_code: 0) if cmd_status.empty?
52
+
53
+ # The child sends a Hash, so loading it needs only core classes
54
+ CommandResult.from_h(Marshal.load(cmd_status))
51
55
  end
52
56
 
53
57
  # Run the command in the child, which execute_blocking forked
54
58
  #
55
- # With a :timeout, the child gets a process group of its own first.
59
+ # With a timeout, the child gets a process group of its own first.
56
60
  #
57
61
  # @param [String] cmd the command
58
- # @param [Hash{Symbol => Object}] options as for execute_blocking
62
+ # @param [CommandOptions] options as for execute_blocking
59
63
  # @param [IO] reader the end of the pipe that only the parent uses
60
64
  # @param [IO] result_pipe where to write the result, marshaled
61
65
  #
@@ -65,7 +69,7 @@ module Bluepill
65
69
  def start_blocking_child(cmd, options, reader, result_pipe)
66
70
  reader.close
67
71
  # The command and what it starts can all be killed at once on a timeout
68
- ::Process.setpgid(0, 0) if options[:timeout]
72
+ ::Process.setpgid(0, 0) if options.timeout
69
73
  run_blocking_child(cmd, options, result_pipe)
70
74
  end
71
75
 
@@ -84,8 +88,7 @@ module Bluepill
84
88
  #
85
89
  # @api private
86
90
  def read_blocking_status(cmd, timeout, child, reader)
87
- # The signatures for IO.select leave out its timeout
88
- ready = timeout.nil? || IO.select([reader], nil, nil, timeout.to_f) # steep:ignore UnresolvedOverloading
91
+ ready = timeout.nil? || reader.wait_readable(timeout.to_f)
89
92
  kill_group_after(child, timeout, cmd) unless ready
90
93
  reader.read
91
94
  ensure
@@ -119,7 +122,7 @@ module Bluepill
119
122
  # result_pipe.
120
123
  #
121
124
  # @param [String] cmd the command
122
- # @param [Hash{Symbol => Object}] options as for execute_blocking
125
+ # @param [CommandOptions] options as for execute_blocking
123
126
  # @param [IO] result_pipe where to write the result, marshaled
124
127
  #
125
128
  # @return [void] it never returns, since it exits the child
@@ -191,7 +194,7 @@ module Bluepill
191
194
  # Replace the grandchild with the command, its output going to the pipes
192
195
  #
193
196
  # @param [String] cmd the command
194
- # @param [Hash{Symbol => Object}] options as for execute_blocking
197
+ # @param [CommandOptions] options as for execute_blocking
195
198
  # @param [Hash{Symbol => Object}] pipes the :out and :err pipes, each a
196
199
  # reader and a writer, and the :result pipe
197
200
  #
@@ -200,7 +203,7 @@ module Bluepill
200
203
  #
201
204
  # @api private
202
205
  def exec_blocking_grandchild(cmd, options, pipes)
203
- drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
206
+ drop_privileges(options.uid, options.gid, options.supplementary_groups)
204
207
  setup_command_environment(options)
205
208
 
206
209
  # close unused fds so ancestors wont hang. This line is the only reason we are not
@@ -2,10 +2,16 @@
2
2
 
3
3
  require "shellwords"
4
4
  require "bluepill/daemonize"
5
+ require "bluepill/system/command_options"
5
6
  require "bluepill/system/command_setup"
6
7
 
7
8
  module Bluepill
8
9
  module System
10
+ # The mode that a daemon's stdout and stderr files are opened in
11
+ #
12
+ # @api private
13
+ APPEND_MODE = "a"
14
+
9
15
  # Run commands in the background, as daemons
10
16
  #
11
17
  # System extends this module, so these are System's module functions.
@@ -21,14 +27,15 @@ module Bluepill
21
27
  # parent the daemon's pid over a pipe.
22
28
  #
23
29
  # @param [String] cmd the command, split into words without a shell
24
- # @param [Hash{Symbol => Object}] options :uid, :gid,
25
- # :supplementary_groups, :working_dir, :environment, :pid_file,
26
- # :logger, :stdin, :stdout and :stderr
30
+ # @param [CommandOptions] options its uid, gid, supplementary_groups,
31
+ # working_dir, environment, stdin, stdout and stderr
32
+ # @param [String] pid_file where to write the daemon's pid
33
+ # @param [Logger] logger where to log why the pid file can't be written
27
34
  #
28
35
  # @return [Integer, nil] the daemon's pid, or nil if it didn't start
29
36
  #
30
37
  # @api private
31
- def daemonize(cmd, options = {})
38
+ def daemonize(cmd, options = CommandOptions.new, pid_file:, logger:)
32
39
  rd, wr = IO.pipe
33
40
 
34
41
  child = Daemonize.safefork
@@ -41,8 +48,8 @@ module Bluepill
41
48
  # child
42
49
  rd.close
43
50
 
44
- drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
45
- start_daemon(cmd, options, wr)
51
+ drop_privileges(options.uid, options.gid, options.supplementary_groups)
52
+ start_daemon(cmd, options, wr, pid_file: pid_file, logger: logger)
46
53
  end
47
54
  end
48
55
 
@@ -69,17 +76,15 @@ module Bluepill
69
76
  # It runs in the child, once it has dropped privileges.
70
77
  #
71
78
  # @param [String] cmd the command
72
- # @param [Hash{Symbol => Object}] options as for daemonize
79
+ # @param [CommandOptions] options as for daemonize
73
80
  # @param [IO] pid_pipe where to write the daemon's pid for the parent
81
+ # @param [String] pid_file where to write the daemon's pid
82
+ # @param [Logger] logger where to log why the pid file can't be written
74
83
  #
75
84
  # @return [void] it never returns, since it exits the child
76
85
  #
77
86
  # @api private
78
- def start_daemon(cmd, options, pid_pipe)
79
- # Processes that bluepill daemonizes always have a pid file and a logger
80
- pid_file = options[:pid_file] #: String
81
- logger = options[:logger] #: Logger
82
-
87
+ def start_daemon(cmd, options, pid_pipe, pid_file:, logger:)
83
88
  # if we cannot write the pid file as the provided user, err out
84
89
  exit unless can_write_pid_file(pid_file, logger)
85
90
 
@@ -97,7 +102,7 @@ module Bluepill
97
102
  # Return what the daemon runs: the command, in its environment
98
103
  #
99
104
  # @param [String] cmd the command
100
- # @param [Hash{Symbol => Object}] options as for daemonize
105
+ # @param [CommandOptions] options as for daemonize
101
106
  #
102
107
  # @return [Proc] a block that execs the command
103
108
  #
@@ -105,7 +110,7 @@ module Bluepill
105
110
  def daemon_command(cmd, options)
106
111
  lambda do
107
112
  setup_command_environment(options)
108
- redirect_io(options[:stdin], options[:stdout], options[:stderr])
113
+ redirect_io(options.stdin, options.stdout, options.stderr)
109
114
 
110
115
  Kernel.exec(*Shellwords.shellwords(cmd))
111
116
  exit
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module System
5
+ # Delete the files that bluepill leaves behind
6
+ #
7
+ # System extends this module, so these are System's module functions.
8
+ #
9
+ # @api private
10
+ module Files
11
+ # Delete a file if it exists
12
+ #
13
+ # It tries three times when permission is denied, and then prints a
14
+ # warning instead of raising.
15
+ #
16
+ # @param [String, nil] filename the file's path
17
+ #
18
+ # @return [void]
19
+ #
20
+ # @api private
21
+ def delete_if_exists(filename)
22
+ tries = 0
23
+
24
+ begin
25
+ File.unlink(filename) if filename && File.exist?(filename)
26
+ rescue IOError, Errno::ENOENT
27
+ # Already gone, or another process removed it in between
28
+ rescue Errno::EACCES
29
+ retry if (tries += 1) < 3
30
+ $stderr.puts("Warning: permission denied trying to delete #{filename}")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -4,6 +4,16 @@ require "bluepill/system/ps_table"
4
4
 
5
5
  module Bluepill
6
6
  module System
7
+ # Raised when ps could not be run, so there is no data for this tick
8
+ #
9
+ # @api private
10
+ class PsError < StandardError; end
11
+
12
+ # Watches run in their own threads, so only let one of them run ps per tick
13
+ #
14
+ # @api private
15
+ PS_MUTEX = Mutex.new
16
+
7
17
  # Run ps once a tick, and read its table of processes
8
18
  #
9
19
  # System extends this module, so these are System's module functions.
@@ -12,43 +22,36 @@ module Bluepill
12
22
  #
13
23
  # @api private
14
24
  module Ps
15
- # Return what this tick has memoized
16
- #
17
- # @return [Hash{Symbol => PsTable, String}] the ps table, or ps's
18
- # error, by name
19
- #
20
- # @api private
21
- def store
22
- @store ||= {}
23
- end
24
-
25
- # Forget what the last tick memoized, so ps runs again
25
+ # Forget the last tick's ps table, so ps runs again
26
26
  #
27
27
  # @return [void]
28
28
  #
29
29
  # @api private
30
30
  def reset_data
31
31
  @children_table = nil
32
- store.clear
32
+ @ps_table = nil
33
33
  end
34
34
 
35
35
  # Return this tick's ps table, running ps the first time it's asked for
36
36
  #
37
+ # When ps could not be run, the error is kept instead, and raised
38
+ # again until the next tick.
39
+ #
37
40
  # @return [PsTable] each process's ps fields
38
41
  #
39
42
  # @raise [PsError] if ps could not be run this tick
40
43
  #
41
44
  # @api private
42
45
  def ps_axu
43
- ps = PS_MUTEX.synchronize { store[:ps_axu] ||= read_ps_table }
44
- raise PsError, ps if ps.instance_of?(String)
46
+ ps = PS_MUTEX.synchronize { @ps_table ||= read_ps_table }
47
+ raise ps if ps.instance_of?(PsError)
45
48
 
46
49
  ps
47
50
  end
48
51
 
49
52
  # Run ps, and parse what it prints into a table
50
53
  #
51
- # @return [PsTable, String] each process's ps fields, or why ps could
54
+ # @return [PsTable, PsError] each process's ps fields, or why ps could
52
55
  # not be run
53
56
  #
54
57
  # @api private
@@ -58,8 +61,8 @@ module Bluepill
58
61
  PsTable.parse(`ps -Ao pid,ppid,pcpu,rss,etime,args`)
59
62
  rescue SystemCallError => e
60
63
  # Forking ps can fail, for example with ENOMEM or EAGAIN under memory pressure.
61
- # Cache the message so ps is not run again until the next tick.
62
- "Could not run ps: #{e.class}: #{e}"
64
+ # Keep the error so ps is not run again until the next tick.
65
+ PsError.new("Could not run ps: #{e.class}: #{e}")
63
66
  end
64
67
 
65
68
  # Return the pids of a process's descendants, from this tick's ps table