bluepill 0.1.6 → 0.2.1

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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +106 -9
  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 +56 -254
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/clock.rb +25 -0
  13. data/lib/bluepill/condition_watch.rb +136 -58
  14. data/lib/bluepill/controller/directory.rb +59 -0
  15. data/lib/bluepill/controller.rb +99 -94
  16. data/lib/bluepill/daemonize.rb +234 -0
  17. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  18. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  20. data/lib/bluepill/dsl/process_factory.rb +81 -92
  21. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +60 -657
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +52 -24
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -26
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +33 -36
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +50 -871
  74. metadata +40 -45
@@ -0,0 +1,98 @@
1
+ require "etc"
2
+
3
+ module Bluepill
4
+ module System
5
+ # Set up the process that a command runs in: its user, groups, working
6
+ # directory and environment
7
+ #
8
+ # Commands and Daemon include it, and System extends them, so these are
9
+ # System's module functions.
10
+ #
11
+ # @api private
12
+ module CommandSetup
13
+ # Change to the command's working directory and set its environment
14
+ #
15
+ # @param [Hash{Symbol => Object}] options :working_dir and :environment
16
+ #
17
+ # @return [void]
18
+ #
19
+ # @api private
20
+ def setup_command_environment(options)
21
+ # Setting end PWD env emulates bash behavior when dealing with symlinks
22
+ working_dir = options[:working_dir]
23
+ Dir.chdir(ENV["PWD"] = working_dir.to_s) if working_dir
24
+ environment = options[:environment]
25
+ environment.each { |key, value| ENV[key.to_s] = value.to_s } if environment
26
+ end
27
+
28
+ # Switch to another user and groups, when bluepill runs as root
29
+ #
30
+ # With a uid, the command gets the user's own supplementary groups, the
31
+ # way logging in as the user does, and the user's primary group unless
32
+ # gid names another. supplementary_groups adds more groups to those.
33
+ #
34
+ # Be sure to call this from a fork, or it changes the user and groups of
35
+ # the bluepill daemon itself.
36
+ #
37
+ # @param [String, nil] uid the user's name
38
+ # @param [String, nil] gid the group's name
39
+ # @param [Array<String>, nil] supplementary_groups more groups' names
40
+ #
41
+ # @return [void]
42
+ #
43
+ # @api private
44
+ def drop_privileges(uid, gid, supplementary_groups)
45
+ return unless ::Process::Sys.geteuid.zero?
46
+
47
+ user = Etc.getpwnam(uid) if uid
48
+ gid_num = join_groups(user, (Etc.getgrnam(gid).gid if gid))
49
+ add_supplementary_groups(supplementary_groups)
50
+ ::Process::Sys.setgid(gid_num) if gid_num
51
+ switch_to_user(user) if user
52
+ end
53
+
54
+ # Join the user's groups, or just the named group without a user
55
+ #
56
+ # @param [Etc::Passwd, nil] user the user
57
+ # @param [Integer, nil] gid_num the named group's id
58
+ #
59
+ # @return [Integer, nil] the primary group's id: the named group's, or
60
+ # else the user's own
61
+ #
62
+ # @api private
63
+ def join_groups(user, gid_num)
64
+ if user
65
+ gid_num ||= user.gid
66
+ ::Process.initgroups(user.name, gid_num)
67
+ elsif gid_num
68
+ ::Process.groups = [gid_num]
69
+ end
70
+ gid_num
71
+ end
72
+
73
+ # Add more groups to the ones the process is in
74
+ #
75
+ # @param [Array<String>, nil] supplementary_groups the groups' names
76
+ #
77
+ # @return [void]
78
+ #
79
+ # @api private
80
+ def add_supplementary_groups(supplementary_groups)
81
+ group_nums = (supplementary_groups || []).collect { |group| Etc.getgrnam(group).gid }
82
+ ::Process.groups |= group_nums unless group_nums.empty?
83
+ end
84
+
85
+ # Become the user, once the groups are set, and point HOME at its home directory
86
+ #
87
+ # @param [Etc::Passwd] user the user
88
+ #
89
+ # @return [void]
90
+ #
91
+ # @api private
92
+ def switch_to_user(user)
93
+ ::Process::Sys.setuid(user.uid)
94
+ ENV["HOME"] = user.dir if user.dir
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,285 +1,256 @@
1
- require 'etc'
2
- require 'shellwords'
1
+ require "shellwords"
2
+ require "bluepill/daemonize"
3
+ require "bluepill/system/command_setup"
4
+ require "bluepill/system/daemon"
3
5
 
4
6
  module Bluepill
5
7
  module System
6
- # Runs commands for bluepill: in the background as daemons, or until they
7
- # finish, optionally as another user. System's documentation is in
8
- # system.rb.
9
-
10
- # Start a command as a daemon, and write its pid file
11
- #
12
- # It forks a child that drops privileges, sets up the environment and
13
- # standard streams, and daemonizes the command. The child tells the
14
- # parent the daemon's pid over a pipe.
15
- #
16
- # @param [String] cmd the command, split into words without a shell
17
- # @param [Hash{Symbol => Object}] options :uid, :gid,
18
- # :supplementary_groups, :working_dir, :environment, :pid_file,
19
- # :logger, :stdin, :stdout and :stderr
8
+ # Run commands until they finish, and collect what they print
20
9
  #
21
- # @return [Integer, nil] the daemon's pid, or nil if it didn't start
10
+ # System extends this module, so these are System's module functions.
11
+ # Daemon runs commands in the background instead.
22
12
  #
23
13
  # @api private
24
- def self.daemonize(cmd, options = {})
25
- rd, wr = IO.pipe
26
-
27
- child = Daemonize.safefork
28
- if child
29
- # we don't want to create zombies, so detach ourselves from the child exit status
30
- ::Process.detach(child)
31
-
32
- # parent
33
- wr.close
34
-
35
- daemon_id = rd.read.to_i
36
- rd.close
37
-
38
- daemon_id if daemon_id > 0
39
- else
40
- # child
41
- rd.close
42
-
43
- drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
14
+ module Commands
15
+ include CommandSetup
16
+
17
+ # Run a command until it finishes, and return what it printed
18
+ #
19
+ # With a :timeout, the command runs in a process group of its own, and
20
+ # if it hasn't finished in time, that whole group is killed, so a hung
21
+ # command and anything it started don't keep running.
22
+ #
23
+ # @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 and :timeout (in
26
+ # seconds)
27
+ #
28
+ # @return [Hash{Symbol => Object}] its :stdout and :stderr Strings, and
29
+ # its :exit_code (nil if a signal ended it)
30
+ #
31
+ # @raise [Timeout::Error] if it ran longer than the :timeout
32
+ #
33
+ # @api private
34
+ def execute_blocking(cmd, options = {})
35
+ rd, wr = IO.pipe
36
+
37
+ child = Daemonize.safefork
38
+ return start_blocking_child(cmd, options, rd, wr) unless child
44
39
 
45
- # Processes that bluepill daemonizes always have a pid file and a logger
46
- pid_file = options[:pid_file] #: String
47
- logger = options[:logger] #: Logger
48
-
49
- # if we cannot write the pid file as the provided user, err out
50
- exit unless can_write_pid_file(pid_file, logger)
51
-
52
- to_daemonize = lambda do
53
- setup_command_environment(options)
54
- redirect_io(options[:stdin], options[:stdout], options[:stderr])
55
-
56
- Kernel.exec(*Shellwords.shellwords(cmd))
57
- exit
58
- end
59
-
60
- daemon_id = Daemonize.call_as_daemon(to_daemonize, nil, cmd)
61
-
62
- # call_as_daemon returns 0 when the daemon fails to report its pid
63
- File.open(pid_file, 'w') { |f| f.write(daemon_id) } if daemon_id > 0
64
-
65
- wr.write daemon_id
66
- wr.close
67
-
68
- ::Process.exit!(true)
69
- end
70
- end
71
-
72
- # Run a command until it finishes, and return what it printed
73
- #
74
- # @param [String] cmd the command, split into words without a shell
75
- # @param [Hash{Symbol => Object}] options :uid, :gid,
76
- # :supplementary_groups, :working_dir and :environment
77
- #
78
- # @return [Hash{Symbol => Object}] its :stdout and :stderr Strings, and
79
- # its :exit_code (nil if a signal ended it)
80
- #
81
- # @api private
82
- def self.execute_blocking(cmd, options = {})
83
- rd, wr = IO.pipe
84
-
85
- child = Daemonize.safefork
86
- if child
87
40
  # Reap the child in a background thread, so it never becomes a zombie,
88
41
  # even if a timeout interrupts us before it exits
89
42
  waiter = ::Process.detach(child)
90
43
 
91
44
  wr.close
92
- # @type var cmd_status: String
93
- begin
94
- cmd_status = rd.read
95
- ensure
96
- rd.close
97
- end
45
+ cmd_status = read_blocking_status(cmd, options[:timeout], child, rd)
98
46
  waiter.join
99
47
 
100
- cmd_status.empty? ? {exit_code: 0, stdout: '', stderr: ''} : Marshal.load(cmd_status)
101
- else
102
- rd.close
103
- run_blocking_child(cmd, options, wr)
48
+ cmd_status.empty? ? {exit_code: 0, stdout: "", stderr: ""} : Marshal.load(cmd_status)
104
49
  end
105
- end
106
50
 
107
- # Run a command in a grandchild, report its result, and exit
108
- #
109
- # Sends the grandchild's output and exit code to the parent over
110
- # result_pipe.
111
- #
112
- # @param [String] cmd the command
113
- # @param [Hash{Symbol => Object}] options as for execute_blocking
114
- # @param [IO] result_pipe where to write the result, marshaled
115
- #
116
- # @return [void] it never returns, since it exits the child
117
- #
118
- # @api private
119
- def self.run_blocking_child(cmd, options, result_pipe)
120
- # create a child in which we can override the stdin, stdout and stderr
121
- cmd_out_read, cmd_out_write = IO.pipe
122
- cmd_err_read, cmd_err_write = IO.pipe
51
+ # Run the command in the child, which execute_blocking forked
52
+ #
53
+ # With a :timeout, the child gets a process group of its own first.
54
+ #
55
+ # @param [String] cmd the command
56
+ # @param [Hash{Symbol => Object}] options as for execute_blocking
57
+ # @param [IO] reader the end of the pipe that only the parent uses
58
+ # @param [IO] result_pipe where to write the result, marshaled
59
+ #
60
+ # @return [void] it never returns, since it exits the child
61
+ #
62
+ # @api private
63
+ def start_blocking_child(cmd, options, reader, result_pipe)
64
+ reader.close
65
+ # The command and what it starts can all be killed at once on a timeout
66
+ ::Process.setpgid(0, 0) if options[:timeout]
67
+ run_blocking_child(cmd, options, result_pipe)
68
+ end
123
69
 
124
- pid = fork do
125
- # @type var pipes: grandchild_pipes
126
- pipes = {out: [cmd_out_read, cmd_out_write], err: [cmd_err_read, cmd_err_write], result: result_pipe}
127
- exec_blocking_grandchild(cmd, options, pipes)
70
+ # Read what the child reports about the command, in the parent
71
+ #
72
+ # @param [String] cmd the command, for the error on a timeout
73
+ # @param [Numeric, ActiveSupport::Duration, nil] timeout the seconds to
74
+ # wait for it, or nil to wait until it finishes
75
+ # @param [Integer] child the child's pid
76
+ # @param [IO] reader the end of the pipe that the child writes to
77
+ #
78
+ # @return [String] the marshaled result, or "" if the child reported
79
+ # nothing
80
+ #
81
+ # @raise [Timeout::Error] if it ran longer than the timeout
82
+ #
83
+ # @api private
84
+ def read_blocking_status(cmd, timeout, child, reader)
85
+ # The signatures for IO.select leave out its timeout
86
+ ready = timeout.nil? || IO.select([reader], nil, nil, timeout.to_f) # steep:ignore UnresolvedOverloading
87
+ kill_group_after(child, timeout, cmd) unless ready
88
+ reader.read
89
+ ensure
90
+ reader.close
128
91
  end
129
92
 
130
- # we do not use these ends of the pipes in the child
131
- cmd_out_write.close
132
- cmd_err_write.close
93
+ # Kill a command's process group, once it has run past its timeout
94
+ #
95
+ # @param [Integer] child the pid of the group's leader, which runs the
96
+ # command
97
+ # @param [Numeric, ActiveSupport::Duration] timeout the seconds it was given
98
+ # @param [String] cmd the command, for the error
99
+ #
100
+ # @return [void] it never returns
101
+ #
102
+ # @raise [Timeout::Error] always
103
+ #
104
+ # @api private
105
+ def kill_group_after(child, timeout, cmd)
106
+ begin
107
+ ::Process.kill("KILL", -child)
108
+ rescue Errno::ESRCH
109
+ nil # it finished just now
110
+ end
111
+ raise Timeout::Error, "#{cmd} took longer than #{timeout} seconds"
112
+ end
133
113
 
134
- # wait for the cmd to finish executing and acknowledge it's death
135
- _, status = ::Process.waitpid2(pid)
114
+ # Run a command in a grandchild, report its result, and exit
115
+ #
116
+ # Sends the grandchild's output and exit code to the parent over
117
+ # result_pipe.
118
+ #
119
+ # @param [String] cmd the command
120
+ # @param [Hash{Symbol => Object}] options as for execute_blocking
121
+ # @param [IO] result_pipe where to write the result, marshaled
122
+ #
123
+ # @return [void] it never returns, since it exits the child
124
+ #
125
+ # @api private
126
+ def run_blocking_child(cmd, options, result_pipe)
127
+ # create a child in which we can override the stdin, stdout and stderr
128
+ cmd_out_read, cmd_out_write = IO.pipe
129
+ cmd_err_read, cmd_err_write = IO.pipe
130
+
131
+ pid = fork do
132
+ # @type var pipes: grandchild_pipes
133
+ pipes = {out: [cmd_out_read, cmd_out_write], err: [cmd_err_read, cmd_err_write], result: result_pipe}
134
+ exec_blocking_grandchild(cmd, options, pipes)
135
+ end
136
136
 
137
- result = {
138
- stdout: cmd_out_read.read,
139
- stderr: cmd_err_read.read,
140
- exit_code: status.exitstatus,
141
- }
142
- cmd_out_read.close
143
- cmd_err_read.close
137
+ # we do not use these ends of the pipes in the child
138
+ cmd_out_write.close
139
+ cmd_err_write.close
144
140
 
145
- # Time to tell the parent about what went down
146
- begin
147
- result_pipe.write Marshal.dump(result)
148
- rescue Errno::EPIPE
149
- # The parent timed out and stopped waiting for the result
141
+ report_blocking_result(result_pipe, collect_blocking_result(pid, cmd_out_read, cmd_err_read))
150
142
  end
151
- result_pipe.close
152
-
153
- ::Process.exit!
154
- end
155
-
156
- # Replace the grandchild with the command, its output going to the pipes
157
- #
158
- # @param [String] cmd the command
159
- # @param [Hash{Symbol => Object}] options as for execute_blocking
160
- # @param [Hash{Symbol => Object}] pipes the :out and :err pipes, each a
161
- # reader and a writer, and the :result pipe
162
- #
163
- # @return [void] it never returns, since it execs the command, or exits
164
- # with status 1 if it can't
165
- #
166
- # @api private
167
- def self.exec_blocking_grandchild(cmd, options, pipes)
168
- # @type var cmd_err_write: IO
169
- cmd_out_read, cmd_out_write = pipes.fetch(:out)
170
- cmd_err_read, cmd_err_write = pipes.fetch(:err)
171
143
 
172
- drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
173
- setup_command_environment(options)
144
+ # Wait for the grandchild to finish, and collect what it printed
145
+ #
146
+ # @param [Integer] pid the grandchild's pid
147
+ # @param [IO] cmd_out_read where to read its standard output
148
+ # @param [IO] cmd_err_read where to read its standard error
149
+ #
150
+ # @return [Hash{Symbol => Object}] its :stdout and :stderr Strings, and
151
+ # its :exit_code (nil if a signal ended it)
152
+ #
153
+ # @api private
154
+ def collect_blocking_result(pid, cmd_out_read, cmd_err_read)
155
+ # wait for the cmd to finish executing and acknowledge it's death
156
+ _, status = ::Process.waitpid2(pid)
157
+
158
+ # @type var result: blocking_result
159
+ result = {
160
+ stdout: cmd_out_read.read,
161
+ stderr: cmd_err_read.read,
162
+ exit_code: status.exitstatus
163
+ }
164
+ cmd_out_read.close
165
+ cmd_err_read.close
166
+ result
167
+ end
174
168
 
175
- # close unused fds so ancestors wont hang. This line is the only reason we are not
176
- # using something like popen3. If this fd is not closed, the .read call on the parent
177
- # will never return because the result pipe would still be open in the "exec"-ed cmd
178
- pipes.fetch(:result).close
169
+ # Tell the parent the command's result, and exit the child
170
+ #
171
+ # @param [IO] result_pipe where to write the result, marshaled
172
+ # @param [Hash{Symbol => Object}] result the command's result
173
+ #
174
+ # @return [void] it never returns, since it exits the child
175
+ #
176
+ # @api private
177
+ def report_blocking_result(result_pipe, result)
178
+ # Time to tell the parent about what went down
179
+ begin
180
+ result_pipe.write Marshal.dump(result)
181
+ rescue Errno::EPIPE
182
+ # The parent timed out and stopped waiting for the result
183
+ end
184
+ result_pipe.close
179
185
 
180
- # we do not care about stdin of cmd
181
- STDIN.reopen('/dev/null')
186
+ ::Process.exit!
187
+ end
182
188
 
183
- # point stdout and stderr of cmd to somewhere we can read
184
- cmd_out_read.close
185
- STDOUT.reopen(cmd_out_write)
186
- cmd_out_write.close
187
- cmd_err_read.close
188
- STDERR.reopen(cmd_err_write)
189
- cmd_err_write.close
189
+ # Replace the grandchild with the command, its output going to the pipes
190
+ #
191
+ # @param [String] cmd the command
192
+ # @param [Hash{Symbol => Object}] options as for execute_blocking
193
+ # @param [Hash{Symbol => Object}] pipes the :out and :err pipes, each a
194
+ # reader and a writer, and the :result pipe
195
+ #
196
+ # @return [void] it never returns, since it execs the command, or exits
197
+ # with status 1 if it can't
198
+ #
199
+ # @api private
200
+ def exec_blocking_grandchild(cmd, options, pipes)
201
+ drop_privileges(options[:uid], options[:gid], options[:supplementary_groups])
202
+ setup_command_environment(options)
190
203
 
191
- Kernel.exec(*Shellwords.shellwords(cmd))
192
- rescue StandardError => e
193
- err = cmd_err_write.closed? ? STDERR : cmd_err_write
194
- err.puts "Exception in grandchild: #{e}."
195
- err.puts e.backtrace
196
- exit 1
197
- end
204
+ # close unused fds so ancestors wont hang. This line is the only reason we are not
205
+ # using something like popen3. If this fd is not closed, the .read call on the parent
206
+ # will never return because the result pipe would still be open in the "exec"-ed cmd
207
+ pipes.fetch(:result).close
198
208
 
199
- # Change to the command's working directory and set its environment
200
- #
201
- # @param [Hash{Symbol => Object}] options :working_dir and :environment
202
- #
203
- # @return [void]
204
- #
205
- # @api private
206
- def self.setup_command_environment(options)
207
- # Setting end PWD env emulates bash behavior when dealing with symlinks
208
- working_dir = options[:working_dir]
209
- Dir.chdir(ENV['PWD'] = working_dir.to_s) if working_dir
210
- environment = options[:environment]
211
- environment.each { |key, value| ENV[key.to_s] = value.to_s } if environment
212
- end
209
+ # we do not care about stdin of cmd
210
+ STDIN.reopen(File::NULL)
213
211
 
214
- # Switch to another user and groups, when bluepill runs as root
215
- #
216
- # Be sure to call this from a fork, or it changes the user and groups of
217
- # the bluepill daemon itself.
218
- #
219
- # @param [String, nil] uid the user's name
220
- # @param [String, nil] gid the group's name
221
- # @param [Array<String>, nil] supplementary_groups more groups' names
222
- #
223
- # @return [void]
224
- #
225
- # @api private
226
- def self.drop_privileges(uid, gid, supplementary_groups)
227
- return unless ::Process::Sys.geteuid.zero?
212
+ # point stdout and stderr of cmd to somewhere we can read
213
+ reopen_on_pipe(STDOUT, pipes.fetch(:out))
214
+ reopen_on_pipe(STDERR, pipes.fetch(:err))
228
215
 
229
- uid_num = Etc.getpwnam(uid).uid if uid
230
- gid_num = Etc.getgrnam(gid).gid if gid
231
- supplementary_groups ||= [] #: Array[String]
232
- group_nums = supplementary_groups.collect do |group|
233
- Etc.getgrnam(group).gid
216
+ Kernel.exec(*Shellwords.shellwords(cmd))
217
+ rescue => e
218
+ report_grandchild_error(e, pipes)
234
219
  end
235
- ::Process.groups = [gid_num] if gid_num
236
- ::Process.groups |= group_nums unless group_nums.empty?
237
- ::Process::Sys.setgid(gid_num) if gid_num
238
- ::Process::Sys.setuid(uid_num) if uid_num
239
- ENV['HOME'] = Etc.getpwuid(uid_num).try(:dir) || ENV['HOME'] if uid_num
240
- end
241
220
 
242
- # Return whether the pid file can be written, logging why if it can't
243
- #
244
- # @param [String] pid_file the pid file's path
245
- # @param [Logger] logger where to log the error
246
- #
247
- # @return [Boolean] true if the file could be created and removed
248
- #
249
- # @api private
250
- def self.can_write_pid_file(pid_file, logger)
251
- FileUtils.touch(pid_file)
252
- File.unlink(pid_file)
253
- true
254
- rescue StandardError => e
255
- logger.warning(format('%s - %s', e.class, e))
256
- # A rescued exception was raised, so it has a backtrace
257
- backtrace = e.backtrace #: Array[String]
258
- backtrace.each { |l| logger.warning l }
259
- false
260
- end
261
-
262
- # Point standard input, output and error at files
263
- #
264
- # Output and error share one file when they name the same one.
265
- #
266
- # @param [String, nil] io_in the file for standard input
267
- # @param [String, nil] io_out the file for standard output, appended to
268
- # @param [String, nil] io_err the file for standard error, appended to
269
- #
270
- # @return [void]
271
- #
272
- # @api private
273
- def self.redirect_io(io_in, io_out, io_err)
274
- $stdin.reopen(io_in) if io_in
275
-
276
- if !io_out.nil? && io_out.eql?(io_err)
277
- $stdout.reopen(io_out, APPEND_MODE)
278
- $stderr.reopen($stdout)
221
+ # Point a standard stream at a pipe's writer, and close the pipe's ends
222
+ #
223
+ # @param [IO] stream STDOUT or STDERR
224
+ # @param [Array(IO, IO)] pipe the pipe's reader, which only the
225
+ # grandchild's parent uses, and its writer
226
+ #
227
+ # @return [void]
228
+ #
229
+ # @api private
230
+ def reopen_on_pipe(stream, pipe)
231
+ reader, writer = pipe
232
+ reader.close
233
+ stream.reopen(writer)
234
+ writer.close
235
+ end
279
236
 
280
- else
281
- $stdout.reopen(io_out, APPEND_MODE) if io_out
282
- $stderr.reopen(io_err, APPEND_MODE) if io_err
237
+ # Print why the grandchild couldn't exec the command, and exit
238
+ #
239
+ # It prints to the pipe for the command's standard error, unless that's
240
+ # closed already.
241
+ #
242
+ # @param [Exception] error what went wrong
243
+ # @param [Hash{Symbol => Object}] pipes as for exec_blocking_grandchild
244
+ #
245
+ # @return [void] it never returns, since it exits with status 1
246
+ #
247
+ # @api private
248
+ def report_grandchild_error(error, pipes)
249
+ cmd_err_write = pipes.fetch(:err).last
250
+ err = cmd_err_write.closed? ? STDERR : cmd_err_write
251
+ err.puts "Exception in grandchild: #{error}."
252
+ err.puts error.backtrace
253
+ exit 1
283
254
  end
284
255
  end
285
256
  end