473,388 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

System() question

Bob
Howdy,

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()?

Thanks in advance.

Dec 6 '07 #1
12 1618
Bob wrote:
Howdy,

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()?

Thanks in advance.
Fork() and exec() are not present on many systems where C is
implemented. Also they are not as generic as system(). As far as the C
Standard is concerned, everthing about the argument to system() is
implementation specific.

You can always find out if a shell is available by doing:

int rc = system(NULL);
if (rc) { /* shell present */ }
else { /* no shell */ }

Dec 6 '07 #2
santosh wrote, On 06/12/07 22:20:
Bob wrote:
>Howdy,

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()?

Thanks in advance.

Fork() and exec() are not present on many systems where C is
implemented. Also they are not as generic as system(). As far as the C
Standard is concerned, everthing about the argument to system() is
implementation specific.

You can always find out if a shell is available by doing:

int rc = system(NULL);
if (rc) { /* shell present */ }
else { /* no shell */ }
Not quite everything is system specific, and the answer to why system()
is having a problem if no shell is available is *not* system specific.

The standard *requires* that the system call pass the parameter to the
command processor (AKA shell), so if there is no shell available then it
is required to fail. Whether some other behaviour would be better is
another matter.

If you want the behaviour of fork/exec and your implementation provides
them (or some equivalent) then I recommend using these system specific
functions. Note that another standard (POSIX) might give the guarantees
required, if so then for information on using them see
comp.unix.programmer or some other group appropriate to the system(s) of
interest.
--
Flash Gordon
Dec 6 '07 #3
In article <sl*******************@nospam.invalid>,
Bob <no****@nospam.invalidwrote:
>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()?
As far as the C standard is concerned, it could work either way.

But in practice it's much more useful if commands passed to system()
are treated the same way as commands typed to the shell. For example,
I expect system("rm *") to remove all the files in the directory,
which wouldn't happen if you just executed the "rm" program with with
argv[1] set to "*".

If you're writing code to work in a chroot()ed environment you quite
likely want to take special care with any commands you execute: I
suggest writing your own system()-like function that uses fork()
and exec() (and probably wait()) directly.

-- Richard
--
:wq
Dec 7 '07 #4
In article <0s************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>The standard *requires* that the system call pass the parameter to the
command processor (AKA shell), so if there is no shell available then it
is required to fail. Whether some other behaviour would be better is
another matter.
Unix has several shells available, and no doubt so do many other
operating systems. The standard doesn't specify which one to use - it
just refers to "a command processor". If you wrote some code to
execute commands using fork() and exec(), that would be another
(trivial) command processor, and using it would satisfy the C
standard. For most purposes it would be less useful than using
/bin/sh or $SHELL.

-- Richard
--
:wq
Dec 7 '07 #5
Richard Tobin wrote, On 07/12/07 00:03:
In article <0s************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>The standard *requires* that the system call pass the parameter to the
command processor (AKA shell), so if there is no shell available then it
is required to fail. Whether some other behaviour would be better is
another matter.

Unix has several shells available, and no doubt so do many other
operating systems. The standard doesn't specify which one to use - it
just refers to "a command processor". If you wrote some code to
execute commands using fork() and exec(), that would be another
(trivial) command processor, and using it would satisfy the C
standard. For most purposes it would be less useful than using
/bin/sh or $SHELL.
Yes, that is all true. However the OP was specifically talking about
running the program in an environment without a command processor.
--
Flash Gordon
Dec 7 '07 #6
>>The standard *requires* that the system call pass the parameter to the
>>command processor (AKA shell), so if there is no shell available then it
is required to fail. Whether some other behaviour would be better is
another matter.

Unix has several shells available, and no doubt so do many other
operating systems. The standard doesn't specify which one to use - it
just refers to "a command processor". If you wrote some code to
execute commands using fork() and exec(), that would be another
(trivial) command processor, and using it would satisfy the C
standard. For most purposes it would be less useful than using
/bin/sh or $SHELL.

Yes, that is all true. However the OP was specifically talking about
running the program in an environment without a command processor.
The minimum needed under UNIX to run a command line is to parse the
command line (possibly just splitting at every white-space character,
with no quoting conventions whatever, and no i/o redirection), form
argv[], and call fork() and some form of exec*(). That arguably
*IS* a command processor (for that matter, you can argue that exec()
alone is a command processor), even if it isn't a separate program.
It might even take less code than the existing system() function
does in the C library.

Dec 7 '07 #7
Gordon Burditt wrote:
....
The minimum needed under UNIX to run a command line is to parse the
command line (possibly just splitting at every white-space character,
with no quoting conventions whatever, and no i/o redirection), form
argv[], and call fork() and some form of exec*(). That arguably
*IS* a command processor
It could be a command processor. I don't think it is one; because it
hasn't been written, yet. You could write it pretty easily though.
Dec 7 '07 #8
>The minimum needed under UNIX to run a command line is to parse the
>command line (possibly just splitting at every white-space character,
with no quoting conventions whatever, and no i/o redirection), form
argv[], and call fork() and some form of exec*(). That arguably
*IS* a command processor

It could be a command processor. I don't think it is one; because it
hasn't been written, yet. You could write it pretty easily though.
I think something like this, although a little more sophisticated
(like knowing how to strip quotes off of quoted strings), was written
as an optimization. If the command doesn't contain any of certain
shell metacharacters (pipe, I/O redirection, $, *, {}, (), ?, \, and
~ at least would force use of a shell), it would use this instead.
I believe at least alphanumerics, white space, - and / were safe.
Otherwise it would use a shell.

I can't recall what used it, though: I'm thinking it was one of
sendmail, exim, certain versions of make, or perl. I learned about
it because the optimization had some side effect related to environment
variables that was tripping me up somehow.
exec() itself could be considered as a command procesor which accepts
the name of the program to run as the command (no arguments allowed).
Very, very crude, but the standard doesn't require much.

Dec 7 '07 #9
Bob <no****@nospam.invalidwrites:
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.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 7 '07 #10
Bob
On 7 Dec 2007 at 7:45, Keith Thompson wrote:
Bob <no****@nospam.invalidwrites:
>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?

Dec 7 '07 #11
In article <sl*******************@nospam.invalid>,
Bob <no****@nospam.invalidwrote:
>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
Dec 7 '07 #12
>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.
Dec 7 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Abraham Lopez | last post by:
Hi.. Is there a way to convert a System.Array to XML... If you know thanks very much... if you don't... Please do not respond stupid things like " Yes -- many ways."
7
by: MLH | last post by:
Where is system.mdw normally stored in typical A97 installation?
20
by: SR | last post by:
Hi, I need to read the output from a system( ) function within a C program. The function however only returns the exit status. How can I read what system( ) sends to stdout ? (is there a simpler...
4
by: The Spoon | last post by:
I am looking for functions that can be used to automatically update the system date and time on an NT based system, resulting from manually entered date and time values input by an operator via a...
5
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and...
8
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's...
1
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.