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/rsync': 'rsyncd',
86 '/usr/bin/stunnel': 'stunnel',
87 '/usr/bin/tor': 'tor',
88 '/usr/bin/transmission-daemon': 'transmission-daemon',
89 '/usr/bin/mediatomb': 'mediatomb',
90 '/lib/systemd/systemd-udevd': 'udev',
91 '/usr/libexec/nrpe': 'nrpe',
92 '//usr/libexec/postfix/master': 'postfix',
93 'denyhosts.py': 'denyhosts',
94 'dropbear': 'dropbear',
96 'tlsdated': 'tlsdated',
98 def auto_restart(opts, svcs):
101 for pid, svc in svcs.items():
102 if svc['cmdline'][0] == '/sbin/agetty':
104 elif 'postgres:' in svc['cmdline'][0]:
105 p = os.path.basename(glob.glob('/etc/runlevels/default/postgresql-*')[0])
107 elif svc['cmdline'][0].startswith('metalog'):
108 restart.add('metalog')
110 prog = svc['cmdline'][0]
111 if prog.startswith('/usr/bin/python'):
112 prog = os.path.basename(svc['cmdline'][1])
114 init = SERVICES.get(prog)
120 print('killing %s (%s)' % (pid, svcs[pid]['cmdline'][0]))
122 os.kill(pid, signal.SIGTERM)
124 print('restarting %s' % init)
126 os.system('/etc/init.d/%s -q restart' % init)
138 for pid in svcs.keys():
139 if svcs[pid]['cmdline'][0] == '/usr/sbin/sslh':
143 print('sslh needs restart')
146 for pid, svc in svcs.items():
147 print(pid, svc['cmdline'])
148 print('\t%s' % '\n\t'.join(svc['old_paths']))
152 parser = argparse.ArgumentParser(description=__doc__)
153 parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
154 help='Show what would be restarted (and why)')
159 parser = get_parser()
160 opts = parser.parse_args(argv)
163 svcs = auto_restart(opts, svcs)
167 if __name__ == '__main__':
168 exit(main(sys.argv[1:]))