Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

system返回值

Posted on 2010-11-01 17:46 Prayer 阅读(5506) 评论(0)  编辑 收藏 引用 所属分类: C/C++LINUX/UNIX/AIX

  system(执行shell 命令)

  相关函数  fork,execve,waitpid,popen

  表头文件  #include<stdlib.h>

  定义函数  int system(const char * string);

  函数说明  system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。

  返回值  如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。

  附加说明  在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。

  范例  #include<stdlib.h>

  main()

  {

  system(“ls -al /etc/passwd /etc/shadow”);

  }

  执行  -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

  -r--------- 1 root root 572 Sep 2 15 :34 /etc/shadow


       Upon successful completion, the system subroutine returns the exit status of the shell. The exit status of the shell
       is returned in the same manner as a call to the wait or waitpid subroutine, using the structures in the sys/wait.h
       file.

       If the String parameter is a null pointer and a command processor is available, the system subroutine returns a
       nonzero value. If the fork subroutine fails or if the exit status of the shell cannot be obtained, the system
       subroutine returns a value of -1. If the exec l subroutine fails, the system subroutine returns a value of 127. In
       all cases, the errno global variable is set to indicate the error.



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理