Commit 9a77d03e authored by vieira's avatar vieira Committed by Brian May

Respect --syslog as soon as possible

When executing with the option --syslog start redirecting to
syslog immediately after the command line options are validated.
This way when using with some init daemon, e.g., upstart all the
relevant information (connection failures, etc) can be retrieved from
the log instead of being lost to stdout or stderr.
parent 4fdd715b
...@@ -6,6 +6,7 @@ import sshuttle.options as options ...@@ -6,6 +6,7 @@ import sshuttle.options as options
import sshuttle.client as client import sshuttle.client as client
import sshuttle.firewall as firewall import sshuttle.firewall as firewall
import sshuttle.hostwatch as hostwatch import sshuttle.hostwatch as hostwatch
import sshuttle.ssyslog as ssyslog
from sshuttle.helpers import family_ip_tuple, log, Fatal from sshuttle.helpers import family_ip_tuple, log, Fatal
...@@ -197,6 +198,9 @@ try: ...@@ -197,6 +198,9 @@ try:
ipport_v6 = parse_ipport6(ip) ipport_v6 = parse_ipport6(ip)
else: else:
ipport_v4 = parse_ipport4(ip) ipport_v4 = parse_ipport4(ip)
if opt.syslog:
ssyslog.start_syslog()
ssyslog.stderr_to_syslog()
return_code = client.main(ipport_v6, ipport_v4, return_code = client.main(ipport_v6, ipport_v4,
opt.ssh_cmd, opt.ssh_cmd,
remotename, remotename,
...@@ -209,7 +213,7 @@ try: ...@@ -209,7 +213,7 @@ try:
opt.auto_nets, opt.auto_nets,
parse_subnets(includes), parse_subnets(includes),
parse_subnets(excludes), parse_subnets(excludes),
opt.syslog, opt.daemon, opt.pidfile) opt.daemon, opt.pidfile)
if return_code == 0: if return_code == 0:
log('Normal exit code, exiting...') log('Normal exit code, exiting...')
......
...@@ -81,8 +81,6 @@ def daemonize(): ...@@ -81,8 +81,6 @@ def daemonize():
os.dup2(si.fileno(), 1) os.dup2(si.fileno(), 1)
si.close() si.close()
ssyslog.stderr_to_syslog()
def daemon_cleanup(): def daemon_cleanup():
try: try:
...@@ -389,8 +387,7 @@ def ondns(listener, method, mux, handlers): ...@@ -389,8 +387,7 @@ def ondns(listener, method, mux, handlers):
def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
python, latency_control, python, latency_control,
dns_listener, seed_hosts, auto_nets, dns_listener, seed_hosts, auto_nets, daemon):
syslog, daemon):
debug1('Starting client with Python version %s\n' debug1('Starting client with Python version %s\n'
% platform.python_version()) % platform.python_version())
...@@ -440,15 +437,11 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, ...@@ -440,15 +437,11 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
if initstring != expected: if initstring != expected:
raise Fatal('expected server init string %r; got %r' raise Fatal('expected server init string %r; got %r'
% (expected, initstring)) % (expected, initstring))
debug1('connected.\n') log('Connected.\n')
print('Connected.')
sys.stdout.flush() sys.stdout.flush()
if daemon: if daemon:
daemonize() daemonize()
log('daemonizing (%s).\n' % _pidname) log('daemonizing (%s).\n' % _pidname)
elif syslog:
debug1('switching to syslog.\n')
ssyslog.stderr_to_syslog()
def onroutes(routestr): def onroutes(routestr):
if auto_nets: if auto_nets:
...@@ -500,10 +493,8 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, ...@@ -500,10 +493,8 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
def main(listenip_v6, listenip_v4, def main(listenip_v6, listenip_v4,
ssh_cmd, remotename, python, latency_control, dns, nslist, ssh_cmd, remotename, python, latency_control, dns, nslist,
method_name, seed_hosts, auto_nets, method_name, seed_hosts, auto_nets,
subnets_include, subnets_exclude, syslog, daemon, pidfile): subnets_include, subnets_exclude, daemon, pidfile):
if syslog:
ssyslog.start_syslog()
if daemon: if daemon:
try: try:
check_daemon(pidfile) check_daemon(pidfile)
...@@ -651,8 +642,7 @@ def main(listenip_v6, listenip_v4, ...@@ -651,8 +642,7 @@ def main(listenip_v6, listenip_v4,
try: try:
return _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, return _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
python, latency_control, dns_listener, python, latency_control, dns_listener,
seed_hosts, auto_nets, syslog, seed_hosts, auto_nets, daemon)
daemon)
finally: finally:
try: try:
if daemon: if daemon:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment