Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nemu3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
nemu3
Commits
b17d2af5
Commit
b17d2af5
authored
Jul 01, 2010
by
Martín Ferrari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Subprocess class and tests, fix some other stuff
parent
bfb3a065
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
16 deletions
+86
-16
src/netns/node.py
src/netns/node.py
+1
-0
src/netns/subprocess.py
src/netns/subprocess.py
+48
-6
t/test_subprocess.py
t/test_subprocess.py
+37
-10
No files found.
src/netns/node.py
View file @
b17d2af5
...
...
@@ -64,6 +64,7 @@ def _start_child(debug = False):
s1
.
close
()
return
(
s0
,
pid
)
# FIXME: clean up signal handers, atexit functions, etc.
try
:
s0
.
close
()
srv
=
netns
.
protocol
.
Server
(
s1
,
debug
)
...
...
src/netns/subprocess.py
View file @
b17d2af5
...
...
@@ -76,17 +76,17 @@ def spawn(executable, argv = None, cwd = None, env = None,
if
cwd
!=
None
:
os
.
chdir
(
cwd
)
if
not
argv
:
argv
=
[
fi
le
]
if
'/'
in
fi
le
:
# Should not search in PATH
argv
=
[
executab
le
]
if
'/'
in
executab
le
:
# Should not search in PATH
if
env
!=
None
:
os
.
execve
(
fi
le
,
argv
,
env
)
os
.
execve
(
executab
le
,
argv
,
env
)
else
:
os
.
execv
(
fi
le
,
argv
)
os
.
execv
(
executab
le
,
argv
)
else
:
# use PATH
if
env
!=
None
:
os
.
execvpe
(
fi
le
,
argv
,
env
)
os
.
execvpe
(
executab
le
,
argv
,
env
)
else
:
os
.
execvp
(
fi
le
,
argv
)
os
.
execvp
(
executab
le
,
argv
)
raise
RuntimeError
(
"Unreachable reached!"
)
except
:
try
:
...
...
@@ -119,6 +119,7 @@ def spawn(executable, argv = None, cwd = None, env = None,
os
.
waitpid
(
pid
,
0
)
exc
=
pickle
.
loads
(
s
)
# XXX: sys.excepthook
#print exc.child_traceback
raise
exc
# Used to print extra info in nested exceptions
...
...
@@ -145,3 +146,44 @@ def wait(pid):
"""Wait for process to die and return the exit code."""
return
os
.
waitpid
(
pid
,
0
)[
1
]
class
Subprocess
(
object
):
# FIXME: this is the visible interface; documentation should move here.
"""OO-style interface to spawn(), but invoked through the controlling
process."""
# FIXME
default_user
=
None
def
__init__
(
self
,
node
,
executable
,
argv
=
None
,
cwd
=
None
,
env
=
None
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
user
=
None
):
self
.
_slave
=
node
.
_slave
if
user
==
None
:
user
=
Subprocess
.
default_user
# confusingly enough, to go to the function at the top of this file,
# I need to call it thru the communications protocol: remember that
# happens in another process!
self
.
_pid
=
self
.
_slave
.
spawn
(
executable
,
argv
=
argv
,
cwd
=
cwd
,
env
=
env
,
stdin
=
stdin
,
stdout
=
stdout
,
stderr
=
stderr
,
user
=
user
)
node
.
_add_subprocess
(
self
)
@
property
def
pid
(
self
):
return
self
.
_pid
def
poll
(
self
):
r
=
self
.
_slave
.
poll
(
self
.
_pid
)
if
r
!=
None
:
del
self
.
_pid
self
.
return_value
=
r
return
r
def
wait
(
self
):
r
=
self
.
_slave
.
wait
(
self
.
_pid
)
del
self
.
_pid
self
.
return_value
=
r
return
r
def
signal
(
self
,
sig
=
signal
.
SIGTERM
):
return
self
.
_slave
.
signal
(
self
.
_pid
,
sig
)
t/test_subprocess.py
View file @
b17d2af5
...
...
@@ -54,37 +54,64 @@ class TestSubprocess(unittest.TestCase):
# XXX: unittest still cannot skip tests
#@unittest.skipUnless(os.getuid() == 0, "Test requires root privileges")
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_
pope
n_chuser
(
self
):
def
test_
spaw
n_chuser
(
self
):
user
=
'nobody'
pid
=
netns
.
subprocess
.
spawn
(
user
,
'/bin/sleep'
,
[
'/bin/sleep'
,
'100'
])
pid
=
netns
.
subprocess
.
spawn
(
'/bin/sleep'
,
[
'/bin/sleep'
,
'100'
],
user
=
user
)
self
.
_check_ownership
(
user
,
pid
)
os
.
kill
(
pid
,
signal
.
SIGTERM
)
self
.
assertEquals
(
netns
.
subprocess
.
wait
(
pid
),
signal
.
SIGTERM
)
def
test_
pope
n_basic
(
self
):
def
test_
spaw
n_basic
(
self
):
# User does not exist
self
.
assertRaises
(
ValueError
,
netns
.
subprocess
.
spawn
,
self
.
nouser
,
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
]
)
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouser
)
self
.
assertRaises
(
ValueError
,
netns
.
subprocess
.
spawn
,
self
.
nouid
,
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
]
)
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouid
)
# Invalid CWD: it is a file
self
.
assertRaises
(
OSError
,
netns
.
subprocess
.
spawn
,
None
,
'/bin/sleep'
,
None
,
cwd
=
'/bin/sleep'
)
'/bin/sleep'
,
cwd
=
'/bin/sleep'
)
# Invalid CWD: does not exist
self
.
assertRaises
(
OSError
,
netns
.
subprocess
.
spawn
,
None
,
'/bin/sleep'
,
None
,
cwd
=
self
.
nofile
)
'/bin/sleep'
,
cwd
=
self
.
nofile
)
# Exec failure
self
.
assertRaises
(
OSError
,
netns
.
subprocess
.
spawn
,
None
,
self
.
nofile
,
None
)
self
.
assertRaises
(
OSError
,
netns
.
subprocess
.
spawn
,
self
.
nofile
)
# Test that the environment is cleared: sleep should not be found
# XXX: This should be a python bug: if I don't set PATH explicitly, it
# uses a default search path
self
.
assertRaises
(
OSError
,
netns
.
subprocess
.
spawn
,
None
,
'sleep'
,
None
,
env
=
{
'PATH'
:
''
})
'sleep'
,
env
=
{
'PATH'
:
''
})
#p = netns.subprocess.spawn(None, '/bin/sleep', ['/bin/sleep', '1000'],
# cwd = '/', env = [])
# FIXME: tests fds
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_Subprocess_basic
(
self
):
node
=
netns
.
Node
()
# User does not exist
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouser
)
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
'/bin/sleep'
,
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouid
)
# Invalid CWD: it is a file
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
'/bin/sleep'
,
cwd
=
'/bin/sleep'
)
# Invalid CWD: does not exist
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
'/bin/sleep'
,
cwd
=
self
.
nofile
)
# Exec failure
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
self
.
nofile
)
# Test that the environment is cleared: sleep should not be found
# XXX: This should be a python bug: if I don't set PATH explicitly, it
# uses a default search path
self
.
assertRaises
(
RuntimeError
,
netns
.
subprocess
.
Subprocess
,
node
,
'sleep'
,
env
=
{
'PATH'
:
''
})
#p = netns.subprocess.Subprocess(None, '/bin/sleep', ['/bin/sleep', '1000'], cwd = '/', env = [])
# FIXME: tests fds
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment