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.lstrip().rsplit(' ', 2)[0]
45 if (path == '/[aio]' or
46 path.startswith('/SYSV') or
47 path.startswith('/dev/shm/') or
48 path.startswith('/tmp/')):
52 old_paths -= IGNORE_PATHS
56 cmdline = open('/proc/%s/cmdline' % pid).read().split('\0')
64 'old_paths': old_paths,
69 # Mapping of known programs to their init.d scripts.
71 '/usr/sbin/acpid': 'acpid',
72 '/usr/sbin/apache2': 'apache2',
73 '/usr/sbin/atd': 'atd',
74 '/usr/sbin/bacula-fd': 'bacula-fd',
75 '/usr/sbin/cron': 'vixie-cron',
76 '/usr/sbin/crond': 'dcron',
77 '/usr/sbin/ntpd': 'ntpd',
78 '/usr/sbin/snmpd': 'snmpd',
79 '/usr/sbin/sshd': 'sshd',
80 '/usr/sbin/syslog-ng': 'syslog-ng',
81 '/usr/sbin/xinetd': 'xinetd',
82 '/usr/bin/daisydog': 'daisydog',
83 '/usr/bin/distccd': 'distccd',
84 '/usr/bin/monit': 'monit',
85 '/usr/bin/stunnel': 'stunnel',
86 '/usr/bin/tor': 'tor',
87 '/usr/bin/transmission-daemon': 'transmission-daemon',
88 '/usr/bin/mediatomb': 'mediatomb',
89 '/lib/systemd/systemd-udevd': 'udev',
90 '/usr/libexec/nrpe': 'nrpe',
91 '//usr/libexec/postfix/master': 'postfix',
92 'denyhosts.py': 'denyhosts',
93 'dropbear': 'dropbear',
95 'tlsdated': 'tlsdated',
97 def auto_restart(opts, svcs):
100 for pid, svc in svcs.items():
101 if svc['cmdline'][0] == '/sbin/agetty':
103 elif 'postgres:' in svc['cmdline'][0]:
104 p = os.path.basename(glob.glob('/etc/runlevels/default/postgresql-*')[0])
106 elif svc['cmdline'][0].startswith('metalog'):
107 restart.add('metalog')
109 prog = svc['cmdline'][0]
110 if prog.startswith('/usr/bin/python'):
111 prog = os.path.basename(svc['cmdline'][1])
113 init = SERVICES.get(prog)
119 print('killing %s (%s)' % (pid, svcs[pid]['cmdline'][0]))
121 os.kill(pid, signal.SIGTERM)
123 print('restarting %s' % init)
125 os.system('/etc/init.d/%s -q restart' % init)
137 for pid in svcs.keys():
138 if svcs[pid]['cmdline'][0] == '/usr/sbin/sslh':
142 print('sslh needs restart')
145 for pid, svc in svcs.items():
146 print(pid, svc['cmdline'])
147 print('\t%s' % '\n\t'.join(svc['old_paths']))
151 parser = argparse.ArgumentParser(description=__doc__)
152 parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
153 help='Show what would be restarted (and why)')
158 parser = get_parser()
159 opts = parser.parse_args(argv)
162 svcs = auto_restart(opts, svcs)
166 if __name__ == '__main__':
167 exit(main(sys.argv[1:]))