]> git.wh0rd.org - home.git/blob - .bin/gentoo-check-services
gentoo-check-services: handle samba
[home.git] / .bin / gentoo-check-services
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """Script for restarting services that refer to old/deleted libs."""
5
6 from __future__ import print_function
7
8 import argparse
9 import glob
10 import os
11 import signal
12 import sys
13 import time
14
15
16 # Set of paths that are "OK" and safe to ignore.
17 IGNORE_PATHS = set((
18 '/dev/zero',
19 '/etc/ld.so.cache',
20 '/usr/share/icons/Adwaita/icon-theme.cache',
21 '/usr/share/icons/gnome/icon-theme.cache',
22 '/usr/share/icons/hicolor/icon-theme.cache',
23 '/usr/share/mime/mime.cache',
24 ))
25
26
27 def find_svcs():
28 """Find all programs w/deleted paths."""
29 svcs = {}
30 for pid in os.listdir('/proc'):
31 try:
32 pid_nr = int(pid)
33 except ValueError:
34 continue
35
36 map = '/proc/%s/maps' % pid
37 if not os.path.exists(map):
38 print('skipping %s' % pid)
39 continue
40
41 old_paths = set()
42 for line in open(map):
43 if not line.endswith(' (deleted)\n'):
44 continue
45 # b71c7000-b7307000 rw-s 00000000 00:04 17024337 /dev/zero (deleted)
46 addr, perm, offset, dev, inode, path = line.split(' ', 5)
47 # Handle paths with spaces.
48 path = path.lstrip().rsplit(' ', 2)[0]
49 if (path == '/[aio]' or
50 path.startswith('/memfd:') or
51 path.startswith('/SYSV') or
52 path.startswith('/dev/shm/') or
53 path.startswith('/tmp/')):
54 continue
55 old_paths.add(path)
56
57 old_paths -= IGNORE_PATHS
58 if not old_paths:
59 continue
60
61 cmdline = open('/proc/%s/cmdline' % pid).read().split('\0')
62 try:
63 while True:
64 cmdline.remove('')
65 except ValueError:
66 pass
67 svcs[pid_nr] = {
68 'cmdline': cmdline,
69 'old_paths': old_paths,
70 }
71 return svcs
72
73
74 # Mapping of known programs to their init.d scripts.
75 SERVICES = {
76 '/usr/sbin/acpid': 'acpid',
77 '/usr/sbin/apache2': 'apache2',
78 '/usr/sbin/atd': 'atd',
79 '/usr/sbin/bacula-fd': 'bacula-fd',
80 '/usr/sbin/cron': 'vixie-cron',
81 '/usr/sbin/crond': 'dcron',
82 '/usr/sbin/ntpd': 'ntpd',
83 '/usr/sbin/snmpd': 'snmpd',
84 '/usr/sbin/sshd': 'sshd',
85 '/usr/sbin/syslog-ng': 'syslog-ng',
86 '/usr/sbin/xinetd': 'xinetd',
87 '/usr/bin/daisydog': 'daisydog',
88 '/usr/bin/distccd': 'distccd',
89 '/usr/bin/monit': 'monit',
90 '/usr/bin/rsync': 'rsyncd',
91 '/usr/sbin/smbd': 'samba',
92 '/usr/bin/stunnel': 'stunnel',
93 '/usr/bin/tor': 'tor',
94 '/usr/bin/transmission-daemon': 'transmission-daemon',
95 '/usr/bin/mediatomb': 'mediatomb',
96 '/lib/systemd/systemd-udevd': 'udev',
97 '/usr/libexec/nrpe': 'nrpe',
98 '//usr/libexec/postfix/master': 'postfix',
99 'denyhosts.py': 'denyhosts',
100 'dropbear': 'dropbear',
101 'galileo': 'galileo',
102 'tlsdated': 'tlsdated',
103 }
104 def auto_restart(opts, svcs):
105 kill = set()
106 restart = set()
107 for pid, svc in svcs.items():
108 if svc['cmdline'][0] == '/sbin/agetty':
109 kill.add(pid)
110 elif 'postgres:' in svc['cmdline'][0]:
111 p = os.path.basename(glob.glob('/etc/runlevels/default/postgresql-*')[0])
112 restart.add(p)
113 elif svc['cmdline'][0].startswith('metalog'):
114 restart.add('metalog')
115 else:
116 prog = svc['cmdline'][0]
117 if prog.startswith('/usr/bin/python'):
118 prog = os.path.basename(svc['cmdline'][1])
119
120 init = SERVICES.get(prog)
121 if init:
122 restart.add(init)
123
124 if kill or restart:
125 for pid in kill:
126 print('killing %s (%s)' % (pid, svcs[pid]['cmdline'][0]))
127 if not opts.dryrun:
128 os.kill(pid, signal.SIGTERM)
129 for init in restart:
130 print('restarting %s' % init)
131 if not opts.dryrun:
132 os.system('/etc/init.d/%s -q restart' % init)
133
134 if not opts.dryrun:
135 time.sleep(1)
136 svcs = find_svcs()
137
138 return svcs
139
140
141 def summarize(svcs):
142 print()
143 sslh = False
144 for pid in svcs.keys():
145 if svcs[pid]['cmdline'][0] == '/usr/sbin/sslh':
146 del svcs[pid]
147 sslh = True
148 if sslh:
149 print('sslh needs restart')
150
151 print()
152 for pid, svc in svcs.items():
153 print(pid, svc['cmdline'])
154 print('\t%s' % '\n\t'.join(svc['old_paths']))
155
156
157 def get_parser():
158 parser = argparse.ArgumentParser(description=__doc__)
159 parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
160 help='Show what would be restarted (and why)')
161 return parser
162
163
164 def main(argv):
165 parser = get_parser()
166 opts = parser.parse_args(argv)
167
168 svcs = find_svcs()
169 svcs = auto_restart(opts, svcs)
170 summarize(svcs)
171
172
173 if __name__ == '__main__':
174 exit(main(sys.argv[1:]))