2 # -*- coding: utf-8 -*-
4 """Script for restarting services that refer to old/deleted libs."""
6 from __future__ import print_function
16 # Set of paths that are "OK" and safe to ignore.
24 """Find all programs w/deleted paths."""
26 for pid in os.listdir('/proc'):
32 map = '/proc/%s/maps' % pid
33 if not os.path.exists(map):
34 print('skipping %s' % pid)
38 for line in open(map):
39 if not line.endswith(' (deleted)\n'):
41 # b71c7000-b7307000 rw-s 00000000 00:04 17024337 /dev/zero (deleted)
42 addr, perm, offset, dev, inode, path = line.split(' ', 5)
43 # Handle paths with spaces.
44 path = path.rsplit(' ', 2)[0]
45 if (path == '/[aio]' or
46 path.startswith('/SYSV') or
47 path.startswith('/dev/shm/')):
51 old_paths -= IGNORE_PATHS
55 cmdline = open('/proc/%s/cmdline' % pid).read().split('\0')
63 'old_paths': old_paths,
68 # Mapping of known programs to their init.d scripts.
70 '/usr/sbin/acpid': 'acpid',
71 '/usr/sbin/apache2': 'apache2',
72 '/usr/sbin/atd': 'atd',
73 '/usr/sbin/bacula-fd': 'bacula-fd',
74 '/usr/sbin/cron': 'vixie-cron',
75 '/usr/sbin/crond': 'dcron',
76 '/usr/sbin/snmpd': 'snmpd',
77 '/usr/sbin/sshd': 'sshd',
78 '/usr/sbin/syslog-ng': 'syslog-ng',
79 '/usr/sbin/xinetd': 'xinetd',
80 '/usr/bin/distccd': 'distccd',
81 '/usr/bin/monit': 'monit',
82 '/usr/bin/stunnel': 'stunnel',
83 '/usr/bin/tor': 'tor',
84 '/usr/bin/transmission-daemon': 'transmission-daemon',
85 '/usr/bin/mediatomb': 'mediatomb',
86 '/lib/systemd/systemd-udevd': 'udev',
87 '/usr/libexec/nrpe': 'nrpe',
88 '/usr/libexec/postfix/master': 'postfix',
89 'denyhosts.py': 'denyhosts',
90 'dropbear': 'dropbear',
92 'tlsdated': 'tlsdated',
94 def auto_restart(svcs):
97 for pid, svc in svcs.items():
98 if svc['cmdline'][0] == '/sbin/agetty':
100 elif 'postgres:' in svc['cmdline'][0]:
101 p = os.path.basename(glob.glob('/etc/runlevels/default/postgresql-*')[0])
103 elif svc['cmdline'][0].startswith('metalog'):
104 restart.add('metalog')
106 prog = svc['cmdline'][0]
107 if prog.startswith('/usr/bin/python'):
108 prog = os.path.basename(svc['cmdline'][1])
110 init = SERVICES.get(prog)
116 print('killing %s (%s)' % (pid, svcs[pid]['cmdline'][0]))
117 os.kill(pid, signal.SIGTERM)
119 print('restarting %s' % init)
120 os.system('/etc/init.d/%s -q restart' % init)
131 for pid in svcs.keys():
132 if svcs[pid]['cmdline'][0] == '/usr/sbin/sslh':
136 print('sslh needs restart')
139 for pid, svc in svcs.items():
140 print(pid, svc['cmdline'])
141 print('\t%s' % '\n\t'.join(svc['old_paths']))
145 parser = argparse.ArgumentParser(description=__doc__)
150 parser = get_parser()
151 opts = parser.parse_args(argv)
154 svcs = auto_restart(svcs)
158 if __name__ == '__main__':
159 exit(main(sys.argv[1:]))