diff options
Diffstat (limited to 'pkgdistcache-daemon')
-rwxr-xr-x | pkgdistcache-daemon | 69 |
1 files changed, 1 insertions, 68 deletions
diff --git a/pkgdistcache-daemon b/pkgdistcache-daemon index c1cccd5394e6..5d5b7899d8e8 100755 --- a/pkgdistcache-daemon +++ b/pkgdistcache-daemon @@ -4,23 +4,17 @@ # pkgdistcache daemon v0.3.1 # by Alessio Bianchi <venator85@gmail.com> # -# Daemon code by Chad J. Schroeder -# http://code.activestate.com/recipes/278731/ -# import http.server import os import os.path import signal import socket -import string -import subprocess import sys import avahi import dbus import dbus.glib -from gi.repository import GObject as gobject avahi_service = None @@ -30,57 +24,6 @@ def terminate(signum, frame): sys.exit(0) -# Run a command synchronously, redirecting stdout and stderr to strings -def runcmd(cmd, cwd=None): - pipe = subprocess.Popen(cmd, shell=True, cwd=cwd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - # wait for process to terminate and return stdout and stderr - (stdout, stderr) = pipe.communicate() - return { - 'stdout': stdout.strip(), - 'stderr': stderr.strip(), - 'retcode': pipe.returncode} - - -# Detach a process from the controlling terminal and run it in the background -# as a daemon -def daemonize(): - try: - pid = os.fork() - except OSError as e: - raise Exception("%s [%d]" % (e.strerror, e.errno)) - - if (pid == 0): # The first child. - os.setsid() - try: - pid = os.fork() # Fork a second child. - except OSError as e: - raise Exception("%s [%d]" % (e.strerror, e.errno)) - - if (pid != 0): # The second child. - os._exit(0) # Exit parent (the first child) of the second child. - else: - os._exit(0) # Exit parent of the first child. - - import resource # Resource usage information. - maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] - if (maxfd == resource.RLIM_INFINITY): - maxfd = 1024 - - # Iterate through and close all file descriptors. - for fd in range(0, maxfd): - try: - os.close(fd) - except OSError: # ERROR, fd wasn't open to begin with (ignored) - pass - - # The standard I/O file descriptors are redirected to /dev/null by default. - os.open("/dev/null", os.O_RDWR) # standard input (0) - os.dup2(0, 1) # standard output (1) - os.dup2(0, 2) # standard error (2) - return(0) - - class AvahiPublisher: # Based on http://avahi.org/wiki/PythonPublishExample def __init__(self, name, stype, host, port): @@ -120,16 +63,6 @@ class HTTPServerV6(http.server.HTTPServer): def main(args): - import optparse - parser = optparse.OptionParser() - parser.add_option("-F", "--foreground", action="store_true", - dest="no_daemon", default=False, - help="run pkgdistcache-daemon in foreground") - (options, args) = parser.parse_args() - if not options.no_daemon: - # fork daemon in background - daemonize() - # load configuration file conf_file = '/etc/pkgdistcache.conf' if os.path.isfile(conf_file): @@ -139,7 +72,7 @@ def main(args): return 2 port = config['port'] - hostname = runcmd('hostname')['stdout'] + hostname = socket.gethostname() global avahi_service avahi_service = AvahiPublisher(hostname, '_pkgdistcache._tcp', '', port) avahi_service.publish() |