#!/usr/bin/env python # Copyright: 2006 Gentoo Foundation # License: GPL2 # $Id:$ # # Zac Medico # import portage_locks import atexit, errno, os, random, signal, sys def locker_process(data_file): try: while True: mylock = portage_locks.lockfile(data_file, wantnewlockfile=1) rand = random.random() rand_str = repr(rand) f = open(data_file, "w") f.write(rand_str) f.close() f = open(data_file) s = f.read() f.close() portage_locks.unlockfile(mylock) if rand_str != s: print "%s!=%s" % (rand_str,s) break except KeyboardInterrupt: pass # clean up the lock file and data file mylock = portage_locks.lockfile(data_file, wantnewlockfile=1) try: os.unlink(data_file) except OSError, oe: if oe.errno == errno.ENOENT: pass else: raise oe portage_locks.unlockfile(mylock) os._exit(0) if __name__ == "__main__": fork_count = 2 data_file = "/dev/shm/aux_db_key_temp" if len(sys.argv) > 1: data_file = sys.argv[1] spawned_pids = [] def cleanup(): print "cleaning up..." while spawned_pids: pid = spawned_pids.pop() try: if os.waitpid(pid, os.WNOHANG) == (0, 0): os.kill(pid, signal.SIGTERM) os.waitpid(pid, 0) except OSError, oe: if oe.errno == errno.ESRCH: pass else: raise oe atexit.register(cleanup) def child_signal_handler(signum, frame): signal.signal(signal.SIGCHLD, signal.SIG_IGN) sys.exit(1) signal.signal(signal.SIGCHLD, child_signal_handler) for i in xrange(fork_count): pid = os.fork() if pid == 0: locker_process(data_file) else: spawned_pids.append(pid) try: os.wait() except KeyboardInterrupt: pass