This article transferred from: http://www.jb51.net/article/37404.htm
1. System (execute shell command)
Correlation function Fork,execve,waitpid,popen
table header file #include <stdlib.h>
defines the function int system (const char * string);
The function Description system () calls fork () to produce a child process that calls/bin/sh-c
String to execute the command represented by the argument string string, which, when executed, follows the
The process that returned the original call. During call to System (), the SIGCHLD signal is temporarily
On hold, the SIGINT and sigquit signals are ignored.
The return value returns 127 if System () fails when calling/bin/sh, and other reasons for failure return-
1. If the argument string is a null pointer (null), a value other than 0 is returned. if the system () tune
With success, the return value is returned after the shell command is executed, but this return value also has
It is possible for system () to call the 127 returned by/bin/sh failure, so it is better to check
errno to confirm the success of the execution.
Additional instructions do not use System () when writing programs with Suid/sgid permissions, and System () will
Inherit environment variables, which can cause system security problems through environment variables.
Example:
Copy CodeThe code is as follows:
#include <stdlib.h>
Main () {
System ("Ls-al/etc/passwd/etc/shadow");
}
2, Popen (Establish pipeline I/O)
Correlation function Pipe,mkfifo,pclose,fork,system,fopen
table header file #include <stdio.h>
Defining Functions FILE * Popen (const char * command,const char * type);
The function Description popen () invokes fork () to produce the child process and then calls the/bin/sh-c from the child process
To execute the command of the parameter command. The parameter type can be read with "R", "W"
Represents the write. In accordance with this type value, Popen () establishes the standard output of the pipe connection to the child process.
The device or standard input device, and then returns a file pointer. Subsequent processes can facilitate
This file pointer is used to read the output device of the child process or to write to the child process's standard input
into the device. In addition, all functions that use the file pointer (file*) operation can also make
In addition to Fclose ().
The return value Returns a file pointer if successful, otherwise returns NULL, and the reason for the error is stored in errno.
The error code einval parameter type is not valid.
note When writing programs with Suid/sgid permissions, try to avoid using popen (), Popen ()
will inherit environment variables and may cause system security problems through environment variables.
Example:
Copy CodeThe code is as follows:
#include <stdio.h>
Main ()
{
FILE * FP;
Char buffer[80];
Fp=popen ("cat/etc/passwd", "R");
Fgets (buffer,sizeof (buffer), FP);
printf ("%s", buffer);
Pclose (FP);
}
Executive root:x:0 0:root:/root:/bin/bash
3. Create a new child process using vfork () and call the EXEC function family
Copy CodeThe code is as follows:
#include <unistd.h>
Main ()
{
char * argv[]={"ls", "-al", "/etc/passwd", (char*)};
if (vfork () = =0)
{
EXECV ("/bin/ls", argv);
}else{
printf ("This is the parent process\n");
}
}
Resolve how to invoke the implementation of the shell command in the C language "go"