2022-08-25 19:48:05 +02:00
|
|
|
# Copyright (c) 2022 Johannes Findeisen <you@hanez.org>
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is furnished
|
|
|
|
|
# to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
# Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
|
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
|
|
|
|
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
|
|
|
|
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2021-12-27 21:04:49 +01:00
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
import atexit
|
|
|
|
|
import os
|
|
|
|
|
import signal
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-12-27 21:04:49 +01:00
|
|
|
from logging import getLogger
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-12-27 21:04:49 +01:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
2021-11-13 07:30:05 +01:00
|
|
|
|
|
|
|
|
class Daemon:
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
|
|
|
|
Usage: subclass the daemon class and override the run() method.
|
|
|
|
|
"""
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def __init__(self, pid_file):
|
|
|
|
|
self.pid_file = pid_file
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def daemonize(self):
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
2022-08-25 19:48:05 +02:00
|
|
|
Daemonize the class using the UNIX double fork mechanism.
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
|
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Do first fork
|
2021-11-14 04:57:09 +01:00
|
|
|
try:
|
|
|
|
|
pid = os.fork()
|
|
|
|
|
if pid > 0:
|
2022-08-25 19:48:05 +02:00
|
|
|
# Exit first parent
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
except OSError as err:
|
2022-08-25 19:48:05 +02:00
|
|
|
logger.error(str("fork #1 failed: {0}".format(err)))
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(1)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Decouple from parent environment
|
2021-11-14 04:57:09 +01:00
|
|
|
os.chdir('/')
|
|
|
|
|
os.setsid()
|
|
|
|
|
os.umask(0)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Do second fork
|
2021-11-14 04:57:09 +01:00
|
|
|
try:
|
|
|
|
|
pid = os.fork()
|
|
|
|
|
if pid > 0:
|
2022-08-25 19:48:05 +02:00
|
|
|
# Exit from second parent
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
except OSError as err:
|
2022-08-25 19:48:05 +02:00
|
|
|
logger.error(str("fork #2 failed: {0}".format(err)))
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(1)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Redirect standard file descriptors
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
si = open(os.devnull, 'r')
|
|
|
|
|
so = open(os.devnull, 'a+')
|
|
|
|
|
se = open(os.devnull, 'a+')
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
os.dup2(si.fileno(), sys.stdin.fileno())
|
|
|
|
|
os.dup2(so.fileno(), sys.stdout.fileno())
|
|
|
|
|
os.dup2(se.fileno(), sys.stderr.fileno())
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Write pidfile
|
2021-11-14 04:57:09 +01:00
|
|
|
atexit.register(self.del_pid)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
pid = str(os.getpid())
|
|
|
|
|
with open(self.pid_file, 'w+') as f:
|
|
|
|
|
f.write(pid + '\n')
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def del_pid(self):
|
|
|
|
|
os.remove(self.pid_file)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def start(self):
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
|
|
|
|
Start the daemon.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Check for a pidfile to see if the daemon already runs
|
2021-11-14 04:57:09 +01:00
|
|
|
try:
|
|
|
|
|
with open(self.pid_file, 'r') as pf:
|
|
|
|
|
pid = int(pf.read().strip())
|
|
|
|
|
except IOError:
|
|
|
|
|
pid = None
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
if pid:
|
2022-08-25 19:48:05 +02:00
|
|
|
message = "pid_file {0} already exist. daemon already running?"
|
|
|
|
|
logger.error(str(message.format(self.pid_file)))
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(1)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-12-27 21:04:49 +01:00
|
|
|
# Start the daemon
|
2021-11-14 04:57:09 +01:00
|
|
|
self.daemonize()
|
|
|
|
|
self.run()
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def stop(self):
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
|
|
|
|
Stop the daemon.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-08-25 19:48:05 +02:00
|
|
|
# Get the pid from the pid_file
|
2021-11-14 04:57:09 +01:00
|
|
|
try:
|
|
|
|
|
with open(self.pid_file, 'r') as pf:
|
|
|
|
|
pid = int(pf.read().strip())
|
|
|
|
|
except IOError:
|
|
|
|
|
pid = None
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
if not pid:
|
2022-08-25 19:48:05 +02:00
|
|
|
message = "pid_file {0} does not exist. daemon not running?"
|
|
|
|
|
logger.error(str(message.format(self.pid_file)))
|
|
|
|
|
return # not an error in a restart
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-12-27 21:04:49 +01:00
|
|
|
# Try killing the daemon process
|
2021-11-14 04:57:09 +01:00
|
|
|
try:
|
|
|
|
|
while 1:
|
|
|
|
|
os.kill(pid, signal.SIGTERM)
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
except OSError as err:
|
|
|
|
|
e = str(err.args)
|
|
|
|
|
if e.find("No such process") > 0:
|
|
|
|
|
if os.path.exists(self.pid_file):
|
|
|
|
|
os.remove(self.pid_file)
|
|
|
|
|
else:
|
2021-12-27 21:04:49 +01:00
|
|
|
logger.error(str(err.args))
|
2021-11-14 04:57:09 +01:00
|
|
|
sys.exit(1)
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def restart(self):
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
|
|
|
|
Restart the daemon.
|
|
|
|
|
"""
|
2021-11-14 04:57:09 +01:00
|
|
|
self.stop()
|
|
|
|
|
self.start()
|
2021-11-13 07:30:05 +01:00
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
def run(self):
|
2021-12-27 21:04:49 +01:00
|
|
|
"""
|
2022-08-25 19:48:05 +02:00
|
|
|
You should override this method when you subclass Daemon.
|
|
|
|
|
|
2021-11-14 04:57:09 +01:00
|
|
|
It will be called after the process has been daemonized by
|
2021-12-27 21:04:49 +01:00
|
|
|
start() or restart().
|
|
|
|
|
"""
|