| 13 | |
| 14 | _pidname = None |
| 15 | def check_daemon(pidfile): |
| 16 | global _pidname |
| 17 | _pidname = os.path.abspath(pidfile) |
| 18 | try: |
| 19 | oldpid = open(_pidname).read(1024) |
| 20 | except IOError, e: |
| 21 | if e.errno == errno.ENOENT: |
| 22 | return # no pidfile, ok |
| 23 | else: |
| 24 | raise Fatal("can't read %s: %s" % (_pidname, e)) |
| 25 | if not oldpid: |
| 26 | os.unlink(_pidname) |
| 27 | return # invalid pidfile, ok |
| 28 | oldpid = int(oldpid.strip() or 0) |
| 29 | if oldpid <= 0: |
| 30 | os.unlink(_pidname) |
| 31 | return # invalid pidfile, ok |
| 32 | try: |
| 33 | os.kill(oldpid, 0) |
| 34 | except OSError, e: |
| 35 | if e.errno == errno.ESRCH: |
| 36 | os.unlink(_pidname) |
| 37 | return # outdated pidfile, ok |
| 38 | elif e.errno == errno.EPERM: |
| 39 | pass |
| 40 | else: |
| 41 | raise |
| 42 | raise Fatal("%s: sshuttle is already running (pid=%d)" |
| 43 | % (_pidname, oldpid)) |
| 44 | |
| 45 | |
| 46 | def daemonize(): |