INT system (const char * character string)
{
Pid_t PID;
Int status;
If (else string = NULL ){
Return (1 );
}
If (pid = fork () <0 ){
Status =-1;
}
Else if (pid = 0 ){
Execl ("/bin/sh", "sh", "-c", character string, (char *) 0 );
-Exit (127); // The sub-process will not execute this statement if it is executed normally.
}
Else {
While (waitpid (PID, & status, 0) <0 ){
If (errno! = Einter ){
Status =-1;
Break;
}
}
}
Return status;
}
Http://blog.sina.com.cn/s/blog_62274fd70100iscf.html
Related functions
Fork, execve, waitpid, popen
Header file
# Include <stdlib. h>
Define functions
INT system (const char * string );
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.
Linux system function return value
This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2011-09/42425.htm
Example:
1. Status = system ("./test. Sh ");
1. Unify the two statements:
(1) system return value: the return value after the system function is called. For example, in the previous example, Status indicates the return value of system.
(2) Shell return value: the return value of the shell command called by system. For example, in the preceding example, the value returned by test. Sh is the shell return value.
2. How to correctly determine whether test. Sh is correctly executed?
Only determine whether status is = 0? Or, you can only determine whether the status is correct! =-1?
All errors!
3. System Description in man
Return Value
The value returned is-1 on error (e.g. Fork () failed), and the return
Status of the command otherwise. This latter return status is in
Format specified in wait (2). Thus, the exit code of the command will
Be wexitstatus (Status). In case/bin/sh cocould not be executed,
Exit status will be that of a command that does exit (127 ).
Are you dizzy?
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 is successfully executed in Stage 2? 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)
Note:
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.
The following is a sound code used to determine whether a shell script is successfully executed:
1. # include <stdio. h>
2. # include <stdlib. h> 3. # include <sys/Wait. H> 4. # include <sys/types. h>
5.
6. Int main () 7 .{
8. pid_t status;
9.
10.
11. Status = system ("./test. Sh ");
12.
13. If (-1 = Status)
14 .{
15. printf ("system error! ");
16 .}
17. Else
18 .{
19. printf ("Exit status value = [0x % x] \ n", status );
20.
21. If (wifexited (Status ))
22 .{
23. If (0 = wexitstatus (Status ))
24 .{
25. printf ("Run shell script successfully. \ n ");
26 .}
27. Else
28 .{
29. printf ("Run shell script fail, script exit code: % d \ n", wexitstatus (Status ));
30 .}
31 .}
32. Else
33 .{
34. printf ("Exit status = [% d] \ n", wexitstatus (Status ));
35 .}
36 .}
37.
38. Return 0;
39 .}
Wifexited (stat_val) evaluates to a non-zero value if status
Was returned for a child process that
Terminated normally.
Wexitstatus (stat_val) if the value of wifexited (stat_val) is
Non-zero, this macro evaluates to
Low-order 8 bits of the Status argument
That the child process passed to _ exit ()
Or exit (), or the value the child
Process returned from main ().
This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2011-09/42425.htm