import subprocess import stat import os suid_prog = './suid' st = os.stat(suid_prog) if not (st.st_mode & stat.S_ISUID): raise Exception("File %s is not setuid!" % suid_prog) expected_uid = st.st_uid if os.geteuid() == expected_uid: raise Exception("Already have uid %d, setuid won't do anything!" % expected_uid) i = 1 while subprocess.call([suid_prog, str(expected_uid)]) == 0: i += 1 else: print 'Failed after %d tries!' % i