1 """This is the child program run by the su module. Do not import this module."""
12 """Contains a file descriptor and a function to call when it's ready"""
13 def __init__(self
, fd
, fn
):
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
)
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):
41 return os
.spawnvp(mode
, file, args
)
43 return os
.spawnvpe(mode
, file, args
, env
)
45 def waitpid(self
, pid
, flags
):
46 return os
.waitpid(pid
, flags
)
51 def setuid(self
, 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
65 def close(self
, stream
):
66 streams
[stream
].close()
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]
94 read_watches
.remove(w
)
97 write_watches
.remove(w
)