Moved into a sub-dir, so that a svn checkout has the same structure as
[rox-lib/lack.git] / ROX-Lib2 / python / rox / suchild.py
blob490158cb9ad6b0f678ae1af8e8ef4c0f95fec646
1 """This is the child program run by the su module. Do not import this module."""
2 import os, sys
3 import shutil
5 import proxy
7 read_watches = []
8 write_watches = []
9 streams = {}
11 class Watch:
12 """Contains a file descriptor and a function to call when it's ready"""
13 def __init__(self, fd, fn):
14 self.fd = fd
15 self.ready = fn
17 def fileno(self):
18 return self.fd
20 class SuSlave(proxy.SlaveProxy):
21 """A simple implementation of SlaveProxy that doesn't use gtk"""
22 def __init__(self, to_parent, from_parent, slave):
23 self.read_watch = Watch(from_parent, self.read_ready)
24 self.write_watch = Watch(to_parent, self.write_ready)
25 proxy.SlaveProxy.__init__(self, to_parent, from_parent, slave)
27 def enable_read_watch(self):
28 assert self.read_watch not in read_watches
29 read_watches.append(self.read_watch)
31 def enable_write_watch(self):
32 assert self.write_watch not in write_watches
33 write_watches.append(self.write_watch)
35 class Slave:
36 """This object runs as another user. Most methods behave in a similar
37 way to the standard python methods of the same name."""
39 def spawnvpe(self, mode, file, args, env = None):
40 if env is None:
41 return os.spawnvp(mode, file, args)
42 else:
43 return os.spawnvpe(mode, file, args, env)
45 def waitpid(self, pid, flags):
46 return os.waitpid(pid, flags)
48 def getuid(self):
49 return os.getuid()
51 def setuid(self, uid):
52 return os.setuid(uid)
54 def rmtree(self, path):
55 return shutil.rmtree(path)
57 def unlink(self, path):
58 return os.unlink(path)
60 def open(self, path, mode = 'r'):
61 stream = file(path, mode)
62 streams[id(stream)] = stream
63 return id(stream)
65 def close(self, stream):
66 streams[stream].close()
67 del streams[stream]
69 def read(self, stream, length = 0):
70 return streams[stream].read(length)
72 def write(self, stream, data):
73 return streams[stream].write(data)
75 def rename(self, old, new):
76 return os.rename(old, new)
78 def chmod(self, path, mode):
79 return os.chmod(path, mode)
81 if __name__ == '__main__':
82 from select import select
84 to_parent, from_parent = map(int, sys.argv[1:])
86 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))
88 slave_proxy = SuSlave(to_parent, from_parent, Slave())
90 while read_watches or write_watches:
91 readable, writable = select(read_watches, write_watches, [])[:2]
92 for w in readable:
93 if not w.ready():
94 read_watches.remove(w)
95 for w in writable:
96 if not w.ready():
97 write_watches.remove(w)