| 1 |
#! /usr/bin/python3 |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import json |
| 5 |
import os |
| 6 |
import pwd |
| 7 |
import signal |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import time |
| 11 |
|
| 12 |
# mount-users -- Find and terminate processes that keep a mount busy |
| 13 |
|
| 14 |
|
| 15 |
def fuser(mount_point): |
| 16 |
|
| 17 |
import dbus |
| 18 |
bus = dbus.SystemBus() |
| 19 |
systemd_manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), |
| 20 |
dbus_interface='org.freedesktop.systemd1.Manager') |
| 21 |
|
| 22 |
results = {} |
| 23 |
|
| 24 |
def get_cmdline(pid): |
| 25 |
with open(f"/proc/{pid}/cmdline") as f: |
| 26 |
return " ".join(f.read().split("\0")) |
| 27 |
|
| 28 |
def get_stat(pid): |
| 29 |
with open(f"/proc/{pid}/stat") as f: |
| 30 |
stat = f.read() |
| 31 |
# Field two is everything between the first "(" and the last ")", the rest is space separated. |
| 32 |
comm_start = stat.index("(") + 1 |
| 33 |
comm_end = stat.rindex(")") |
| 34 |
return ([stat[0:comm_start - 1].strip(), stat[comm_start:comm_end], |
| 35 |
*list(filter(lambda f: f != '', stat[comm_end + 1:-1].split(' ')))]) |
| 36 |
|
| 37 |
def get_loginuser(pid): |
| 38 |
uid = os.stat("/proc/%s" % pid).st_uid |
| 39 |
try: |
| 40 |
return pwd.getpwuid(uid).pw_name |
| 41 |
except OSError: |
| 42 |
return uid |
| 43 |
|
| 44 |
def check(path, pid): |
| 45 |
t = os.readlink(path) |
| 46 |
if t == mount_point or t.startswith(mount_point + "/"): |
| 47 |
unit = systemd_manager.GetUnitByPID(int(pid)) |
| 48 |
if unit not in results: |
| 49 |
unit_obj = bus.get_object('org.freedesktop.systemd1', unit) |
| 50 |
unit_id = unit_obj.Get("org.freedesktop.systemd1.Unit", "Id", |
| 51 |
dbus_interface="org.freedesktop.DBus.Properties") |
| 52 |
if unit_id.endswith(".scope"): |
| 53 |
stat = get_stat(pid) |
| 54 |
start = int(stat[21]) / os.sysconf('SC_CLK_TCK') |
| 55 |
results[pid] = {"pid": int(pid), |
| 56 |
"cmd": get_cmdline(pid), |
| 57 |
"comm": stat[1], |
| 58 |
"user": get_loginuser(pid), |
| 59 |
"since": time.clock_gettime(time.CLOCK_MONOTONIC) - start} |
| 60 |
else: |
| 61 |
desc = unit_obj.Get("org.freedesktop.systemd1.Unit", "Description", |
| 62 |
dbus_interface="org.freedesktop.DBus.Properties") |
| 63 |
timestamp = unit_obj.Get("org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", |
| 64 |
dbus_interface="org.freedesktop.DBus.Properties") |
| 65 |
results[unit] = {"unit": unit_id, |
| 66 |
"cmd": get_cmdline(pid), |
| 67 |
"desc": desc, |
| 68 |
"user": get_loginuser(pid), |
| 69 |
"since": time.time() - timestamp / 1e6} |
| 70 |
return True |
| 71 |
return False |
| 72 |
|
| 73 |
def checkdir(path, pid): |
| 74 |
for f in os.listdir(path): |
| 75 |
if check(os.path.join(path, f), pid): |
| 76 |
return True |
| 77 |
return False |
| 78 |
|
| 79 |
my_pid = os.getpid() |
| 80 |
|
| 81 |
for p in os.listdir("/proc/"): |
| 82 |
if not p.isdigit(): |
| 83 |
continue |
| 84 |
if int(p) == my_pid: |
| 85 |
continue |
| 86 |
proc = "/proc/%s/" % p |
| 87 |
try: |
| 88 |
if check(proc + "exe", p): |
| 89 |
continue |
| 90 |
if check(proc + "root", p): |
| 91 |
continue |
| 92 |
if check(proc + "cwd", p): |
| 93 |
continue |
| 94 |
if checkdir(proc + "fd", p): |
| 95 |
continue |
| 96 |
if checkdir(proc + "map_files", p): |
| 97 |
continue |
| 98 |
except OSError: |
| 99 |
pass |
| 100 |
|
| 101 |
return list(results.values()) |
| 102 |
|
| 103 |
|
| 104 |
def users(mount_point): |
| 105 |
data = fuser(mount_point) |
| 106 |
sys.stdout.write(json.dumps(data) + "\n") |
| 107 |
sys.stdout.flush() |
| 108 |
|
| 109 |
|
| 110 |
def stop_pids(pids): |
| 111 |
def sendsig(pid, sig): |
| 112 |
try: |
| 113 |
os.kill(pid, sig) |
| 114 |
return True |
| 115 |
except Exception: |
| 116 |
return False |
| 117 |
|
| 118 |
def sendsigs(pids, sig): |
| 119 |
for p in pids: |
| 120 |
sendsig(p, sig) |
| 121 |
|
| 122 |
def pollpids(pids, count, interval): |
| 123 |
while count > 0: |
| 124 |
i = 0 |
| 125 |
while i < len(pids): |
| 126 |
if not sendsig(pids[i], 0): |
| 127 |
pids.pop(i) |
| 128 |
else: |
| 129 |
i += 1 |
| 130 |
if len(pids) == 0: |
| 131 |
break |
| 132 |
count -= 1 |
| 133 |
time.sleep(interval) |
| 134 |
|
| 135 |
sendsigs(pids, signal.SIGTERM) |
| 136 |
sendsigs(pids, signal.SIGHUP) |
| 137 |
pollpids(pids, 10, 0.1) |
| 138 |
pollpids(pids, 19, 1) |
| 139 |
sendsigs(pids, signal.SIGKILL) |
| 140 |
pollpids(pids, 10, 0.1) |
| 141 |
|
| 142 |
|
| 143 |
def stop_units(units): |
| 144 |
if len(units) > 0: |
| 145 |
subprocess.check_call(['systemctl', 'stop', *units]) |
| 146 |
|
| 147 |
|
| 148 |
def stop(users): |
| 149 |
stop_pids([u["pid"] for u in users if "pid" in u]) |
| 150 |
stop_units([u["unit"] for u in users if "unit" in u]) |
| 151 |
|
| 152 |
|
| 153 |
def dispatch(argv): |
| 154 |
if argv[1] == "users": |
| 155 |
users(argv[2]) |
| 156 |
elif argv[1] == "stop": |
| 157 |
stop(json.loads(argv[2])) |
| 158 |
|
| 159 |
|
| 160 |
try: |
| 161 |
dispatch(sys.argv) |
| 162 |
except subprocess.CalledProcessError as e: |
| 163 |
sys.exit(e.returncode) |
| 164 |
except Exception as e: |
| 165 |
sys.stderr.write(str(e) + "\n") |
| 166 |
sys.exit(1) |