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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +57 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +105 -8
- data/bin/bluepill +25 -30
- data/bluepill.gemspec +21 -28
- data/lib/bluepill/application/lifecycle.rb +166 -0
- data/lib/bluepill/application/listener.rb +124 -0
- data/lib/bluepill/application/previous_daemon.rb +81 -0
- data/lib/bluepill/application.rb +59 -231
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +134 -56
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -94
- data/lib/bluepill/daemonize.rb +234 -0
- data/lib/bluepill/dsl/app_proxy.rb +30 -6
- data/lib/bluepill/dsl/process_factory/children.rb +71 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
- data/lib/bluepill/dsl/process_factory.rb +81 -92
- data/lib/bluepill/dsl/process_proxy.rb +109 -21
- data/lib/bluepill/group.rb +187 -78
- data/lib/bluepill/logger.rb +139 -48
- data/lib/bluepill/pid_file.rb +23 -0
- data/lib/bluepill/process/attributes.rb +379 -0
- data/lib/bluepill/process/children.rb +70 -0
- data/lib/bluepill/process/pid.rb +154 -0
- data/lib/bluepill/process/state_machine.rb +196 -0
- data/lib/bluepill/process/transition.rb +45 -0
- data/lib/bluepill/process/transition_callbacks.rb +101 -0
- data/lib/bluepill/process/user_commands.rb +71 -0
- data/lib/bluepill/process/watches.rb +141 -0
- data/lib/bluepill/process.rb +56 -653
- data/lib/bluepill/process_commands/execution.rb +89 -0
- data/lib/bluepill/process_commands/hooks.rb +186 -0
- data/lib/bluepill/process_commands/stop_signals.rb +114 -0
- data/lib/bluepill/process_commands.rb +104 -172
- data/lib/bluepill/process_conditions/always_true.rb +1 -1
- data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
- data/lib/bluepill/process_conditions/file_time.rb +4 -5
- data/lib/bluepill/process_conditions/http.rb +52 -24
- data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
- data/lib/bluepill/process_conditions/process_condition.rb +34 -3
- data/lib/bluepill/process_conditions/running_time.rb +1 -25
- data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
- data/lib/bluepill/process_conditions.rb +3 -2
- data/lib/bluepill/process_journal/cleanup.rb +200 -0
- data/lib/bluepill/process_journal/lock.rb +106 -0
- data/lib/bluepill/process_journal.rb +126 -233
- data/lib/bluepill/process_statistics.rb +2 -2
- data/lib/bluepill/socket.rb +60 -20
- data/lib/bluepill/system/command_setup.rb +98 -0
- data/lib/bluepill/system/commands.rb +219 -248
- data/lib/bluepill/system/daemon.rb +161 -0
- data/lib/bluepill/system/ps.rb +85 -0
- data/lib/bluepill/system/ps_table.rb +163 -0
- data/lib/bluepill/system.rb +45 -126
- data/lib/bluepill/trigger.rb +52 -26
- data/lib/bluepill/triggers/flapping.rb +7 -15
- data/lib/bluepill/units.rb +126 -0
- data/lib/bluepill/util/inflections.rb +34 -0
- data/lib/bluepill/util/levenshtein.rb +53 -0
- data/lib/bluepill/util/rotational_array.rb +1 -1
- data/lib/bluepill/version.rb +13 -12
- data/lib/bluepill.rb +33 -36
- data/sig/bluepill/application.rbs +257 -0
- data/sig/bluepill/checks.rbs +203 -0
- data/sig/bluepill/dsl.rbs +126 -0
- data/sig/bluepill/logger.rbs +104 -0
- data/sig/bluepill/process.rbs +542 -0
- data/sig/bluepill/system.rbs +316 -0
- data/sig/bluepill.rbs +47 -869
- metadata +39 -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
|
|
2
|
-
require
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
#
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
#
|
|
135
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
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
|
-
|
|
173
|
-
|
|
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
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
|
|
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
|
-
|
|
181
|
-
|
|
186
|
+
::Process.exit!
|
|
187
|
+
end
|
|
182
188
|
|
|
183
|
-
#
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|