| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
""" Launches or restarts cinnamon |
| 4 |
""" |
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
import time |
| 9 |
import gettext |
| 10 |
from setproctitle import setproctitle |
| 11 |
from pathlib import Path |
| 12 |
import psutil |
| 13 |
import subprocess |
| 14 |
import threading |
| 15 |
import gi |
| 16 |
import shutil |
| 17 |
gi.require_version('Gtk', '3.0') # noqa |
| 18 |
from gi.repository import Gtk, GLib, Gio, GLib |
| 19 |
|
| 20 |
FALLBACK_COMMAND = "metacity" |
| 21 |
FALLBACK_ARGS = ("--replace",) |
| 22 |
|
| 23 |
gettext.install("cinnamon", "/usr/share/locale") |
| 24 |
|
| 25 |
panel_process_name = None |
| 26 |
panel_cmd = None |
| 27 |
if shutil.which("mate-panel"): |
| 28 |
panel_process_name = "mate-panel" |
| 29 |
panel_cmd = "mate-panel --replace &" |
| 30 |
elif shutil.which("gnome-panel"): |
| 31 |
panel_process_name = "gnome-panel" |
| 32 |
panel_cmd = "gnome-panel --replace &" |
| 33 |
elif shutil.which("tint2"): |
| 34 |
panel_process_name = "tint2" |
| 35 |
panel_cmd = "killall tint2; tint2 &" |
| 36 |
|
| 37 |
# Used as a decorator to run things in the background |
| 38 |
def async_function(func): |
| 39 |
def wrapper(*args, **kwargs): |
| 40 |
thread = threading.Thread(target=func, args=args, kwargs=kwargs) |
| 41 |
thread.daemon = True |
| 42 |
thread.start() |
| 43 |
return thread |
| 44 |
return wrapper |
| 45 |
|
| 46 |
# Used as a decorator to run things in the main loop, from another thread |
| 47 |
def idle_function(func): |
| 48 |
def wrapper(*args): |
| 49 |
GLib.idle_add(func, *args) |
| 50 |
return wrapper |
| 51 |
|
| 52 |
class Launcher: |
| 53 |
|
| 54 |
def __init__(self): |
| 55 |
self.fb_pid = 0 |
| 56 |
self.kill_lock = threading.Lock() |
| 57 |
|
| 58 |
self.polkit_agent_proc = None |
| 59 |
self.nm_applet_proc = None |
| 60 |
|
| 61 |
self.cinnamon_pid = os.fork() |
| 62 |
if self.cinnamon_pid == 0: |
| 63 |
os.execvp("cinnamon", ("cinnamon", "--replace", ) + tuple(sys.argv[1:])) |
| 64 |
else: |
| 65 |
self.settings = Gio.Settings(schema="org.cinnamon.launcher") |
| 66 |
self.settings.connect("changed::memory-limit-enabled", self.on_settings_changed) |
| 67 |
self.killed = False |
| 68 |
if self.settings.get_boolean("memory-limit-enabled"): |
| 69 |
print("Cinnamon memory limit enabled: %d MB" % self.settings.get_int("memory-limit")) |
| 70 |
self.monitor_memory() |
| 71 |
self.wait_for_process() |
| 72 |
Gtk.main() |
| 73 |
|
| 74 |
def on_settings_changed(self, settings, key): |
| 75 |
print("Memory profiler status changed, restarting Cinnamon.") |
| 76 |
self.restart_cinnamon() |
| 77 |
|
| 78 |
@async_function |
| 79 |
def wait_for_process(self): |
| 80 |
exit_status = os.waitpid(self.cinnamon_pid, 0)[1] |
| 81 |
if not os.WIFEXITED(exit_status): |
| 82 |
with self.kill_lock: |
| 83 |
if self.killed: |
| 84 |
# Restart Cinnamon |
| 85 |
self.restart_cinnamon() |
| 86 |
else: |
| 87 |
self.fb_pid = os.fork() |
| 88 |
if self.fb_pid == 0: |
| 89 |
# Start the fallback panel |
| 90 |
if panel_cmd is not None: |
| 91 |
os.system(panel_cmd) |
| 92 |
# Start the fallback window manager |
| 93 |
os.execvp(FALLBACK_COMMAND, (FALLBACK_COMMAND,) + FALLBACK_ARGS) |
| 94 |
else: |
| 95 |
self.launch_fallback_helpers() |
| 96 |
self.confirm_restart() |
| 97 |
else: |
| 98 |
Gtk.main_quit() |
| 99 |
|
| 100 |
def launch_fallback_helpers(self): |
| 101 |
agents = ("/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1", |
| 102 |
"/usr/libexec/polkit-gnome-authentication-agent-1", |
| 103 |
"/usr/libexec/polkit-mate-authentication-agent-1") |
| 104 |
|
| 105 |
for path in agents: |
| 106 |
if Path(path).exists(): |
| 107 |
print(f"Launching for fallback session: {path}") |
| 108 |
self.polkit_agent_proc = subprocess.Popen([path]) |
| 109 |
break |
| 110 |
|
| 111 |
# Start NM applet |
| 112 |
if shutil.which("nm-applet"): |
| 113 |
print(f"Launching for fallback session: nm-applet") |
| 114 |
self.nm_applet_proc = subprocess.Popen(["nm-applet"]) |
| 115 |
|
| 116 |
def kill_fallback_helpers(self): |
| 117 |
if self.polkit_agent_proc is not None: |
| 118 |
print("Killing fallback polkit agent") |
| 119 |
self.polkit_agent_proc.terminate() |
| 120 |
try: |
| 121 |
self.polkit_agent_proc.wait(timeout=5) |
| 122 |
except subprocess.TimeoutExpired: |
| 123 |
self.polkit_agent_proc.kill() |
| 124 |
self.polkit_agent_proc = None |
| 125 |
|
| 126 |
if self.nm_applet_proc is not None: |
| 127 |
print("Killing fallback nm-applet") |
| 128 |
self.nm_applet_proc.terminate() |
| 129 |
try: |
| 130 |
self.nm_applet_proc.wait(timeout=5) |
| 131 |
except subprocess.TimeoutExpired: |
| 132 |
self.nm_applet_proc.kill() |
| 133 |
self.nm_applet_proc = None |
| 134 |
|
| 135 |
@async_function |
| 136 |
def monitor_memory(self): |
| 137 |
if psutil.pid_exists(self.cinnamon_pid): |
| 138 |
process = psutil.Process(self.cinnamon_pid) |
| 139 |
while True: |
| 140 |
delay = self.settings.get_int("check-frequency") |
| 141 |
limit = self.settings.get_int("memory-limit") |
| 142 |
time.sleep(delay) |
| 143 |
if not process.is_running(): |
| 144 |
break |
| 145 |
memory = ((process.memory_full_info().rss - process.memory_full_info().shared) / 1024 ** 2) |
| 146 |
if memory > limit: |
| 147 |
print ("Memory threshold reached: %d" % memory) |
| 148 |
self.log_kill(memory, limit, delay) |
| 149 |
with self.kill_lock: |
| 150 |
self.killed = True |
| 151 |
process.kill() |
| 152 |
break |
| 153 |
|
| 154 |
def restart_cinnamon(self): |
| 155 |
os.execvp(sys.argv[0], sys.argv) |
| 156 |
|
| 157 |
def log_kill(self, memory, limit, delay): |
| 158 |
try: |
| 159 |
directory = os.path.join(GLib.get_user_state_dir(), 'cinnamon') |
| 160 |
except AttributeError: |
| 161 |
directory = os.path.expanduser("~/.cinnamon") |
| 162 |
|
| 163 |
string = "%s - Usage: %d MB - Limit: %d MB - Freq: %s sec" % (time.strftime("%Y.%m.%d %H:%M:%S"), memory, limit, delay) |
| 164 |
if not os.path.exists(directory): |
| 165 |
os.mkdir(directory) |
| 166 |
with open(os.path.join(directory, "restarts.log"), "a") as log_file: |
| 167 |
print(string, file=log_file) |
| 168 |
|
| 169 |
@idle_function |
| 170 |
def confirm_restart(self): |
| 171 |
d = Gtk.MessageDialog(title=None, parent=None, flags=0, message_type=Gtk.MessageType.WARNING) |
| 172 |
d.add_buttons(_("No"), Gtk.ResponseType.NO, |
| 173 |
_("Yes"), Gtk.ResponseType.YES) |
| 174 |
d.set_keep_above(True) |
| 175 |
d.set_position(Gtk.WindowPosition.CENTER) |
| 176 |
d.set_title(_("Cinnamon just crashed")) |
| 177 |
box = d.get_content_area() |
| 178 |
checkbutton = Gtk.CheckButton(label=_("Disable downloaded applets, desklets and extensions")) |
| 179 |
d.set_markup("<span size='large'><b>%s</b></span>\n\n%s\n\n%s\n\n" % |
| 180 |
( |
| 181 |
_("You are currently running in fallback mode."), |
| 182 |
_("If you suspect the source of the crash is a local extension, applet or desklet, you can disable downloaded add-ons using the checkbox below."), |
| 183 |
_("Do you want to restart Cinnamon?"), |
| 184 |
)) |
| 185 |
box.set_border_width(20) |
| 186 |
box.add(checkbutton) |
| 187 |
checkbutton.show_all() |
| 188 |
resp = d.run() |
| 189 |
d.destroy() |
| 190 |
if resp == Gtk.ResponseType.YES: |
| 191 |
if checkbutton.get_active(): |
| 192 |
os.environ["CINNAMON_TROUBLESHOOT"] = "1" |
| 193 |
os.system("killall %s" % panel_process_name) |
| 194 |
self.kill_fallback_helpers() |
| 195 |
os.system("kill %d" % self.fb_pid) |
| 196 |
os.waitpid(self.fb_pid, 0) |
| 197 |
self.restart_cinnamon() |
| 198 |
else: |
| 199 |
Gtk.main_quit() |
| 200 |
|
| 201 |
if __name__ == "__main__": |
| 202 |
setproctitle("cinnamon-launcher") |
| 203 |
Launcher() |