系统()问题 [英] System() question

查看:75
本文介绍了系统()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


为什么通过传递命令行字符串

提供给sh -c来调用system()?在没有sh可用的chroot

环境中运行时,这会导致问题。 C对于
用fork() - exec()对替换system()调用不是更好吗?是否有一些

编译器选项可以改变system()的行为?


提前谢谢。

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.

推荐答案

Bob写道:
Bob wrote:

你好,


为什么通过传递给sh -c的命令行字符串

调用system()工作?在没有sh可用的chroot

环境中运行时,这会导致问题。 C对于
用fork() - exec()对替换system()调用不是更好吗?是否有一些

编译器选项可以改变system()的行为?


提前谢谢。
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.



在C实现
的许多系统上,Fork()和exec()不存在。它们也不像system()那样通用。就C

标准而言,关于system()的论证是关于实现的具体实现。


你总是可以通过以下方式找出shell是否可用:


int rc = system(NULL);

if(rc){/ * shell present * /}

else {/ * no shell * /}

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 */ }


santosh写道,On 06/12/07 22:20 :
santosh wrote, On 06/12/07 22:20:

Bob写道:
Bob wrote:

>你好,

为什么通过传递提供给sh -c的命令行字符串来调用system()工作?这在没有sh的chroot
环境中运行时会导致问题。对于C来说,用fork() - exec()对替换system()调用会不会更好?是否有一些
编译器选项可以改变system()的行为?

在此先感谢。
>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.



在C实现
的许多系统上,Fork()和exec()不存在。它们也不像system()那样通用。就C

标准而言,关于system()的论证是关于实现的具体实现。


你总是可以通过以下方式找出shell是否可用:


int rc = system(NULL);

if(rc){/ * shell present * /}

else {/ * no shell * /}


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 */ }



不是所有的都是系统特定的,以及为什么system()的答案

遇到问题,如果没有shell可用*不是*系统特定的。


标准*要求*系统调用将参数传递给

命令处理器(AKA shell),所以如果没有shell可用,那么它需要
才能失败。其他一些行为会更好是

另一件事。


如果你想要fork / exec的行为和你的实现提供

他们(或一些等价物)然后我建议使用这些系统特定的

函数。请注意,另一个标准(POSIX)可能需要保证

,如果是这样,那么有关使用它们的信息请参阅

comp.unix.programmer或其他一些适合于系统

利息。

-

Flash Gordon

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


在文章< sl ******************* @ nospam.invalid>,

Bob< no **** @ nospam.invalidwrote :
In article <sl*******************@nospam.invalid>,
Bob <no****@nospam.invalidwrote:

>为什么通过传递提供给sh -c的命令行字符串来调用system()?这在没有sh的chroot
环境中运行时会导致问题。对于C来说,用fork() - exec()对替换system()调用会不会更好?是否有一些
编译器选项可以改变system()的行为?
>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()?



就C标准而言,它可以以任何方式工作。


但在实践中它是'如果传递给system()的命令更有用,那么
的处理方式与键入shell的命令相同。例如,

我希望系统(rm *)删除目录中的所有文件,

如果你刚刚执行了这个文件就不会发生" RM"程序与

argv [1]设置为*。


如果您正在编写代码以在chroot()编辑中工作环境你很好

可能想要特别注意你执行的任何命令:我建议你自己写一个系统() - 就像使用fork()的函数一样

和exec()(可能还有wait())直接。


- Richard

-

:wq

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


这篇关于系统()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆