dante 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +68 -0
- data/README.md +116 -4
- data/lib/dante/runner.rb +132 -60
- data/lib/dante/version.rb +1 -1
- metadata +7 -13
- data/.github/workflows/ci.yml +0 -30
- data/.github/workflows/release.yml +0 -210
- data/.gitignore +0 -4
- data/Gemfile +0 -4
- data/Rakefile +0 -23
- data/dante.gemspec +0 -23
- data/test/dante_test.rb +0 -16
- data/test/runner_test.rb +0 -790
- data/test/test_helper.rb +0 -197
- data/test/user_switch_test.rb +0 -194
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 002c335950d80d25888ddea19c51b32dab911ed619240009580a6a8730ad42df
|
|
4
|
+
data.tar.gz: 6ad1ac5e79050b951ff285b4b634cf37be3a210926a4bfab9d21870b11b7acd8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: de8097d1197747d7169d4b11cc54e6f3f800acb5e4f38cdf73194eadfa46c127c3ddc5ab0ef634ebc18b28d65190b3fdfc8399f27874ad3c7fdba80f99a48a0a
|
|
7
|
+
data.tar.gz: bc9254daba971b9ccb0e0dcc99fb7b13a0366bbc722ed66e74e4016c4fe68711a30584787221de01578219f999a77beb79c4c1c45ae63bf88d5edb80738ff464
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,73 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.1 (2026-12-02)
|
|
4
|
+
|
|
5
|
+
Changes since 0.4.0:
|
|
6
|
+
|
|
7
|
+
- Fixed: when daemonizing with a pid file in a directory the daemon cannot write and no pid file exists there yet,
|
|
8
|
+
the error reported is the original `Errno::EACCES` rather than `Errno::ENOENT` from the in-place fallback. (#37,
|
|
9
|
+
@nesquena)
|
|
10
|
+
- Packaging: the gem now ships only `lib/`, `LICENSE`, `README.md` and `CHANGELOG.md` instead of every file in
|
|
11
|
+
`git ls-files`, so test files, CI workflows and repository metadata are no longer in the gem. (#38,
|
|
12
|
+
@nesquena)
|
|
13
|
+
|
|
14
|
+
## 0.4.0 (2026-11-01)
|
|
15
|
+
|
|
16
|
+
Changes since 0.3.0:
|
|
17
|
+
|
|
18
|
+
- Fixed: the daemon writes its pid file to a new file in the same directory and renames it into place, so a
|
|
19
|
+
symlink or another file already at the pid file's path is replaced instead of written through, and a reader
|
|
20
|
+
never sees a half-written pid file. The pid file now always belongs to the daemon's user, with mode 0666 less
|
|
21
|
+
the umask; an existing pid file used to keep its owner and mode. In a directory the daemon's user cannot write
|
|
22
|
+
(a pid file an init script created and handed to that user, say), it writes the existing pid file in place as
|
|
23
|
+
before, except through a symlink. **What to do:** if another user's tool reads the pid file (a monitor such as
|
|
24
|
+
god), start the daemon with a umask that leaves it readable (022 gives 0644). (#31, @nesquena)
|
|
25
|
+
- Fixed: stopping (`-k`, `stop`) and `daemon_running?` ignore a pid file that another user could have written to
|
|
26
|
+
have the process it names stopped: a symlink, a file with other hard links, or a file owned by neither root, nor
|
|
27
|
+
the user stopping, nor any of the uids the process runs as. A stop then keeps the pid file and returns false,
|
|
28
|
+
saying "No <name> processes are running" for a symlink and why for the others (for one, "pid file X is owned by
|
|
29
|
+
uid A but process P belongs to uid B; not stopping it"), and a start goes ahead and replaces the pid file. The owner is checked through `/proc` (Linux); elsewhere only symlinks and
|
|
30
|
+
hard links are. **What to do:** nothing for pid files the daemon writes itself, including a daemon started with
|
|
31
|
+
`sudo ... -u nobody` and stopped with `sudo ... -k`; a pid file written by another tool must be a plain file
|
|
32
|
+
belonging to root or to the daemon's user. (#31, @nesquena)
|
|
33
|
+
- Fixed: the log file is no longer given mode 0644 at every start. An existing log file keeps its mode (through a
|
|
34
|
+
symlinked log path, so does the file it points to), and a new one is created with mode 0644 less the umask:
|
|
35
|
+
0644 with the usual umask of 022, 0640 with 027. **What to do:** for a log file other users can read under a
|
|
36
|
+
stricter umask, create it with the mode you want before starting, or set the umask. (#31, @nesquena)
|
|
37
|
+
- New `:argv` option: `Dante::Runner.new(name, :argv => [...])`, `Dante.run(name, :argv => [...])` or
|
|
38
|
+
`execute(:argv => [...])` parses the options in that array instead of `ARGV`, so dante can run inside a program
|
|
39
|
+
that has options of its own (a test runner, for one) without failing on them; `:argv => []` parses nothing. The
|
|
40
|
+
array is not changed. Without `:argv`, `ARGV` is parsed as before. Reported by @neomantic-zz in #4.
|
|
41
|
+
(#33, @nesquena)
|
|
42
|
+
- Fixed: a daemon's standard input is now `/dev/null`. It stayed on the terminal or pipe it was started from, so a
|
|
43
|
+
daemon reading stdin read the user's keystrokes, and a terminal or pty that started it never saw end-of-file
|
|
44
|
+
while the daemon ran. (#30, @nesquena)
|
|
45
|
+
- Fixed: stopping (`-k`, `stop`) reads the pid file once, then signals and waits for that daemon, and removes the
|
|
46
|
+
pid file only if it still names it. Two stops at once no longer fail with `Errno::ENOENT`; a new daemon that
|
|
47
|
+
records its pid while an old one stops keeps its pid file, and `--force` no longer sends it SIGKILL. A pid file
|
|
48
|
+
path containing `[`, `*` or `?` can now be stopped. The `-k` help text now says what it does: it stops the daemon
|
|
49
|
+
in the pid file, and ignores its optional argument. (#30, @nesquena)
|
|
50
|
+
- Fixed: stopping checks whether the daemon has exited every 0.1 seconds instead of every second, so `-k` returns
|
|
51
|
+
as soon as the daemon has stopped rather than after a second at least. It still waits up to 5 seconds (and 5
|
|
52
|
+
more with `--force`). (#30, @nesquena)
|
|
53
|
+
- Fixed: `:kill => false` and `:restart => false` passed to `execute` are now off. Before, giving either key at
|
|
54
|
+
all stopped the daemon (`:kill`) or stopped and restarted it (`:restart`), whatever its value. `:kill => nil`, as
|
|
55
|
+
`-k` sets it, still stops. (#30, @nesquena)
|
|
56
|
+
- Fixed: `:daemonize => false` with a `:log_path` now writes the output to the log file, as leaving `:daemonize`
|
|
57
|
+
out does; before, the log path was ignored. (#30, @nesquena)
|
|
58
|
+
- Fixed: `Runner#restart` starts the service as a daemon when `:daemonize` is set, as `execute` does; it used to
|
|
59
|
+
run it in the foreground, in the calling process. **What to do:** to keep running the service in the calling
|
|
60
|
+
process, call `restart` on a runner without `:daemonize`. (#30, @nesquena)
|
|
61
|
+
- For subclasses of `Dante::Runner`: `stop` now reads the pid file and checks the process itself instead of calling
|
|
62
|
+
`daemon_running?` and `daemon_stopped?`, and the protected `kill_pid` and `wait_for_stop` now take the pid first
|
|
63
|
+
(`kill_pid(pid, k, sig)`, `wait_for_stop(pid, signalled)`). (#30, @nesquena)
|
|
64
|
+
- Documentation: the README's `-u nobody -G nogroup` example now gives pid and log paths that user can write (with
|
|
65
|
+
the defaults, the daemon failed to start), says what `-k` and `-f`/`--force` do, lists all the options, and adds
|
|
66
|
+
the requirements (MRI on Unix, for `fork`), running under systemd, and log rotation with `copytruncate` (SIGHUP
|
|
67
|
+
stops the daemon). (#30, @nesquena)
|
|
68
|
+
- The gemspec links the source code, this changelog and the issue tracker (shown on rubygems.org), and sets
|
|
69
|
+
`rubygems_mfa_required`, so pushes of the gem to rubygems.org now require MFA. (#30, @nesquena)
|
|
70
|
+
|
|
3
71
|
## 0.3.0 (2026-10-31)
|
|
4
72
|
|
|
5
73
|
Changes since 0.2.0:
|
data/README.md
CHANGED
|
@@ -10,6 +10,11 @@ can be started via command line or start/stop a daemon, and will store a pid fil
|
|
|
10
10
|
If you need to create a ruby executable and you want standard daemon start/stop with pid files
|
|
11
11
|
and no hassle, this gem will be a great way to get started.
|
|
12
12
|
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
Dante daemonizes with `fork`, so it needs MRI (CRuby) 2.7 or newer on Unix: Linux, macOS or a BSD. It does not
|
|
16
|
+
run on Windows or JRuby, which have no `fork`.
|
|
17
|
+
|
|
13
18
|
## Installation
|
|
14
19
|
|
|
15
20
|
Add to your Gemfile:
|
|
@@ -81,24 +86,54 @@ All stdout and stderr will be redirected to the specified logfile. If no logfile
|
|
|
81
86
|
stdout and stderr will be directed to /var/log/<myapp name>.log.
|
|
82
87
|
|
|
83
88
|
```
|
|
84
|
-
|
|
89
|
+
sudo install -d -o nobody -g nogroup /var/run/myapp /var/log/myapp
|
|
90
|
+
sudo ./bin/myapp -d -u nobody -G nogroup -P /var/run/myapp/myapp.pid -l /var/log/myapp/myapp.log
|
|
85
91
|
```
|
|
86
92
|
|
|
87
93
|
will switch to the user nobody before starting, with the group given by `-G`. When started as root, the group
|
|
88
94
|
defaults to the user's primary group, and the process takes on the user's supplementary groups (plus the `-G`
|
|
89
|
-
group, if given).
|
|
95
|
+
group, if given). The switch happens before the pid file and the log file are written, so both must be in
|
|
96
|
+
directories that user can write: the defaults, `/var/run/myapp.pid` and `/var/log/myapp.log`, are not, and the
|
|
97
|
+
daemon would fail to start. Keep the pid file and the log in directories that only the daemon's user (or root) can
|
|
98
|
+
write, never in one that other users can write, such as `/tmp`: another user could put a file or a symlink of
|
|
99
|
+
their own at that path. On most Linux systems `/var/run` is emptied at boot, so create its directory at each boot
|
|
100
|
+
(systemd's `RuntimeDirectory=`, below, or a `tmpfiles.d` entry) rather than once.
|
|
90
101
|
|
|
91
102
|
```
|
|
92
103
|
./bin/myapp -k -P /var/run/myapp.pid
|
|
93
104
|
```
|
|
94
105
|
|
|
95
|
-
will stop
|
|
106
|
+
will stop the daemon whose pid is in the specified pid file: it sends SIGTERM, waits up to 5 seconds for the
|
|
107
|
+
daemon to exit, and then removes the pid file. If the daemon is still running, it says "Failed to kill daemonized
|
|
108
|
+
process" and keeps the pid file.
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
./bin/myapp -k -f -P /var/run/myapp.pid
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
will do the same, and if the daemon survives SIGTERM, send it SIGKILL (`-f`/`--force`, or `:force => true`) and
|
|
115
|
+
wait up to 5 more seconds.
|
|
96
116
|
|
|
97
117
|
```
|
|
98
118
|
./bin/myapp --help
|
|
99
119
|
```
|
|
100
120
|
|
|
101
|
-
Will return a useful help banner message explaining the simple usage.
|
|
121
|
+
Will return a useful help banner message explaining the simple usage. These are all the built-in options:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
-p, --port PORT Specify port
|
|
125
|
+
(default: )
|
|
126
|
+
-P, --pid FILE save PID in FILE when using -d option.
|
|
127
|
+
(default: /var/run/myapp.pid)
|
|
128
|
+
-d, --daemon Daemonize mode
|
|
129
|
+
-l, --log FILE Logfile for output
|
|
130
|
+
(default: /var/log/myapp.log)
|
|
131
|
+
-k, --kill [PORT] Stop the daemon in the pid file (PORT is ignored)
|
|
132
|
+
-u, --user USER User to run as
|
|
133
|
+
-G, --group GROUP Group to run as
|
|
134
|
+
-f, --force Force kill if SIGTERM fails
|
|
135
|
+
-?, --help Display this usage information.
|
|
136
|
+
```
|
|
102
137
|
|
|
103
138
|
### Advanced
|
|
104
139
|
|
|
@@ -158,6 +193,83 @@ Dante::Runner.new('gitdocs').execute(:daemonize => true, :restart => true, :pid_
|
|
|
158
193
|
|
|
159
194
|
so you can use dante as part of a more complex CLI executable.
|
|
160
195
|
|
|
196
|
+
### Options from somewhere other than ARGV
|
|
197
|
+
|
|
198
|
+
`execute` parses the command-line options in `ARGV`. Inside a program that has options of its own (a test runner,
|
|
199
|
+
say, or a Rake task), pass dante the options to parse with `:argv` instead, to `Dante::Runner.new`, `Dante.run`
|
|
200
|
+
or `execute`:
|
|
201
|
+
|
|
202
|
+
```ruby
|
|
203
|
+
# parses only these, and leaves ARGV alone
|
|
204
|
+
Dante::Runner.new('gitdocs', :argv => ['-d', '-P', @pid]).execute { something! }
|
|
205
|
+
# parses nothing
|
|
206
|
+
Dante::Runner.new('gitdocs').execute(:argv => [], :daemonize => true, :pid_path => @pid) { something! }
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Without `:argv`, ARGV is parsed as before, and the options dante knows are removed from it. The array given as
|
|
210
|
+
`:argv` is never changed.
|
|
211
|
+
|
|
212
|
+
## systemd
|
|
213
|
+
|
|
214
|
+
Under systemd, let systemd run the process in the foreground (`Type=simple`), as the user it names, with the
|
|
215
|
+
output going to the journal:
|
|
216
|
+
|
|
217
|
+
```ini
|
|
218
|
+
# /etc/systemd/system/myapp.service
|
|
219
|
+
[Unit]
|
|
220
|
+
Description=myapp
|
|
221
|
+
After=network.target
|
|
222
|
+
|
|
223
|
+
[Service]
|
|
224
|
+
Type=simple
|
|
225
|
+
User=myapp
|
|
226
|
+
Group=myapp
|
|
227
|
+
ExecStart=/path/to/myapp/bin/myapp -p 8080
|
|
228
|
+
Restart=on-failure
|
|
229
|
+
|
|
230
|
+
[Install]
|
|
231
|
+
WantedBy=multi-user.target
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
systemd stops it with SIGTERM, which dante handles. To have dante daemonize instead, use `Type=forking` with the
|
|
235
|
+
pid file and the log in directories systemd creates for that user:
|
|
236
|
+
|
|
237
|
+
```ini
|
|
238
|
+
[Service]
|
|
239
|
+
Type=forking
|
|
240
|
+
User=myapp
|
|
241
|
+
Group=myapp
|
|
242
|
+
RuntimeDirectory=myapp
|
|
243
|
+
LogsDirectory=myapp
|
|
244
|
+
PIDFile=/run/myapp/myapp.pid
|
|
245
|
+
ExecStart=/path/to/myapp/bin/myapp -d -p 8080 -P /run/myapp/myapp.pid -l /var/log/myapp/myapp.log
|
|
246
|
+
Restart=on-failure
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Use `User=` and `Group=` rather than `-u` and `-G` under systemd, so that the process starts as that user and the
|
|
250
|
+
directories systemd creates belong to it.
|
|
251
|
+
|
|
252
|
+
## Log rotation
|
|
253
|
+
|
|
254
|
+
The daemon opens its log file once, for appending, and has no SIGHUP handler: SIGHUP stops it. So rotate the log
|
|
255
|
+
with logrotate's `copytruncate`, rather than a `postrotate` script that sends SIGHUP:
|
|
256
|
+
|
|
257
|
+
```
|
|
258
|
+
# /etc/logrotate.d/myapp
|
|
259
|
+
/var/log/myapp/myapp.log {
|
|
260
|
+
weekly
|
|
261
|
+
rotate 4
|
|
262
|
+
compress
|
|
263
|
+
delaycompress
|
|
264
|
+
missingok
|
|
265
|
+
notifempty
|
|
266
|
+
copytruncate
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
`copytruncate` copies the log and empties it in place, and the daemon carries on writing at the start of the
|
|
271
|
+
emptied file. Lines written between the copy and the truncation are lost.
|
|
272
|
+
|
|
161
273
|
## God
|
|
162
274
|
|
|
163
275
|
Dante can be used well in conjunction with the excellent God process manager. Simply, use Dante to daemonize a process
|
data/lib/dante/runner.rb
CHANGED
|
@@ -47,26 +47,29 @@ module Dante
|
|
|
47
47
|
@with_options = block
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
# Executes the runner based on options
|
|
50
|
+
# Executes the runner based on options, after parsing the command-line options in
|
|
51
|
+
# options[:argv] (given to new or execute), or else in ARGV
|
|
51
52
|
# @runner.execute
|
|
52
53
|
# @runner.execute { ... }
|
|
53
54
|
def execute(opts={}, &block)
|
|
55
|
+
options[:argv] = opts[:argv] if opts.key?(:argv) # needed before the rest of opts is merged
|
|
54
56
|
parse_options
|
|
55
57
|
self.options.merge!(opts)
|
|
56
58
|
|
|
57
59
|
@verify_options_hook.call(self.options) if @verify_options_hook
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
# A key given as false is off; -k with no argument sets :kill to nil, which is on.
|
|
62
|
+
if options.fetch(:kill, false) != false
|
|
60
63
|
self.stop
|
|
61
64
|
else # create process
|
|
62
|
-
self.stop if options.
|
|
65
|
+
self.stop if options.fetch(:restart, false) != false
|
|
63
66
|
|
|
64
67
|
# If a username, uid, groupname, or gid is passed,
|
|
65
68
|
# drop privileges accordingly.
|
|
66
69
|
switch_user(options[:user], options[:group]) if options[:user] || options[:group]
|
|
67
70
|
|
|
68
71
|
@startup_command = block if block_given?
|
|
69
|
-
|
|
72
|
+
start_service
|
|
70
73
|
end
|
|
71
74
|
end
|
|
72
75
|
|
|
@@ -89,6 +92,7 @@ module Dante
|
|
|
89
92
|
trap_signals
|
|
90
93
|
store_pid(Process.pid)
|
|
91
94
|
redirect_output!
|
|
95
|
+
reopen_stream(IO.for_fd(0, autoclose: false), File::NULL, 'r') # fd 0, whatever STDIN or $stdin is
|
|
92
96
|
start
|
|
93
97
|
end
|
|
94
98
|
started = daemon_survives?(daemon)
|
|
@@ -130,26 +134,31 @@ module Dante
|
|
|
130
134
|
def start
|
|
131
135
|
log "Starting #{@name} service..."
|
|
132
136
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
end
|
|
137
|
+
# A daemon has redirected its output already
|
|
138
|
+
redirect_output! if options[:log_path] && !options[:daemonize]
|
|
136
139
|
|
|
137
140
|
trap_signals
|
|
138
141
|
|
|
139
142
|
@startup_command.call(self.options) if @startup_command
|
|
140
143
|
end
|
|
141
144
|
|
|
142
|
-
# Stops a daemonized process
|
|
145
|
+
# Stops a daemonized process. The pid file is read once: the daemon it names then is
|
|
146
|
+
# the one signalled and waited for, whatever the pid file says by the time it stops.
|
|
143
147
|
def stop(kill_arg=nil)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
pid, pid_file_stat = read_pid_file(options[:pid_path])
|
|
149
|
+
running = pid && process_running?(pid)
|
|
150
|
+
if running && (untrusted = untrusted_pid_file(pid, pid_file_stat))
|
|
151
|
+
log "#{untrusted}; not stopping it"
|
|
152
|
+
false
|
|
153
|
+
elsif running
|
|
154
|
+
if wait_for_stop(pid, kill_pid(pid, kill_arg || options[:kill]))
|
|
155
|
+
remove_pid_file(pid) # Only once the daemon has stopped
|
|
147
156
|
log "Daemonized process killed after term."
|
|
148
157
|
else
|
|
149
158
|
log "Failed to kill daemonized process"
|
|
150
159
|
if options[:force]
|
|
151
|
-
if wait_for_stop(kill_pid(kill_arg || options[:kill], 'KILL'))
|
|
152
|
-
|
|
160
|
+
if wait_for_stop(pid, kill_pid(pid, kill_arg || options[:kill], 'KILL'))
|
|
161
|
+
remove_pid_file(pid) # Only once the daemon has stopped
|
|
153
162
|
log "Daemonized process killed after kill."
|
|
154
163
|
end
|
|
155
164
|
end
|
|
@@ -162,7 +171,7 @@ module Dante
|
|
|
162
171
|
|
|
163
172
|
def restart
|
|
164
173
|
self.stop
|
|
165
|
-
|
|
174
|
+
start_service
|
|
166
175
|
end
|
|
167
176
|
|
|
168
177
|
def interrupt
|
|
@@ -179,21 +188,22 @@ module Dante
|
|
|
179
188
|
! self.daemon_running?
|
|
180
189
|
end
|
|
181
190
|
|
|
182
|
-
# Returns running for the daemonized process
|
|
191
|
+
# Returns running for the daemonized process. A pid file that another user could have
|
|
192
|
+
# written (see untrusted_pid_file) names no daemon.
|
|
183
193
|
# self.daemon_running?
|
|
184
194
|
def daemon_running?
|
|
185
|
-
|
|
186
|
-
return false unless pid
|
|
187
|
-
|
|
188
|
-
true
|
|
189
|
-
rescue Errno::EPERM # the process exists but belongs to another user
|
|
190
|
-
true
|
|
191
|
-
rescue Errno::ESRCH
|
|
192
|
-
false
|
|
195
|
+
pid, pid_file_stat = read_pid_file(options[:pid_path])
|
|
196
|
+
return false unless pid
|
|
197
|
+
process_running?(pid) && !untrusted_pid_file(pid, pid_file_stat)
|
|
193
198
|
end
|
|
194
199
|
|
|
195
200
|
protected
|
|
196
201
|
|
|
202
|
+
# Starts the service: as a daemon with :daemonize, else in the foreground.
|
|
203
|
+
def start_service
|
|
204
|
+
options[:daemonize] ? daemonize : start
|
|
205
|
+
end
|
|
206
|
+
|
|
197
207
|
def parse_options
|
|
198
208
|
headline = [@name, @description].compact.join(" - ")
|
|
199
209
|
OptionParser.new do |opts|
|
|
@@ -219,7 +229,7 @@ module Dante
|
|
|
219
229
|
options[:log_path] = v
|
|
220
230
|
end
|
|
221
231
|
|
|
222
|
-
opts.on("-k", "--kill [PORT]", String, "
|
|
232
|
+
opts.on("-k", "--kill [PORT]", String, "Stop the daemon in the pid file (PORT is ignored)") do |v|
|
|
223
233
|
options[:kill] = v
|
|
224
234
|
end
|
|
225
235
|
|
|
@@ -242,7 +252,7 @@ module Dante
|
|
|
242
252
|
|
|
243
253
|
# Load options specified through 'with_options'
|
|
244
254
|
instance_exec(opts, &@with_options) if @with_options
|
|
245
|
-
end.parse!
|
|
255
|
+
end.parse!(options[:argv] ? options[:argv].dup : ARGV) # a copy of the caller's array
|
|
246
256
|
options
|
|
247
257
|
end
|
|
248
258
|
|
|
@@ -293,9 +303,34 @@ module Dante
|
|
|
293
303
|
}
|
|
294
304
|
end
|
|
295
305
|
|
|
306
|
+
# Writes pid to a new file in the pid file's directory and renames it into place, so
|
|
307
|
+
# that whatever was at the pid file's path (a symlink, or another file) is replaced
|
|
308
|
+
# rather than written into, and a reader sees either the old pid file or the whole new
|
|
309
|
+
# one. In a directory the daemon cannot write, it writes the pid file there already.
|
|
296
310
|
def store_pid(pid)
|
|
297
|
-
|
|
298
|
-
File.
|
|
311
|
+
pid_path = options[:pid_path]
|
|
312
|
+
FileUtils.mkdir_p(File.dirname(pid_path))
|
|
313
|
+
tmp_path = "#{pid_path}.#{pid}.tmp"
|
|
314
|
+
FileUtils.rm_f(tmp_path) # one left by a crash (a symlink is removed, not followed)
|
|
315
|
+
begin
|
|
316
|
+
# EXCL: never written through a file or symlink put there since
|
|
317
|
+
File.open(tmp_path, File::WRONLY | File::CREAT | File::EXCL) { |f| f.write("#{pid}\n") }
|
|
318
|
+
rescue Errno::EACCES => e
|
|
319
|
+
# A pid file made for the daemon's user in a directory it cannot write, as an init
|
|
320
|
+
# script might. Without CREAT, a missing one re-raises the original EACCES, as before.
|
|
321
|
+
# ceiling: not atomic, so a reader can see it half-written; giving the daemon's user
|
|
322
|
+
# the directory restores the rename.
|
|
323
|
+
tmp_path = nil
|
|
324
|
+
begin
|
|
325
|
+
File.open(pid_path, File::WRONLY | File::TRUNC | File::NOFOLLOW) { |f| f.write("#{pid}\n") }
|
|
326
|
+
rescue Errno::ENOENT
|
|
327
|
+
raise e
|
|
328
|
+
end
|
|
329
|
+
return
|
|
330
|
+
end
|
|
331
|
+
File.rename(tmp_path, pid_path)
|
|
332
|
+
ensure
|
|
333
|
+
FileUtils.rm_f(tmp_path) if tmp_path # left only when writing or renaming failed
|
|
299
334
|
end
|
|
300
335
|
|
|
301
336
|
# Whether daemon, a child of this process, is still running START_GRACE seconds after
|
|
@@ -320,38 +355,77 @@ module Dante
|
|
|
320
355
|
end
|
|
321
356
|
|
|
322
357
|
# Removes the pid file if it still holds pid (another start may have replaced it).
|
|
358
|
+
# ceiling: the read and the delete are two steps, so a daemon that writes its pid between them loses its
|
|
359
|
+
# pid file; a lock on the pid file (flock) would close that window
|
|
323
360
|
def remove_pid_file(pid)
|
|
324
361
|
File.delete(options[:pid_path]) if read_pid(options[:pid_path]) == pid
|
|
325
362
|
rescue Errno::ENOENT
|
|
326
363
|
end
|
|
327
364
|
|
|
328
|
-
# The pid in pid_file, or nil when
|
|
329
|
-
# between creating and writing it names no process (and pid 0 or a negative pid
|
|
330
|
-
# signal a whole process group), nor does a number too large for a pid_t.
|
|
365
|
+
# The pid in pid_file, or nil when there is none: no pid file, or one left empty by a
|
|
366
|
+
# crash between creating and writing it, names no process (and pid 0 or a negative pid
|
|
367
|
+
# would signal a whole process group), nor does a number too large for a pid_t.
|
|
331
368
|
def read_pid(pid_file)
|
|
332
|
-
|
|
333
|
-
pid if pid > 0 && pid < 2**31
|
|
369
|
+
read_pid_file(pid_file)&.first
|
|
334
370
|
end
|
|
335
371
|
|
|
336
|
-
#
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
372
|
+
# The pid in pid_file, as read_pid gives it, and the pid file's File::Stat, both from
|
|
373
|
+
# the one file opened; nil without a pid. A symlink is not followed, and names no pid
|
|
374
|
+
# (a start replaces it).
|
|
375
|
+
def read_pid_file(pid_file)
|
|
376
|
+
return unless File.exist?(pid_file)
|
|
377
|
+
File.open(pid_file, File::RDONLY | File::NOFOLLOW) do |file|
|
|
378
|
+
pid = file.read.to_i
|
|
379
|
+
[pid, file.stat] if pid > 0 && pid < 2**31
|
|
380
|
+
end
|
|
381
|
+
rescue Errno::ENOENT, Errno::ELOOP # removed since the check, or a symlink
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# Why the pid file, with File::Stat pid_file_stat, cannot be trusted to name pid, its
|
|
385
|
+
# daemon: it has other hard links (which keep the owner of the file they link to), or
|
|
386
|
+
# it is owned by neither root, nor a user that pid runs as (any of its uids), nor the
|
|
387
|
+
# user running this (whose own permissions are all a stop could use), so another user
|
|
388
|
+
# could have written it to have that process stopped. nil when it can be trusted, or
|
|
389
|
+
# when the owner of pid cannot be told (it has gone, say).
|
|
390
|
+
# ceiling: the owner of a process is read from /proc (Linux); elsewhere only the hard
|
|
391
|
+
# links are checked. `ps -o ruid=,uid= -p PID` would tell the owner on other Unixes.
|
|
392
|
+
def untrusted_pid_file(pid, pid_file_stat)
|
|
393
|
+
return "pid file #{options[:pid_path]} has other hard links" if pid_file_stat.nlink > 1
|
|
394
|
+
pid_file_uid = pid_file_stat.uid
|
|
395
|
+
return if pid_file_uid == 0 || pid_file_uid == Process.euid
|
|
396
|
+
uids = File.read("/proc/#{pid}/status")[/^Uid:(.*)$/, 1] or return # no Uid line
|
|
397
|
+
process_uids = uids.split.map { |uid| Integer(uid) } # real, effective, saved, filesystem
|
|
398
|
+
return if process_uids.include?(pid_file_uid)
|
|
399
|
+
"pid file #{options[:pid_path]} is owned by uid #{pid_file_uid} but process #{pid} belongs to uid #{process_uids.first}"
|
|
400
|
+
rescue SystemCallError # no /proc
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Whether pid is a running process, counting one that belongs to another user.
|
|
404
|
+
def process_running?(pid)
|
|
405
|
+
Process.kill 0, pid
|
|
406
|
+
true
|
|
407
|
+
rescue Errno::EPERM # the process exists but belongs to another user
|
|
408
|
+
true
|
|
409
|
+
rescue Errno::ESRCH
|
|
410
|
+
false
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
# Sends sig to pid, the daemon, and returns whether it reached it.
|
|
414
|
+
def kill_pid(pid, k, sig='TERM')
|
|
415
|
+
Process.kill(sig, pid)
|
|
416
|
+
log "Sent SIG#{sig} to PID: #{pid} at #{options[:pid_path]}"
|
|
417
|
+
true
|
|
418
|
+
rescue => e
|
|
419
|
+
log "Failed to stop! #{k}: #{e}"
|
|
420
|
+
false
|
|
349
421
|
end
|
|
350
422
|
|
|
351
|
-
# Waits for the daemon to stop once a signal reached it
|
|
352
|
-
#
|
|
353
|
-
|
|
354
|
-
|
|
423
|
+
# Waits for pid, the daemon, to stop once a signal reached it, checking every 0.1
|
|
424
|
+
# seconds (a Rational step, so the steps add up to exactly MAX_START_TRIES seconds).
|
|
425
|
+
# When no signal reached it (the daemon belongs to another user), it cannot stop, so
|
|
426
|
+
# this only checks once.
|
|
427
|
+
def wait_for_stop(pid, signalled)
|
|
428
|
+
signalled ? until_true(MAX_START_TRIES, Rational(1, 10)) { !process_running?(pid) } : !process_running?(pid)
|
|
355
429
|
end
|
|
356
430
|
|
|
357
431
|
# Redirect output based on log settings (reopens stdout/stderr to specified logfile)
|
|
@@ -360,12 +434,10 @@ module Dante
|
|
|
360
434
|
if log_path = options[:log_path]
|
|
361
435
|
# if the log directory doesn't exist, create it
|
|
362
436
|
FileUtils.mkdir_p File.dirname(options[:log_path]), :mode => 0755
|
|
363
|
-
#
|
|
364
|
-
|
|
365
|
-
#
|
|
366
|
-
|
|
367
|
-
# Reopen $stdout (NOT +STDOUT+) to start writing to the log file
|
|
368
|
-
reopen_stream($stdout, log_path, 'a')
|
|
437
|
+
# Reopen $stdout (NOT +STDOUT+) to append to the log file, created with mode 0644
|
|
438
|
+
# (less the umask) if absent. An existing log file keeps its mode; a symlink is
|
|
439
|
+
# followed.
|
|
440
|
+
reopen_stream($stdout, log_path, 'a', 0644)
|
|
369
441
|
# Redirect $stderr to $stdout
|
|
370
442
|
$stderr.reopen $stdout
|
|
371
443
|
$stdout.sync = true
|
|
@@ -379,11 +451,11 @@ module Dante
|
|
|
379
451
|
log_path = options[:log_path] ? options[:log_path] : '/dev/null'
|
|
380
452
|
end
|
|
381
453
|
|
|
382
|
-
# Reopens stream onto path, opened with mode
|
|
383
|
-
# than a path works whatever mode the stream itself was opened
|
|
384
|
-
# one, when run under Spring or Zeus).
|
|
385
|
-
def reopen_stream(stream, path, mode)
|
|
386
|
-
File.open(path, mode) { |file| stream.reopen(file) }
|
|
454
|
+
# Reopens stream onto path, opened with mode (and created with perm). Reopening onto
|
|
455
|
+
# an open File rather than a path works whatever mode the stream itself was opened
|
|
456
|
+
# with (a socket, for one, when run under Spring or Zeus).
|
|
457
|
+
def reopen_stream(stream, path, mode, perm=0666)
|
|
458
|
+
File.open(path, mode, perm) { |file| stream.reopen(file) }
|
|
387
459
|
end
|
|
388
460
|
|
|
389
461
|
# Runs until the block condition is met or the timeout_seconds is exceeded
|
data/lib/dante/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dante
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nathan Esquenazi
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -45,26 +45,20 @@ executables: []
|
|
|
45
45
|
extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
|
47
47
|
files:
|
|
48
|
-
- ".github/workflows/ci.yml"
|
|
49
|
-
- ".github/workflows/release.yml"
|
|
50
|
-
- ".gitignore"
|
|
51
48
|
- CHANGELOG.md
|
|
52
|
-
- Gemfile
|
|
53
49
|
- LICENSE
|
|
54
50
|
- README.md
|
|
55
|
-
- Rakefile
|
|
56
|
-
- dante.gemspec
|
|
57
51
|
- lib/dante.rb
|
|
58
52
|
- lib/dante/runner.rb
|
|
59
53
|
- lib/dante/version.rb
|
|
60
|
-
- test/dante_test.rb
|
|
61
|
-
- test/runner_test.rb
|
|
62
|
-
- test/test_helper.rb
|
|
63
|
-
- test/user_switch_test.rb
|
|
64
54
|
homepage: https://github.com/nesquena/dante
|
|
65
55
|
licenses:
|
|
66
56
|
- MIT
|
|
67
|
-
metadata:
|
|
57
|
+
metadata:
|
|
58
|
+
source_code_uri: https://github.com/nesquena/dante
|
|
59
|
+
changelog_uri: https://github.com/nesquena/dante/blob/master/CHANGELOG.md
|
|
60
|
+
bug_tracker_uri: https://github.com/nesquena/dante/issues
|
|
61
|
+
rubygems_mfa_required: 'true'
|
|
68
62
|
post_install_message:
|
|
69
63
|
rdoc_options: []
|
|
70
64
|
require_paths:
|
data/.github/workflows/ci.yml
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [ "master" ]
|
|
6
|
-
pull_request:
|
|
7
|
-
branches: [ "master" ]
|
|
8
|
-
|
|
9
|
-
permissions:
|
|
10
|
-
contents: read
|
|
11
|
-
|
|
12
|
-
jobs:
|
|
13
|
-
test:
|
|
14
|
-
runs-on: ubuntu-latest
|
|
15
|
-
strategy:
|
|
16
|
-
fail-fast: false
|
|
17
|
-
matrix:
|
|
18
|
-
ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3', '3.4', '4.0']
|
|
19
|
-
|
|
20
|
-
steps:
|
|
21
|
-
- uses: actions/checkout@v4
|
|
22
|
-
- name: Set up Ruby
|
|
23
|
-
uses: ruby/setup-ruby@v1
|
|
24
|
-
with:
|
|
25
|
-
ruby-version: ${{ matrix.ruby-version }}
|
|
26
|
-
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
27
|
-
- name: Run tests
|
|
28
|
-
run: bundle exec rake test
|
|
29
|
-
- name: Run the user and group switching tests as root
|
|
30
|
-
run: sudo -E env "PATH=$PATH" bundle exec ruby -Ilib -Itest test/user_switch_test.rb
|