daemonization: make sure the firewall subproc sends to syslog too.

This commit is contained in:
Avery Pennarun 2011-01-01 00:06:04 -08:00
parent 8a5ae1a40a
commit 5bf6e40682
4 changed files with 33 additions and 24 deletions

View File

@ -1,25 +1,10 @@
import struct, socket, select, errno, re, signal import struct, socket, select, errno, re, signal
import compat.ssubprocess as ssubprocess import compat.ssubprocess as ssubprocess
import helpers, ssnet, ssh import helpers, ssnet, ssh, ssyslog
from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper
from helpers import * from helpers import *
_loggerp = None
def start_syslog():
global _loggerp
_loggerp = ssubprocess.Popen(['logger',
'-p', 'daemon.info',
'-t', 'sshuttle'], stdin=ssubprocess.PIPE)
def stderr_to_syslog():
sys.stdout.flush()
sys.stderr.flush()
os.dup2(_loggerp.stdin.fileno(), 1)
os.dup2(_loggerp.stdin.fileno(), 2)
def got_signal(signum, frame): def got_signal(signum, frame):
log('exiting on signal %d\n' % signum) log('exiting on signal %d\n' % signum)
sys.exit(1) sys.exit(1)
@ -77,10 +62,10 @@ def daemonize():
si = open('/dev/null', 'r+') si = open('/dev/null', 'r+')
os.dup2(si.fileno(), 0) os.dup2(si.fileno(), 0)
os.dup2(si.fileno(), 1)
si.close() si.close()
stderr_to_syslog() ssyslog.stderr_to_syslog()
log('daemonizing (%s).\n' % _pidname)
def daemon_cleanup(): def daemon_cleanup():
@ -118,6 +103,8 @@ class FirewallClient:
argvbase = ([sys.argv[0]] + argvbase = ([sys.argv[0]] +
['-v'] * (helpers.verbose or 0) + ['-v'] * (helpers.verbose or 0) +
['--firewall', str(port)]) ['--firewall', str(port)])
if ssyslog._p:
argvbase += ['--syslog']
argv_tries = [ argv_tries = [
['sudo', '-p', '[local sudo] Password: '] + argvbase, ['sudo', '-p', '[local sudo] Password: '] + argvbase,
['su', '-c', ' '.join(argvbase)], ['su', '-c', ' '.join(argvbase)],
@ -197,7 +184,7 @@ def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets,
try: try:
(serverproc, serversock) = ssh.connect(ssh_cmd, remotename, python, (serverproc, serversock) = ssh.connect(ssh_cmd, remotename, python,
stderr=_loggerp.stdin) stderr=ssyslog._p and ssyslog._p.stdin)
except socket.error, e: except socket.error, e:
if e.errno == errno.EPIPE: if e.errno == errno.EPIPE:
raise Fatal("failed to establish ssh session") raise Fatal("failed to establish ssh session")
@ -219,8 +206,10 @@ def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets,
debug1('connected.\n') debug1('connected.\n')
if daemon: if daemon:
daemonize() daemonize()
log('daemonizing (%s).\n' % _pidname)
elif syslog: elif syslog:
stderr_to_syslog() debug1('switching to syslog.\n')
ssyslog.stderr_to_syslog()
def onroutes(routestr): def onroutes(routestr):
if auto_nets: if auto_nets:
@ -279,7 +268,7 @@ def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets,
def main(listenip, ssh_cmd, remotename, python, seed_hosts, auto_nets, def main(listenip, ssh_cmd, remotename, python, seed_hosts, auto_nets,
subnets_include, subnets_exclude, syslog, daemon, pidfile): subnets_include, subnets_exclude, syslog, daemon, pidfile):
if syslog: if syslog:
start_syslog() ssyslog.start_syslog()
if daemon: if daemon:
try: try:
check_daemon(pidfile) check_daemon(pidfile)

View File

@ -1,6 +1,6 @@
import re, errno import re, errno
import compat.ssubprocess as ssubprocess import compat.ssubprocess as ssubprocess
import helpers import helpers, ssyslog
from helpers import * from helpers import *
@ -216,7 +216,7 @@ def restore_etc_hosts(port):
# exit. In case that fails, it's not the end of the world; future runs will # exit. In case that fails, it's not the end of the world; future runs will
# supercede it in the transproxy list, at least, so the leftover rules # supercede it in the transproxy list, at least, so the leftover rules
# are hopefully harmless. # are hopefully harmless.
def main(port): def main(port, syslog):
assert(port > 0) assert(port > 0)
assert(port <= 65535) assert(port <= 65535)
@ -235,6 +235,10 @@ def main(port):
# can read from it. # can read from it.
os.dup2(1, 0) os.dup2(1, 0)
if syslog:
ssyslog.start_syslog()
ssyslog.stderr_to_syslog()
debug1('firewall manager ready.\n') debug1('firewall manager ready.\n')
sys.stdout.write('READY\n') sys.stdout.write('READY\n')
sys.stdout.flush() sys.stdout.flush()

View File

@ -82,7 +82,7 @@ try:
elif opt.firewall: elif opt.firewall:
if len(extra) != 1: if len(extra) != 1:
o.fatal('exactly one argument expected') o.fatal('exactly one argument expected')
sys.exit(firewall.main(int(extra[0]))) sys.exit(firewall.main(int(extra[0]), opt.syslog))
elif opt.hostwatch: elif opt.hostwatch:
sys.exit(hostwatch.hw_main(extra)) sys.exit(hostwatch.hw_main(extra))
else: else:

16
ssyslog.py Normal file
View File

@ -0,0 +1,16 @@
import sys, os
from compat import ssubprocess
_p = None
def start_syslog():
global _p
_p = ssubprocess.Popen(['logger',
'-p', 'daemon.info',
'-t', 'sshuttle'], stdin=ssubprocess.PIPE)
def stderr_to_syslog():
sys.stdout.flush()
sys.stderr.flush()
os.dup2(_p.stdin.fileno(), 2)