1 """This is the child program run by the su module. Do not import this module."""
3 import cPickle
as pickle
14 """Contains a file descriptor and a function to call when it's ready"""
15 def __init__(self
, fd
, fn
):
22 class SuSlave(proxy
.SlaveProxy
):
23 """A simple implementation of SlaveProxy that doesn't use gtk"""
24 def __init__(self
, to_parent
, from_parent
, slave
):
25 self
.read_watch
= Watch(from_parent
, self
.read_ready
)
26 self
.write_watch
= Watch(to_parent
, self
.write_ready
)
27 proxy
.SlaveProxy
.__init
__(self
, to_parent
, from_parent
, slave
)
29 def enable_read_watch(self
):
30 assert self
.read_watch
not in read_watches
31 read_watches
.append(self
.read_watch
)
33 def enable_write_watch(self
):
34 assert self
.write_watch
not in write_watches
35 write_watches
.append(self
.write_watch
)
38 """This object runs as another user. Most methods behave in a similar
39 way to the standard python methods of the same name."""
41 def spawnvpe(self
, mode
, file, args
, env
= None):
43 return os
.spawnvp(mode
, file, args
)
45 return os
.spawnvpe(mode
, file, args
, env
)
47 def waitpid(self
, pid
, flags
):
48 return os
.waitpid(pid
, flags
)
53 def setuid(self
, uid
):
56 def rmtree(self
, path
):
57 return shutil
.rmtree(path
)
59 def unlink(self
, path
):
60 return os
.unlink(path
)
62 def open(self
, path
, mode
= 'r'):
63 stream
= file(path
, mode
)
64 streams
[id(stream
)] = stream
67 def close(self
, stream
):
68 streams
[stream
].close()
71 def read(self
, stream
, length
= 0):
72 return streams
[stream
].read(length
)
74 def write(self
, stream
, data
):
75 return streams
[stream
].write(data
)
77 def rename(self
, old
, new
):
78 return os
.rename(old
, new
)
80 def chmod(self
, path
, mode
):
81 return os
.chmod(path
, mode
)
83 if __name__
== '__main__':
84 from select
import select
86 to_parent
, from_parent
= map(int, sys
.argv
[1:])
88 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0]))))
90 slave_proxy
= SuSlave(to_parent
, from_parent
, Slave())
92 while read_watches
or write_watches
:
93 readable
, writable
= select(read_watches
, write_watches
, [])[:2]
96 read_watches
.remove(w
)
99 write_watches
.remove(w
)