Fix to let documentation build.
[rox-lib.git] / python / rox / suchild.py
blobdcd79b85544d08e3f30f4a2558fee3261860d3ad
1 """This is the child program run by the su module. Do not import this module."""
2 import os, sys
3 import cPickle as pickle
4 import time
5 import shutil
7 import proxy
9 read_watches = []
10 write_watches = []
12 class Watch:
13 """Contains a file descriptor and a function to call when it's ready"""
14 def __init__(self, fd, fn):
15 self.fd = fd
16 self.ready = fn
18 def fileno(self):
19 return self.fd
21 class SuSlave(proxy.SlaveProxy):
22 """A simple implementation of SlaveProxy that doesn't use gtk"""
23 def __init__(self, to_parent, from_parent, slave):
24 self.read_watch = Watch(from_parent, self.read_ready)
25 self.write_watch = Watch(to_parent, self.write_ready)
26 proxy.SlaveProxy.__init__(self, to_parent, from_parent, slave)
28 def enable_read_watch(self):
29 assert self.read_watch not in read_watches
30 read_watches.append(self.read_watch)
32 def enable_write_watch(self):
33 assert self.write_watch not in write_watches
34 write_watches.append(self.write_watch)
36 class Slave:
37 """This object runs as another user. Most methods behave in a similar
38 way to the standard python methods of the same name."""
40 def spawnvpe(self, request, mode, file, args, env = None):
41 if env is None:
42 request.send(os.spawnvp(mode, file, args))
43 else:
44 request.send(os.spawnvpe(mode, file, args, env))
46 def waitpid(self, request, pid, flags):
47 request.send(os.waitpid(pid, flags))
49 def getuid(self, request):
50 request.send(os.getuid())
52 def setuid(self, request, uid):
53 request.send(os.setuid(uid))
55 def rmtree(self, request, path):
56 shutil.rmtree(path)
57 request.send(None)
59 if __name__ == '__main__':
60 from select import select
62 to_parent, from_parent = map(int, sys.argv[1:])
64 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))
66 slave_proxy = SuSlave(to_parent, from_parent, Slave())
68 while read_watches or write_watches:
69 readable, writable = select(read_watches, write_watches, [])[:2]
70 for w in readable:
71 if not w.ready():
72 read_watches.remove(w)
73 for w in writable:
74 if not w.ready():
75 write_watches.remove(w)