Parsing how to invoke a shell command in C language _c language

Source: Internet
Author: User
1. System (Execute shell command)
related FunctionsFork,execve,waitpid,popen
Table header File#include <stdlib.h>
Defining Functionsint system (const char * string);
function DescriptionSystem () invokes fork () to produce a subprocess that is invoked by the child process/bin/sh-c
String to execute the command represented by the parameter string string, which is executed with the
Returns the process of the original call. During the call to System () the SIGCHLD signal is temporarily
Shelving, SIGINT and sigquit signals are ignored.
The return value returns 127 if System () fails while calling/bin/sh, and the other failure reason returns-
1. If the argument string is a null pointer (null), a value other than 0 is returned. If system () is tuned
With success, the return value is returned after executing the shell command, but the return value is also
It is possible to return 127 for system () call/bin/sh failure, so it is best to check again
errno to confirm execution success.
Additional InstructionsDo not use System () when writing programs with Suid/sgid permissions, System ()
Inherit environment variables, which can cause system security problems through environment variables.
Example:
Copy Code code as follows:

#include <stdlib.h>
Main () {
System ("Ls-al/etc/passwd/etc/shadow");
}

2, Popen (Establish pipeline I/O)
related FunctionsPipe,mkfifo,pclose,fork,system,fopen
Table header File#include <stdio.h>
Defining FunctionsFILE * Popen (const char * command,const char * type);
function DescriptionPopen () invokes fork () to produce the child process and then calls the/bin/sh-c from within the child process
To execute the command for the parameter command. The parameter type can be read by using "R", "W"
Represents a write. According to this type value, Popen () establishes the standard loss of the pipeline to the child process
A device or standard input device, and then returns a file pointer. Then the process will benefit
Use this file pointer to read the output device of a child process or a standard loss written to a child process
into the device. In addition, all functions that use the file pointer (file*) operation can also
Used, except fclose ().
return valueIf successful, return the file pointer, otherwise null is returned, and the reason for the error is stored in the errno.
Error code einval parameter type is not valid.
Attention MattersTry to avoid using popen (), Popen () When writing programs with Suid/sgid permissions.
Will inherit environment variables, which can cause system security problems through environment variables.
Example:
Copy Code code 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);
}

Execute root:x:0 0:root:/root:/bin/bash
3. Use Vfork () to create a new subprocess, and then call the EXEC function family
Copy Code code 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");
}
}

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.