Process of programming in linux (7): use of the system () function

Source: Internet
Author: User

1. system () comprehension

Function: the system () function calls "/bin/sh-c command" to execute a specific command and blocks the current process until the command is executed.


Int system (const char * command );

Return Value:

If the shell cannot be started to run the command, system will return 127; if there are other errors that cannot be called by system,-1 will be returned. If the system runs smoothly, return the exit code of the command.


Man help:

# 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 has been completed. During exe-
Cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
Will be ignored.

The value returned is-1 on error (e.g. fork (2) failed), and
Return status of the command otherwise. This latter return status is
In the format specified in wait (2). Thus, the exit code of the command
Will be WEXITSTATUS (status). In case/bin/sh cocould not be executed,
The exit status will be that of a command that does exit (127 ).

If the value of command is NULL, system () returns non-zero if the shell
Is available, and zero if not.

System () does not affect the wait status of any other children.

Ii. Principle of system () Functions

During system function execution, fork, execve, waitpid, and other functions are called.

Source code for linux system functions:

 system(  *(cmdstring == (((pid = fork())<= - (pid == , , , cmdstring, ( *)); (waitpid(pid, &status, ) < 
(errno !== -
  • Function Description
    System () calls fork () to generate sub-processes. The sub-process calls/bin/sh-c string to execute the command represented by the string parameter, this command> after the command is executed, the original called process is returned.
    The SIGCHLD signal is temporarily shelved during system () calls, while the SIGINT and SIGQUIT signals are ignored.
    Return Value
    =-1: an error occurs.
    = 0: The call is successful but no sub-process exists.
    > 0: id of the child process that successfully exits
    If system () fails to call/bin/sh, 127 is returned, and-1 is returned for other causes of failure. If the string parameter is a NULL pointer, a non-zero value is returned. If system () is successfully called, The system returns
    The return value after the shell command is executed, but the returned value may also be 127 returned by the system () call/bin/sh failure. Therefore, it is best to check errno again to confirm that the execution is successful.
    Additional instructions
    Do not use system () when writing programs with SUID/SGID permissions. system () inherits environment variables, which may cause system security problems.
The system function processes the returned values in three stages:

Phase 1: Create Sub-processes and other preparations. If it fails,-1 is returned.
Phase 2: Call/bin/sh to pull the shell script. If the pulling fails or the shell fails to run normally (see note 1), the cause value is written to the lower status 8 ~ 15 bits. System man only indicates that the value 127 is written, but the actual test shows that the value 126 is also written.
Phase 3: If the shell script runs normally and ends, fill in the shell return value as low as 8 ~ 15 bits.
Note 1:
As long as it can be called to/bin/sh and is not interrupted by other signals during shell execution, it is considered normal.
For example, no matter what reason value is returned in the shell script, whether it is 0 or not 0, the execution ends normally. Even if the shell script does not exist or has no execution permission, the execution ends normally.
If the shell script is forced to kill during execution, the exception ends.

How can I determine whether the shell script process in phase 2 is normally executed? The system provides a macro: WIFEXITED (status ). If WIFEXITED (status) is true, the process ends normally.
How to obtain the shell return value in stage 3? You can achieve this directly by shifting 8 bits to the right, but the security method is to use the macro WEXITSTATUS (status) provided by the system ).

Generally, in shell scripts, the return value is used to determine whether the script is executed normally. If 0 is returned successfully, a positive number is returned for failure.
To sum up, the method to determine whether a system function calls the shell script normally ends should be the following three conditions:
(1)-1! = Status
(2) WIFEXITED (status) is true
(3) 0 = WEXITSTATUS (status)
According to the above analysis, when the shell script does not exist and has no execution permission, the first two conditions will still be true. In this case, WEXITSTATUS (status) is 127,126 and other values.
Therefore, in shell scripts, values such as 127,126 cannot be defined as return values. Otherwise, it cannot be used to identify whether it is the return value of shell or the cause value of the shell script exception. The return value in the shell script should start to increase at 1.

Example program:

#include <stdio.h><unistd.h><stdlib.h> EXIT_ERR(m) \ ( main(= system((status == -(WEXITSTATUS(status) ==  


Related Article

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: 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.