def _do_kill(self, process_id): os.system("kill %d" % process_id) time.sleep(0.5) if process_id in gtop.proclist(): time.sleep(5.0) if process_id in gtop.proclist(): os.system("kill -9 %d" % process_id)
def __launch_desktop_blocker(self, session_name, user_id, x11_display): print "Launch desktop-blocker to '%s'" % session_name from os import environ env = environ.copy() env["DISPLAY"] = x11_display proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) if len(proclist) > 0: from subprocess import Popen, PIPE lang_var = Popen( 'cat /proc/%s/environ | tr "\\000" "\\n" | grep ^LANG= ' % proclist[0], shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(lang_var) > 0: env["LANG"] = lang_var.replace("LANG=", "") pid = Popen('nanny-desktop-blocker', env=env).pid else: pid = Popen('nanny-desktop-blocker', env=env).pid pid_file = "/var/lib/nanny/desktop_blocks_pids/%s.%s" % ( user_id, os.path.basename(session_name)) fd = open(pid_file, "w") fd.write(str(pid)) fd.close() pid, ret = os.waitpid(pid, 0) if os.path.exists(pid_file): os.unlink(pid_file) return session_name, user_id, ret
def __update_cb(self, quarterback, block_status, user_id, app_id, next_change, available_time, active): '''Callback that updates the used times of the categories.''' if block_status == False: app_list = self.__get_application_list(self.categories) proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) if app_id == SESSION_APPID : try: d = dbus.SystemBus() manager = dbus.Interface(d.get_object("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager"), "org.freedesktop.ConsoleKit.Manager") sessions = manager.GetSessionsForUnixUser(int(user_id)) for session_name in sessions : session = dbus.Interface(d.get_object("org.freedesktop.ConsoleKit", session_name), "org.freedesktop.ConsoleKit.Session") x11_display = session.GetX11Display() if x11_display == "": continue self.quarterback.subtract_time(user_id, app_id) break except: print "Crash Chrono __update_cb" else: category = self.categories[app_id] for proc in proclist: if len(gtop.proc_args(proc)) > 0: process = gtop.proc_args(proc)[0] if self.is_a_controlled_app(process, category, app_list): self.quarterback.subtract_time(user_id, app_id) break
def __logout_session_if_is_running(self, user_id): proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) for proc in proclist: if gtop.proc_args(proc)[0] == "x-session-manager" or gtop.proc_args(proc)[0] == "gnome-session": users = self.quarterback.usersmanager.get_users() for uid, uname, ufname in users : if str(uid) == user_id : if not self.logout_petitions.has_key(user_id): self.logout_petitions[user_id] = 3 if self.logout_petitions[user_id] != 0 : print "Sending logout petition to '%s'" % uname cmd='su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:0"' % (uname, proc) os.system(cmd) self.logout_petitions[user_id] = self.logout_petitions[user_id] - 1 return else: print "Sending Force logout petition to '%s'" % uname cmd='su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1"' % (uname, proc) os.system(cmd) self.logout_petitions.pop(user_id) return if self.logout_petitions.has_key(user_id) : self.logout_petitions.pop(user_id)
def __launch_desktop_blocker(self, session_name, user_id, x11_display): print "Launch desktop-blocker to '%s'" % session_name from os import environ env = environ.copy() env["DISPLAY"] = x11_display proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) if len(proclist) > 0 : from subprocess import Popen, PIPE lang_var = Popen('cat /proc/%s/environ | tr "\\000" "\\n" | grep ^LANG= ' % proclist[0] , shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(lang_var) > 0 : env["LANG"] = lang_var.replace("LANG=","") pid = Popen('nanny-desktop-blocker', env=env).pid else: pid = Popen('nanny-desktop-blocker', env=env).pid pid_file = "/var/lib/nanny/desktop_blocks_pids/%s.%s" % (user_id, os.path.basename(session_name)) fd = open(pid_file, "w") fd.write(str(pid)) fd.close() pid, ret = os.waitpid(pid, 0) if os.path.exists(pid_file) : os.unlink(pid_file) return session_name, user_id, ret
def __logout_session_if_is_running(self, user_id): proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) for proc in proclist: if gtop.proc_args(proc)[0] == "x-session-manager" or gtop.proc_args(proc)[0] == "gnome-session": users = self.quarterback.usersmanager.get_users() for uid, uname, ufname in users: if str(uid) == user_id: if not self.logout_petitions.has_key(user_id): self.logout_petitions[user_id] = 3 if self.logout_petitions[user_id] != 0: print "Sending logout petition to '%s'" % uname cmd = ( 'su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:0"' % (uname, proc) ) os.system(cmd) self.logout_petitions[user_id] = self.logout_petitions[user_id] - 1 return else: print "Sending Force logout petition to '%s'" % uname cmd = ( 'su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1"' % (uname, proc) ) os.system(cmd) self.logout_petitions.pop(user_id) return if self.logout_petitions.has_key(user_id): self.logout_petitions.pop(user_id)
def __launch_blocker_thread(self, user_id, user_name, x11_display, linuxsb): try: proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) env_lang_var = "C" if len(proclist) > 0: for proc in proclist: lang_var = ( Popen('cat /proc/%s/environ | tr "\\000" "\\n" | grep ^LANG= ' % proc, shell=True, stdout=PIPE) .stdout.readline() .strip("\n") ) if len(lang_var) > 0: env_lang_var = lang_var.replace("LANG=", "") break cmd = ["su", user_name, "-c", "LANG=%s DISPLAY=%s nanny-desktop-blocker" % (env_lang_var, x11_display)] print cmd p = Popen(cmd) print "[LinuxSessionFiltering] launching blocker (pid : %s)" % p.pid while p.poll() == None: time.sleep(1) b = threads.blockingCallFromThread(reactor, linuxsb.is_user_blocked, user_id) if b == False: p.terminate() print "[LinuxSessionFiltering] Unblocking session %s" % user_id return print "[LinuxSessionFiltering] blocker terminated by user interaction" threads.blockingCallFromThread(reactor, linuxsb.blocker_terminate_from_thread, user_id, p.poll()) except: print "[LinuxSessionFiltering] blocker terminated by exception" threads.blockingCallFromThread(reactor, linuxsb.blocker_terminate_from_thread, user_id, 1)
def _do_reload_menu(self): if not self.active: return this_items = {} if self._mode == "applications": if self.bamf_matcher != None: for window in self.bamf_matcher.RunningApplications(): try: item = self._get_item_for_bamf_application(window) this_items[item.id] = item except Exception as e: logger.debug("Could not get info from BAMF", exc_info = e) pass else: import wnck screen = wnck.screen_get_default() for window in screen.get_windows(): pid = window.get_pid() if pid > 0: item = self._get_menu_item(pid) item.process_name = window.get_name() this_items[item.id] = item pixbuf = window.get_icon() if pixbuf: item.icon = g15cairo.pixbuf_to_surface(pixbuf) else: for process_id in gtop.proclist(): process_id = "%d" % process_id try : pid = int(process_id) proc_state = gtop.proc_state(pid) proc_args = gtop.proc_args(pid) if self._mode == "all" or ( self._mode != "all" and proc_state.uid == os.getuid()): item = self._get_menu_item(pid) item.icon = None item.process_name = self._get_process_name(proc_args, proc_state.cmd) this_items[item.id] = item except Exception as e: logger.debug("Process may have disappeared", exc_info = e) # In case the process disappears pass # Remove any missing items for item in self.menu.get_children(): if not item.id in this_items: self.menu.remove_child(item) # Make sure selected still exists if self.menu.selected != None and self.menu.get_child_by_id(self.menu.selected.id) is None: if len(self.menu.get_child_count()) > 0: self.menu.selected = self.menu.get_children()[0] else: self.menu.selected = None self.page.mark_dirty() self.screen.redraw(self.page)
def childmem(self): if self.childpids == None: self.childpids = set() for each in gtop.proclist(): if gtop.proc_uid(each).ppid == self.p: self.childpids.add(each) mem = 0 for each in self.childpids: mem += gtop.proc_mem(each).resident return mem
def __close_session_fallback(self): """Fallback for the moments org.gnome.SessionManager doesn't connect""" proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(self.uid)) for proc in proclist: if len(gtop.proc_args(proc))==0: continue if gtop.proc_args(proc)[0] == "x-session-manager" or gtop.proc_args(proc)[0] == "/usr/bin/x-session-manager" or gtop.proc_args(proc)[0] == "/usr/bin/gnome-session" or gtop.proc_args(proc)[0] == "gnome-session" or gtop.proc_args(proc)[0] == "/usr/bin/lxsession" or gtop.proc_args(proc)[0] == "lxsession": cmd = "kill -9 %s" % (proc) print "Executing fallback:", cmd Popen(cmd, shell=True, stdout=PIPE)
def __remote_close_session(self, user_id): proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) for proc in proclist: if gtop.proc_args(proc)[0] == "x-session-manager" or gtop.proc_args(proc)[0] == "gnome-session": users = self.quarterback.usersmanager.get_users() for uid, uname, ufname in users : if str(uid) == user_id : print "Sending Force logout petition to '%s'" % uname cmd='su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1"' % (uname, proc) os.system(cmd) return
def flash_running(): global flash_process_time flash_process_times = [] for pid in gtop.proclist(gtop.PROCLIST_EXCLUDE_SYSTEM | gtop.PROCLIST_KERN_PROC_UID, os.getuid()): if is_flash(pid): flash_pid = pid flash_process_times.append(gtop.proc_time(pid).dict()["rtime"]) if flash_process_times: old_flash_process_time = flash_process_time flash_process_time = max(flash_process_times) if old_flash_process_time: return flash_process_time - FLASH_ACTIVE_TIME > old_flash_process_time return False
def __remote_close_session(self, user_id): proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) for proc in proclist: if gtop.proc_args( proc)[0] == "x-session-manager" or gtop.proc_args( proc)[0] == "gnome-session": users = self.quarterback.usersmanager.get_users() for uid, uname, ufname in users: if str(uid) == user_id: print "Sending Force logout petition to '%s'" % uname cmd = 'su %s -c "`grep -z DBUS_SESSION_BUS_ADDRESS /proc/%s/environ | sed -e "s:\\r::g"` dbus-send --dest=\'org.gnome.SessionManager\' /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1"' % ( uname, proc) os.system(cmd) return
def __close_session_fallback(self): """Fallback for the moments org.gnome.SessionManager doesn't connect""" proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(self.uid)) for proc in proclist: if len(gtop.proc_args(proc)) == 0: continue if gtop.proc_args(proc)[0] == "x-session-manager" or gtop.proc_args( proc)[0] == "/usr/bin/x-session-manager" or gtop.proc_args( proc)[0] == "/usr/bin/gnome-session" or gtop.proc_args( proc)[0] == "gnome-session" or gtop.proc_args( proc )[0] == "/usr/bin/lxsession" or gtop.proc_args( proc)[0] == "lxsession": cmd = "kill -9 %s" % (proc) print "Executing fallback:", cmd Popen(cmd, shell=True, stdout=PIPE)
def __update_cb(self, quarterback, block_status, user_id, app_id, next_change, available_time): '''Callback that updates the used times of the categories.''' if block_status == False: app_list = self.__get_application_list(self.categories) proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) if app_id == SESSION_APPID: try: d = dbus.SystemBus() manager = dbus.Interface( d.get_object("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager"), "org.freedesktop.ConsoleKit.Manager") sessions = manager.GetSessionsForUnixUser(int(user_id)) for session_name in sessions: session = dbus.Interface( d.get_object("org.freedesktop.ConsoleKit", session_name), "org.freedesktop.ConsoleKit.Session") x11_display = session.GetX11Display() if x11_display == "": continue self.quarterback.subtract_time(user_id, app_id) break except: print "Crash Chrono __update_cb" else: category = self.categories[app_id] found = False for proc in proclist: if len(gtop.proc_args(proc)) > 0: process = gtop.proc_args(proc)[0] if self.is_a_controlled_app(process, category, app_list): self.quarterback.subtract_time(user_id, app_id) break
def __polling_cb(self): self.auth_pid_cache = list(set(gtop.proclist()).intersection(set(self.auth_pid_cache))) return True
def __launch_blocker_thread(self, user_id, user_name, x11_display, linuxsb): try: proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) env_lang_var = 'C' if len(proclist) > 0: for proc in proclist: lang_var = Popen('cat /proc/%s/environ | tr "\\000" "\\n" | grep ^LANG= ' % proc , shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(lang_var) > 0 : env_lang_var = lang_var.replace("LANG=","") break cmd = ['su', user_name, '-c', 'LANG=%s DISPLAY=%s %s &>> /var/tmp/desktop-blocker-%s.log' % (env_lang_var, x11_display, self.sb, user_id)] print cmd # hack to start after desktop has actually been loaded # see https://bugs.launchpad.net/nanny/+bug/916788 etc # # BOH env_session_type = None if len(proclist) > 0: proclist.reverse() # find the last instance with session defined for proc in proclist: session_type = Popen('cat /proc/%s/environ | tr "\\000" "\\n" | grep ^DESKTOP_SESSION= ' % proc , shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(session_type) > 0 : env_session_type = session_type.replace("DESKTOP_SESSION=","") break print "DESKTOP_SESSION=" + env_session_type DEFAULT_SLEEP_TIME = 36 SLEEP_INTERVAL = 2 INTERVALS = INTERVALS_COUNT = 18 if env_session_type == "ubuntu" or env_session_type == "ubuntu-2d": while os.system("pgrep -fl unity-panel-service | grep -v pgrep") != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) elif env_session_type == "gnome-classic": while os.system("pgrep -fl gnome-panel | grep -v pgrep") != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) elif env_session_type == "gnome-shell": while os.system("pgrep -fl gnome-shell | grep -v pgrep") != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) SLEEP_INTERVAL = DEFAULT_SLEEP_TIME - (INTERVALS_COUNT*SLEEP_INTERVAL - INTERVALS*SLEEP_INTERVAL) elif env_session_type == "Lubuntu" or env_session_type == "LXDE": while os.system("pgrep -fl lxpanel | grep -v pgrep") != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) else: print "Sleeping for %s seconds just like that..." % DEFAULT_SLEEP_TIME time.sleep(DEFAULT_SLEEP_TIME) print "Taking a %s second snooze before starting..." % SLEEP_INTERVAL time.sleep(SLEEP_INTERVAL) # EOH p = Popen(cmd) print "[LinuxSessionFiltering] launching blocker (pid : %s)" % p.pid while p.poll() == None : time.sleep(1) b = threads.blockingCallFromThread(reactor, linuxsb.is_user_blocked, user_id) if b == False: p.terminate() print "[LinuxSessionFiltering] Unblocking session %s" % user_id return print "[LinuxSessionFiltering] blocker terminated by user interaction" threads.blockingCallFromThread(reactor, linuxsb.blocker_terminate_from_thread, user_id, p.poll()) except: print "[LinuxSessionFiltering] blocker terminated by exception" threads.blockingCallFromThread(reactor, linuxsb.blocker_terminate_from_thread, user_id, 1)
def _do_reload_menu(self): if not self.active: return this_items = {} if self._mode == "applications": if self.bamf_matcher != None: for window in self.bamf_matcher.RunningApplications(): try: item = self._get_item_for_bamf_application(window) this_items[item.id] = item except Exception as e: logger.debug("Could not get info from BAMF", exc_info=e) pass else: import wnck screen = wnck.screen_get_default() for window in screen.get_windows(): pid = window.get_pid() if pid > 0: item = self._get_menu_item(pid) item.process_name = window.get_name() this_items[item.id] = item pixbuf = window.get_icon() if pixbuf: item.icon = g15cairo.pixbuf_to_surface(pixbuf) else: for process_id in gtop.proclist(): process_id = "%d" % process_id try: pid = int(process_id) proc_state = gtop.proc_state(pid) proc_args = gtop.proc_args(pid) if self._mode == "all" or (self._mode != "all" and proc_state.uid == os.getuid()): item = self._get_menu_item(pid) item.icon = None item.process_name = self._get_process_name( proc_args, proc_state.cmd) this_items[item.id] = item except Exception as e: logger.debug("Process may have disappeared", exc_info=e) # In case the process disappears pass # Remove any missing items for item in self.menu.get_children(): if not item.id in this_items: self.menu.remove_child(item) # Make sure selected still exists if self.menu.selected != None and self.menu.get_child_by_id( self.menu.selected.id) is None: if len(self.menu.get_child_count()) > 0: self.menu.selected = self.menu.get_children()[0] else: self.menu.selected = None self.page.mark_dirty() self.screen.redraw(self.page)
def __launch_blocker_thread(self, user_id, user_name, x11_display, linuxsb): try: proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id)) env_lang_var = 'C' if len(proclist) > 0: for proc in proclist: lang_var = Popen( 'cat /proc/%s/environ | tr "\\000" "\\n" | grep ^LANG= ' % proc, shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(lang_var) > 0: env_lang_var = lang_var.replace("LANG=", "") break cmd = [ 'su', user_name, '-c', 'LANG=%s DISPLAY=%s %s &>> /var/tmp/desktop-blocker-%s.log' % (env_lang_var, x11_display, self.sb, user_id) ] print cmd # hack to start after desktop has actually been loaded # see https://bugs.launchpad.net/nanny/+bug/916788 etc # # BOH env_session_type = None if len(proclist) > 0: proclist.reverse( ) # find the last instance with session defined for proc in proclist: session_type = Popen( 'cat /proc/%s/environ | tr "\\000" "\\n" | grep ^DESKTOP_SESSION= ' % proc, shell=True, stdout=PIPE).stdout.readline().strip("\n") if len(session_type) > 0: env_session_type = session_type.replace( "DESKTOP_SESSION=", "") break print "DESKTOP_SESSION=" + env_session_type DEFAULT_SLEEP_TIME = 36 SLEEP_INTERVAL = 2 INTERVALS = INTERVALS_COUNT = 18 if env_session_type == "ubuntu" or env_session_type == "ubuntu-2d": while os.system("pgrep -fl unity-panel-service | grep -v pgrep" ) != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) elif env_session_type == "gnome-classic": while os.system("pgrep -fl gnome-panel | grep -v pgrep" ) != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) elif env_session_type == "gnome-shell": while os.system("pgrep -fl gnome-shell | grep -v pgrep" ) != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) SLEEP_INTERVAL = DEFAULT_SLEEP_TIME - ( INTERVALS_COUNT * SLEEP_INTERVAL - INTERVALS * SLEEP_INTERVAL) elif env_session_type == "Lubuntu" or env_session_type == "LXDE": while os.system("pgrep -fl lxpanel | grep -v pgrep" ) != 0 and INTERVALS > 0: INTERVALS = INTERVALS - 1 print "Waiting for the desktop to start", INTERVALS time.sleep(SLEEP_INTERVAL) else: print "Sleeping for %s seconds just like that..." % DEFAULT_SLEEP_TIME time.sleep(DEFAULT_SLEEP_TIME) print "Taking a %s second snooze before starting..." % SLEEP_INTERVAL time.sleep(SLEEP_INTERVAL) # EOH p = Popen(cmd) print "[LinuxSessionFiltering] launching blocker (pid : %s)" % p.pid while p.poll() == None: time.sleep(1) b = threads.blockingCallFromThread(reactor, linuxsb.is_user_blocked, user_id) if b == False: p.terminate() print "[LinuxSessionFiltering] Unblocking session %s" % user_id return print "[LinuxSessionFiltering] blocker terminated by user interaction" threads.blockingCallFromThread( reactor, linuxsb.blocker_terminate_from_thread, user_id, p.poll()) except: print "[LinuxSessionFiltering] blocker terminated by exception" threads.blockingCallFromThread( reactor, linuxsb.blocker_terminate_from_thread, user_id, 1)
def __polling_cb(self): self.auth_pid_cache = list( set(gtop.proclist()).intersection(set(self.auth_pid_cache))) return True