Skip to content

Commit 300bc7b

Browse files
authored
Prevent multiple daemons running at the same time
1 parent c6cd27f commit 300bc7b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

autostart/crew-sudo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
if grep -q '^/sbin/frecon' < "/proc/$(ps -p $PPID -o ppid= | tr -d ' ')/cmdline"; then
22
# start crew-sudo daemon if running in VT-2
3-
[ -f /tmp/crew-sudo.socket ] || crew-sudo --daemon
3+
[ -f /tmp/crew-sudo.socket ] || crew-sudo --daemon --bashrc
44
fi

component/daemon.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
require_relative '../lib/pty_helper'
66

77
def runas_daemon(argv)
8-
$mode = :daemon
8+
$mode = :daemon
9+
10+
if File.exist?(SOCKET_PATH) && File.exist?(PID_FILE_PATH)
11+
if ARGV.include?('--replace')
12+
Process.kill('TERM', File.read(PID_FILE_PATH).to_i)
13+
else
14+
if IS_BASHRC
15+
warn "crew-sudo: Daemon started with PID #{File.read(PID_FILE_PATH)}"
16+
else
17+
message <<~EOT, loglevel: error
18+
crew-sudo daemon (process #{File.read(PID_FILE_PATH)}) is already running.
19+
20+
Use `#{PROGNAME} --daemon --replace` to replace the running daemon
21+
EOT
22+
end
23+
exit 1
24+
end
25+
end
26+
927
@server = UNIXServer.new(SOCKET_PATH) # create unix socket
1028

1129
# fix permission if we are running as root
@@ -27,6 +45,7 @@ def runas_daemon(argv)
2745
end
2846

2947
Process.setproctitle('crew-sudo daemon process')
48+
File.write(PID_FILE_PATH, Process.pid)
3049

3150
warn "crew-sudo: Daemon started with PID #{Process.pid}"
3251

@@ -113,4 +132,5 @@ def runas_daemon(argv)
113132
ensure
114133
@server.close
115134
File.delete(SOCKET_PATH) if File.exist?(SOCKET_PATH)
135+
File.delete(PID_FILE_PATH) if File.exist?(PID_FILE_PATH)
116136
end

lib/const.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
PROGNAME = File.basename($0, '.rb')
2+
IS_BASHRC = ARGV.include?('--bashrc')
23
SOCKET_PATH = '/tmp/crew-sudo.socket'
4+
PID_FILE_PATH = '/tmp/crew-sudo.pid'
35
DAEMON_LOG_PATH = '/tmp/crew-sudo.log'

0 commit comments

Comments
 (0)