os.execv

Donn Cave donn at drizzle.com
Wed Mar 5 22:51:53 EST 2003


Quoth Nick Coghlan <ncoghlan at email.com>:
| Ulrich Petri wrote:
|> "Skip Montanaro" <skip at pobox.com> schrieb im Newsbeitrag
|> news:mailman.1046828168.14751.python-list at python.org...
|> 
|>>    >>> import os
|>>    >>> os.execv("/usr/bin/find", ("find", "/ -name test"))
|>>    find: / -name test: No such file or directory
|>>
|>> Maybe
|>>
|>>    os.execv("/usr/bin/find", "find", "/", "-name", "test")
|>>
|
| I don't have an interpreter handy, but wouldn't the version below achieve the 
| 'split literal on whitespace" effect?
|
|  >>> os.execv("/usr/bin/find", *"find / -name test".split())

Close.  Skip was probably thinking of execl, which would need that "*".
For execv, the 2nd parameter should be a list.  But of course as already
discussed, this should not be mistaken for a useful thing.

I expect the usual way to do this kind of thing is

    os.execv("/bin/sh", ["sh", "-c", command_string])

That solves path problems, and gets the command parsed the way
its author probably expects.  It's dangerous - if that command was
generated using user input data, then it becomes quite easy for that
user to run anything he or she wants under the ID of your program.
In cases like that, a direct execv (or spawnv as opposed to system)
is safe inasmuch as the command is not subject to shell interpretation.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list