bluepill 0.1.4 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26066730802a39d8f2aad6fb204d6d8430e8683bd20d146ff51ceca6a0e45410
4
- data.tar.gz: ea860bf052753fd121a0418e94c4bb33b7d3e4424217d773529228adb54900a1
3
+ metadata.gz: 3fc2c246694d286bd189536ffd4e3abd220d04fde87872246a81179d6fd23b6f
4
+ data.tar.gz: 9ca9c2325d09134b23b8c6f7f1b3265805bdd5f56f8476ca75425ef4e0d92094
5
5
  SHA512:
6
- metadata.gz: 046ae39d7890fd773663e46a64bb133fdfeec4bfad0aef0ffe9c3abd1fddf41db3037448d9adeb9dcda14209cc320efb9d2bac111684e7f2febdf87505bab9c0
7
- data.tar.gz: 1dbe87dce2e2f5f9333d76b1103a0def54028e6cd0972b4817e36576fbc206c0dc185e04376789754ba71c2eb7e520184887c70df6e29875d012c6b8396c780f
6
+ metadata.gz: 18a4f5d99a942e3d81675b0e2b2c31f360aac01f36008f81a8a2ca4f13f02d02f42307df877704fdbce9f108659e0972ff7a8bcbe966ff5002186ab1051c97d1
7
+ data.tar.gz: f34d0e714b9194bbca3c279ebd8c34f5c387ecd5822b549e0e237c896def85ae98dd9d8195f67c37b33a3c027c0a6764d130ab30faeab5d2ab1b7c7ecd43b8b7
data/CHANGELOG.md CHANGED
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.6] - 2026-11-02
11
+
12
+ ### Security
13
+
14
+ - The daemon now runs only the commands that the CLI sends over its socket: `start`, `stop`, `restart`, `unmonitor`, `status`, `version` and `log_file`. Before, it called whatever method a line on the socket named, so anyone who could write to the socket could call any of the daemon's public methods, such as `instance_eval` with code of their own. The socket takes the daemon's umask, so with the usual umask of 022 only the daemon's own user could connect. Any other command now gets a `NoMethodError` back.
15
+
16
+ ## [0.1.5] - 2026-11-01
17
+
18
+ ### Removed
19
+
20
+ - `ProcessJournal.kill_all_from_all_journals`, which was meant to kill every journaled process when bluepill started or quit, but looked in the wrong directory and never found any ([#59](https://github.com/bluepill-rb/bluepill/issues/59)). Bluepill still kills what a process's journal lists before it starts that process, and `bluepill load` keeps monitoring the processes that are already running instead of restarting them.
21
+
22
+ ### Fixed
23
+
24
+ - A restart that stops and starts the process, because a check asked for one or from `bluepill restart`, left bluepill watching the old pid. So the process went from `restarting` to `down` and back to `up`, and bluepill logged an error about signaling the old pid. `bluepill restart` now also goes through `restarting`, like a check's restart, instead of leaving the process `up` with its old pid while it restarts.
25
+ - `SIGHUP` never reopened bluepill's log, because daemonizing ignored it after bluepill trapped it. So after log rotation, bluepill kept writing to the rotated file. Bluepill now traps `SIGHUP` again once it has daemonized. Reopening syslog on `SIGHUP` also no longer crashes bluepill, which 0.1.4 did in foreground mode, since it loads syslog only when it logs there.
26
+ - Stale journal locks, which a daemon that died leaves behind, were never cleared, since bluepill looked for them in the current directory instead of the journal directory, and removed them as if they were files ([#59](https://github.com/bluepill-rb/bluepill/issues/59)). A stale lock made the next write to its journal wait 10 seconds and fail. Bluepill now clears the app's own locks when it starts and quits.
27
+ - After a reboot, or once pids wrapped around, bluepill could kill an unrelated process whose pid it had journaled ([#3](https://github.com/bluepill-rb/bluepill/issues/3)). The journal now records when each process started, and bluepill only kills a pid whose process started then. A process group whose leader has exited, and a journal line from bluepill 0.1 and earlier, are only trusted if they were journaled since the machine booted.
28
+ - Two apps with a process of the same name shared its journal, so starting the process in one app killed the other app's ([#34](https://github.com/bluepill-rb/bluepill/issues/34)). Each app now keeps its journals in a directory of its own, `journals/<app>`. Journals that bluepill 0.1 and earlier wrote are still read and cleared, so upgrading doesn't leave their processes running.
29
+
10
30
  ## [0.1.4] - 2026-11-01
11
31
 
12
32
  ### Added
@@ -744,7 +764,9 @@ Initial release.
744
764
  - Added the `bluepill` CLI with the `load`, `status`, `start`, `stop`, `restart`, `unmonitor`, `quit` and `log` commands.
745
765
  - Added syslog logging.
746
766
 
747
- [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...HEAD
767
+ [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...HEAD
768
+ [0.1.6]: https://github.com/bluepill-rb/bluepill/compare/v0.1.5...v0.1.6
769
+ [0.1.5]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...v0.1.5
748
770
  [0.1.4]: https://github.com/bluepill-rb/bluepill/compare/v0.1.3...v0.1.4
749
771
  [0.1.3]: https://github.com/bluepill-rb/bluepill/compare/v0.1.2...v0.1.3
750
772
  [0.1.2]: https://github.com/bluepill-rb/bluepill/compare/v0.1.1...v0.1.2
data/README.md CHANGED
@@ -347,6 +347,8 @@ To quit the bluepill daemon for an application:
347
347
 
348
348
  sudo bluepill quit
349
349
 
350
+ Its processes keep running. The next `bluepill load` monitors them again, from their pid files, without restarting them, so you can upgrade bluepill this way.
351
+
350
352
  #### bpsv
351
353
 
352
354
  `bpsv` runs a bluepill command on the process it's named after, so a
@@ -13,6 +13,12 @@ module Bluepill
13
13
  # The commands that act on a process or group, and that the CLI sends
14
14
  PROCESS_COMMANDS = [:start, :stop, :restart, :unmonitor, :status].freeze
15
15
 
16
+ # The commands that the daemon answers over its socket: the
17
+ # PROCESS_COMMANDS, and the version and log file that the CLI asks for
18
+ #
19
+ # @api private
20
+ DAEMON_COMMANDS = (PROCESS_COMMANDS + [:version, :log_file]).freeze
21
+
16
22
  attr_accessor :name, :logger, :base_dir, :socket, :pid_file, :kill_timeout, :groups, :work_queue, :pids_dir, :log_file
17
23
 
18
24
  # @!attribute [rw] name
@@ -104,9 +110,10 @@ module Bluepill
104
110
  self.log_file = options[:log_file]
105
111
  # Only used for a non-root user, who has a home
106
112
  home = ENV['HOME'] #: String
107
- self.base_dir = ProcessJournal.base_dir = options[:base_dir] ||
108
- ENV['BLUEPILL_BASE_DIR'] ||
109
- (::Process.euid.zero? ? '/var/run/bluepill' : File.join(home, '.bluepill'))
113
+ self.base_dir = options[:base_dir] ||
114
+ ENV['BLUEPILL_BASE_DIR'] ||
115
+ (::Process.euid.zero? ? '/var/run/bluepill' : File.join(home, '.bluepill'))
116
+ ProcessJournal.setup(base_dir, name)
110
117
  self.pid_file = File.join(base_dir, 'pids', "#{name}.pid")
111
118
  self.pids_dir = File.join(base_dir, 'pids', name)
112
119
  self.kill_timeout = options[:kill_timeout] || 10
@@ -297,8 +304,8 @@ module Bluepill
297
304
  # The target is group:process and the process name may contain ':'
298
305
  command, *args = client.readline.strip.split(':', 3).reject(&:empty?)
299
306
  response = begin
300
- # An empty line leaves command nil, so public_send raises an error for the client
301
- mutex { public_send(command, *args) } # steep:ignore ArgumentTypeMismatch
307
+ method = daemon_command(command)
308
+ mutex { public_send(method, *args) }
302
309
  rescue StandardError => e
303
310
  e
304
311
  end
@@ -318,18 +325,38 @@ module Bluepill
318
325
  end
319
326
  end
320
327
 
328
+ # Return the one of the DAEMON_COMMANDS that a client's command names
329
+ #
330
+ # Anyone who can write to the socket can send any line, so only these
331
+ # commands may run, and not any other of the daemon's public methods.
332
+ #
333
+ # @param [String, nil] command the command, or nil for an empty line
334
+ #
335
+ # @raise [NoMethodError] if it names none of them
336
+ #
337
+ # @return [Symbol]
338
+ #
339
+ # @api private
340
+ def daemon_command(command)
341
+ # The backtrace is caller's lines, with no locations, so Ruby 3.1's
342
+ # error_highlight doesn't add a snippet of this file to the message
343
+ DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
344
+ raise(NoMethodError, "Unknown command `#{command}`", caller)
345
+ end
346
+
321
347
  # Start the daemon
322
348
  #
323
- # It stops a previous daemon for the application, kills what the
324
- # journals recorded, daemonizes unless it runs in the foreground, opens
325
- # the socket, and runs until a signal stops it.
349
+ # It stops a previous daemon for the application, clears journal locks
350
+ # it left behind, daemonizes unless it runs in the foreground, opens the
351
+ # socket, and runs until a signal stops it. Processes that are already
352
+ # running, from their pid files, are monitored again, not restarted, so
353
+ # bluepill can be upgraded without restarting them.
326
354
  #
327
355
  # @return [void]
328
356
  #
329
357
  # @api private
330
358
  def start_server
331
359
  kill_previous_bluepill
332
- ProcessJournal.kill_all_from_all_journals
333
360
  ProcessJournal.clear_all_atomic_fs_locks
334
361
 
335
362
  begin
@@ -337,7 +364,11 @@ module Bluepill
337
364
  rescue Errno::EPERM
338
365
  end
339
366
 
340
- Daemonize.daemonize unless foreground?
367
+ unless foreground?
368
+ Daemonize.daemonize
369
+ # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
370
+ setup_signal_traps
371
+ end
341
372
 
342
373
  logger.reopen
343
374
 
@@ -368,13 +399,14 @@ module Bluepill
368
399
  end
369
400
  end
370
401
 
371
- # Kill what the journals recorded and remove the socket and pid file
402
+ # Clear the app's journal locks, and remove the socket and pid file
403
+ #
404
+ # The processes keep running, so the next daemon monitors them again.
372
405
  #
373
406
  # @return [void]
374
407
  #
375
408
  # @api private
376
409
  def cleanup
377
- ProcessJournal.kill_all_from_all_journals
378
410
  ProcessJournal.clear_all_atomic_fs_locks
379
411
  begin
380
412
  System.delete_if_exists(socket.path) if socket
@@ -150,7 +150,9 @@ module Bluepill
150
150
  #
151
151
  # @api private
152
152
  def require_syslog
153
- require 'syslog'
153
+ # A HUP reopens the log from a signal trap, where require raises
154
+ # ThreadError, and syslog is loaded by then
155
+ require 'syslog' unless defined?(::Syslog)
154
156
  rescue LoadError
155
157
  message = 'Bluepill logs to syslog unless log_file is set, but the syslog library ' \
156
158
  "could not be loaded. Since Ruby 3.4 it's a separate gem, so add gem 'syslog' " \
@@ -660,7 +660,10 @@ module Bluepill
660
660
  stop_process
661
661
  dispatch!(:unmonitor, 'user initiated')
662
662
  when 'restart'
663
- restart_process
663
+ # Through the state machine, like a check's restart, so the process
664
+ # shows as restarting until it's up again. A state restart can't
665
+ # leave, such as unmonitored, restarts it directly.
666
+ restart_process unless dispatch!(:restart, 'user initiated')
664
667
  when 'unmonitor'
665
668
  # When the user issues an unmonitor cmd, reset any triggers so that
666
669
  # scheduled events gets cleared
@@ -120,7 +120,8 @@ module Bluepill
120
120
  #
121
121
  # Bluepill only writes the pid file for processes it daemonizes, but it
122
122
  # removes it for every process, so a stopped process never leaves a stale
123
- # pid behind for a later process to reuse.
123
+ # pid behind for a later process to reuse. It forgets the pid it cached
124
+ # too, so a restart that stops and starts the process reads the new one.
124
125
  #
125
126
  # @return [void]
126
127
  #
@@ -128,6 +129,7 @@ module Bluepill
128
129
  def cleanup_process
129
130
  ProcessJournal.kill_all_from_journal(name) # finish cleanup
130
131
  unlink_pid
132
+ clear_pid
131
133
  end
132
134
 
133
135
  # Return a command with <tt>{{PID}}</tt> replaced by the process's pid
@@ -4,8 +4,22 @@ module Bluepill
4
4
  # Remember the pids and process groups of the processes bluepill starts
5
5
  #
6
6
  # Each process has a pid journal and a process group journal, files in the
7
- # journal directory with one id a line. When bluepill starts again, it
8
- # kills whatever an earlier run left in them.
7
+ # app's journal directory with one id a line. Before bluepill starts a
8
+ # process, it kills whatever an earlier run left in that process's
9
+ # journals.
10
+ #
11
+ # A pid can belong to another process by then: after a reboot, or once
12
+ # pids wrap around. So each line also has when the process started, and
13
+ # bluepill only kills a pid that still belongs to a process that started
14
+ # then. A process group whose leader has exited is only killed if it was
15
+ # journaled since the machine booted. Lines without a start time, from
16
+ # bluepill 0.1 and earlier, are only trusted if the journal was written
17
+ # since the machine booted.
18
+ #
19
+ # Each app has a journal directory of its own, so two apps with a process
20
+ # of the same name don't share a journal. Bluepill 0.1 and earlier kept
21
+ # every app's journals in one directory, and those are still read and
22
+ # cleared, so upgrading doesn't leave their processes behind.
9
23
  #
10
24
  # @api private
11
25
  module ProcessJournal
@@ -17,13 +31,20 @@ module Bluepill
17
31
  # @api private
18
32
  attr_reader :logger
19
33
 
20
- # Return the directory the journals are in
34
+ # Return the directory the app's journals are in
21
35
  #
22
- # @return [String] the journals directory under bluepill's base dir
36
+ # @return [String] the app's directory in the journals directory
23
37
  #
24
38
  # @api private
25
39
  attr_reader :journal_base_dir
26
40
 
41
+ # Return the directory every app's journals were in before 0.2
42
+ #
43
+ # @return [String] the journals directory under bluepill's base dir
44
+ #
45
+ # @api private
46
+ attr_reader :legacy_journal_dir
47
+
27
48
  # Set the logger the journal writes to, unless it already has one
28
49
  #
29
50
  # Only the first logger is kept.
@@ -37,20 +58,25 @@ module Bluepill
37
58
  @logger ||= new_logger
38
59
  end
39
60
 
40
- # Set the journal directory from bluepill's base dir, and create it
61
+ # Set the app's journal directory from bluepill's base dir, and create it
41
62
  #
42
- # Only the first base dir is kept. The directory is made world-writable
43
- # so processes that run as other users can lock their journals.
63
+ # Only the first base dir and app are kept. The directories are made
64
+ # world-writable so processes that run as other users can lock their
65
+ # journals.
44
66
  #
45
67
  # @param [String] base_dir bluepill's base directory
68
+ # @param [#to_s] app_name the app's name
46
69
  #
47
70
  # @return [void]
48
71
  #
49
72
  # @api private
50
- def base_dir=(base_dir)
51
- @journal_base_dir ||= File.join(base_dir, 'journals')
52
- FileUtils.mkdir_p(@journal_base_dir) unless File.exist?(@journal_base_dir)
53
- FileUtils.chmod(0o777, @journal_base_dir)
73
+ def setup(base_dir, app_name)
74
+ @legacy_journal_dir ||= File.join(base_dir, 'journals')
75
+ @journal_base_dir ||= File.join(@legacy_journal_dir, app_name.to_s)
76
+ [@legacy_journal_dir, @journal_base_dir].each do |dir|
77
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
78
+ FileUtils.chmod(0o777, dir)
79
+ end
54
80
  end
55
81
  end
56
82
 
@@ -117,37 +143,72 @@ module Bluepill
117
143
  end
118
144
  end
119
145
 
120
- # Remove the journal locks in the current directory
146
+ # Remove the app's journal locks, which a daemon that died left behind
147
+ #
148
+ # Without this, a stale lock would make the next write to its journal
149
+ # wait 10 seconds and fail.
121
150
  #
122
151
  # @return [void]
123
152
  #
124
153
  # @api private
125
154
  def self.clear_all_atomic_fs_locks
126
- Dir['.*.lock'].each do |f|
127
- System.delete_if_exists(f) if File.directory?(f)
155
+ Dir[File.join(journal_base_dir, '.*.lock')].each do |lock|
156
+ clear_atomic_fs_lock(lock)
128
157
  end
129
158
  end
130
159
 
131
160
  # Return the path of a process's pid journal
132
161
  #
133
162
  # @param [String] journal_name the process's name
163
+ # @param [String] dir the journal directory
134
164
  #
135
165
  # @return [String] the journal's path
136
166
  #
137
167
  # @api private
138
- def self.pid_journal_filename(journal_name)
139
- File.join(journal_base_dir, ".bluepill_pids_journal.#{journal_name}")
168
+ def self.pid_journal_filename(journal_name, dir = journal_base_dir)
169
+ File.join(dir, ".bluepill_pids_journal.#{journal_name}")
140
170
  end
141
171
 
142
172
  # Return the path of a process's process group journal
143
173
  #
144
174
  # @param [String] journal_name the process's name
175
+ # @param [String] dir the journal directory
145
176
  #
146
177
  # @return [String] the journal's path
147
178
  #
148
179
  # @api private
149
- def self.pgid_journal_filename(journal_name)
150
- File.join(journal_base_dir, ".bluepill_pgids_journal.#{journal_name}")
180
+ def self.pgid_journal_filename(journal_name, dir = journal_base_dir)
181
+ File.join(dir, ".bluepill_pgids_journal.#{journal_name}")
182
+ end
183
+
184
+ # Return the ids in a journal, each with when its process started
185
+ #
186
+ # @param [String] filename the journal's path
187
+ #
188
+ # @return [Array<Array(Integer, Integer)>, Array<Array(Integer, nil)>]
189
+ # each id, with when its process started, or nil for a line from
190
+ # bluepill 0.1 and earlier, which has none
191
+ #
192
+ # @raise [Errno::ENOENT] if there's no journal
193
+ #
194
+ # @api private
195
+ def self.journal_entries(filename)
196
+ File.readlines(filename).collect do |line|
197
+ id, started = line.split
198
+ [id.to_i, started && started.to_i]
199
+ end
200
+ end
201
+
202
+ # Return a journal line for an id: the id, and when its process started
203
+ #
204
+ # @param [Integer] id a pid, or a process group id, whose leader's start
205
+ # is recorded
206
+ #
207
+ # @return [String] the line, without a start time if ps can't tell
208
+ #
209
+ # @api private
210
+ def self.journal_line(id)
211
+ [id, System.started_at(id)].compact.join(' ')
151
212
  end
152
213
 
153
214
  # Return the valid pids in a pid journal
@@ -159,7 +220,7 @@ module Bluepill
159
220
  # @api private
160
221
  def self.pid_journal(filename)
161
222
  logger.debug("pid journal file: #{filename}")
162
- result = File.readlines(filename).collect(&:to_i).reject { |pid| skip_pid?(pid) }
223
+ result = journal_entries(filename).collect(&:first).reject { |pid| skip_pid?(pid) }
163
224
  logger.debug("pid journal = #{result.join(' ')}")
164
225
  result
165
226
  rescue Errno::ENOENT
@@ -176,7 +237,7 @@ module Bluepill
176
237
  # @api private
177
238
  def self.pgid_journal(filename)
178
239
  logger.debug("pgid journal file: #{filename}")
179
- result = File.readlines(filename).collect(&:to_i).reject { |pgid| skip_pgid?(pgid) }
240
+ result = journal_entries(filename).collect(&:first).reject { |pgid| skip_pgid?(pgid) }
180
241
  logger.debug("pgid journal = #{result.join(' ')}")
181
242
  result
182
243
  rescue Errno::ENOENT
@@ -197,19 +258,6 @@ module Bluepill
197
258
  logger.debug("Cleared lock #{name}")
198
259
  end
199
260
 
200
- # Kill what every journal in the current directory lists
201
- #
202
- # @return [void]
203
- #
204
- # @api private
205
- def self.kill_all_from_all_journals
206
- pids = Dir['.bluepill_pids_journal.*'].collect { |p| p.sub(/\A\.bluepill_pids_journal\./, '') }
207
- pids.reject! { |p| p.end_with?('.lock') }
208
- pids.each do |journal_name|
209
- kill_all_from_journal(journal_name)
210
- end
211
- end
212
-
213
261
  # Kill the processes and process groups that a process's journals list
214
262
  #
215
263
  # @param [String] journal_name the process's name
@@ -222,7 +270,10 @@ module Bluepill
222
270
  kill_all_pgids_from_journal(journal_name)
223
271
  end
224
272
 
225
- # Kill the process groups that a process's journal lists
273
+ # Kill the process groups that a process's journals list
274
+ #
275
+ # That's the app's journal, and the one that bluepill 0.1 and earlier
276
+ # shared between apps.
226
277
  #
227
278
  # @param [String] journal_name the process's name
228
279
  #
@@ -230,11 +281,16 @@ module Bluepill
230
281
  #
231
282
  # @api private
232
283
  def self.kill_all_pgids_from_journal(journal_name)
233
- filename = pgid_journal_filename(journal_name)
234
- kill_all_from_journal_file(filename, pgid_journal(filename), 'process group', -1)
284
+ journal_dirs.each do |dir|
285
+ filename = pgid_journal_filename(journal_name, dir)
286
+ kill_all_from_journal_file(filename, pgid_journal(filename), 'process group', -1)
287
+ end
235
288
  end
236
289
 
237
- # Kill the processes that a process's journal lists
290
+ # Kill the processes that a process's journals list
291
+ #
292
+ # That's the app's journal, and the one that bluepill 0.1 and earlier
293
+ # shared between apps.
238
294
  #
239
295
  # @param [String] journal_name the process's name
240
296
  #
@@ -242,14 +298,27 @@ module Bluepill
242
298
  #
243
299
  # @api private
244
300
  def self.kill_all_pids_from_journal(journal_name)
245
- filename = pid_journal_filename(journal_name)
246
- kill_all_from_journal_file(filename, pid_journal(filename), 'process', 1)
301
+ journal_dirs.each do |dir|
302
+ filename = pid_journal_filename(journal_name, dir)
303
+ kill_all_from_journal_file(filename, pid_journal(filename), 'process', 1)
304
+ end
305
+ end
306
+
307
+ # Return the directories a process's journals can be in
308
+ #
309
+ # @return [Array<String>] the app's, and then the one before 0.2
310
+ #
311
+ # @api private
312
+ def self.journal_dirs
313
+ [journal_base_dir, legacy_journal_dir]
247
314
  end
248
315
 
249
316
  # Kill the ids a journal lists, and then delete the journal
250
317
  #
251
- # Sends TERM, waits a second for any survivors, then sends KILL to those
252
- # still alive. A negative sign targets process groups instead of pids.
318
+ # Only kills the ids that still belong to what bluepill journaled (see
319
+ # journaled_ids). Sends TERM, waits a second for any survivors, then
320
+ # sends KILL to those still alive. A negative sign targets process groups
321
+ # instead of pids.
253
322
  #
254
323
  # @param [String] filename the journal's path
255
324
  # @param [Array<Integer>] ids the pids or process group ids in it
@@ -266,6 +335,7 @@ module Bluepill
266
335
  end
267
336
 
268
337
  acquire_atomic_fs_lock(filename) do
338
+ ids = journaled_ids(filename, ids, label)
269
339
  signal_journaled('TERM', ids, label, sign)
270
340
  survivors = ids.select { |id| System.pid_alive?(sign * id) }
271
341
  unless survivors.empty?
@@ -278,6 +348,42 @@ module Bluepill
278
348
  end
279
349
  end
280
350
 
351
+ # Return the ids that still belong to what bluepill journaled
352
+ #
353
+ # An id whose process is running is kept only if that process started
354
+ # when the journal says, give or take 2 seconds, since ps's elapsed time
355
+ # is only accurate to a second. One whose process has exited (a process
356
+ # group whose leader is gone) is kept only if it was journaled since the
357
+ # machine booted, and so is one without a start time.
358
+ #
359
+ # @param [String] filename the journal's path
360
+ # @param [Array<Integer>] ids the valid ids in it
361
+ # @param [String] label what the ids are, for the log
362
+ #
363
+ # @return [Array<Integer>] the ids that are safe to signal, or none if
364
+ # the journal was cleared since its ids were read
365
+ #
366
+ # @api private
367
+ def self.journaled_ids(filename, ids, label)
368
+ # Journals only grow until they're cleared, under the lock this runs in
369
+ return [] unless File.exist?(filename)
370
+
371
+ started = Hash[journal_entries(filename)]
372
+ boot = System.boot_time
373
+ written = Integer(File.mtime(filename))
374
+ ids.select do |id|
375
+ now = System.started_at(id)
376
+ journaled = started.fetch(id)
377
+ ours = if now && journaled
378
+ (now - journaled).abs <= 2
379
+ else
380
+ !boot.nil? && (journaled || written) >= boot - 2
381
+ end
382
+ logger.info("Skipping old #{label} #{id}, which isn't the one bluepill started") unless ours
383
+ ours
384
+ end
385
+ end
386
+
281
387
  # Send a signal to each journaled id, and log what happened
282
388
  #
283
389
  # @param [String] signal TERM or KILL
@@ -313,6 +419,8 @@ module Bluepill
313
419
  logger.debug("Skipping invalid pgid #{pgid} (class #{pgid.class})")
314
420
  return
315
421
  end
422
+ # skip_pgid? made sure it's an Integer
423
+ valid_pgid = pgid #: Integer
316
424
 
317
425
  filename = pgid_journal_filename(journal_name)
318
426
  acquire_atomic_fs_lock(filename) do
@@ -320,7 +428,7 @@ module Bluepill
320
428
  logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}")
321
429
  else
322
430
  logger.debug("Saving pgid #{pgid} to process journal #{journal_name}")
323
- File.open(filename, 'a+', 0o600) { |f| f.puts(pgid) }
431
+ File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pgid)) }
324
432
  logger.info("Saved pgid #{pgid} to journal #{journal_name}")
325
433
  logger.debug("Journal now = #{File.read(filename)}")
326
434
  end
@@ -353,7 +461,7 @@ module Bluepill
353
461
  logger.debug("Skipping duplicate pid #{pid} already in journal #{journal_name}")
354
462
  else
355
463
  logger.debug("Saving pid #{pid} to process journal #{journal_name}")
356
- File.open(filename, 'a+', 0o600) { |f| f.puts(pid) }
464
+ File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pid)) }
357
465
  logger.info("Saved pid #{pid} to journal #{journal_name}")
358
466
  logger.debug("Journal now = #{File.read(filename)}")
359
467
  end
@@ -46,6 +46,37 @@ module Bluepill
46
46
  false # no such process (ESRCH), or no pid
47
47
  end
48
48
 
49
+ # Return when a process started, in seconds since the epoch
50
+ #
51
+ # It's read from ps's elapsed time for the process, so it's accurate to
52
+ # about a second, and works wherever ps does.
53
+ #
54
+ # @param [Integer] pid the process's pid
55
+ #
56
+ # @return [Integer, nil] when it started, or nil if there's no such
57
+ # process
58
+ #
59
+ # @api private
60
+ def self.started_at(pid)
61
+ # 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
63
+ Integer(Time.now) - parse_elapsed_time(elapsed) unless elapsed.empty?
64
+ rescue SystemCallError
65
+ nil
66
+ end
67
+
68
+ # Return when the machine booted, in seconds since the epoch
69
+ #
70
+ # That's when its first process, pid 1, started. In a container, it's
71
+ # when the container started, which is when its pids start over.
72
+ #
73
+ # @return [Integer, nil] when it booted, or nil if ps can't tell
74
+ #
75
+ # @api private
76
+ def self.boot_time
77
+ started_at(1)
78
+ end
79
+
49
80
  # Return a process's CPU usage, from this tick's ps table
50
81
  #
51
82
  # @param [Integer, nil] pid the process's pid
@@ -2,7 +2,7 @@ module Bluepill
2
2
  # The version of bluepill
3
3
  #
4
4
  # @example
5
- # Bluepill::Version.to_s # => "0.1.4"
5
+ # Bluepill::Version.to_s # => "0.1.6"
6
6
  #
7
7
  # @api public
8
8
  module Version
@@ -33,13 +33,13 @@ module Bluepill
33
33
  # Return the patch version
34
34
  #
35
35
  # @example
36
- # Bluepill::Version.patch # => 4
36
+ # Bluepill::Version.patch # => 6
37
37
  #
38
38
  # @return [Integer] the patch version
39
39
  #
40
40
  # @api public
41
41
  def self.patch
42
- 4
42
+ 6
43
43
  end
44
44
 
45
45
  # Return the pre-release version
@@ -55,7 +55,7 @@ module Bluepill
55
55
  # Return the version's parts by name
56
56
  #
57
57
  # @example
58
- # Bluepill::Version.to_h # => {major: 0, minor: 1, patch: 4, pre: nil}
58
+ # Bluepill::Version.to_h # => {major: 0, minor: 1, patch: 6, pre: nil}
59
59
  #
60
60
  # @return [Hash{Symbol => Integer, nil}] the major, minor, patch and
61
61
  # pre-release versions
@@ -73,7 +73,7 @@ module Bluepill
73
73
  # Return the version's parts, without a missing pre-release version
74
74
  #
75
75
  # @example
76
- # Bluepill::Version.to_a # => [0, 1, 4]
76
+ # Bluepill::Version.to_a # => [0, 1, 6]
77
77
  #
78
78
  # @return [Array<Integer>] the parts of the version
79
79
  #
@@ -85,9 +85,9 @@ module Bluepill
85
85
  # Return the version as a string
86
86
  #
87
87
  # @example
88
- # Bluepill::Version.to_s # => "0.1.4"
88
+ # Bluepill::Version.to_s # => "0.1.6"
89
89
  #
90
- # @return [String] the version, such as "0.1.4"
90
+ # @return [String] the version, such as "0.1.6"
91
91
  #
92
92
  # @api public
93
93
  def self.to_s
data/sig/bluepill.rbs CHANGED
@@ -41,6 +41,8 @@ module Bluepill
41
41
  class Application
42
42
  PROCESS_COMMANDS: Array[Symbol]
43
43
 
44
+ DAEMON_COMMANDS: Array[Symbol]
45
+
44
46
  attr_accessor name: String
45
47
  attr_accessor logger: Logger
46
48
  attr_accessor base_dir: String
@@ -90,6 +92,8 @@ module Bluepill
90
92
 
91
93
  def start_listener: () -> Thread
92
94
 
95
+ def daemon_command: (String? command) -> Symbol
96
+
93
97
  def start_server: () -> void
94
98
 
95
99
  def run: () -> void
@@ -182,6 +186,7 @@ module Bluepill
182
186
  def clean_threads: () -> void
183
187
  def signal_process: (Integer | String | Symbol code) -> bool
184
188
  def unlink_pid: () -> void
189
+ def clear_pid: () -> void
185
190
  def skip_ticks_for: (seconds seconds) -> Integer
186
191
  def dispatch!: (Symbol | String event, ?String? reason) -> bool
187
192
  end
@@ -529,7 +534,7 @@ module Bluepill
529
534
 
530
535
  def create_logger: () -> (Object & _Backend)
531
536
 
532
- def require_syslog: () -> bool
537
+ def require_syslog: () -> void
533
538
  end
534
539
 
535
540
  # A value a ConditionWatch got, and whether it passed the check
@@ -731,6 +736,10 @@ module Bluepill
731
736
 
732
737
  def self.pid_alive?: (Integer pid) -> bool
733
738
 
739
+ def self.started_at: (Integer pid) -> Integer?
740
+
741
+ def self.boot_time: () -> Integer?
742
+
734
743
  def self.cpu_usage: (Integer? pid, boolish include_children) -> Float?
735
744
 
736
745
  def self.memory_usage: (Integer? pid, boolish include_children) -> Float?
@@ -774,15 +783,18 @@ module Bluepill
774
783
  module ProcessJournal
775
784
  self.@logger: Logger
776
785
  self.@journal_base_dir: String
786
+ self.@legacy_journal_dir: String
777
787
 
778
788
  def self.logger: () -> Logger
779
789
 
780
790
  def self.journal_base_dir: () -> String
781
791
 
792
+ def self.legacy_journal_dir: () -> String
793
+
782
794
  # Only the first logger is kept
783
795
  def self.logger=: (Logger new_logger) -> Logger
784
796
 
785
- def self.base_dir=: (String base_dir) -> void
797
+ def self.setup: (String base_dir, _ToS app_name) -> void
786
798
 
787
799
  def self.skip_pid?: (Integer? pid) -> bool
788
800
 
@@ -792,9 +804,14 @@ module Bluepill
792
804
 
793
805
  def self.clear_all_atomic_fs_locks: () -> void
794
806
 
795
- def self.pid_journal_filename: (String journal_name) -> String
807
+ def self.pid_journal_filename: (String journal_name, ?String dir) -> String
796
808
 
797
- def self.pgid_journal_filename: (String journal_name) -> String
809
+ def self.pgid_journal_filename: (String journal_name, ?String dir) -> String
810
+
811
+ # Each id, with when its process started (nil before 0.2)
812
+ def self.journal_entries: (String filename) -> Array[[Integer, Integer?]]
813
+
814
+ def self.journal_line: (Integer id) -> String
798
815
 
799
816
  def self.pid_journal: (String filename) -> Array[Integer]
800
817
 
@@ -802,16 +819,18 @@ module Bluepill
802
819
 
803
820
  def self.clear_atomic_fs_lock: (String name) -> void
804
821
 
805
- def self.kill_all_from_all_journals: () -> void
806
-
807
822
  def self.kill_all_from_journal: (String journal_name) -> void
808
823
 
809
824
  def self.kill_all_pgids_from_journal: (String journal_name) -> void
810
825
 
811
826
  def self.kill_all_pids_from_journal: (String journal_name) -> void
812
827
 
828
+ def self.journal_dirs: () -> Array[String]
829
+
813
830
  def self.kill_all_from_journal_file: (String filename, Array[Integer] ids, String label, Integer sign) -> void
814
831
 
832
+ def self.journaled_ids: (String filename, Array[Integer] ids, String label) -> Array[Integer]
833
+
815
834
  def self.signal_journaled: (String signal, Array[Integer] ids, String label, Integer sign) -> void
816
835
 
817
836
  def self.append_pgid_to_journal: (String journal_name, Integer? pgid) -> void
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bluepill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arya Asemanfar