Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > System() question

Reply
Thread Tools

System() question

 
 
Bob
Guest
Posts: n/a
 
      12-07-2007
On 7 Dec 2007 at 7:45, Keith Thompson wrote:
> Bob <(E-Mail Removed)> writes:
>> Why does a call to system() work by passing the command line string
>> supplied to sh -c? This is causing problems when running in a chroot
>> environment where no sh is available. Wouldn't it be better for C to
>> replace the system() call by a fork()-exec() pair? Is there some
>> compiler option that can change the behavior of system()?

>
> The C standard doesn't specify sh -c; it merely requires the string to
> be passed to a "command processor". (Also standard C doesn't specify
> fork or exec.)
>
> POSIX specifies "sh -c". You might try asking in
> comp.unix.programmer. (Note that the job of splitting the command's
> arguments on whitespace, handling wildcards, etc. is passed off to the
> shell; a function that uses fork/exec directly would have to reproduce
> that functionality.)


But wouldn't it be better to first try "sh -c" and then if no shell is
available fall back to fork-exec, rather than failing the system() call?

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      12-07-2007
In article <(E-Mail Removed)>,
Bob <(E-Mail Removed)> wrote:
>But wouldn't it be better to first try "sh -c" and then if no shell is
>available fall back to fork-exec, rather than failing the system() call?


Only if the fork()/exec() version does exactly the same thing;
executing the wrong command could be disastrous.

And if it does do exactly the same thing, why not use it in the first
place?

-- Richard
--
:wq
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      12-07-2007
>> POSIX specifies "sh -c". You might try asking in
>> comp.unix.programmer. (Note that the job of splitting the command's
>> arguments on whitespace, handling wildcards, etc. is passed off to the
>> shell; a function that uses fork/exec directly would have to reproduce
>> that functionality.)

>
>But wouldn't it be better to first try "sh -c" and then if no shell is
>available fall back to fork-exec, rather than failing the system() call?


No, unless you've got a *complete* shell emulation in the library, and if
that's the case, why bother loading another program when it's already
loaded? Use the emulation all the time.

fork-exec is missing a critical piece: turning a command string
into a program name and an argv[] array of command arguments. Along
with that go a bunch of other (POSIX) features invoked by the
parsing: I/O redirection, sequential command execution, pipelines,
wildcard expansion, shell variables, argument quoting, etc.

You can do a simple-minded implementation, for example, if the
command consists entirely of alphanumeric characters, white space,
hyphen, and slash, you can get pretty close with a parser that just
breaks arguments at white space, then does fork()/execvp().
This can go horribly wrong if the command intended to use some
of the features not implemented.

Example:
rm -f foo.o ; cc -c foo.c ; cc -o foo foo.o -lbar ; ./foo
when run by the simple-minded parser runs "rm" with lots of arguments
and will end up deleting foo.c, along with complaining about some
probably-nonexistent files.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments