Using the system () function under Linux must be cautious

Source: Internet
Author: User

Once, it was tortured by the system () function, because the system () function was not well understood. Simply knowing that using this function to execute a system command is not enough, it is not sufficient, its return value, the return value of the command it executes, and the reason for the failure of the command execution, which is the point. Originally because of this function risk is more, so abandon not use, use other method. Let's not say what I'm using here, it's important to understand the system () function, because there are still a lot of people using the system () function, and sometimes you have to face it. Let's take a look at a brief introduction to the System () function:
#include <stdlib.h>int system (const char *command);

System () executes a command specified in command by CALLING/BIN/SH-C command, and returns after the command have been COM Pleted. During execution of the command, SIGCHLD would be blocked, and SIGINT and Sigquit would be ignored.

The system () function calls/bin/sh to execute the command specified by the parameter,/bin/sh is typically a soft connection, pointing to a specific shell, such as the BASH,-C option, which tells the Shell to read the command from the String command, and during the command execution, SIGCHLD is blocked, like saying: Hi, kernel, this will not send me sigchld signal, and so on I am busy to say; During the command execution, SIGINT and sigquit are ignored, meaning that the process receives both signals without any action.   Take a look at the system () function return value: The value returned is-1 on error (e.g. fork (2) failed), and the return status of the command oth Erwise. This latter return status was in the format specified in Wait (2). Thus, the exit code of the command would be Wexitstatus (status). In case/bin/sh could not being executed, the exit status would be, the a command that does exit (127). If the value of command is NULL, System () returns nonzero if the shell was available, and zero if not. To better understand the system () function return value, To understand its execution, the system () function actually performs a three-step operation: 1.fork a subprocess; 2. Call the EXEC function in the subprocess to execute command;3. Call wait in the parent process to wait for the child process to end. For fork failure, the system () function returns-1. If exec executes successfully, that is, command executes successfully, returns the value returned by command via exit or return. (Note that command smooth execution does not mean execution succeeds, such as command: "rm debuglog.txt", regardless of whether the file does not exist, the command is executed successfully) if Exec fails, that is, command is not executed smoothly, such as by signal interruption, or command commands do not exist at all, the system () functionReturns 127. If command is NULL, the system () function returns a value other than 0, usually 1.  look at the source code of the system () function read these, I want to be sure someone to the system () function return value is unclear, see the source is the clearest, The implementation of a system () function is given below:
int system (const char * cmdstring) {    pid_t pid;    int status;if (cmdstring = = NULL) {    return (1);//If cmdstring is empty, returns a value other than 0, typically 1}if ((PID = fork ()) <0) {    status =-1; /fork failed, return -1}else if (pid = = 0) {    execl ("/bin/sh", "sh", "-C", Cmdstring, (char *) 0);    _exit (127); Exec execution Failure Returns 127, note that EXEC only returns to the current process if it fails, and if successful, the current process does not exist. ~~}else//Parent process {while    (Waitpid (PID, &status, 0) < 0)    {        if (errno! = eintr)        {            status =-1;//If the waitpid is interrupted by a signal, 1 break is returned            ;    }}} return status; Returns the return status of the child process if the Waitpid succeeds}

After reading through the simple implementation of the system () function, the return value of the function is clear, so when does the system () function return 0? Returns 0 o'clock only in command commands. Take a look at how to monitor the system () function execution State Here's what I'm doing:
int status;if (NULL = = cmdstring)//If the cmdstring is empty, it will flash back, although the system () function can handle null pointer {    return XXX;} Status = System (cmdstring), if (status < 0) {    printf ("cmd:%s\t Error:%s", Cmdstring, Strerror (errno)); No information output or log    return XXX;} if (wifexited (status)) {    printf ("Normal termination, exit status =%d\n", Wexitstatus (status));//Get cmdstring Execution result} else if (wifsignaled (status)) {    printf ("Abnormal termination,signal number =%d\n", Wtermsig (status)); If the cmdstring is interrupted by the signal, obtain the signal value}else if (wifstopped (status)) {    printf ("Process stopped, signal number =%d\n", Wstopsig ( status)); If the cmdstring is paused for signal execution, the signal value is obtained}

To get a description of the return value of the child process refer to another article: http://my.oschina.net/renhc/blog/35116

The system () function is easily error-prone, returns too many values, and the return value can easily be confused with the command's return value. It is recommended to use the Popen () function instead, and the simple use of the Popen () function can also be viewed through the links above.

The advantage of the Popen () function over the system () function is that it is simple to use, and the Popen () function returns only two values:
The status of the child process is successfully returned, and the return result of the command can be obtained using the wifexited related macro;
Failure returns-1, we can use the Perro () function or the strerror () function to get useful error information.

This article deals only with the simple use of the system () function, and does not talk about the effects of SIGCHLD, SIGINT, and Sigquit on system () functions, and in fact, this article was written today because the system was used by someone in the project () The function caused a very serious accident. Now, as the system () function executes, an error occurs: "No child Processes".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.