def ppid_cascade(process=None): if process is None: process = Process() process = process.parent() while process: yield process.pid process = process.parent()
def _discover_proc_name(self, process: psutil.Process, ns_id: int) \ -> Optional[str]: """Discovers the process "name" for a given process. The name is taken from the most senior process in the process tree which is still in the same (PID or user) namespace as the process initially specified to this function. """ parent = process.parent() while parent: try: parent_ns_id = os.stat( '/proc/%d/ns/%s' % (parent.pid, self._nstypename))\ .st_ino except PermissionError: parent_ns_id = -1 if parent_ns_id != ns_id: break process = parent parent = process.parent() # prepare the pretty-print-able process name: only use the last # executable path element, and strip of a leading "-" indicating a # login shell. In case the process' command line is inaccessible to # us, then go for the process name. try: proc_name = process.cmdline()[0].split('/')[-1] except IndexError: # Mimic what the "ps" CLI tool does in case of process names... proc_name = "[%s]" % process.name() if proc_name[:1] == '-': proc_name = proc_name[1:] return '%s (%d)' % (proc_name, process.pid)
def killTika(): tikatree_proc = Process() parent = tikatree_proc.parent() tikatree_children = parent.children(recursive=True) for child in tikatree_children: if child.name() == "java.exe": child.terminate()
def _get_shell_pid(): """Returns parent process pid.""" proc = Process(os.getpid()) try: return proc.parent().pid except TypeError: return proc.parent.pid
def get_session_conf_dir(self, cleanup=False): """ Tries to find the session configuration directory by looking in ~/.dnanexus_config/sessions/<PID>, where <PID> is pid of the parent of this process, then its parent, and so on. If none of those exist, the path for the immediate parent is given, even if it doesn't exist. If *cleanup* is True, looks up and deletes all session configuration directories that belong to nonexistent processes. """ sessions_dir = os.path.join(self._user_conf_dir, "sessions") try: from psutil import Process, pid_exists if cleanup: try: session_dirs = os.listdir(sessions_dir) except OSError as e: # Silently skip cleanup and continue if we are unable to # enumerate the session directories for any reason # (including, most commonly, because the sessions dir # doesn't exist) session_dirs = [] for session_dir in session_dirs: try: session_pid = int(session_dir) except ValueError: # If dir name doesn't look like an int, leave it # alone continue if not pid_exists(session_pid): rmtree(os.path.join(sessions_dir, session_dir), ignore_errors=True) parent_process = Process(os.getpid()).parent() default_session_dir = os.path.join(sessions_dir, str(parent_process.pid)) while parent_process is not None and parent_process.pid != 0: session_dir = os.path.join(sessions_dir, str(parent_process.pid)) if os.path.exists(session_dir): return session_dir parent_process = parent_process.parent() return default_session_dir except (ImportError, IOError, AttributeError) as e: warn( fill("Error while retrieving session configuration: " + format_exception(e))) except Exception as e: warn( fill( "Unexpected error while retrieving session configuration: " + format_exception(e))) return self._get_ppid_session_conf_dir(sessions_dir)
def get_session_conf_dir(self, cleanup=False): """ Tries to find the session configuration directory by looking in ~/.dnanexus_config/sessions/<PID>, where <PID> is pid of the parent of this process, then its parent, and so on. If none of those exist, the path for the immediate parent is given, even if it doesn't exist. If *cleanup* is True, looks up and deletes all session configuration directories that belong to nonexistent processes. """ sessions_dir = os.path.join(self._user_conf_dir, "sessions") try: from psutil import Process, pid_exists if cleanup: try: session_dirs = os.listdir(sessions_dir) except OSError as e: # Silently skip cleanup and continue if we are unable to # enumerate the session directories for any reason # (including, most commonly, because the sessions dir # doesn't exist) session_dirs = [] for session_dir in session_dirs: try: session_pid = int(session_dir) except ValueError: # If dir name doesn't look like an int, leave it # alone continue if not pid_exists(session_pid): rmtree(os.path.join(sessions_dir, session_dir), ignore_errors=True) parent_process = Process(os.getpid()).parent() default_session_dir = os.path.join(sessions_dir, str(parent_process.pid)) while parent_process is not None and parent_process.pid != 0: session_dir = os.path.join(sessions_dir, str(parent_process.pid)) if os.path.exists(session_dir): return session_dir parent_process = parent_process.parent() return default_session_dir except (ImportError, IOError, AttributeError) as e: # We don't bundle psutil with Windows, so failure to import # psutil would be expected. if platform.system() != 'Windows': warn(fill("Error while retrieving session configuration: " + format_exception(e))) except Exception as e: warn(fill("Unexpected error while retrieving session configuration: " + format_exception(e))) return self._get_ppid_session_conf_dir(sessions_dir)
def main(): connections = psutil.net_connections() listen = list(filter(lambda x: x.status == 'LISTEN', connections)) listen_sorted = sorted(listen, key=lambda x: x.laddr.port) print(f"{'ip':<12} {'port':<6} {'user':<16} {'started':<15} {'parent':<15} {'cwd':<30} {'cmdline'}".upper()) for l in listen_sorted: ip = l.laddr.ip port = l.laddr.port if not l.pid: cwd, user, cmdline, started = 4 * ["NOPERM"] else: p = Process(l.pid) parent = p.parent().name() cwd = p.cwd() user = p.username() cmdline = " ".join(p.cmdline()) started = arrow.get(p.create_time()).humanize() print(f"{ip:<12} {port:<6} {user:<16} {started:<15} {parent:<15} {cwd:<30} {cmdline}")
def __init__(self, proc: psutil.Process, proctable): """ Class constructor """ _dead = False self._children = list() self._parent = 0 parent = None self._proc, self._pt = proc, proctable try: self._pgid = os.getpgid(proc.pid) except: self._pgid = 0 _dead = True if not _dead: parent = proc.parent() if parent: self._parent = parent.pid if not _dead: self._children = [ p.pid for p in proc.children() ] with proc.oneshot(): if proc.is_running(): self.rss = proc.memory_info().rss self.vms = proc.memory_info().vms self.ctx_vol = proc.num_ctx_switches().voluntary self.ctx_invol = proc.num_ctx_switches().involuntary self._cmdline = proc.cmdline() self.pcpu = None #self.pcpu += proc.cpu_percent(interval=DEFAULT_INTERVAL) else: self.rss = 0 self.vms = 0 self.ctx_vol = 0 self.ctx_invol = 0 self.cmdline = [ ] self.pcpu = 0.0
def _get_shell_from_proc(): proc = Process(os.getpid()) while proc is not None and proc.pid > 0: try: name = proc.name() except TypeError: name = proc.name name = os.path.splitext(name)[0] if name in shells: return shells[name]() try: proc = proc.parent() except TypeError: proc = proc.parent return Generic()
def _get_py_process() -> Process: p = Process() assert p.name() == "python.exe" par = p.parent() # In jupyter notebook in code, "python.exe" has "python.exe" as a parent, then "Code.exe" return par if par.name() in ("py.exe", "python.exe") else p
def setup_pyramid(comp, config): env = comp.env is_debug = env.core.debug # Session factory config.set_session_factory(WebSession) # Empty authorization policy. Why do we need this? # NOTE: Authentication policy is set up in then authentication component! authz_policy = ACLAuthorizationPolicy() config.set_authorization_policy(authz_policy) _setup_pyramid_debugtoolbar(comp, config) _setup_pyramid_tm(comp, config) _setup_pyramid_mako(comp, config) # COMMON REQUEST'S ATTRIBUTES config.add_request_method(lambda req: env, 'env', property=True) config.add_request_method( lambda req: req.path_info.lower().startswith('/api/'), 'is_api', property=True) # ERROR HANGLING comp.error_handlers = list() @comp.error_handlers.append def error_renderer_handler(request, err_info, exc, exc_info): error_renderer = None mroute = request.matched_route if mroute is not None: for predicate in mroute.predicates: if isinstance(predicate, ErrorRendererPredicate): error_renderer = predicate.val break if error_renderer is not None: return error_renderer(request, err_info, exc, exc_info, debug=is_debug) @comp.error_handlers.append def api_error_handler(request, err_info, exc, exc_info): if request.is_api or request.is_xhr: return exception.json_error_response(request, err_info, exc, exc_info, debug=is_debug) @comp.error_handlers.append def html_error_handler(request, err_info, exc, exc_info): return exception.html_error_response(request, err_info, exc, exc_info, debug=is_debug) def error_handler(request, err_info, exc, exc_info, **kwargs): for handler in comp.error_handlers: result = handler(request, err_info, exc, exc_info) if result is not None: return result config.registry.settings['error.err_response'] = error_handler config.registry.settings['error.exc_response'] = error_handler config.include(exception) config.add_tween( 'nextgisweb.pyramid.util.header_encoding_tween_factory', over=('nextgisweb.pyramid.exception.unhandled_exception_tween_factory', )) # INTERNATIONALIZATION # Substitute localizer from pyramid with our own, original is # too tied to translationstring, that works strangely with string # interpolation via % operator. def localizer(request): return request.env.core.localizer(request.locale_name) config.add_request_method(localizer, 'localizer', property=True) # Replace default locale negotiator with session-based one def locale_negotiator(request): return request.session.get('pyramid.locale', env.core.locale_default) config.set_locale_negotiator(locale_negotiator) # TODO: Need to get rid of translation dirs! # Currently used only to search for jed-files. from ..package import pkginfo as _pkginfo for pkg in _pkginfo.packages: dirname = resource_filename(pkg, 'locale') if os.path.isdir(dirname): config.add_translation_dirs(dirname) # STATIC FILES if is_debug: # In debug build static_key from proccess startup time rproc = Process(os.getpid()) # When running under control of uWSGI master process use master's startup time if rproc.name() == 'uwsgi' and rproc.parent().name() == 'uwsgi': rproc = rproc.parent() _logger.debug("Found uWSGI master process PID=%d", rproc.pid) # Use 4-byte hex representation of 1/5 second intervals comp.static_key = '/' + hex(int(rproc.create_time() * 5) % (2 ** 64)) \ .replace('0x', '').replace('L', '') _logger.debug("Using startup time static key [%s]", comp.static_key[1:]) else: # In production mode build static_key from pip freeze output comp.static_key = '/' + pip_freeze()[0] _logger.debug("Using pip freeze static key [%s]", comp.static_key[1:]) config.add_static_view('/static{}/asset'.format(comp.static_key), 'nextgisweb:static', cache_max_age=3600) config.add_route('amd_package', '/static{}/amd/*subpath'.format(comp.static_key)) \ .add_view(static_amd_file) # Collect external AMD-packages from other components amd_base = [] for c in comp._env.chain('amd_base'): amd_base.extend(c.amd_base) config.add_request_method(lambda r: amd_base, 'amd_base', property=True, reify=True) # RENDERERS config.add_renderer('json', json_renderer) # Filter for quick translation. Defines function tr, which we can use # instead of request.localizer.translate in mako templates. def tr_subscriber(event): event['tr'] = event['request'].localizer.translate config.add_subscriber(tr_subscriber, BeforeRender) # OTHERS config.add_route('home', '/').add_view(home) def ctpl(n): return 'nextgisweb:pyramid/template/%s.mako' % n config.add_route('pyramid.control_panel', '/control-panel', client=()) \ .add_view(control_panel, renderer=ctpl('control_panel')) config.add_route('pyramid.favicon', '/favicon.ico').add_view(favicon) config.add_route('pyramid.control_panel.pkginfo', '/control-panel/pkginfo').add_view( pkginfo, renderer=ctpl('pkginfo')) config.add_route('pyramid.control_panel.backup.browse', '/control-panel/backup/').add_view( backup_browse, renderer=ctpl('backup')) config.add_route( 'pyramid.control_panel.backup.download', '/control-panel/backup/{filename}').add_view(backup_download) config.add_route('pyramid.control_panel.cors', '/control-panel/cors').add_view(cors, renderer=ctpl('cors')) config.add_route('pyramid.control_panel.custom_css', '/control-panel/custom-css').add_view( custom_css, renderer=ctpl('custom_css')) config.add_route('pyramid.control_panel.logo', '/control-panel/logo').add_view(cp_logo, renderer=ctpl('logo')) config.add_route('pyramid.control_panel.system_name', '/control-panel/system-name').add_view( system_name, renderer=ctpl('system_name')) config.add_route('pyramid.control_panel.miscellaneous', '/control-panel/miscellaneous').add_view( miscellaneous, renderer=ctpl('miscellaneous')) config.add_route('pyramid.control_panel.home_path', '/control-panel/home_path').add_view( home_path, renderer=ctpl('home_path')) config.add_route('pyramid.locale', '/locale/{locale}').add_view(locale) comp.test_request_handler = None config.add_route('pyramid.test_request', '/test/request/') \ .add_view(test_request) config.add_route('pyramid.test_exception_handled', '/test/exception/handled') \ .add_view(test_exception_handled) config.add_route('pyramid.test_exception_unhandled', '/test/exception/unhandled') \ .add_view(test_exception_unhandled) config.add_route('pyramid.test_timeout', '/test/timeout') \ .add_view(test_timeout) comp.control_panel = dm.DynMenu( dm.Label('info', _("Info")), dm.Link( 'info/pkginfo', _("Package versions"), lambda args: (args.request.route_url('pyramid.control_panel.pkginfo'))), dm.Label('settings', _("Settings")), dm.Link( 'settings/core', _("Web GIS name"), lambda args: (args.request.route_url('pyramid.control_panel.system_name'))), dm.Link( 'settings/cors', _("Cross-origin resource sharing (CORS)"), lambda args: (args.request.route_url('pyramid.control_panel.cors'))), dm.Link( 'settings/custom_css', _("Custom CSS"), lambda args: (args.request.route_url('pyramid.control_panel.custom_css'))), dm.Link( 'settings/logo', _("Custom logo"), lambda args: (args.request.route_url('pyramid.control_panel.logo'))), dm.Link( 'settings/miscellaneous', _("Miscellaneous"), lambda args: (args.request.route_url('pyramid.control_panel.miscellaneous'))), dm.Link( 'settings/home_path', _("Home path"), lambda args: (args.request.route_url('pyramid.control_panel.home_path'))), ) if comp.options['backup.download']: comp.control_panel.add( dm.Link( 'info/backups', _("Backups"), lambda args: args.request. route_url('pyramid.control_panel.backup.browse')))
def setup_pyramid(comp, config): env = comp.env is_debug = env.core.debug # Session factory config.set_session_factory(WebSession) _setup_pyramid_debugtoolbar(comp, config) _setup_pyramid_tm(comp, config) _setup_pyramid_mako(comp, config) # COMMON REQUEST'S ATTRIBUTES config.add_request_method(lambda req: env, 'env', property=True) config.add_request_method( lambda req: req.path_info.lower().startswith('/api/'), 'is_api', property=True) # ERROR HANGLING comp.error_handlers = list() @comp.error_handlers.append def error_renderer_handler(request, err_info, exc, exc_info): error_renderer = None mroute = request.matched_route if mroute is not None: for predicate in mroute.predicates: if isinstance(predicate, ErrorRendererPredicate): error_renderer = predicate.val break if error_renderer is not None: return error_renderer(request, err_info, exc, exc_info, debug=is_debug) @comp.error_handlers.append def api_error_handler(request, err_info, exc, exc_info): if request.is_api or request.is_xhr: return exception.json_error_response(request, err_info, exc, exc_info, debug=is_debug) @comp.error_handlers.append def html_error_handler(request, err_info, exc, exc_info): return exception.html_error_response(request, err_info, exc, exc_info, debug=is_debug) def error_handler(request, err_info, exc, exc_info, **kwargs): for handler in comp.error_handlers: result = handler(request, err_info, exc, exc_info) if result is not None: return result config.registry.settings['error.err_response'] = error_handler config.registry.settings['error.exc_response'] = error_handler config.include(exception) # INTERNATIONALIZATION # Substitute localizer from pyramid with our own, original is # too tied to translationstring, that works strangely with string # interpolation via % operator. def localizer(request): return request.env.core.localizer(request.locale_name) config.add_request_method(localizer, 'localizer', property=True) locale_default = comp.env.core.locale_default locale_sorted = [locale_default] + [ lc for lc in comp.env.core.locale_available if lc != locale_default ] # Replace default locale negotiator with session-based one def locale_negotiator(request): environ = request.environ if 'auth.user' in environ: user_loaded = True else: # Force to load user's profile. But it might fail because of # authentication or transaction failueres. try: request.user except Exception: user_loaded = False else: user_loaded = True if user_loaded: environ_language = environ['auth.user']['language'] if environ_language is not None: return environ_language session_language = request.session.get('pyramid.locale') if session_language is not None: return session_language return request.accept_language.lookup(locale_sorted, default=locale_default) config.set_locale_negotiator(locale_negotiator) # STATIC FILES if 'static_key' in comp.options: comp.static_key = '/' + comp.options['static_key'] logger.debug("Using static key from options '%s'", comp.static_key[1:]) elif is_debug: # In debug build static_key from proccess startup time rproc = Process(os.getpid()) # When running under control of uWSGI master process use master's startup time if rproc.name() == 'uwsgi' and rproc.parent().name() == 'uwsgi': rproc = rproc.parent() logger.debug("Found uWSGI master process PID=%d", rproc.pid) # Use 4-byte hex representation of 1/5 second intervals comp.static_key = '/' + hex(int(rproc.create_time() * 5) % (2 ** 64)) \ .replace('0x', '').replace('L', '') logger.debug("Using startup time static key [%s]", comp.static_key[1:]) else: # In production mode build static_key from nextgisweb_* package versions package_hash = md5('\n'.join( ('{}=={}+{}'.format(pobj.name, pobj.version, pobj.commit) for pobj in comp.env.packages.values())).encode('utf-8')) comp.static_key = '/' + package_hash.hexdigest()[:8] logger.debug("Using package based static key '%s'", comp.static_key[1:]) config.add_static_view('/static{}/asset'.format(comp.static_key), 'nextgisweb:static', cache_max_age=3600) config.add_route('amd_package', '/static{}/amd/*subpath'.format(comp.static_key)) \ .add_view(static_amd_file) # Base template includes comp._template_include = list( chain( *[c.template_include for c in comp.env.chain('template_include')])) # RENDERERS config.add_renderer('json', renderer.JSON()) # Filter for quick translation. Defines function tr, which we can use # instead of request.localizer.translate in mako templates. def tr_subscriber(event): event['tr'] = event['request'].localizer.translate config.add_subscriber(tr_subscriber, BeforeRender) # OTHERS config.add_route('home', '/').add_view(home) def ctpl(n): return 'nextgisweb:pyramid/template/%s.mako' % n config.add_route('pyramid.control_panel', '/control-panel', client=()) \ .add_view(control_panel, renderer=ctpl('control_panel')) config.add_route('pyramid.favicon', '/favicon.ico').add_view(favicon) config.add_route( 'pyramid.control_panel.sysinfo', '/control-panel/sysinfo', client=(), ).add_view(sysinfo, renderer=ctpl('sysinfo')) config.add_route('pyramid.control_panel.pkginfo', '/control-panel/pkginfo').add_view(pkginfo) if env.core.options['storage.enabled']: config.add_route('pyramid.control_panel.storage', '/control-panel/storage').add_view( storage, renderer=REACT_RENDERER) config.add_route('pyramid.control_panel.backup.browse', '/control-panel/backup/').add_view( backup_browse, renderer=ctpl('backup')) config.add_route( 'pyramid.control_panel.backup.download', '/control-panel/backup/{filename}').add_view(backup_download) config.add_route( 'pyramid.control_panel.cors', '/control-panel/cors', client=(), ).add_view(cors, renderer=REACT_RENDERER) config.add_route('pyramid.control_panel.custom_css', '/control-panel/custom-css').add_view( custom_css, renderer=REACT_RENDERER) config.add_route('pyramid.control_panel.logo', '/control-panel/logo').add_view(cp_logo, renderer=REACT_RENDERER) config.add_route('pyramid.control_panel.system_name', '/control-panel/system-name').add_view( system_name, renderer=REACT_RENDERER) config.add_route('pyramid.control_panel.home_path', '/control-panel/home-path').add_view( home_path, renderer=REACT_RENDERER) config.add_route('pyramid.locale', '/locale/{locale}').add_view(locale) comp.test_request_handler = None config.add_route('pyramid.test_request', '/test/request/') \ .add_view(test_request) config.add_route('pyramid.test_exception_handled', '/test/exception/handled') \ .add_view(test_exception_handled) config.add_route('pyramid.test_exception_unhandled', '/test/exception/unhandled') \ .add_view(test_exception_unhandled) config.add_route('pyramid.test_exception_transaction', '/test/exception/transaction') \ .add_view(test_exception_transaction) config.add_route('pyramid.test_timeout', '/test/timeout') \ .add_view(test_timeout) config.add_route('pyramid.test_example', '/test/pyramid/example') \ .add_view(lambda request: {}, renderer="nextgisweb:pyramid/template/example.mako") comp.control_panel = dm.DynMenu( dm.Label('info', _("Info")), dm.Link( 'info/sysinfo', _("System information"), lambda args: (args.request.route_url('pyramid.control_panel.sysinfo'))), dm.Label('settings', _("Settings")), dm.Link( 'settings/core', _("Web GIS name"), lambda args: (args.request.route_url('pyramid.control_panel.system_name'))), dm.Link( 'settings/cors', _("Cross-origin resource sharing (CORS)"), lambda args: (args.request.route_url('pyramid.control_panel.cors'))), dm.Link( 'settings/custom_css', _("Custom CSS"), lambda args: (args.request.route_url('pyramid.control_panel.custom_css'))), dm.Link( 'settings/logo', _("Custom logo"), lambda args: (args.request.route_url('pyramid.control_panel.logo'))), dm.Link( 'settings/home_path', _("Home path"), lambda args: (args.request.route_url('pyramid.control_panel.home_path'))), ) if env.core.options['storage.enabled']: comp.control_panel.add( dm.Link( 'info/storage', _("Storage"), lambda args: (args.request.route_url('pyramid.control_panel.storage')))) if comp.options['backup.download']: comp.control_panel.add( dm.Link( 'info/backups', _("Backups"), lambda args: args.request. route_url('pyramid.control_panel.backup.browse')))
print(*args, file=stderr, **kwargs) # slice msg msg = input() msg1 = msg[0:int(len(msg) / 2)] msg2 = msg[int(len(msg) / 2):len(msg)] # args queue = MessageQueue(int(argv[1])) master_name = argv[2] # master process must be the nearest parent with the exact given name master_proc = Process(getpid()) while master_proc.name() != master_name: master_proc = master_proc.parent() with Semaphore(int(argv[1])): try: # send-receive echo # type=1 : from me to other # type=2 : to me from other queue.send(msg1, type=1) # will block until a process claims the rest of the message (echo1, echo_type) = queue.receive(type=2) echo_proc = Process(queue.last_send_pid) if echo1.decode("ascii") == msg1: parent = echo_proc try: while parent.pid != master_proc.pid: