summaryrefslogtreecommitdiffstats
path: root/sshuttle/client.py
diff options
context:
space:
mode:
authorScott Kuhl <kuhl@mtu.edu>2021-03-09 15:05:01 -0500
committerBrian May <brian@linuxpenguins.xyz>2021-03-10 07:54:55 +1100
commitd6d11b24c8209b73f1f45b9b6d213940468f6b36 (patch)
tree99ebf04da1f4644827d075e23ef81d40561be510 /sshuttle/client.py
parentbb1363ec6b83a8ead6862ab78f003bbc2765b7f7 (diff)
Make exit code indicate a problem when pidfile is not writable.
Here, we try to open the pidfile for writing prior to forking so that the exit code can properly indicate to the user that there was a problem. No error messages are printed to the console in this case because when --daemon implies --syslog. So, the syslog will contain the message indicating that the pidfile wasn't writeable. Fixes bug #598.
Diffstat (limited to 'sshuttle/client.py')
-rw-r--r--sshuttle/client.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/sshuttle/client.py b/sshuttle/client.py
index ae2ea0e..c1cabdb 100644
--- a/sshuttle/client.py
+++ b/sshuttle/client.py
@@ -80,13 +80,25 @@ def check_daemon(pidfile):
def daemonize():
+ # Try to open the pidfile prior to forking. If there is a problem,
+ # the client can then exit with a proper exit status code and
+ # message.
+ try:
+ outfd = os.open(_pidname, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o666)
+ except PermissionError:
+ # User will have to look in syslog for error message since
+ # --daemon implies --syslog, all output gets redirected to
+ # syslog.
+ raise Fatal("failed to create/write pidfile %s" % _pidname)
+
+ # Create a daemon process with a new session id.
if os.fork():
os._exit(0)
os.setsid()
if os.fork():
os._exit(0)
- outfd = os.open(_pidname, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o666)
+ # Write pid to the pidfile.
try:
os.write(outfd, b'%d\n' % os.getpid())
finally: