def get_maestral_pid(config_name): """ Returns Maestral's PID if the daemon is running and responsive, ``None`` otherwise. If the daemon is unresponsive, it will be killed before returning. :param str config_name: The name of the Maestral configuration to use. :returns: The daemon's PID. :rtype: int """ lockfile = PIDLockFile(pidpath_for_config(config_name)) pid = lockfile.read_pid() if pid: try: if not is_pidfile_stale(lockfile): return pid except OSError: os.kill(pid, signal.SIGKILL) logger.debug( f"Daemon process with PID {pid} is not responsive. Killed.") else: logger.debug("Could not find PID file") lockfile.break_lock()
def get_pid_lock_file(pidfile_path, logger, app_name): pidlock = PIDLockFile(pidfile_path) if pidlock.is_locked(): old_pid = pidlock.read_pid() logger.info("Lock file exists for PID %d." % old_pid) if os.getpid() == old_pid: logger.info("Stale lock since we have the same PID.") else: try: old = psutil.Process(old_pid) if os.path.basename(__file__) in old.cmdline(): try: logger.info("Trying to terminate old instance...") old.terminate() try: old.wait(10) except psutil.TimeoutExpired: logger.info("Trying to kill old instance.") old.kill() except psutil.AccessDenied: logger.error( "The process seems to be %s, but " "can not be stopped. Its command line: %s" % app_name, old.cmdline()) else: logger.info( "Process does not seem to be {}.".format(app_name)) except psutil.NoSuchProcess: pass logger.info("No such process exist anymore.") logger.info("Breaking old lock.") pidlock.break_lock() return pidlock
def run_with_lock(remove=False): lock = PIDLockFile( getattr( settings, "HYPERKITTY_JOBS_UPDATE_INDEX_LOCKFILE", os.path.join(gettempdir(), "hyperkitty-jobs-update-index.lock"))) try: lock.acquire(timeout=-1) except AlreadyLocked: if check_pid(lock.read_pid()): logger.warning("The job 'update_index' is already running") return else: lock.break_lock() lock.acquire(timeout=-1) except LockFailed as e: logger.warning( "Could not obtain a lock for the 'update_index' " "job (%s)", e) return try: update_index(remove=remove) except Exception as e: logger.exception("Failed to update the fulltext index: %s", e) finally: lock.release()
def stop_maestral_daemon_thread(config_name='maestral', timeout=10): """Stops maestral's thread. This function tries to shut down Maestral gracefully. If it is not successful within the given timeout, a TimeoutError is raised. :param str config_name: The name of the Maestral configuration to use. :param float timeout: Number of sec to wait for daemon to shut down before killing it. :returns: ``Exit.Ok`` if successful,``Exit.NotRunning`` if the daemon was not running, ``Exit.Failed`` if it could not be stopped within timeout. """ logger.debug('Stopping thread') lockfile = PIDLockFile(pidpath_for_config(config_name)) t = _threads[config_name] if not t.is_alive(): lockfile.break_lock() return Exit.NotRunning # tell maestral daemon to shut down try: with MaestralProxy(config_name) as m: m.stop_sync() m.shutdown_pyro_daemon() except Pyro5.errors.CommunicationError: return Exit.Failed # wait for maestral to carry out shutdown t.join(timeout=timeout) if t.is_alive(): return Exit.Failed else: return Exit.Ok
def check_if_pidfile_process_is_running(pid_file: str, process_name: str): """ Checks if a pidfile already exists and process is still running. If process is dead then pidfile is removed. :param pid_file: path to the pidfile :param process_name: name used in exception if process is up and running """ pid_lock_file = PIDLockFile(path=pid_file) # If file exists if pid_lock_file.is_locked(): # Read the pid pid = pid_lock_file.read_pid() if pid is None: return try: # Check if process is still running proc = psutil.Process(pid) if proc.is_running(): raise AirflowException( f"The {process_name} is already running under PID {pid}.") except psutil.NoSuchProcess: # If process is dead remove the pidfile pid_lock_file.break_lock()
def run_maestral_daemon(config_name="maestral", run=True, log_to_stdout=False): """ Wraps :class:`maestral.main.Maestral` as Pyro daemon object, creates a new instance and start Pyro's event loop to listen for requests on a unix domain socket. This call will block until the event loop shuts down. This command will return silently if the daemon is already running. :param str config_name: The name of the Maestral configuration to use. :param bool run: If ``True``, start syncing automatically. Defaults to ``True``. :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``. """ from maestral.main import Maestral sock_name = sockpath_for_config(config_name) pid_name = pidpath_for_config(config_name) lockfile = PIDLockFile(pid_name) # acquire PID lock file try: lockfile.acquire(timeout=1) except AlreadyLocked: if is_pidfile_stale(lockfile): lockfile.break_lock() else: logger.debug(f"Maestral already running") return logger.debug(f"Starting Maestral daemon on socket '{sock_name}'") try: # clean up old socket, create new one try: os.remove(sock_name) except FileNotFoundError: pass daemon = Daemon(unixsocket=sock_name) # start Maestral as Pyro server ExposedMaestral = expose(Maestral) # mark stop_sync and shutdown_daemon as oneway methods # so that they don't block on call ExposedMaestral.stop_sync = oneway(ExposedMaestral.stop_sync) ExposedMaestral.shutdown_pyro_daemon = oneway( ExposedMaestral.shutdown_pyro_daemon) m = ExposedMaestral(config_name, run=run, log_to_stdout=log_to_stdout) daemon.register(m, f"maestral.{config_name}") daemon.requestLoop(loopCondition=m._loop_condition) daemon.close() except Exception: traceback.print_exc() finally: # remove PID lock lockfile.release()
def get_lock(workdir): pidfile = PIDLockFile(os.path.join(workdir, 'lobster.pid'), timeout=-1) try: pidfile.acquire() except AlreadyLocked: print "Another instance of lobster is accessing {0}".format(workdir) raise pidfile.break_lock() return pidfile
def create_pid_file(path): pid_file = PIDLockFile(arguments['--pid']) if pid_file.is_locked(): pid = pid_file.read_pid() try: os.kill(pid, 0) raise Exception("process already running") except OSError, e: if e.errno in (errno.ESRCH, errno.ENOENT): pid_file.break_lock() else: raise
def main(): serverCfg = piccolo.PiccoloServerConfig() # start logging handler = piccoloLogging(logfile=serverCfg.cfg['logging']['logfile'], debug=serverCfg.cfg['logging']['debug']) log = logging.getLogger("piccolo.server") if serverCfg.cfg['daemon']['daemon']: import daemon try: import lockfile except ImportError: print( "The 'lockfile' Python module is required to run Piccolo Server. Ensure that version 0.12 or later of lockfile is installed." ) sys.exit(1) try: from lockfile.pidlockfile import PIDLockFile except ImportError: print( "An outdated version of the 'lockfile' Python module is installed. Piccolo Server requires at least version 0.12 or later of lockfile." ) sys.exit(1) from lockfile import AlreadyLocked, NotLocked # create a pid file and tidy up if required pidfile = PIDLockFile(serverCfg.cfg['daemon']['pid_file'], timeout=-1) try: pidfile.acquire() except AlreadyLocked: try: os.kill(pidfile.read_pid(), 0) print('Process already running!') exit(1) except OSError: #No process with locked PID print('PID file exists but process is dead') pidfile.break_lock() try: pidfile.release() except NotLocked: pass pstd = open(serverCfg.cfg['daemon']['logfile'], 'w') with daemon.DaemonContext(pidfile=pidfile, files_preserve=[handler.stream], stderr=pstd): # start piccolo piccolo_server(serverCfg) else: # start piccolo piccolo_server(serverCfg)
def get_lock(workdir, force=False): from lockfile.pidlockfile import PIDLockFile from lockfile import AlreadyLocked pidfile = PIDLockFile(os.path.join(workdir, 'lobster.pid'), timeout=-1) try: pidfile.acquire() except AlreadyLocked: if not force: logger.error("another instance of lobster is accessing {0}".format(workdir)) raise pidfile.break_lock() return pidfile
def stop_maestral_daemon_process(config_name='maestral', timeout=10): """Stops maestral by finding its PID and shutting it down. This function first tries to shut down Maestral gracefully. If this fails, it will send SIGTERM. If that fails as well, it will send SIGKILL. :param str config_name: The name of the Maestral configuration to use. :param float timeout: Number of sec to wait for daemon to shut down before killing it. :returns: ``Exit.Ok`` if successful, ``Exit.Killed`` if killed and ``Exit.NotRunning`` if the daemon was not running. """ logger.debug('Stopping daemon') lockfile = PIDLockFile(pidpath_for_config(config_name)) pid = lockfile.read_pid() try: if not pid or not _process_exists(pid): return Exit.NotRunning try: with MaestralProxy(config_name) as m: m.stop_sync() m.shutdown_pyro_daemon() except Pyro5.errors.CommunicationError: logger.debug('Could not communicate with daemon, sending SIGTERM') _send_term(pid) finally: logger.debug('Waiting for shutdown') while timeout > 0: if not _process_exists(pid): logger.debug('Daemon shut down') return Exit.Ok else: time.sleep(0.2) timeout -= 0.2 # send SIGTERM after timeout and delete PID file _send_term(pid) time.sleep(1) if not _process_exists(pid): logger.debug('Daemon shut down') return Exit.Ok else: os.kill(pid, signal.SIGKILL) logger.debug('Daemon killed') return Exit.Killed finally: lockfile.break_lock()
def stop_maestral_daemon_process(config_name="maestral", timeout=10): """Stops maestral by finding its PID and shutting it down. This function first tries to shut down Maestral gracefully. If this fails, it will send SIGTERM. If that fails as well, it will send SIGKILL. :param str config_name: The name of the Maestral configuration to use. :param float timeout: Number of sec to wait for daemon to shut down before killing it. :returns: ``Exit.Ok`` if successful, ``Exit.Killed`` if killed and ``Exit.NotRunning`` if the daemon was not running. """ logger.debug("Stopping daemon") lockfile = PIDLockFile(pidpath_for_config(config_name)) pid = lockfile.read_pid() if not pid or is_pidfile_stale(lockfile): lockfile.break_lock() return Exit.NotRunning try: # tell maestral daemon to shut down with MaestralProxy(config_name) as m: m.stop_sync() m.shutdown_pyro_daemon() except Pyro5.errors.CommunicationError: logger.debug("Could not communicate with daemon") try: os.kill(pid, signal.SIGTERM) # try to send SIGTERM to process logger.debug("Terminating daemon process") except ProcessLookupError: logger.debug("Daemon was not running") return Exit.NotRunning finally: # wait for maestral to carry out shutdown logger.debug("Waiting for shutdown") while timeout > 0: try: os.kill(pid, signal.SIG_DFL) # query if still running except ProcessLookupError: logger.debug("Daemon shut down") return Exit.Ok # return True if not running anymore else: time.sleep(0.2) # wait for 0.2 sec and try again timeout -= 0.2 # send SIGKILL after timeout, delete PID file and return False os.kill(pid, signal.SIGKILL) logger.debug("Daemon process killed") lockfile.break_lock() return Exit.Killed
def get_lock(workdir, force=False): from lockfile.pidlockfile import PIDLockFile from lockfile import AlreadyLocked pidfile = PIDLockFile(os.path.join(workdir, 'lobster.pid'), timeout=-1) try: pidfile.acquire() except AlreadyLocked: if not force: logger.error( "another instance of lobster is accessing {0}".format(workdir)) raise pidfile.break_lock() return pidfile
def get_maestral_pid(config_name): """ Returns Maestral's PID if the daemon is running, ``None`` otherwise. :param str config_name: The name of the Maestral configuration to use. :returns: The daemon's PID. :rtype: int """ lockfile = PIDLockFile(pidpath_for_config(config_name)) pid = lockfile.read_pid() if pid and not is_pidfile_stale(lockfile): return pid else: lockfile.break_lock()
def _get_atm_process(pid_path): """Return `psutil.Process` of the `pid` file. If the pidfile is stale it will release it.""" pid_file = PIDLockFile(pid_path, timeout=1.0) if pid_file.is_locked(): pid = pid_file.read_pid() try: process = psutil.Process(pid) if process.name() == 'atm': return process else: pid_file.break_lock() except psutil.NoSuchProcess: pid_file.break_lock()
def wrapped(*args, **kwargs): logging.debug('Start daemon') if not pid_file and not force_daemon: if signal_map: for key in signal_map.keys(): signal.signal(key, signal_map[key]) logging.debug('Daemons pid: %s', os.getpid()) f(*args, **kwargs) if clean: clean() return if pid_file and pid_file not in ['-']: pid_path = os.path.abspath(pid_file) # clean old pids pidfile = PIDLockFile(pid_path, timeout=-1) try: pidfile.acquire() pidfile.release() except (AlreadyLocked, LockTimeout): try: os.kill(pidfile.read_pid(), 0) logging.warn('Process already running!') exit(2) except OSError: #No process with locked PID pidfile.break_lock() pidfile = PIDLockFile(pid_path, timeout=-1) context = _daemon.DaemonContext( pidfile=pidfile ) else: context = _daemon.DaemonContext() if signal_map: context.signal_map = signal_map context.open() with context: logging.debug('Daemons pid: %s', os.getpid()) f(*args, **kwargs) if clean: clean()
def putioCheck(): """ Should probably be in a class """ global instance instance = putioDaemon() instance.getinputs(sys.argv[1:]) instance.readconfig() instance.setuplogging() pidfile = PIDLockFile(instance.pidfile, timeout=-1) try: pidfile.acquire() except AlreadyLocked: try: os.kill(pidfile.read_pid(),0) print 'Process already running!' exit (1) except OSError: pidfile.break_lock() except: print "Something failed:", sys.exc_info() exit (1) logging.debug('Listen is %s', instance.listen) if instance.listen: WebServer() signal.signal(signal.SIGTERM,handler) while True: if os.path.exists(instance.torrentdir): onlyfiles = [ f for f in os.listdir(instance.torrentdir) if os.path.isfile(os.path.join(instance.torrentdir,f))] if len(onlyfiles): client = putio.Client(instance.token) for torrent in onlyfiles: logging.info('working on %s', torrent) # if we are listening then use the callback_url callback_url = None if instance.listen: callback_url = 'http://'+instance.callback+'/'+instance.httppath+'/api/'+instance.token logging.info('Calling add_torrent for %s with %s',torrent,callback_url) client.Transfer.add_torrent(instance.torrentdir+"/"+torrent, callback_url=callback_url) os.remove(instance.torrentdir+"/"+torrent) time.sleep(5)
def run_with_lock(remove=False): lock = PIDLockFile(getattr( settings, "HYPERKITTY_JOBS_UPDATE_INDEX_LOCKFILE", os.path.join(gettempdir(), "hyperkitty-jobs-update-index.lock"))) try: lock.acquire(timeout=-1) except AlreadyLocked: if check_pid(lock.read_pid()): logger.warning("The job 'update_index' is already running") return else: lock.break_lock() lock.acquire(timeout=-1) except LockFailed as e: logger.warning("Could not obtain a lock for the 'update_index' " "job (%s)", e) return try: update_index(remove=remove) except Exception as e: # pylint: disable-msg=broad-except logger.exception("Failed to update the fulltext index: %s", e) finally: lock.release()
def _get_lock(theargs, stage): """Create lock file to prevent this process from running on same data. This uses ``PIDLockFile`` to create a pid lock file in celppdir directory named celprunner.<stage>.lockpid If pid exists it is assumed the lock is held otherwise lock is broken and recreated :param theargs: return value from argparse and should contain theargs.celppdir should be set to path :param stage: set to stage that is being run :return: ``PIDLockFile`` upon success :raises: LockException: If there was a problem locking :raises: Exception: If valid pid lock file already exists """ mylockfile = os.path.join(theargs.celppdir, "celpprunner." + stage + ".lockpid") logger.debug("Looking for lock file: " + mylockfile) lock = PIDLockFile(mylockfile, timeout=10) if lock.i_am_locking(): logger.debug("My process id" + str(lock.read_pid()) + " had the lock so I am breaking") lock.break_lock() lock.acquire(timeout=10) return lock if lock.is_locked(): logger.debug("Lock file exists checking pid") if psutil.pid_exists(lock.read_pid()): raise Exception("celpprunner with pid " + str(lock.read_pid()) + " is running") lock.break_lock() logger.info("Acquiring lock") lock.acquire(timeout=10) return lock
def main(): parser = argparse.ArgumentParser() parser.add_argument('interface', nargs='?', help='interface to configure with DHCP') parser.add_argument('-v', '--verbose', help='Set logging level to debug', action='store_true') parser.add_argument('--version', action='version', help='version', version='%(prog)s ' + __version__) parser.add_argument('-s', '--delay_selecting', help='Selecting starts after a ramdon delay.', action='store_true') # options to looks like dhclient parser.add_argument( '-sf', metavar='script-file', nargs='?', const=SCRIPT_PATH, help='Path to the network configuration script invoked by ' 'dhcpcanon when it gets a lease. Without this option ' 'dhcpcanon will configure the network by itself.' 'If unspecified, the ' 'default /sbin/dhcpcanon-script is used, which is a copy of' 'dhclient-script(8) for a description of this file.' 'If dhcpcanon is running with NetworkManager, it will' 'be called with the script nm-dhcp-helper.') parser.add_argument( '-pf', metavar='pid-file', nargs='?', const=PID_PATH, help='Path to the process ID file. If unspecified, the' 'default /var/run/dhcpcanon.pid is used. ' 'This option is used by NetworkManager to check whether ' 'dhcpcanon is already running.') args = parser.parse_args() logger.debug('args %s', args) # do not put interfaces in promiscuous mode conf.sniff_promisc = conf.promisc = 0 conf.checkIPaddr = 1 if args.verbose: logger.setLevel(logging.DEBUG) logger.debug('args %s', args) if args.interface: conf.iface = args.interface logger.debug('interface %s' % conf.iface) if args.pf is not None: # This is only needed for nm pf = PIDLockFile(args.pf, timeout=5) try: pf.acquire() logger.debug('using pid file %s', pf) except AlreadyLocked as e: pf.break_lock() pf.acquire() except (LockTimeout, LockFailed) as e: logger.error(e) dhcpcap = DHCPCAPFSM(iface=conf.iface, server_port=SERVER_PORT, client_port=CLIENT_PORT, scriptfile=args.sf, delay_selecting=args.delay_selecting) dhcpcap.run()
def main(): """Standard main function.""" parser = ArgumentParser(description=__doc__, usage="$(prog)s [options] command") parser.add_argument("command", nargs='?', default="start", type=str, help="Daemon action (start|stop|restart|status)") parser.add_argument("-c", "--config", default="/etc/mqttorrd.ini", type=str, help="Path to config file.", metavar="<file>") parser.add_argument("-i", "--info", action="store_true", help="more verbose logging level INFO is set") parser.add_argument("-d", "--debug", action="store_true", help="DEBUG logging level is set") parser.add_argument("-f", "--foreground", action="store_true", help="Run as script on foreground") args = parser.parse_args() try: config = Config(args) daemon = Daemon(config, args.foreground) if args.foreground: print("Starting process ...") return daemon.run(False) pid_file = PIDLockFile(config.pid_file) pid = pid_file.read_pid() if pid_file.is_locked() else None if args.command == "stop": if pid and check_process(pid): print("Stopping service with pid", pid) kill(pid, SIGTERM) else: print("Service not running") return 0 if args.command == "status": if pid and check_process(pid): print("Service running with pid", pid) return 0 print("Service not running") return 1 if args.command == "restart": if pid and check_process(pid): print("Restarting service with pid", pid) kill(pid, SIGTERM) if pid: if not check_process(pid): pid_file.break_lock() else: print("Service is already running") return 1 context = DaemonContext(working_directory=config.data_dir, pidfile=pid_file, signal_map={SIGTERM: daemon.shutdown}) if geteuid() == 0: context.uid = config.uid context.gid = config.gid if config.log_handler == "file": context.files_preserve = [daemon.handler.stream] else: # SysLogHandler context.files_preserve = [daemon.handler.socket] print("Starting service ...") with context: daemon.logger.info("Starting service with pid %d", pid_file.read_pid()) retval = daemon.run() daemon.logger.info("Shutdown") return retval except Exception as exc: # pylint: disable=broad-except logger.info("%s", args) logger.debug("%s", format_exc()) logger.fatal("%s", exc) parser.error("%s" % exc) return 1
class SimpleMainLock: """ Tools like gprecoverseg prohibit running multiple instances at the same time via a simple lock file created in the MASTER_DATA_DIRECTORY. This class takes care of the work to manage this lock as appropriate based on the mainOptions specified. Note that in some cases, the utility may want to recursively invoke itself (e.g. gprecoverseg -r). To handle this, the caller may specify the name of an environment variable holding the pid already acquired by the parent process. """ def __init__(self, mainOptions): self.pidfilename = mainOptions.get( 'pidfilename', None) # the file we're using for locking self.parentpidvar = mainOptions.get( 'parentpidvar', None) # environment variable holding parent pid self.parentpid = None # parent pid which already has the lock self.ppath = None # complete path to the lock file self.pidlockfile = None # PIDLockFile object self.pidfilepid = None # pid of the process which has the lock self.locktorelease = None # PIDLockFile object we should release when done if self.parentpidvar is not None and self.parentpidvar in os.environ: self.parentpid = int(os.environ[self.parentpidvar]) if self.pidfilename is not None: self.ppath = os.path.join(gp.get_masterdatadir(), self.pidfilename) self.pidlockfile = PIDLockFile(self.ppath) def acquire(self): """ Attempts to acquire the lock this process needs to proceed. Returns None on successful acquisition of the lock or the pid of the other process which already has the lock. """ # nothing to do if utiliity requires no locking if self.pidlockfile is None: return None # look for a lock file self.pidfilepid = self.pidlockfile.read_pid() if self.pidfilepid is not None: # we found a lock file # allow the process to proceed if the locker was our parent if self.pidfilepid == self.parentpid: return None # cleanup stale locks try: os.kill(self.pidfilepid, signal.SIG_DFL) except OSError, exc: if exc.errno == errno.ESRCH: self.pidlockfile.break_lock() self.pidfilepid = None # try and acquire the lock try: self.pidlockfile.acquire(1) except LockTimeout: self.pidfilepid = self.pidlockfile.read_pid() return self.pidfilepid # we have the lock # prepare for a later call to release() and take good # care of the process environment for the sake of our children self.locktorelease = self.pidlockfile self.pidfilepid = self.pidlockfile.read_pid() if self.parentpidvar is not None: os.environ[self.parentpidvar] = str(self.pidfilepid) return None
old_pid = pidlock.read_pid() if os.getpid() != old_pid: try: old = psutil.Process(old_pid) if os.path.basename(__file__) in old.cmdline(): try: old.terminate() try: old.wait(10) except psutil.TimeoutExpired: old.kill() except psutil.AccessDenied: pass except psutil.NoSuchProcess: pass pidlock.break_lock() pidlock.acquire(timeout=10) application = PermalinkServer() http_server = tornado.httpserver.HTTPServer(application, xheaders=True) http_server.listen(options.port) def handler(signum, frame): tornado.ioloop.IOLoop.instance().stop() signal.signal(signal.SIGHUP, handler) signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGTERM, handler) try: from systemd.daemon import notify
old.terminate() try: old.wait(10) except psutil.TimeoutExpired: logger.info("Trying to kill old instance.") old.kill() except psutil.AccessDenied: logger.error("The process seems to be SageCell, but " "can not be stopped. Its command line: %s" % old.cmdline()) else: logger.info("Process does not seem to be SageCell.") except psutil.NoSuchProcess: logger.info("No such process exist anymore.") logger.info("Breaking old lock.") pidlock.break_lock() pidlock.acquire(timeout=10) app = SageCellServer(args.baseurl, args.dir) listen = {'port': args.port, 'xheaders': True} if args.interface is not None: listen['address'] = get_ip_address(args.interface) logger.info("Listening configuration: %s", listen) def handler(signum, frame): logger.info("Received %s, shutting down...", signum) app.kernel_dealer.stop() app.ioloop.stop() signal.signal(signal.SIGHUP, handler) signal.signal(signal.SIGINT, handler)
def run_maestral_daemon(config_name='maestral', run=True, log_to_stdout=False): """ Wraps :class:`maestral.main.Maestral` as Pyro daemon object, creates a new instance and start Pyro's event loop to listen for requests on a unix domain socket. This call will block until the event loop shuts down. This command will return silently if the daemon is already running. :param str config_name: The name of the Maestral configuration to use. :param bool run: If ``True``, start syncing automatically. Defaults to ``True``. :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``. """ import threading from maestral.main import Maestral sock_name = sockpath_for_config(config_name) pid_name = pidpath_for_config(config_name) lockfile = PIDLockFile(pid_name) if threading.current_thread() is threading.main_thread(): signal.signal(signal.SIGTERM, _sigterm_handler) # acquire PID lock file try: lockfile.acquire(timeout=1) except (AlreadyLocked, LockTimeout): if is_pidfile_stale(lockfile): lockfile.break_lock() else: logger.debug(f'Maestral already running') return # Nice ourselves give other processes priority. We will likely only # have significant CPU usage in case of many concurrent downloads. os.nice(10) logger.debug(f'Starting Maestral daemon on socket "{sock_name}"') try: # clean up old socket try: os.remove(sock_name) except FileNotFoundError: pass daemon = Daemon(unixsocket=sock_name) # start Maestral as Pyro server ExposedMaestral = expose(Maestral) # mark stop_sync and shutdown_daemon as one way # methods so that they don't block on call ExposedMaestral.stop_sync = oneway(ExposedMaestral.stop_sync) ExposedMaestral.pause_sync = oneway(ExposedMaestral.pause_sync) ExposedMaestral.shutdown_pyro_daemon = oneway( ExposedMaestral.shutdown_pyro_daemon) m = ExposedMaestral(config_name, run=run, log_to_stdout=log_to_stdout) daemon.register(m, f'maestral.{config_name}') daemon.requestLoop(loopCondition=m._loop_condition) daemon.close() except Exception: traceback.print_exc() except (KeyboardInterrupt, SystemExit): logger.info('Received system exit') sys.exit(0) finally: lockfile.release()
def main(): parser = argparse.ArgumentParser() parser.add_argument('interface', nargs='?', help='interface to configure with DHCP') parser.add_argument('-v', '--verbose', help='Set logging level to debug', action='store_true') parser.add_argument('--version', action='version', help='version', version='%(prog)s ' + __version__) parser.add_argument('-s', '--delay_selecting', help='Selecting starts after a ramdon delay.', action='store_true') # options to looks like dhclient parser.add_argument( '-sf', metavar='script-file', nargs='?', const=SCRIPT_PATH, help='Path to the network configuration script invoked by ' 'dhcpcanon when it gets a lease. Without this option ' 'dhcpcanon will configure the network by itself.' 'If unspecified, the ' 'default /sbin/dhcpcanon-script is used, which is a copy of' 'dhclient-script(8) for a description of this file.' 'If dhcpcanon is running with NetworkManager, it will' 'be called with the script nm-dhcp-helper.') parser.add_argument( '-pf', metavar='pid-file', nargs='?', const=PID_PATH, help='Path to the process ID file. If unspecified, the' 'default /var/run/dhcpcanon.pid is used. ' 'This option is used by NetworkManager to check whether ' 'dhcpcanon is already running.') args = parser.parse_args() logger.debug('args %s', args) # do not put interfaces in promiscuous mode conf.sniff_promisc = conf.promisc = 0 conf.checkIPaddr = 1 if args.verbose: logger.setLevel(logging.DEBUG) logger.debug('args %s', args) if args.interface: conf.iface = args.interface logger.debug('interface %s' % conf.iface) if args.pf is not None: # This is only needed for nm pf = PIDLockFile(args.pf, timeout=5) try: pf.acquire() logger.debug('using pid file %s', pf) except AlreadyLocked as e: pf.break_lock() pf.acquire() except (LockTimeout, LockFailed) as e: logger.error(e) dhcpcap = DHCPCAPFSM(iface=conf.iface, server_port=SERVER_PORT, client_port=CLIENT_PORT, scriptfile=args.sf, delay_selecting=args.delay_selecting) dhcpcap.run()
class SimpleMainLock: """ Tools like gprecoverseg prohibit running multiple instances at the same time via a simple lock file created in the MASTER_DATA_DIRECTORY. This class takes care of the work to manage this lock as appropriate based on the mainOptions specified. Note that in some cases, the utility may want to recursively invoke itself (e.g. gprecoverseg -r). To handle this, the caller may specify the name of an environment variable holding the pid already acquired by the parent process. """ def __init__(self, mainOptions): self.pidfilename = mainOptions.get('pidfilename', None) # the file we're using for locking self.parentpidvar = mainOptions.get('parentpidvar', None) # environment variable holding parent pid self.parentpid = None # parent pid which already has the lock self.ppath = None # complete path to the lock file self.pidlockfile = None # PIDLockFile object self.pidfilepid = None # pid of the process which has the lock self.locktorelease = None # PIDLockFile object we should release when done if self.parentpidvar is not None and self.parentpidvar in os.environ: self.parentpid = int(os.environ[self.parentpidvar]) if self.pidfilename is not None: self.ppath = os.path.join(gp.get_masterdatadir(), self.pidfilename) self.pidlockfile = PIDLockFile(self.ppath) def acquire(self): """ Attempts to acquire the lock this process needs to proceed. Returns None on successful acquisition of the lock or the pid of the other process which already has the lock. """ # nothing to do if utiliity requires no locking if self.pidlockfile is None: return None # look for a lock file self.pidfilepid = self.pidlockfile.read_pid() if self.pidfilepid is not None: # we found a lock file # allow the process to proceed if the locker was our parent if self.pidfilepid == self.parentpid: return None # cleanup stale locks try: os.kill(self.pidfilepid, signal.SIG_DFL) except OSError, exc: if exc.errno == errno.ESRCH: self.pidlockfile.break_lock() self.pidfilepid = None # try and acquire the lock try: self.pidlockfile.acquire(1) except LockTimeout: self.pidfilepid = self.pidlockfile.read_pid() return self.pidfilepid # we have the lock # prepare for a later call to release() and take good # care of the process environment for the sake of our children self.locktorelease = self.pidlockfile self.pidfilepid = self.pidlockfile.read_pid() if self.parentpidvar is not None: os.environ[self.parentpidvar] = str(self.pidfilepid) return None