diff options
author | Michael Stone <michael@laptop.org> | 2009-12-06 00:23:38 (GMT) |
---|---|---|
committer | Michael Stone <michael@laptop.org> | 2009-12-06 00:30:49 (GMT) |
commit | ffd4bc469644caf8a28cd9244cd71c1f69ad93b3 (patch) | |
tree | 9dc3b68b7fd3c29aaeab5fc7ca7d9ac159e47b06 | |
parent | bfd5c0286da8c1669cba9136c013e258c4394e3a (diff) |
First pass at updated rainbow-gc.
-rwxr-xr-x | rainbow/bin/rainbow-gc | 84 | ||||
-rw-r--r-- | rainbow/rainbow/gc.py | 50 |
2 files changed, 84 insertions, 50 deletions
diff --git a/rainbow/bin/rainbow-gc b/rainbow/bin/rainbow-gc new file mode 100755 index 0000000..22e9e3e --- /dev/null +++ b/rainbow/bin/rainbow-gc @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +import sys + +from subprocess import call, check_call, CalledProcessError +from os import listdir, unlink +from os.path import join, isdir, islink, exists +from optparse import OptionParser + +from rainbow.util import trace, make_reporter + +def active_uid(uid): + cmd = ['/usr/bin/pgrep', '-U', uid] + ret = call(cmd, stdout=open('/dev/null')) + if ret in (0, 1): + return ret == 0 + raise CalledProcessError(ret, cmd) + +def gc_uid(spool, uid): + """This function conservatively attempts to garbage-collect stale uid + reservations. + """ + + # XXX: D-Bus caches passwd-db data! + # XXX: D-Bus uses a fixed 1k passwd buffer - be careful with long paths & + # comments + + reservation = join(spool, 'uid_pool', uid) + assert not isdir(reservation) and not islink(reservation) + + # XXX: We perform several execv()'s as root based on strings derived from + # this 'uid' parameter, which originates as a file-name in a user-writable + # directory. Better ideas for input validation would be welcome. + uid_num = int(uid) + assert uid_num >= 1000 and uid_num <= 65534 # XXX: magic numbers from util/spool.py + + if active_uid(uid): + return + + for table in ('uid_to_instance_dir', 'uid_to_home_dir', 'uid_to_gid'): + row = join(spool, table, uid) + # NB: it is important that rm -rf doesn't follow links. <MS> + check_call(['/bin/rm', '-r', '-f', row]) + + # So long as we unlink the reservation last, we run no risk of seeing inconsistency + unlink(reservation) + +def gc_spool(log, spool): + ret = 0 + uspool = join(spool, 'uid_pool') + if exists(uspool) and isdir(uspool): + for maybe_uid in listdir(uspool): + try: gc_uid(spool, maybe_uid) + except: + trace() + ret = 1 + else: + log(1, "Skipping spool %s", spool) + ret = 1 + return ret + +def main(): + sys.excepthook = trace + + parser = OptionParser(version='0.1') + parser.add_option('-v', '--verbose', default=0, action='count', + help='Verbosity. Repeat for more verbose output.') + parser.add_option('-q', '--quiet', default=False, action='store_true', + help='Quiet. Disable all output.') + parser.add_option('-s', '--spool', default=None, + help='Location of the rainbow spool.') + + opts, _ = parser.parse_args() + + report = make_reporter(opts.verbose, opts.quiet, sys.stdout) + + def check_spool(opts): + return opts.spool + + spool = check_spool(opts) + return gc_spool(report, spool) + +if __name__ == "__main__": + exit(main()) diff --git a/rainbow/rainbow/gc.py b/rainbow/rainbow/gc.py deleted file mode 100644 index 2198367..0000000 --- a/rainbow/rainbow/gc.py +++ /dev/null @@ -1,50 +0,0 @@ -from subprocess import call, check_call, CalledProcessError -from os import listdir, unlink -from os.path import join, isdir, islink, exists -from rainbow.util import trace - -def active_uid(uid): - cmd = ['/usr/bin/pgrep', '-U', uid] - ret = call(cmd, stdout=open('/dev/null')) - if ret in (0, 1): - return ret == 0 - raise CalledProcessError(ret, cmd) - -def gc_uid(spool, uid): - """This function conservatively attempts to garbage-collect stale uid - reservations. - """ - - # XXX: D-Bus caches passwd-db data! - # XXX: D-Bus uses a fixed 1k passwd buffer - be careful with long paths & - # comments - - reservation = join(spool, 'uid_pool', uid) - assert not isdir(reservation) and not islink(reservation) - - # XXX: We perform several execv()'s as root based on strings derived from - # this 'uid' parameter, which originates as a file-name in a user-writable - # directory. Better ideas for input validation would be welcome. - uid_num = int(uid) - assert uid_num >= 1000 and uid_num <= 65534 # XXX: magic numbers from util/spool.py - - if active_uid(uid): - return - - for table in ('uid_to_instance_dir', 'uid_to_home_dir', 'uid_to_gid'): - row = join(spool, table, uid) - # NB: it is important that rm -rf doesn't follow links. <MS> - check_call(['/bin/rm', '-r', '-f', row]) - - # So long as we unlink the reservation last, we run no risk of seeing inconsistency - unlink(reservation) - -def gc_spool(log, spool): - uspool = join(spool, 'uid_pool') - if exists(uspool) and isdir(uspool): - for maybe_uid in listdir(uspool): - try: gc_uid(spool, maybe_uid) - except: trace() - else: - log(1, "Skipping spool %s", spool) - |